blob: 2d16c3c63c62a334fb6f1a7a0150a73f2f4aac1f [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);
501
Bill Wendling74027e92007-03-15 21:24:36 +0000502 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
503 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
504
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000505 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000506 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000507 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000508 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
509 setOperationAction(ISD::AND, MVT::v2i32, Promote);
510 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
511 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000512
513 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000514 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000515 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000516 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
517 setOperationAction(ISD::OR, MVT::v2i32, Promote);
518 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
519 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000520
521 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000522 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000523 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000524 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
525 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
526 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
527 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000528
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000529 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000530 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000531 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000532 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
533 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
534 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
535 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000536
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000537 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
538 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
539 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
540 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000541
542 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
543 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
544 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000545 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000546
547 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
548 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000549 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
550 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000551 }
552
Evan Chenga88973f2006-03-22 19:22:18 +0000553 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000554 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
555
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000556 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
557 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
558 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
559 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000560 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
561 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000562 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
563 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
564 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000565 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000566 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000567 }
568
Evan Chenga88973f2006-03-22 19:22:18 +0000569 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000570 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
571 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
572 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
573 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
574 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
575
Evan Chengf7c378e2006-04-10 07:23:14 +0000576 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
577 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
578 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000579 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000580 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
581 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
582 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000583 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000584 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000585 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
586 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
587 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
588 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000589 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
590 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000591
Evan Chengf7c378e2006-04-10 07:23:14 +0000592 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
593 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000594 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000595 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
596 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
597 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000598
Evan Cheng2c3ae372006-04-12 21:21:57 +0000599 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
600 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
601 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
602 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
603 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
604 }
605 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
606 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
607 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
608 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
609 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
610 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
611
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000612 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000613 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
614 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
615 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
616 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
617 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
618 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
619 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000620 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
621 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000622 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
623 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000624 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000625
626 // Custom lower v2i64 and v2f64 selects.
627 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000628 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000629 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000630 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000631 }
632
Evan Cheng6be2c582006-04-05 23:38:46 +0000633 // We want to custom lower some of our intrinsics.
634 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
635
Evan Cheng206ee9d2006-07-07 08:33:52 +0000636 // We have target-specific dag combine patterns for the following nodes:
637 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000638 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000639
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000640 computeRegisterProperties();
641
Evan Cheng87ed7162006-02-14 08:25:08 +0000642 // FIXME: These should be based on subtarget info. Plus, the values should
643 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000644 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
645 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
646 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000647 allowUnalignedMemoryAccesses = true; // x86 supports it!
648}
649
Chris Lattner2b02a442007-02-25 08:29:00 +0000650
651//===----------------------------------------------------------------------===//
652// Return Value Calling Convention Implementation
653//===----------------------------------------------------------------------===//
654
Chris Lattner59ed56b2007-02-28 04:55:35 +0000655#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000656
657/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
658/// exists skip possible ISD:TokenFactor.
659static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
660 if (Chain.getOpcode()==X86ISD::TAILCALL) {
661 return Chain;
662 } else if (Chain.getOpcode()==ISD::TokenFactor) {
663 if (Chain.getNumOperands() &&
664 Chain.getOperand(0).getOpcode()==X86ISD::TAILCALL)
665 return Chain.getOperand(0);
666 }
667 return Chain;
668}
Chris Lattner9774c912007-02-27 05:28:59 +0000669
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000670/// LowerRET - Lower an ISD::RET node.
671SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
672 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
673
Chris Lattner9774c912007-02-27 05:28:59 +0000674 SmallVector<CCValAssign, 16> RVLocs;
675 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000676 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
677 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000678 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000679
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000680 // If this is the first return lowered for this function, add the regs to the
681 // liveout set for the function.
682 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000683 for (unsigned i = 0; i != RVLocs.size(); ++i)
684 if (RVLocs[i].isRegLoc())
685 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000686 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000687 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000688
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000689 // Handle tail call return.
690 Chain = GetPossiblePreceedingTailCall(Chain);
691 if (Chain.getOpcode() == X86ISD::TAILCALL) {
692 SDOperand TailCall = Chain;
693 SDOperand TargetAddress = TailCall.getOperand(1);
694 SDOperand StackAdjustment = TailCall.getOperand(2);
695 assert ( ((TargetAddress.getOpcode() == ISD::Register &&
696 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
697 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
698 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
699 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
700 "Expecting an global address, external symbol, or register");
701 assert( StackAdjustment.getOpcode() == ISD::Constant &&
702 "Expecting a const value");
703
704 SmallVector<SDOperand,8> Operands;
705 Operands.push_back(Chain.getOperand(0));
706 Operands.push_back(TargetAddress);
707 Operands.push_back(StackAdjustment);
708 // Copy registers used by the call. Last operand is a flag so it is not
709 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000710 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000711 Operands.push_back(Chain.getOperand(i));
712 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000713 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
714 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000715 }
716
717 // Regular return.
718 SDOperand Flag;
719
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000720 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000721 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
722 RVLocs[0].getLocReg() != X86::ST0) {
723 for (unsigned i = 0; i != RVLocs.size(); ++i) {
724 CCValAssign &VA = RVLocs[i];
725 assert(VA.isRegLoc() && "Can only return in registers!");
726 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
727 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000728 Flag = Chain.getValue(1);
729 }
730 } else {
731 // We need to handle a destination of ST0 specially, because it isn't really
732 // a register.
733 SDOperand Value = Op.getOperand(1);
734
735 // If this is an FP return with ScalarSSE, we need to move the value from
736 // an XMM register onto the fp-stack.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000737 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
738 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000739 SDOperand MemLoc;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000740
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000741 // If this is a load into a scalarsse value, don't store the loaded value
742 // back to the stack, only to reload it: just replace the scalar-sse load.
743 if (ISD::isNON_EXTLoad(Value.Val) &&
744 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
745 Chain = Value.getOperand(0);
746 MemLoc = Value.getOperand(1);
747 } else {
748 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000749 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000750 MachineFunction &MF = DAG.getMachineFunction();
751 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
752 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
753 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
754 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000755 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000756 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000757 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
758 Chain = Value.getValue(1);
759 }
760
761 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
762 SDOperand Ops[] = { Chain, Value };
763 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
764 Flag = Chain.getValue(1);
765 }
766
767 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
768 if (Flag.Val)
769 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
770 else
771 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
772}
773
774
Chris Lattner3085e152007-02-25 08:59:22 +0000775/// LowerCallResult - Lower the result values of an ISD::CALL into the
776/// appropriate copies out of appropriate physical registers. This assumes that
777/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
778/// being lowered. The returns a SDNode with the same number of values as the
779/// ISD::CALL.
780SDNode *X86TargetLowering::
781LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
782 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000783
784 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000785 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000786 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
787 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000788 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
789
Chris Lattner3085e152007-02-25 08:59:22 +0000790
Chris Lattnere32bbf62007-02-28 07:09:55 +0000791 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000792
793 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000794 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
795 for (unsigned i = 0; i != RVLocs.size(); ++i) {
796 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
797 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000798 InFlag = Chain.getValue(2);
799 ResultVals.push_back(Chain.getValue(0));
800 }
801 } else {
802 // Copies from the FP stack are special, as ST0 isn't a valid register
803 // before the fp stackifier runs.
804
805 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000806 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000807 SDOperand GROps[] = { Chain, InFlag };
808 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
809 Chain = RetVal.getValue(1);
810 InFlag = RetVal.getValue(2);
811
812 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
813 // an XMM register.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000814 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
815 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner3085e152007-02-25 08:59:22 +0000816 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
817 // shouldn't be necessary except that RFP cannot be live across
818 // multiple blocks. When stackifier is fixed, they can be uncoupled.
819 MachineFunction &MF = DAG.getMachineFunction();
820 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
821 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
822 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000823 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000824 };
825 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000826 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000827 Chain = RetVal.getValue(1);
828 }
Chris Lattner3085e152007-02-25 08:59:22 +0000829 ResultVals.push_back(RetVal);
830 }
831
832 // Merge everything together with a MERGE_VALUES node.
833 ResultVals.push_back(Chain);
834 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
835 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000836}
837
838
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000839//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000840// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000841//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000842// StdCall calling convention seems to be standard for many Windows' API
843// routines and around. It differs from C calling convention just a little:
844// callee should clean up the stack, not caller. Symbols should be also
845// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000846// For info on fast calling convention see Fast Calling Convention (tail call)
847// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000848
Evan Cheng85e38002006-04-27 05:35:28 +0000849/// AddLiveIn - This helper function adds the specified physical register to the
850/// MachineFunction as a live in value. It also creates a corresponding virtual
851/// register for it.
852static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000853 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000854 assert(RC->contains(PReg) && "Not the correct regclass!");
855 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
856 MF.addLiveIn(PReg, VReg);
857 return VReg;
858}
859
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000860// align stack arguments according to platform alignment needed for tail calls
861unsigned GetAlignedArgumentStackSize(unsigned StackSize, SelectionDAG& DAG);
862
Rafael Espindola7effac52007-09-14 15:48:13 +0000863SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
864 const CCValAssign &VA,
865 MachineFrameInfo *MFI,
866 SDOperand Root, unsigned i) {
867 // Create the nodes corresponding to a load from this parameter slot.
868 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
869 VA.getLocMemOffset());
870 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
871
872 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
873
874 if (Flags & ISD::ParamFlags::ByVal)
875 return FIN;
876 else
877 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
878}
879
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000880SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
881 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000882 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000883 MachineFunction &MF = DAG.getMachineFunction();
884 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000885 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000886 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000887 unsigned CC = MF.getFunction()->getCallingConv();
Chris Lattner638402b2007-02-28 07:00:42 +0000888 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000889 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000890 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +0000891 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000892 // Check for possible tail call calling convention.
893 if (CC == CallingConv::Fast && PerformTailCallOpt)
894 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_TailCall);
895 else
896 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
897
Chris Lattnerf39f7712007-02-28 05:46:49 +0000898 SmallVector<SDOperand, 8> ArgValues;
899 unsigned LastVal = ~0U;
900 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
901 CCValAssign &VA = ArgLocs[i];
902 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
903 // places.
904 assert(VA.getValNo() != LastVal &&
905 "Don't support value assigned to multiple locs yet");
906 LastVal = VA.getValNo();
907
908 if (VA.isRegLoc()) {
909 MVT::ValueType RegVT = VA.getLocVT();
910 TargetRegisterClass *RC;
911 if (RegVT == MVT::i32)
912 RC = X86::GR32RegisterClass;
913 else {
914 assert(MVT::isVector(RegVT));
915 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000916 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000917
Chris Lattner82932a52007-03-02 05:12:29 +0000918 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
919 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000920
921 // If this is an 8 or 16-bit value, it is really passed promoted to 32
922 // bits. Insert an assert[sz]ext to capture this, then truncate to the
923 // right size.
924 if (VA.getLocInfo() == CCValAssign::SExt)
925 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
926 DAG.getValueType(VA.getValVT()));
927 else if (VA.getLocInfo() == CCValAssign::ZExt)
928 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
929 DAG.getValueType(VA.getValVT()));
930
931 if (VA.getLocInfo() != CCValAssign::Full)
932 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
933
934 ArgValues.push_back(ArgValue);
935 } else {
936 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +0000937 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +0000938 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000939 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000940
941 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000942 // align stack specially for tail calls
943 if (CC==CallingConv::Fast)
944 StackSize = GetAlignedArgumentStackSize(StackSize,DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +0000945
Evan Cheng25caf632006-05-23 21:06:34 +0000946 ArgValues.push_back(Root);
947
Evan Cheng1bc78042006-04-26 01:20:17 +0000948 // If the function takes variable number of arguments, make a frame index for
949 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000950 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000951 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000952
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000953 // Tail call calling convention (CallingConv::Fast) does not support varargs.
954 assert( !(isVarArg && CC == CallingConv::Fast) &&
955 "CallingConv::Fast does not support varargs.");
956
957 if (isStdCall && !isVarArg &&
958 (CC==CallingConv::Fast && PerformTailCallOpt || CC!=CallingConv::Fast)) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000959 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000960 BytesCallerReserves = 0;
961 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000962 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000963
964 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000965 if (NumArgs &&
966 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000967 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000968 BytesToPopOnReturn = 4;
969
970 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000971 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000972
Evan Cheng25ab6902006-09-08 06:48:29 +0000973 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng25caf632006-05-23 21:06:34 +0000974
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000975 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
976 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000977
Evan Cheng25caf632006-05-23 21:06:34 +0000978 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000979 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000980 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000981}
982
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000983SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000984 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000985 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000986 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000987 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +0000988 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000989
Chris Lattner638402b2007-02-28 07:00:42 +0000990 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +0000991 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000992 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000993 if(CC==CallingConv::Fast && PerformTailCallOpt)
994 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
995 else
996 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000997
Chris Lattner423c5f42007-02-28 05:31:48 +0000998 // Get a count of how many bytes are to be pushed on the stack.
999 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001000 if (CC==CallingConv::Fast)
1001 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001002
Evan Cheng32fe1032006-05-25 00:59:30 +00001003 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001004
Chris Lattner5a88b832007-02-25 07:10:00 +00001005 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1006 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001007
Chris Lattner423c5f42007-02-28 05:31:48 +00001008 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001009
1010 // Walk the register/memloc assignments, inserting copies/loads.
1011 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1012 CCValAssign &VA = ArgLocs[i];
1013 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001014
Chris Lattner423c5f42007-02-28 05:31:48 +00001015 // Promote the value if needed.
1016 switch (VA.getLocInfo()) {
1017 default: assert(0 && "Unknown loc info!");
1018 case CCValAssign::Full: break;
1019 case CCValAssign::SExt:
1020 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1021 break;
1022 case CCValAssign::ZExt:
1023 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1024 break;
1025 case CCValAssign::AExt:
1026 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1027 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001028 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001029
1030 if (VA.isRegLoc()) {
1031 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1032 } else {
1033 assert(VA.isMemLoc());
1034 if (StackPtr.Val == 0)
1035 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001036
1037 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1038 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001039 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001040 }
1041
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001042 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001043 bool isSRet = NumOps &&
1044 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00001045 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001046
Evan Cheng32fe1032006-05-25 00:59:30 +00001047 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001048 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1049 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001050
Evan Cheng347d5f72006-04-28 21:29:37 +00001051 // Build a sequence of copy-to-reg nodes chained together with token chain
1052 // and flag operands which copy the outgoing args into registers.
1053 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001054 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1055 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1056 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001057 InFlag = Chain.getValue(1);
1058 }
1059
Evan Chengf4684712007-02-21 21:18:14 +00001060 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1061 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +00001062 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1063 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001064 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1065 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1066 InFlag);
1067 InFlag = Chain.getValue(1);
1068 }
1069
Evan Cheng32fe1032006-05-25 00:59:30 +00001070 // If the callee is a GlobalAddress node (quite common, every direct call is)
1071 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001072 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001073 // We should use extra load for direct calls to dllimported functions in
1074 // non-JIT mode.
1075 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1076 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001077 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1078 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001079 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1080
Chris Lattnerd96d0722007-02-25 06:40:16 +00001081 // Returns a chain & a flag for retval copy to use.
1082 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001083 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001084 Ops.push_back(Chain);
1085 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001086
1087 // Add argument registers to the end of the list so that they are known live
1088 // into the call.
1089 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001090 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001091 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +00001092
1093 // Add an implicit use GOT pointer in EBX.
1094 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1095 Subtarget->isPICStyleGOT())
1096 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001097
Evan Cheng347d5f72006-04-28 21:29:37 +00001098 if (InFlag.Val)
1099 Ops.push_back(InFlag);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001100
1101 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001102 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001103
Chris Lattner2d297092006-05-23 18:50:38 +00001104 // Create the CALLSEQ_END node.
1105 unsigned NumBytesForCalleeToPush = 0;
1106
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001107 if (CC == CallingConv::X86_StdCall ||
1108 (CC == CallingConv::Fast && PerformTailCallOpt)) {
Chris Lattner09c75a42007-02-25 09:06:15 +00001109 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001110 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +00001111 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001112 NumBytesForCalleeToPush = NumBytes;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001113 assert(!(isVarArg && CC==CallingConv::Fast) &&
1114 "CallingConv::Fast does not support varargs.");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001115 } else {
1116 // If this is is a call to a struct-return function, the callee
1117 // pops the hidden struct pointer, so we have to push it back.
1118 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001119 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001120 }
1121
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001122 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001123 Ops.clear();
1124 Ops.push_back(Chain);
1125 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +00001126 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001127 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001128 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner3085e152007-02-25 08:59:22 +00001129 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001130
Chris Lattner3085e152007-02-25 08:59:22 +00001131 // Handle result values, copying them out of physregs into vregs that we
1132 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +00001133 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001134}
1135
Evan Cheng25ab6902006-09-08 06:48:29 +00001136
1137//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001138// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001139//===----------------------------------------------------------------------===//
1140//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001141// The X86 'fastcall' calling convention passes up to two integer arguments in
1142// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1143// and requires that the callee pop its arguments off the stack (allowing proper
1144// tail calls), and has the same return value conventions as C calling convs.
1145//
1146// This calling convention always arranges for the callee pop value to be 8n+4
1147// bytes, which is needed for tail recursion elimination and stack alignment
1148// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +00001149SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +00001150X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001151 MachineFunction &MF = DAG.getMachineFunction();
1152 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001153 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +00001154 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001155
Chris Lattner638402b2007-02-28 07:00:42 +00001156 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001157 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001158 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1159 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001160 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001161
1162 SmallVector<SDOperand, 8> ArgValues;
1163 unsigned LastVal = ~0U;
1164 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1165 CCValAssign &VA = ArgLocs[i];
1166 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1167 // places.
1168 assert(VA.getValNo() != LastVal &&
1169 "Don't support value assigned to multiple locs yet");
1170 LastVal = VA.getValNo();
1171
1172 if (VA.isRegLoc()) {
1173 MVT::ValueType RegVT = VA.getLocVT();
1174 TargetRegisterClass *RC;
1175 if (RegVT == MVT::i32)
1176 RC = X86::GR32RegisterClass;
1177 else {
1178 assert(MVT::isVector(RegVT));
1179 RC = X86::VR128RegisterClass;
1180 }
1181
Chris Lattner82932a52007-03-02 05:12:29 +00001182 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1183 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001184
1185 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1186 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1187 // right size.
1188 if (VA.getLocInfo() == CCValAssign::SExt)
1189 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1190 DAG.getValueType(VA.getValVT()));
1191 else if (VA.getLocInfo() == CCValAssign::ZExt)
1192 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1193 DAG.getValueType(VA.getValVT()));
1194
1195 if (VA.getLocInfo() != CCValAssign::Full)
1196 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1197
1198 ArgValues.push_back(ArgValue);
1199 } else {
1200 assert(VA.isMemLoc());
Rafael Espindola1242d282007-09-21 14:55:38 +00001201 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfc664c12007-02-28 06:21:19 +00001202 }
1203 }
1204
Evan Cheng25caf632006-05-23 21:06:34 +00001205 ArgValues.push_back(Root);
1206
Chris Lattnerfc664c12007-02-28 06:21:19 +00001207 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001208
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001209 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001210 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001211 // arguments and the arguments after the retaddr has been pushed are
1212 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001213 if ((StackSize & 7) == 0)
1214 StackSize += 4;
1215 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001216
1217 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001218 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001219 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001220 BytesCallerReserves = 0;
1221
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001222 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1223 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001224
Evan Cheng25caf632006-05-23 21:06:34 +00001225 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001226 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001227 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001228}
1229
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001230SDOperand
1231X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1232 const SDOperand &StackPtr,
1233 const CCValAssign &VA,
1234 SDOperand Chain,
1235 SDOperand Arg) {
1236 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1237 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1238 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1239 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1240 if (Flags & ISD::ParamFlags::ByVal) {
1241 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1242 ISD::ParamFlags::ByValAlignOffs);
1243
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001244 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1245 ISD::ParamFlags::ByValSizeOffs;
1246
1247 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1248 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001249 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i1);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001250
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00001251 return DAG.getMemcpy(Chain, PtrOff, Arg, SizeNode, AlignNode,
1252 AlwaysInline);
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001253 } else {
1254 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1255 }
1256}
1257
Chris Lattnere87e1152006-09-26 03:57:53 +00001258SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001259 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001260 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001261 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001262 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001263 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001264
Chris Lattner638402b2007-02-28 07:00:42 +00001265 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001266 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001267 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001268 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001269
1270 // Get a count of how many bytes are to be pushed on the stack.
1271 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001272
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001273 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001274 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001275 // arguments and the arguments after the retaddr has been pushed are
1276 // aligned.
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001277 if ((NumBytes & 7) == 0)
1278 NumBytes += 4;
1279 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001280
Chris Lattner94dd2922006-02-13 09:00:43 +00001281 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001282
Chris Lattner5a88b832007-02-25 07:10:00 +00001283 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1284 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001285
1286 SDOperand StackPtr;
1287
1288 // Walk the register/memloc assignments, inserting copies/loads.
1289 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1290 CCValAssign &VA = ArgLocs[i];
1291 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1292
1293 // Promote the value if needed.
1294 switch (VA.getLocInfo()) {
1295 default: assert(0 && "Unknown loc info!");
1296 case CCValAssign::Full: break;
1297 case CCValAssign::SExt:
1298 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001299 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001300 case CCValAssign::ZExt:
1301 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1302 break;
1303 case CCValAssign::AExt:
1304 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1305 break;
1306 }
1307
1308 if (VA.isRegLoc()) {
1309 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1310 } else {
1311 assert(VA.isMemLoc());
1312 if (StackPtr.Val == 0)
1313 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001314
1315 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1316 Arg));
Evan Cheng32fe1032006-05-25 00:59:30 +00001317 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001318 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001319
Evan Cheng32fe1032006-05-25 00:59:30 +00001320 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001321 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1322 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001323
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001324 // Build a sequence of copy-to-reg nodes chained together with token chain
1325 // and flag operands which copy the outgoing args into registers.
1326 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001327 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1328 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1329 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001330 InFlag = Chain.getValue(1);
1331 }
1332
Evan Cheng32fe1032006-05-25 00:59:30 +00001333 // If the callee is a GlobalAddress node (quite common, every direct call is)
1334 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001335 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001336 // We should use extra load for direct calls to dllimported functions in
1337 // non-JIT mode.
1338 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1339 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001340 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1341 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001342 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1343
Evan Chengf4684712007-02-21 21:18:14 +00001344 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1345 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001346 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1347 Subtarget->isPICStyleGOT()) {
1348 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1349 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1350 InFlag);
1351 InFlag = Chain.getValue(1);
1352 }
1353
Chris Lattnerd96d0722007-02-25 06:40:16 +00001354 // Returns a chain & a flag for retval copy to use.
1355 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001356 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001357 Ops.push_back(Chain);
1358 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001359
1360 // Add argument registers to the end of the list so that they are known live
1361 // into the call.
1362 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001363 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001364 RegsToPass[i].second.getValueType()));
1365
Evan Chengf4684712007-02-21 21:18:14 +00001366 // Add an implicit use GOT pointer in EBX.
1367 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1368 Subtarget->isPICStyleGOT())
1369 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1370
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001371 if (InFlag.Val)
1372 Ops.push_back(InFlag);
1373
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001374 assert(isTailCall==false && "no tail call here");
1375 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001376 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001377 InFlag = Chain.getValue(1);
1378
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001379 // Returns a flag for retval copy to use.
1380 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001381 Ops.clear();
1382 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001383 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1384 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001385 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001386 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001387 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001388
Chris Lattner339b4392007-02-25 09:10:05 +00001389 // Handle result values, copying them out of physregs into vregs that we
1390 // return.
1391 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001392}
1393
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001394//===----------------------------------------------------------------------===//
1395// Fast Calling Convention (tail call) implementation
1396//===----------------------------------------------------------------------===//
1397
1398// Like std call, callee cleans arguments, convention except that ECX is
1399// reserved for storing the tail called function address. Only 2 registers are
1400// free for argument passing (inreg). Tail call optimization is performed
1401// provided:
1402// * tailcallopt is enabled
1403// * caller/callee are fastcc
1404// * elf/pic is disabled OR
1405// * elf/pic enabled + callee is in module + callee has
1406// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001407// To keep the stack aligned according to platform abi the function
1408// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1409// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001410// If a tail called function callee has more arguments than the caller the
1411// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001412// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001413// original REtADDR, but before the saved framepointer or the spilled registers
1414// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1415// stack layout:
1416// arg1
1417// arg2
1418// RETADDR
1419// [ new RETADDR
1420// move area ]
1421// (possible EBP)
1422// ESI
1423// EDI
1424// local1 ..
1425
1426/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1427/// for a 16 byte align requirement.
1428unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1429 SelectionDAG& DAG) {
1430 if (PerformTailCallOpt) {
1431 MachineFunction &MF = DAG.getMachineFunction();
1432 const TargetMachine &TM = MF.getTarget();
1433 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1434 unsigned StackAlignment = TFI.getStackAlignment();
1435 uint64_t AlignMask = StackAlignment - 1;
1436 int64_t Offset = StackSize;
1437 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1438 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1439 // Number smaller than 12 so just add the difference.
1440 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1441 } else {
1442 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1443 Offset = ((~AlignMask) & Offset) + StackAlignment +
1444 (StackAlignment-SlotSize);
1445 }
1446 StackSize = Offset;
1447 }
1448 return StackSize;
1449}
1450
1451/// IsEligibleForTailCallElimination - Check to see whether the next instruction
1452// following the call is a return. A function is eligible if caller/callee
1453// calling conventions match, currently only fastcc supports tail calls, and the
1454// function CALL is immediatly followed by a RET.
1455bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1456 SDOperand Ret,
1457 SelectionDAG& DAG) const {
1458 bool IsEligible = false;
1459
1460 // Check whether CALL node immediatly preceeds the RET node and whether the
1461 // return uses the result of the node or is a void return.
1462 if ((Ret.getNumOperands() == 1 &&
1463 (Ret.getOperand(0)== SDOperand(Call.Val,1) ||
1464 Ret.getOperand(0)== SDOperand(Call.Val,0))) ||
1465 (Ret.getOperand(0)== SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1466 Ret.getOperand(1)== SDOperand(Call.Val,0))) {
1467 MachineFunction &MF = DAG.getMachineFunction();
1468 unsigned CallerCC = MF.getFunction()->getCallingConv();
1469 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1470 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1471 SDOperand Callee = Call.getOperand(4);
1472 // On elf/pic %ebx needs to be livein.
1473 if(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1474 Subtarget->isPICStyleGOT()) {
1475 // Can only do local tail calls with PIC.
1476 GlobalValue * GV = 0;
1477 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1478 if(G != 0 &&
1479 (GV = G->getGlobal()) &&
1480 (GV->hasHiddenVisibility() || GV->hasProtectedVisibility()))
1481 IsEligible=true;
1482 } else {
1483 IsEligible=true;
1484 }
1485 }
1486 }
1487 return IsEligible;
1488}
1489
1490SDOperand X86TargetLowering::LowerX86_TailCallTo(SDOperand Op,
1491 SelectionDAG &DAG,
1492 unsigned CC) {
1493 SDOperand Chain = Op.getOperand(0);
1494 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1495 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1496 SDOperand Callee = Op.getOperand(4);
1497 bool is64Bit = Subtarget->is64Bit();
1498
1499 assert(isTailCall && PerformTailCallOpt && "Should only emit tail calls.");
1500
1501 // Analyze operands of the call, assigning locations to each operand.
1502 SmallVector<CCValAssign, 16> ArgLocs;
1503 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1504 if (is64Bit)
1505 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1506 else
1507 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_TailCall);
1508
1509
1510 // Lower arguments at fp - stackoffset + fpdiff.
1511 MachineFunction &MF = DAG.getMachineFunction();
1512
1513 unsigned NumBytesToBePushed =
1514 GetAlignedArgumentStackSize(CCInfo.getNextStackOffset(), DAG);
1515
1516 unsigned NumBytesCallerPushed =
1517 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1518 int FPDiff = NumBytesCallerPushed - NumBytesToBePushed;
1519
1520 // Set the delta of movement of the returnaddr stackslot.
1521 // But only set if delta is greater than previous delta.
1522 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1523 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1524
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001525 Chain = DAG.
1526 getCALLSEQ_START(Chain, DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1527
1528 // Adjust the Return address stack slot.
1529 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001530 if (FPDiff) {
1531 MVT::ValueType VT = is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001532 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1533 // Load the "old" Return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001534 RetAddrFrIdx =
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001535 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1536 // Calculate the new stack slot for the return address.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001537 int SlotSize = is64Bit ? 8 : 4;
1538 int NewReturnAddrFI =
1539 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001540 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1541 Chain = SDOperand(RetAddrFrIdx.Val, 1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001542 }
1543
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001544 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1545 SmallVector<SDOperand, 8> MemOpChains;
1546 SmallVector<SDOperand, 8> MemOpChains2;
1547 SDOperand FramePtr, StackPtr;
1548 SDOperand PtrOff;
1549 SDOperand FIN;
1550 int FI = 0;
1551
1552 // Walk the register/memloc assignments, inserting copies/loads. Lower
1553 // arguments first to the stack slot where they would normally - in case of a
1554 // normal function call - be.
1555 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1556 CCValAssign &VA = ArgLocs[i];
1557 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1558
1559 // Promote the value if needed.
1560 switch (VA.getLocInfo()) {
1561 default: assert(0 && "Unknown loc info!");
1562 case CCValAssign::Full: break;
1563 case CCValAssign::SExt:
1564 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1565 break;
1566 case CCValAssign::ZExt:
1567 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1568 break;
1569 case CCValAssign::AExt:
1570 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1571 break;
1572 }
1573
1574 if (VA.isRegLoc()) {
1575 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1576 } else {
1577 assert(VA.isMemLoc());
1578 if (StackPtr.Val == 0)
1579 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1580
1581 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1582 Arg));
1583 }
1584 }
1585
1586 if (!MemOpChains.empty())
1587 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1588 &MemOpChains[0], MemOpChains.size());
1589
1590 // Build a sequence of copy-to-reg nodes chained together with token chain
1591 // and flag operands which copy the outgoing args into registers.
1592 SDOperand InFlag;
1593 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1594 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1595 InFlag);
1596 InFlag = Chain.getValue(1);
1597 }
1598 InFlag = SDOperand();
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001599
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001600 // Copy from stack slots to stack slot of a tail called function. This needs
1601 // to be done because if we would lower the arguments directly to their real
1602 // stack slot we might end up overwriting each other.
1603 // TODO: To make this more efficient (sometimes saving a store/load) we could
1604 // analyse the arguments and emit this store/load/store sequence only for
1605 // arguments which would be overwritten otherwise.
1606 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1607 CCValAssign &VA = ArgLocs[i];
1608 if (!VA.isRegLoc()) {
1609 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1610 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1611
1612 // Get source stack slot.
1613 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1614 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1615 // Create frame index.
1616 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1617 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1618 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1619 FIN = DAG.getFrameIndex(FI, MVT::i32);
1620 if (Flags & ISD::ParamFlags::ByVal) {
1621 // Copy relative to framepointer.
1622 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1623 ISD::ParamFlags::ByValAlignOffs);
1624
1625 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1626 ISD::ParamFlags::ByValSizeOffs;
1627
1628 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1629 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1630 // Copy relative to framepointer.
1631 MemOpChains2.push_back(DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, FIN,
1632 PtrOff, SizeNode, AlignNode));
1633 } else {
1634 SDOperand LoadedArg = DAG.getLoad(VA.getValVT(), Chain, PtrOff, NULL,0);
1635 // Store relative to framepointer.
1636 MemOpChains2.push_back(DAG.getStore(Chain, LoadedArg, FIN, NULL, 0));
1637 }
1638 }
1639 }
1640
1641 if (!MemOpChains2.empty())
1642 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1643 &MemOpChains2[0], MemOpChains.size());
1644
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001645 // Store the return address to the appropriate stack slot.
1646 if (FPDiff)
1647 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1648
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001649 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1650 // GOT pointer.
1651 // Does not work with tail call since ebx is not restored correctly by
1652 // tailcaller. TODO: at least for x86 - verify for x86-64
1653
1654 // If the callee is a GlobalAddress node (quite common, every direct call is)
1655 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1656 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1657 // We should use extra load for direct calls to dllimported functions in
1658 // non-JIT mode.
1659 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1660 getTargetMachine(), true))
1661 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1662 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1663 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1664 else {
1665 assert(Callee.getOpcode() == ISD::LOAD &&
1666 "Function destination must be loaded into virtual register");
1667 unsigned Opc = is64Bit ? X86::R9 : X86::ECX;
1668
1669 Chain = DAG.getCopyToReg(Chain,
1670 DAG.getRegister(Opc, getPointerTy()) ,
1671 Callee,InFlag);
1672 Callee = DAG.getRegister(Opc, getPointerTy());
1673 // Add register as live out.
1674 DAG.getMachineFunction().addLiveOut(Opc);
1675 }
1676
1677 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1678 SmallVector<SDOperand, 8> Ops;
1679
1680 Ops.push_back(Chain);
1681 Ops.push_back(DAG.getConstant(NumBytesToBePushed, getPointerTy()));
1682 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1683 if (InFlag.Val)
1684 Ops.push_back(InFlag);
1685 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1686 InFlag = Chain.getValue(1);
1687
1688 // Returns a chain & a flag for retval copy to use.
1689 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1690 Ops.clear();
1691 Ops.push_back(Chain);
1692 Ops.push_back(Callee);
1693 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1694 // Add argument registers to the end of the list so that they are known live
1695 // into the call.
1696 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1697 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1698 RegsToPass[i].second.getValueType()));
1699 if (InFlag.Val)
1700 Ops.push_back(InFlag);
1701 assert(InFlag.Val &&
1702 "Flag must be set. Depend on flag being set in LowerRET");
1703 Chain = DAG.getNode(X86ISD::TAILCALL,
1704 Op.Val->getVTList(), &Ops[0], Ops.size());
1705
1706 return SDOperand(Chain.Val, Op.ResNo);
1707}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001708
1709//===----------------------------------------------------------------------===//
1710// X86-64 C Calling Convention implementation
1711//===----------------------------------------------------------------------===//
1712
1713SDOperand
1714X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001715 MachineFunction &MF = DAG.getMachineFunction();
1716 MachineFrameInfo *MFI = MF.getFrameInfo();
1717 SDOperand Root = Op.getOperand(0);
1718 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001719 unsigned CC= MF.getFunction()->getCallingConv();
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001720
1721 static const unsigned GPR64ArgRegs[] = {
1722 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1723 };
1724 static const unsigned XMMArgRegs[] = {
1725 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1726 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1727 };
1728
Chris Lattner638402b2007-02-28 07:00:42 +00001729
1730 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001731 SmallVector<CCValAssign, 16> ArgLocs;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001732 CCState CCInfo(CC, isVarArg,
Chris Lattner52387be2007-06-19 00:13:10 +00001733 getTargetMachine(), ArgLocs);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001734 if (CC == CallingConv::Fast && PerformTailCallOpt)
1735 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_TailCall);
1736 else
1737 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001738
1739 SmallVector<SDOperand, 8> ArgValues;
1740 unsigned LastVal = ~0U;
1741 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1742 CCValAssign &VA = ArgLocs[i];
1743 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1744 // places.
1745 assert(VA.getValNo() != LastVal &&
1746 "Don't support value assigned to multiple locs yet");
1747 LastVal = VA.getValNo();
1748
1749 if (VA.isRegLoc()) {
1750 MVT::ValueType RegVT = VA.getLocVT();
1751 TargetRegisterClass *RC;
1752 if (RegVT == MVT::i32)
1753 RC = X86::GR32RegisterClass;
1754 else if (RegVT == MVT::i64)
1755 RC = X86::GR64RegisterClass;
1756 else if (RegVT == MVT::f32)
1757 RC = X86::FR32RegisterClass;
1758 else if (RegVT == MVT::f64)
1759 RC = X86::FR64RegisterClass;
1760 else {
1761 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001762 if (MVT::getSizeInBits(RegVT) == 64) {
1763 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1764 RegVT = MVT::i64;
1765 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001766 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001767 }
Chris Lattner82932a52007-03-02 05:12:29 +00001768
1769 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1770 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001771
1772 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1773 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1774 // right size.
1775 if (VA.getLocInfo() == CCValAssign::SExt)
1776 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1777 DAG.getValueType(VA.getValVT()));
1778 else if (VA.getLocInfo() == CCValAssign::ZExt)
1779 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1780 DAG.getValueType(VA.getValVT()));
1781
1782 if (VA.getLocInfo() != CCValAssign::Full)
1783 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1784
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001785 // Handle MMX values passed in GPRs.
1786 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1787 MVT::getSizeInBits(RegVT) == 64)
1788 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1789
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001790 ArgValues.push_back(ArgValue);
1791 } else {
1792 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001793 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001794 }
1795 }
1796
1797 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001798 if (CC==CallingConv::Fast)
1799 StackSize =GetAlignedArgumentStackSize(StackSize, DAG);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001800
1801 // If the function takes variable number of arguments, make a frame index for
1802 // the start of the first vararg value... for expansion of llvm.va_start.
1803 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001804 assert(CC!=CallingConv::Fast
1805 && "Var arg not supported with calling convention fastcc");
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001806 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1807 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1808
1809 // For X86-64, if there are vararg parameters that are passed via
1810 // registers, then we must store them to their spots on the stack so they
1811 // may be loaded by deferencing the result of va_next.
1812 VarArgsGPOffset = NumIntRegs * 8;
1813 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1814 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1815 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1816
1817 // Store the integer parameter registers.
1818 SmallVector<SDOperand, 8> MemOps;
1819 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1820 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1821 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1822 for (; NumIntRegs != 6; ++NumIntRegs) {
1823 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1824 X86::GR64RegisterClass);
1825 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1826 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1827 MemOps.push_back(Store);
1828 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1829 DAG.getConstant(8, getPointerTy()));
1830 }
1831
1832 // Now store the XMM (fp + vector) parameter registers.
1833 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1834 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1835 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1836 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1837 X86::VR128RegisterClass);
1838 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1839 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1840 MemOps.push_back(Store);
1841 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1842 DAG.getConstant(16, getPointerTy()));
1843 }
1844 if (!MemOps.empty())
1845 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1846 &MemOps[0], MemOps.size());
1847 }
1848
1849 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001850 // Tail call convention (fastcc) needs callee pop.
Evan Cheng36446012007-10-14 10:09:39 +00001851 if (CC == CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001852 BytesToPopOnReturn = StackSize; // Callee pops everything.
1853 BytesCallerReserves = 0;
1854 } else {
1855 BytesToPopOnReturn = 0; // Callee pops nothing.
1856 BytesCallerReserves = StackSize;
1857 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001858 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1859 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1860
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001861 // Return the new list of results.
1862 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1863 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1864}
1865
1866SDOperand
1867X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1868 unsigned CC) {
1869 SDOperand Chain = Op.getOperand(0);
1870 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001871 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001872
1873 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001874 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001875 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng36446012007-10-14 10:09:39 +00001876 if (CC==CallingConv::Fast && PerformTailCallOpt)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001877 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_TailCall);
1878 else
1879 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001880
1881 // Get a count of how many bytes are to be pushed on the stack.
1882 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001883 if (CC == CallingConv::Fast)
1884 NumBytes = GetAlignedArgumentStackSize(NumBytes,DAG);
1885
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001886 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1887
1888 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1889 SmallVector<SDOperand, 8> MemOpChains;
1890
1891 SDOperand StackPtr;
1892
1893 // Walk the register/memloc assignments, inserting copies/loads.
1894 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1895 CCValAssign &VA = ArgLocs[i];
1896 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1897
1898 // Promote the value if needed.
1899 switch (VA.getLocInfo()) {
1900 default: assert(0 && "Unknown loc info!");
1901 case CCValAssign::Full: break;
1902 case CCValAssign::SExt:
1903 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1904 break;
1905 case CCValAssign::ZExt:
1906 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1907 break;
1908 case CCValAssign::AExt:
1909 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1910 break;
1911 }
1912
1913 if (VA.isRegLoc()) {
1914 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1915 } else {
1916 assert(VA.isMemLoc());
1917 if (StackPtr.Val == 0)
1918 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola21485be2007-08-20 15:18:24 +00001919
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001920 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1921 Arg));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001922 }
1923 }
1924
1925 if (!MemOpChains.empty())
1926 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1927 &MemOpChains[0], MemOpChains.size());
1928
1929 // Build a sequence of copy-to-reg nodes chained together with token chain
1930 // and flag operands which copy the outgoing args into registers.
1931 SDOperand InFlag;
1932 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1933 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1934 InFlag);
1935 InFlag = Chain.getValue(1);
1936 }
1937
1938 if (isVarArg) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001939 assert ( CallingConv::Fast != CC &&
1940 "Var args not supported with calling convention fastcc");
1941
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001942 // From AMD64 ABI document:
1943 // For calls that may call functions that use varargs or stdargs
1944 // (prototype-less calls or calls to functions containing ellipsis (...) in
1945 // the declaration) %al is used as hidden argument to specify the number
1946 // of SSE registers used. The contents of %al do not need to match exactly
1947 // the number of registers, but must be an ubound on the number of SSE
1948 // registers used and is in the range 0 - 8 inclusive.
1949
1950 // Count the number of XMM registers allocated.
1951 static const unsigned XMMArgRegs[] = {
1952 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1953 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1954 };
1955 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1956
1957 Chain = DAG.getCopyToReg(Chain, X86::AL,
1958 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1959 InFlag = Chain.getValue(1);
1960 }
1961
1962 // If the callee is a GlobalAddress node (quite common, every direct call is)
1963 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1964 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1965 // We should use extra load for direct calls to dllimported functions in
1966 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001967 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001968 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1969 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001970 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1971 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001972 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1973 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001974
1975 // Returns a chain & a flag for retval copy to use.
1976 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1977 SmallVector<SDOperand, 8> Ops;
1978 Ops.push_back(Chain);
1979 Ops.push_back(Callee);
1980
1981 // Add argument registers to the end of the list so that they are known live
1982 // into the call.
1983 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1984 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1985 RegsToPass[i].second.getValueType()));
1986
1987 if (InFlag.Val)
1988 Ops.push_back(InFlag);
1989
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001990 Chain = DAG.getNode(X86ISD::CALL,
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001991 NodeTys, &Ops[0], Ops.size());
1992 InFlag = Chain.getValue(1);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001993 int NumBytesForCalleeToPush = 0;
Evan Cheng36446012007-10-14 10:09:39 +00001994 if (CC==CallingConv::Fast && PerformTailCallOpt) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001995 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001996 } else {
1997 NumBytesForCalleeToPush = 0; // Callee pops nothing.
1998 }
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001999 // Returns a flag for retval copy to use.
2000 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
2001 Ops.clear();
2002 Ops.push_back(Chain);
2003 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002004 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002005 Ops.push_back(InFlag);
2006 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
2007 InFlag = Chain.getValue(1);
2008
2009 // Handle result values, copying them out of physregs into vregs that we
2010 // return.
2011 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
2012}
2013
2014
2015//===----------------------------------------------------------------------===//
2016// Other Lowering Hooks
2017//===----------------------------------------------------------------------===//
2018
2019
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002020SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002021 MachineFunction &MF = DAG.getMachineFunction();
2022 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2023 int ReturnAddrIndex = FuncInfo->getRAIndex();
2024
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002025 if (ReturnAddrIndex == 0) {
2026 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00002027 if (Subtarget->is64Bit())
2028 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
2029 else
2030 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002031
2032 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002033 }
2034
Evan Cheng25ab6902006-09-08 06:48:29 +00002035 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002036}
2037
2038
2039
Evan Cheng6dfa9992006-01-30 23:41:35 +00002040/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
2041/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00002042/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
2043/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00002044static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00002045 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
2046 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00002047 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002048 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002049 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2050 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2051 // X > -1 -> X == 0, jump !sign.
2052 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00002053 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002054 return true;
2055 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2056 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00002057 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002058 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00002059 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2060 // X < 1 -> X <= 0
2061 RHS = DAG.getConstant(0, RHS.getValueType());
2062 X86CC = X86::COND_LE;
2063 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002064 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002065 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002066
Evan Chengd9558e02006-01-06 00:43:03 +00002067 switch (SetCCOpcode) {
2068 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002069 case ISD::SETEQ: X86CC = X86::COND_E; break;
2070 case ISD::SETGT: X86CC = X86::COND_G; break;
2071 case ISD::SETGE: X86CC = X86::COND_GE; break;
2072 case ISD::SETLT: X86CC = X86::COND_L; break;
2073 case ISD::SETLE: X86CC = X86::COND_LE; break;
2074 case ISD::SETNE: X86CC = X86::COND_NE; break;
2075 case ISD::SETULT: X86CC = X86::COND_B; break;
2076 case ISD::SETUGT: X86CC = X86::COND_A; break;
2077 case ISD::SETULE: X86CC = X86::COND_BE; break;
2078 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002079 }
2080 } else {
2081 // On a floating point condition, the flags are set as follows:
2082 // ZF PF CF op
2083 // 0 | 0 | 0 | X > Y
2084 // 0 | 0 | 1 | X < Y
2085 // 1 | 0 | 0 | X == Y
2086 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00002087 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00002088 switch (SetCCOpcode) {
2089 default: break;
2090 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002091 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002092 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002093 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002094 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002095 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002096 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002097 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002098 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002099 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002100 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002101 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002102 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002103 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002104 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002105 case ISD::SETNE: X86CC = X86::COND_NE; break;
2106 case ISD::SETUO: X86CC = X86::COND_P; break;
2107 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002108 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002109 if (Flip)
2110 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00002111 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00002112
Chris Lattner7fbe9722006-10-20 17:42:20 +00002113 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002114}
2115
Evan Cheng4a460802006-01-11 00:33:36 +00002116/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2117/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002118/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002119static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002120 switch (X86CC) {
2121 default:
2122 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002123 case X86::COND_B:
2124 case X86::COND_BE:
2125 case X86::COND_E:
2126 case X86::COND_P:
2127 case X86::COND_A:
2128 case X86::COND_AE:
2129 case X86::COND_NE:
2130 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002131 return true;
2132 }
2133}
2134
Evan Cheng5ced1d82006-04-06 23:23:56 +00002135/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002136/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00002137static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2138 if (Op.getOpcode() == ISD::UNDEF)
2139 return true;
2140
2141 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002142 return (Val >= Low && Val < Hi);
2143}
2144
2145/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2146/// true if Op is undef or if its value equal to the specified value.
2147static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2148 if (Op.getOpcode() == ISD::UNDEF)
2149 return true;
2150 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002151}
2152
Evan Cheng0188ecb2006-03-22 18:59:22 +00002153/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2154/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2155bool X86::isPSHUFDMask(SDNode *N) {
2156 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2157
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002158 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002159 return false;
2160
2161 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002162 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002163 SDOperand Arg = N->getOperand(i);
2164 if (Arg.getOpcode() == ISD::UNDEF) continue;
2165 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002166 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002167 return false;
2168 }
2169
2170 return true;
2171}
2172
2173/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002174/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002175bool X86::isPSHUFHWMask(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177
2178 if (N->getNumOperands() != 8)
2179 return false;
2180
2181 // Lower quadword copied in order.
2182 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002183 SDOperand Arg = N->getOperand(i);
2184 if (Arg.getOpcode() == ISD::UNDEF) continue;
2185 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2186 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002187 return false;
2188 }
2189
2190 // Upper quadword shuffled.
2191 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002192 SDOperand Arg = N->getOperand(i);
2193 if (Arg.getOpcode() == ISD::UNDEF) continue;
2194 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2195 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002196 if (Val < 4 || Val > 7)
2197 return false;
2198 }
2199
2200 return true;
2201}
2202
2203/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002204/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002205bool X86::isPSHUFLWMask(SDNode *N) {
2206 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2207
2208 if (N->getNumOperands() != 8)
2209 return false;
2210
2211 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002212 for (unsigned i = 4; i != 8; ++i)
2213 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002214 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002215
2216 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002217 for (unsigned i = 0; i != 4; ++i)
2218 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002219 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002220
2221 return true;
2222}
2223
Evan Cheng14aed5e2006-03-24 01:18:28 +00002224/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2225/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00002226static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002227 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002228
Evan Cheng39623da2006-04-20 08:58:49 +00002229 unsigned Half = NumElems / 2;
2230 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002231 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002232 return false;
2233 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002234 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002235 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002236
2237 return true;
2238}
2239
Evan Cheng39623da2006-04-20 08:58:49 +00002240bool X86::isSHUFPMask(SDNode *N) {
2241 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002242 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002243}
2244
Evan Cheng213d2cf2007-05-17 18:45:50 +00002245/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002246/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2247/// half elements to come from vector 1 (which would equal the dest.) and
2248/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002249static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2250 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002251
Chris Lattner5a88b832007-02-25 07:10:00 +00002252 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002253 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002254 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002255 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002256 for (unsigned i = Half; i < NumOps; ++i)
2257 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002258 return false;
2259 return true;
2260}
2261
2262static bool isCommutedSHUFP(SDNode *N) {
2263 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002264 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002265}
2266
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002267/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2268/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2269bool X86::isMOVHLPSMask(SDNode *N) {
2270 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2271
Evan Cheng2064a2b2006-03-28 06:50:32 +00002272 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002273 return false;
2274
Evan Cheng2064a2b2006-03-28 06:50:32 +00002275 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002276 return isUndefOrEqual(N->getOperand(0), 6) &&
2277 isUndefOrEqual(N->getOperand(1), 7) &&
2278 isUndefOrEqual(N->getOperand(2), 2) &&
2279 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002280}
2281
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002282/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2283/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2284/// <2, 3, 2, 3>
2285bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2286 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2287
2288 if (N->getNumOperands() != 4)
2289 return false;
2290
2291 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2292 return isUndefOrEqual(N->getOperand(0), 2) &&
2293 isUndefOrEqual(N->getOperand(1), 3) &&
2294 isUndefOrEqual(N->getOperand(2), 2) &&
2295 isUndefOrEqual(N->getOperand(3), 3);
2296}
2297
Evan Cheng5ced1d82006-04-06 23:23:56 +00002298/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2299/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2300bool X86::isMOVLPMask(SDNode *N) {
2301 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2302
2303 unsigned NumElems = N->getNumOperands();
2304 if (NumElems != 2 && NumElems != 4)
2305 return false;
2306
Evan Chengc5cdff22006-04-07 21:53:05 +00002307 for (unsigned i = 0; i < NumElems/2; ++i)
2308 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2309 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002310
Evan Chengc5cdff22006-04-07 21:53:05 +00002311 for (unsigned i = NumElems/2; i < NumElems; ++i)
2312 if (!isUndefOrEqual(N->getOperand(i), i))
2313 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002314
2315 return true;
2316}
2317
2318/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002319/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2320/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002321bool X86::isMOVHPMask(SDNode *N) {
2322 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2323
2324 unsigned NumElems = N->getNumOperands();
2325 if (NumElems != 2 && NumElems != 4)
2326 return false;
2327
Evan Chengc5cdff22006-04-07 21:53:05 +00002328 for (unsigned i = 0; i < NumElems/2; ++i)
2329 if (!isUndefOrEqual(N->getOperand(i), i))
2330 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002331
2332 for (unsigned i = 0; i < NumElems/2; ++i) {
2333 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002334 if (!isUndefOrEqual(Arg, i + NumElems))
2335 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002336 }
2337
2338 return true;
2339}
2340
Evan Cheng0038e592006-03-28 00:39:58 +00002341/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2342/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002343bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2344 bool V2IsSplat = false) {
2345 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002346 return false;
2347
Chris Lattner5a88b832007-02-25 07:10:00 +00002348 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2349 SDOperand BitI = Elts[i];
2350 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002351 if (!isUndefOrEqual(BitI, j))
2352 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002353 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002354 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002355 return false;
2356 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002357 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002358 return false;
2359 }
Evan Cheng0038e592006-03-28 00:39:58 +00002360 }
2361
2362 return true;
2363}
2364
Evan Cheng39623da2006-04-20 08:58:49 +00002365bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2366 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002367 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002368}
2369
Evan Cheng4fcb9222006-03-28 02:43:26 +00002370/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2371/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002372bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2373 bool V2IsSplat = false) {
2374 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002375 return false;
2376
Chris Lattner5a88b832007-02-25 07:10:00 +00002377 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2378 SDOperand BitI = Elts[i];
2379 SDOperand BitI1 = Elts[i+1];
2380 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002381 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002382 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002383 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002384 return false;
2385 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002386 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002387 return false;
2388 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002389 }
2390
2391 return true;
2392}
2393
Evan Cheng39623da2006-04-20 08:58:49 +00002394bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2395 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002396 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002397}
2398
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002399/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2400/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2401/// <0, 0, 1, 1>
2402bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2403 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2404
2405 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002406 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002407 return false;
2408
2409 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2410 SDOperand BitI = N->getOperand(i);
2411 SDOperand BitI1 = N->getOperand(i+1);
2412
Evan Chengc5cdff22006-04-07 21:53:05 +00002413 if (!isUndefOrEqual(BitI, j))
2414 return false;
2415 if (!isUndefOrEqual(BitI1, j))
2416 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002417 }
2418
2419 return true;
2420}
2421
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002422/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2423/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2424/// <2, 2, 3, 3>
2425bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2426 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2427
2428 unsigned NumElems = N->getNumOperands();
2429 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2430 return false;
2431
2432 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2433 SDOperand BitI = N->getOperand(i);
2434 SDOperand BitI1 = N->getOperand(i + 1);
2435
2436 if (!isUndefOrEqual(BitI, j))
2437 return false;
2438 if (!isUndefOrEqual(BitI1, j))
2439 return false;
2440 }
2441
2442 return true;
2443}
2444
Evan Cheng017dcc62006-04-21 01:05:10 +00002445/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2446/// specifies a shuffle of elements that is suitable for input to MOVSS,
2447/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002448static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2449 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002450 return false;
2451
Chris Lattner5a88b832007-02-25 07:10:00 +00002452 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002453 return false;
2454
Chris Lattner5a88b832007-02-25 07:10:00 +00002455 for (unsigned i = 1; i < NumElts; ++i) {
2456 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002457 return false;
2458 }
2459
2460 return true;
2461}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002462
Evan Cheng017dcc62006-04-21 01:05:10 +00002463bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002464 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002465 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002466}
2467
Evan Cheng017dcc62006-04-21 01:05:10 +00002468/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2469/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002470/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002471static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2472 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002473 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002474 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002475 return false;
2476
2477 if (!isUndefOrEqual(Ops[0], 0))
2478 return false;
2479
Chris Lattner5a88b832007-02-25 07:10:00 +00002480 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002481 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002482 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2483 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2484 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002485 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002486 }
2487
2488 return true;
2489}
2490
Evan Cheng8cf723d2006-09-08 01:50:06 +00002491static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2492 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002493 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002494 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2495 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002496}
2497
Evan Chengd9539472006-04-14 21:59:03 +00002498/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2499/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2500bool X86::isMOVSHDUPMask(SDNode *N) {
2501 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2502
2503 if (N->getNumOperands() != 4)
2504 return false;
2505
2506 // Expect 1, 1, 3, 3
2507 for (unsigned i = 0; i < 2; ++i) {
2508 SDOperand Arg = N->getOperand(i);
2509 if (Arg.getOpcode() == ISD::UNDEF) continue;
2510 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2511 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2512 if (Val != 1) return false;
2513 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002514
2515 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002516 for (unsigned i = 2; i < 4; ++i) {
2517 SDOperand Arg = N->getOperand(i);
2518 if (Arg.getOpcode() == ISD::UNDEF) continue;
2519 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2520 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2521 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002522 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002523 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002524
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002525 // Don't use movshdup if it can be done with a shufps.
2526 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002527}
2528
2529/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2530/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2531bool X86::isMOVSLDUPMask(SDNode *N) {
2532 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2533
2534 if (N->getNumOperands() != 4)
2535 return false;
2536
2537 // Expect 0, 0, 2, 2
2538 for (unsigned i = 0; i < 2; ++i) {
2539 SDOperand Arg = N->getOperand(i);
2540 if (Arg.getOpcode() == ISD::UNDEF) continue;
2541 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2542 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2543 if (Val != 0) return false;
2544 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002545
2546 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002547 for (unsigned i = 2; i < 4; ++i) {
2548 SDOperand Arg = N->getOperand(i);
2549 if (Arg.getOpcode() == ISD::UNDEF) continue;
2550 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2551 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2552 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002553 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002554 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002555
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002556 // Don't use movshdup if it can be done with a shufps.
2557 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002558}
2559
Evan Cheng49892af2007-06-19 00:02:56 +00002560/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2561/// specifies a identity operation on the LHS or RHS.
2562static bool isIdentityMask(SDNode *N, bool RHS = false) {
2563 unsigned NumElems = N->getNumOperands();
2564 for (unsigned i = 0; i < NumElems; ++i)
2565 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2566 return false;
2567 return true;
2568}
2569
Evan Chengb9df0ca2006-03-22 02:53:00 +00002570/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2571/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002572static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002573 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2574
Evan Chengb9df0ca2006-03-22 02:53:00 +00002575 // This is a splat operation if each element of the permute is the same, and
2576 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002577 unsigned NumElems = N->getNumOperands();
2578 SDOperand ElementBase;
2579 unsigned i = 0;
2580 for (; i != NumElems; ++i) {
2581 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002582 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002583 ElementBase = Elt;
2584 break;
2585 }
2586 }
2587
2588 if (!ElementBase.Val)
2589 return false;
2590
2591 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002592 SDOperand Arg = N->getOperand(i);
2593 if (Arg.getOpcode() == ISD::UNDEF) continue;
2594 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002595 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002596 }
2597
2598 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002599 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002600}
2601
Evan Chengc575ca22006-04-17 20:43:08 +00002602/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2603/// a splat of a single element and it's a 2 or 4 element mask.
2604bool X86::isSplatMask(SDNode *N) {
2605 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2606
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002607 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002608 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2609 return false;
2610 return ::isSplatMask(N);
2611}
2612
Evan Chengf686d9b2006-10-27 21:08:32 +00002613/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2614/// specifies a splat of zero element.
2615bool X86::isSplatLoMask(SDNode *N) {
2616 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2617
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002618 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002619 if (!isUndefOrEqual(N->getOperand(i), 0))
2620 return false;
2621 return true;
2622}
2623
Evan Cheng63d33002006-03-22 08:01:21 +00002624/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2625/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2626/// instructions.
2627unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002628 unsigned NumOperands = N->getNumOperands();
2629 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2630 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002631 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002632 unsigned Val = 0;
2633 SDOperand Arg = N->getOperand(NumOperands-i-1);
2634 if (Arg.getOpcode() != ISD::UNDEF)
2635 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002636 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002637 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002638 if (i != NumOperands - 1)
2639 Mask <<= Shift;
2640 }
Evan Cheng63d33002006-03-22 08:01:21 +00002641
2642 return Mask;
2643}
2644
Evan Cheng506d3df2006-03-29 23:07:14 +00002645/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2646/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2647/// instructions.
2648unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2649 unsigned Mask = 0;
2650 // 8 nodes, but we only care about the last 4.
2651 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002652 unsigned Val = 0;
2653 SDOperand Arg = N->getOperand(i);
2654 if (Arg.getOpcode() != ISD::UNDEF)
2655 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002656 Mask |= (Val - 4);
2657 if (i != 4)
2658 Mask <<= 2;
2659 }
2660
2661 return Mask;
2662}
2663
2664/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2665/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2666/// instructions.
2667unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2668 unsigned Mask = 0;
2669 // 8 nodes, but we only care about the first 4.
2670 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002671 unsigned Val = 0;
2672 SDOperand Arg = N->getOperand(i);
2673 if (Arg.getOpcode() != ISD::UNDEF)
2674 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002675 Mask |= Val;
2676 if (i != 0)
2677 Mask <<= 2;
2678 }
2679
2680 return Mask;
2681}
2682
Evan Chengc21a0532006-04-05 01:47:37 +00002683/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2684/// specifies a 8 element shuffle that can be broken into a pair of
2685/// PSHUFHW and PSHUFLW.
2686static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2687 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2688
2689 if (N->getNumOperands() != 8)
2690 return false;
2691
2692 // Lower quadword shuffled.
2693 for (unsigned i = 0; i != 4; ++i) {
2694 SDOperand Arg = N->getOperand(i);
2695 if (Arg.getOpcode() == ISD::UNDEF) continue;
2696 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2697 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2698 if (Val > 4)
2699 return false;
2700 }
2701
2702 // Upper quadword shuffled.
2703 for (unsigned i = 4; i != 8; ++i) {
2704 SDOperand Arg = N->getOperand(i);
2705 if (Arg.getOpcode() == ISD::UNDEF) continue;
2706 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2707 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2708 if (Val < 4 || Val > 7)
2709 return false;
2710 }
2711
2712 return true;
2713}
2714
Evan Cheng5ced1d82006-04-06 23:23:56 +00002715/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2716/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002717static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2718 SDOperand &V2, SDOperand &Mask,
2719 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002720 MVT::ValueType VT = Op.getValueType();
2721 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002722 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002723 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002724 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002725
2726 for (unsigned i = 0; i != NumElems; ++i) {
2727 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002728 if (Arg.getOpcode() == ISD::UNDEF) {
2729 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2730 continue;
2731 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002732 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2733 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2734 if (Val < NumElems)
2735 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2736 else
2737 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2738 }
2739
Evan Cheng9eca5e82006-10-25 21:49:50 +00002740 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002741 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002742 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002743}
2744
Evan Cheng533a0aa2006-04-19 20:35:22 +00002745/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2746/// match movhlps. The lower half elements should come from upper half of
2747/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002748/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002749static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2750 unsigned NumElems = Mask->getNumOperands();
2751 if (NumElems != 4)
2752 return false;
2753 for (unsigned i = 0, e = 2; i != e; ++i)
2754 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2755 return false;
2756 for (unsigned i = 2; i != 4; ++i)
2757 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2758 return false;
2759 return true;
2760}
2761
Evan Cheng5ced1d82006-04-06 23:23:56 +00002762/// isScalarLoadToVector - Returns true if the node is a scalar load that
2763/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002764static inline bool isScalarLoadToVector(SDNode *N) {
2765 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2766 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002767 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002768 }
2769 return false;
2770}
2771
Evan Cheng533a0aa2006-04-19 20:35:22 +00002772/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2773/// match movlp{s|d}. The lower half elements should come from lower half of
2774/// V1 (and in order), and the upper half elements should come from the upper
2775/// half of V2 (and in order). And since V1 will become the source of the
2776/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002777static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002778 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002779 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002780 // Is V2 is a vector load, don't do this transformation. We will try to use
2781 // load folding shufps op.
2782 if (ISD::isNON_EXTLoad(V2))
2783 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002784
Evan Cheng533a0aa2006-04-19 20:35:22 +00002785 unsigned NumElems = Mask->getNumOperands();
2786 if (NumElems != 2 && NumElems != 4)
2787 return false;
2788 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2789 if (!isUndefOrEqual(Mask->getOperand(i), i))
2790 return false;
2791 for (unsigned i = NumElems/2; i != NumElems; ++i)
2792 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2793 return false;
2794 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002795}
2796
Evan Cheng39623da2006-04-20 08:58:49 +00002797/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2798/// all the same.
2799static bool isSplatVector(SDNode *N) {
2800 if (N->getOpcode() != ISD::BUILD_VECTOR)
2801 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002802
Evan Cheng39623da2006-04-20 08:58:49 +00002803 SDOperand SplatValue = N->getOperand(0);
2804 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2805 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002806 return false;
2807 return true;
2808}
2809
Evan Cheng8cf723d2006-09-08 01:50:06 +00002810/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2811/// to an undef.
2812static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002813 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002814 return false;
2815
2816 SDOperand V1 = N->getOperand(0);
2817 SDOperand V2 = N->getOperand(1);
2818 SDOperand Mask = N->getOperand(2);
2819 unsigned NumElems = Mask.getNumOperands();
2820 for (unsigned i = 0; i != NumElems; ++i) {
2821 SDOperand Arg = Mask.getOperand(i);
2822 if (Arg.getOpcode() != ISD::UNDEF) {
2823 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2824 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2825 return false;
2826 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2827 return false;
2828 }
2829 }
2830 return true;
2831}
2832
Evan Cheng213d2cf2007-05-17 18:45:50 +00002833/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2834/// constant +0.0.
2835static inline bool isZeroNode(SDOperand Elt) {
2836 return ((isa<ConstantSDNode>(Elt) &&
2837 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2838 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002839 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002840}
2841
2842/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2843/// to an zero vector.
2844static bool isZeroShuffle(SDNode *N) {
2845 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2846 return false;
2847
2848 SDOperand V1 = N->getOperand(0);
2849 SDOperand V2 = N->getOperand(1);
2850 SDOperand Mask = N->getOperand(2);
2851 unsigned NumElems = Mask.getNumOperands();
2852 for (unsigned i = 0; i != NumElems; ++i) {
2853 SDOperand Arg = Mask.getOperand(i);
2854 if (Arg.getOpcode() != ISD::UNDEF) {
2855 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2856 if (Idx < NumElems) {
2857 unsigned Opc = V1.Val->getOpcode();
2858 if (Opc == ISD::UNDEF)
2859 continue;
2860 if (Opc != ISD::BUILD_VECTOR ||
2861 !isZeroNode(V1.Val->getOperand(Idx)))
2862 return false;
2863 } else if (Idx >= NumElems) {
2864 unsigned Opc = V2.Val->getOpcode();
2865 if (Opc == ISD::UNDEF)
2866 continue;
2867 if (Opc != ISD::BUILD_VECTOR ||
2868 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2869 return false;
2870 }
2871 }
2872 }
2873 return true;
2874}
2875
2876/// getZeroVector - Returns a vector of specified type with all zero elements.
2877///
2878static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2879 assert(MVT::isVector(VT) && "Expected a vector type");
Dan Gohman237898a2007-05-24 14:33:05 +00002880 unsigned NumElems = MVT::getVectorNumElements(VT);
Dan Gohman51eaa862007-06-14 22:58:02 +00002881 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002882 bool isFP = MVT::isFloatingPoint(EVT);
2883 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2884 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2885 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2886}
2887
Evan Cheng39623da2006-04-20 08:58:49 +00002888/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2889/// that point to V2 points to its first element.
2890static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2891 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2892
2893 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002894 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002895 unsigned NumElems = Mask.getNumOperands();
2896 for (unsigned i = 0; i != NumElems; ++i) {
2897 SDOperand Arg = Mask.getOperand(i);
2898 if (Arg.getOpcode() != ISD::UNDEF) {
2899 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2900 if (Val > NumElems) {
2901 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2902 Changed = true;
2903 }
2904 }
2905 MaskVec.push_back(Arg);
2906 }
2907
2908 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002909 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2910 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002911 return Mask;
2912}
2913
Evan Cheng017dcc62006-04-21 01:05:10 +00002914/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2915/// operation of specified width.
2916static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002917 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002918 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002919
Chris Lattner5a88b832007-02-25 07:10:00 +00002920 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002921 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2922 for (unsigned i = 1; i != NumElems; ++i)
2923 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002924 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002925}
2926
Evan Chengc575ca22006-04-17 20:43:08 +00002927/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2928/// of specified width.
2929static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2930 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002931 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002932 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002933 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2934 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2935 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2936 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002937 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002938}
2939
Evan Cheng39623da2006-04-20 08:58:49 +00002940/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2941/// of specified width.
2942static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2943 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002944 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002945 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002946 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002947 for (unsigned i = 0; i != Half; ++i) {
2948 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2949 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2950 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002951 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002952}
2953
Evan Chengc575ca22006-04-17 20:43:08 +00002954/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2955///
2956static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2957 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002958 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002959 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002960 unsigned NumElems = Mask.getNumOperands();
2961 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002962 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002963 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002964 NumElems >>= 1;
2965 }
2966 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2967
2968 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002969 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002970 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002971 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002972 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2973}
2974
Evan Chengba05f722006-04-21 23:03:30 +00002975/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Evan Cheng213d2cf2007-05-17 18:45:50 +00002976/// vector of zero or undef vector.
Evan Chengba05f722006-04-21 23:03:30 +00002977static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002978 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002979 bool isZero, SelectionDAG &DAG) {
2980 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002981 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002982 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002983 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002984 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Cheng017dcc62006-04-21 01:05:10 +00002985 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002986 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2987 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002988 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002989}
2990
Evan Chengc78d3b42006-04-24 18:01:45 +00002991/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2992///
2993static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2994 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002995 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002996 if (NumNonZero > 8)
2997 return SDOperand();
2998
2999 SDOperand V(0, 0);
3000 bool First = true;
3001 for (unsigned i = 0; i < 16; ++i) {
3002 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3003 if (ThisIsNonZero && First) {
3004 if (NumZero)
3005 V = getZeroVector(MVT::v8i16, DAG);
3006 else
3007 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3008 First = false;
3009 }
3010
3011 if ((i & 1) != 0) {
3012 SDOperand ThisElt(0, 0), LastElt(0, 0);
3013 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3014 if (LastIsNonZero) {
3015 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3016 }
3017 if (ThisIsNonZero) {
3018 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3019 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3020 ThisElt, DAG.getConstant(8, MVT::i8));
3021 if (LastIsNonZero)
3022 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3023 } else
3024 ThisElt = LastElt;
3025
3026 if (ThisElt.Val)
3027 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00003028 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003029 }
3030 }
3031
3032 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3033}
3034
Bill Wendlinga348c562007-03-22 18:42:45 +00003035/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003036///
3037static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3038 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003039 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003040 if (NumNonZero > 4)
3041 return SDOperand();
3042
3043 SDOperand V(0, 0);
3044 bool First = true;
3045 for (unsigned i = 0; i < 8; ++i) {
3046 bool isNonZero = (NonZeros & (1 << i)) != 0;
3047 if (isNonZero) {
3048 if (First) {
3049 if (NumZero)
3050 V = getZeroVector(MVT::v8i16, DAG);
3051 else
3052 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3053 First = false;
3054 }
3055 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00003056 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00003057 }
3058 }
3059
3060 return V;
3061}
3062
Evan Cheng0db9fe62006-04-25 20:13:52 +00003063SDOperand
3064X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3065 // All zero's are handled with pxor.
3066 if (ISD::isBuildVectorAllZeros(Op.Val))
3067 return Op;
3068
3069 // All one's are handled with pcmpeqd.
3070 if (ISD::isBuildVectorAllOnes(Op.Val))
3071 return Op;
3072
3073 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003074 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003075 unsigned EVTBits = MVT::getSizeInBits(EVT);
3076
3077 unsigned NumElems = Op.getNumOperands();
3078 unsigned NumZero = 0;
3079 unsigned NumNonZero = 0;
3080 unsigned NonZeros = 0;
Dan Gohmana3941172007-07-24 22:55:08 +00003081 unsigned NumNonZeroImms = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003082 std::set<SDOperand> Values;
3083 for (unsigned i = 0; i < NumElems; ++i) {
3084 SDOperand Elt = Op.getOperand(i);
3085 if (Elt.getOpcode() != ISD::UNDEF) {
3086 Values.insert(Elt);
3087 if (isZeroNode(Elt))
3088 NumZero++;
3089 else {
3090 NonZeros |= (1 << i);
3091 NumNonZero++;
Dan Gohmana3941172007-07-24 22:55:08 +00003092 if (Elt.getOpcode() == ISD::Constant ||
3093 Elt.getOpcode() == ISD::ConstantFP)
3094 NumNonZeroImms++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003095 }
3096 }
3097 }
3098
Dan Gohman7f321562007-06-25 16:23:39 +00003099 if (NumNonZero == 0) {
3100 if (NumZero == 0)
3101 // All undef vector. Return an UNDEF.
3102 return DAG.getNode(ISD::UNDEF, VT);
3103 else
3104 // A mix of zero and undef. Return a zero vector.
3105 return getZeroVector(VT, DAG);
3106 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003107
3108 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3109 if (Values.size() == 1)
3110 return SDOperand();
3111
3112 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003113 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003114 unsigned Idx = CountTrailingZeros_32(NonZeros);
3115 SDOperand Item = Op.getOperand(Idx);
3116 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3117 if (Idx == 0)
3118 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3119 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3120 NumZero > 0, DAG);
3121
3122 if (EVTBits == 32) {
3123 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3124 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3125 DAG);
3126 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003127 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003128 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003129 for (unsigned i = 0; i < NumElems; i++)
3130 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003131 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3132 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003133 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3134 DAG.getNode(ISD::UNDEF, VT), Mask);
3135 }
3136 }
3137
Dan Gohmana3941172007-07-24 22:55:08 +00003138 // A vector full of immediates; various special cases are already
3139 // handled, so this is best done with a single constant-pool load.
3140 if (NumNonZero == NumNonZeroImms)
3141 return SDOperand();
3142
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003143 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003144 if (EVTBits == 64)
3145 return SDOperand();
3146
3147 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003148 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003149 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3150 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003151 if (V.Val) return V;
3152 }
3153
Bill Wendling826f36f2007-03-28 00:57:11 +00003154 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003155 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3156 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003157 if (V.Val) return V;
3158 }
3159
3160 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003161 SmallVector<SDOperand, 8> V;
3162 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003163 if (NumElems == 4 && NumZero > 0) {
3164 for (unsigned i = 0; i < 4; ++i) {
3165 bool isZero = !(NonZeros & (1 << i));
3166 if (isZero)
3167 V[i] = getZeroVector(VT, DAG);
3168 else
3169 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3170 }
3171
3172 for (unsigned i = 0; i < 2; ++i) {
3173 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3174 default: break;
3175 case 0:
3176 V[i] = V[i*2]; // Must be a zero vector.
3177 break;
3178 case 1:
3179 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3180 getMOVLMask(NumElems, DAG));
3181 break;
3182 case 2:
3183 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3184 getMOVLMask(NumElems, DAG));
3185 break;
3186 case 3:
3187 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3188 getUnpacklMask(NumElems, DAG));
3189 break;
3190 }
3191 }
3192
Evan Cheng069287d2006-05-16 07:21:53 +00003193 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003194 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003195 // FIXME: we can do the same for v4f32 case when we know both parts of
3196 // the lower half come from scalar_to_vector (loadf32). We should do
3197 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003198 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003199 return V[0];
3200 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003201 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003202 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 bool Reverse = (NonZeros & 0x3) == 2;
3204 for (unsigned i = 0; i < 2; ++i)
3205 if (Reverse)
3206 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3207 else
3208 MaskVec.push_back(DAG.getConstant(i, EVT));
3209 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3210 for (unsigned i = 0; i < 2; ++i)
3211 if (Reverse)
3212 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3213 else
3214 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003215 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3216 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003217 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3218 }
3219
3220 if (Values.size() > 2) {
3221 // Expand into a number of unpckl*.
3222 // e.g. for v4f32
3223 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3224 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3225 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3226 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3227 for (unsigned i = 0; i < NumElems; ++i)
3228 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3229 NumElems >>= 1;
3230 while (NumElems != 0) {
3231 for (unsigned i = 0; i < NumElems; ++i)
3232 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3233 UnpckMask);
3234 NumElems >>= 1;
3235 }
3236 return V[0];
3237 }
3238
3239 return SDOperand();
3240}
3241
3242SDOperand
3243X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3244 SDOperand V1 = Op.getOperand(0);
3245 SDOperand V2 = Op.getOperand(1);
3246 SDOperand PermMask = Op.getOperand(2);
3247 MVT::ValueType VT = Op.getValueType();
3248 unsigned NumElems = PermMask.getNumOperands();
3249 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3250 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003251 bool V1IsSplat = false;
3252 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003253
Evan Cheng8cf723d2006-09-08 01:50:06 +00003254 if (isUndefShuffle(Op.Val))
3255 return DAG.getNode(ISD::UNDEF, VT);
3256
Evan Cheng213d2cf2007-05-17 18:45:50 +00003257 if (isZeroShuffle(Op.Val))
3258 return getZeroVector(VT, DAG);
3259
Evan Cheng49892af2007-06-19 00:02:56 +00003260 if (isIdentityMask(PermMask.Val))
3261 return V1;
3262 else if (isIdentityMask(PermMask.Val, true))
3263 return V2;
3264
Evan Cheng0db9fe62006-04-25 20:13:52 +00003265 if (isSplatMask(PermMask.Val)) {
3266 if (NumElems <= 4) return Op;
3267 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003268 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003269 }
3270
Evan Cheng9bbbb982006-10-25 20:48:19 +00003271 if (X86::isMOVLMask(PermMask.Val))
3272 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003273
Evan Cheng9bbbb982006-10-25 20:48:19 +00003274 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3275 X86::isMOVSLDUPMask(PermMask.Val) ||
3276 X86::isMOVHLPSMask(PermMask.Val) ||
3277 X86::isMOVHPMask(PermMask.Val) ||
3278 X86::isMOVLPMask(PermMask.Val))
3279 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003280
Evan Cheng9bbbb982006-10-25 20:48:19 +00003281 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3282 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003283 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003284
Evan Cheng9eca5e82006-10-25 21:49:50 +00003285 bool Commuted = false;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003286 V1IsSplat = isSplatVector(V1.Val);
3287 V2IsSplat = isSplatVector(V2.Val);
3288 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003289 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003290 std::swap(V1IsSplat, V2IsSplat);
3291 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003292 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003293 }
3294
3295 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3296 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003297 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003298 if (V2IsSplat) {
3299 // V2 is a splat, so the mask may be malformed. That is, it may point
3300 // to any V2 element. The instruction selectior won't like this. Get
3301 // a corrected mask and commute to form a proper MOVS{S|D}.
3302 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3303 if (NewMask.Val != PermMask.Val)
3304 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003305 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003306 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003307 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003308
Evan Chengd9b8e402006-10-16 06:36:00 +00003309 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003310 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003311 X86::isUNPCKLMask(PermMask.Val) ||
3312 X86::isUNPCKHMask(PermMask.Val))
3313 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003314
Evan Cheng9bbbb982006-10-25 20:48:19 +00003315 if (V2IsSplat) {
3316 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003317 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003318 // new vector_shuffle with the corrected mask.
3319 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3320 if (NewMask.Val != PermMask.Val) {
3321 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3322 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3323 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3324 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3325 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3326 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003327 }
3328 }
3329 }
3330
3331 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003332 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3333 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3334
3335 if (Commuted) {
3336 // Commute is back and try unpck* again.
3337 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3338 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003339 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003340 X86::isUNPCKLMask(PermMask.Val) ||
3341 X86::isUNPCKHMask(PermMask.Val))
3342 return Op;
3343 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003344
3345 // If VT is integer, try PSHUF* first, then SHUFP*.
3346 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003347 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3348 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3349 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3350 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003351 X86::isPSHUFHWMask(PermMask.Val) ||
3352 X86::isPSHUFLWMask(PermMask.Val)) {
3353 if (V2.getOpcode() != ISD::UNDEF)
3354 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3355 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3356 return Op;
3357 }
3358
Chris Lattner07c70cd2007-05-17 17:13:13 +00003359 if (X86::isSHUFPMask(PermMask.Val) &&
3360 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003361 return Op;
3362
3363 // Handle v8i16 shuffle high / low shuffle node pair.
3364 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3365 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003366 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003367 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003368 for (unsigned i = 0; i != 4; ++i)
3369 MaskVec.push_back(PermMask.getOperand(i));
3370 for (unsigned i = 4; i != 8; ++i)
3371 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003372 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3373 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003374 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3375 MaskVec.clear();
3376 for (unsigned i = 0; i != 4; ++i)
3377 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3378 for (unsigned i = 4; i != 8; ++i)
3379 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00003380 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003381 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3382 }
3383 } else {
3384 // Floating point cases in the other order.
3385 if (X86::isSHUFPMask(PermMask.Val))
3386 return Op;
3387 if (X86::isPSHUFDMask(PermMask.Val) ||
3388 X86::isPSHUFHWMask(PermMask.Val) ||
3389 X86::isPSHUFLWMask(PermMask.Val)) {
3390 if (V2.getOpcode() != ISD::UNDEF)
3391 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3392 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3393 return Op;
3394 }
3395 }
3396
Chris Lattner07c70cd2007-05-17 17:13:13 +00003397 if (NumElems == 4 &&
3398 // Don't do this for MMX.
3399 MVT::getSizeInBits(VT) != 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003400 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003401 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003402 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003403 Locs.reserve(NumElems);
Chris Lattner5a88b832007-02-25 07:10:00 +00003404 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3405 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003406 unsigned NumHi = 0;
3407 unsigned NumLo = 0;
3408 // If no more than two elements come from either vector. This can be
3409 // implemented with two shuffles. First shuffle gather the elements.
3410 // The second shuffle, which takes the first shuffle as both of its
3411 // vector operands, put the elements into the right order.
3412 for (unsigned i = 0; i != NumElems; ++i) {
3413 SDOperand Elt = PermMask.getOperand(i);
3414 if (Elt.getOpcode() == ISD::UNDEF) {
3415 Locs[i] = std::make_pair(-1, -1);
3416 } else {
3417 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3418 if (Val < NumElems) {
3419 Locs[i] = std::make_pair(0, NumLo);
3420 Mask1[NumLo] = Elt;
3421 NumLo++;
3422 } else {
3423 Locs[i] = std::make_pair(1, NumHi);
3424 if (2+NumHi < NumElems)
3425 Mask1[2+NumHi] = Elt;
3426 NumHi++;
3427 }
3428 }
3429 }
3430 if (NumLo <= 2 && NumHi <= 2) {
3431 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003432 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3433 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003434 for (unsigned i = 0; i != NumElems; ++i) {
3435 if (Locs[i].first == -1)
3436 continue;
3437 else {
3438 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3439 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3440 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3441 }
3442 }
3443
3444 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003445 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3446 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003447 }
3448
3449 // Break it into (shuffle shuffle_hi, shuffle_lo).
3450 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003451 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3452 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3453 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003454 unsigned MaskIdx = 0;
3455 unsigned LoIdx = 0;
3456 unsigned HiIdx = NumElems/2;
3457 for (unsigned i = 0; i != NumElems; ++i) {
3458 if (i == NumElems/2) {
3459 MaskPtr = &HiMask;
3460 MaskIdx = 1;
3461 LoIdx = 0;
3462 HiIdx = NumElems/2;
3463 }
3464 SDOperand Elt = PermMask.getOperand(i);
3465 if (Elt.getOpcode() == ISD::UNDEF) {
3466 Locs[i] = std::make_pair(-1, -1);
3467 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3468 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3469 (*MaskPtr)[LoIdx] = Elt;
3470 LoIdx++;
3471 } else {
3472 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3473 (*MaskPtr)[HiIdx] = Elt;
3474 HiIdx++;
3475 }
3476 }
3477
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003478 SDOperand LoShuffle =
3479 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003480 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3481 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003482 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003483 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003484 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3485 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003486 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003487 for (unsigned i = 0; i != NumElems; ++i) {
3488 if (Locs[i].first == -1) {
3489 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3490 } else {
3491 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3492 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3493 }
3494 }
3495 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003496 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3497 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003498 }
3499
3500 return SDOperand();
3501}
3502
3503SDOperand
3504X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3505 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3506 return SDOperand();
3507
3508 MVT::ValueType VT = Op.getValueType();
3509 // TODO: handle v16i8.
3510 if (MVT::getSizeInBits(VT) == 16) {
3511 // Transform it so it match pextrw which produces a 32-bit result.
3512 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3513 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3514 Op.getOperand(0), Op.getOperand(1));
3515 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3516 DAG.getValueType(VT));
3517 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3518 } else if (MVT::getSizeInBits(VT) == 32) {
3519 SDOperand Vec = Op.getOperand(0);
3520 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3521 if (Idx == 0)
3522 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003523 // SHUFPS the element to the lowest double word, then movss.
3524 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003525 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003526 IdxVec.
3527 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3528 IdxVec.
3529 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3530 IdxVec.
3531 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3532 IdxVec.
3533 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003534 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3535 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003536 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003537 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003538 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003539 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003540 } else if (MVT::getSizeInBits(VT) == 64) {
3541 SDOperand Vec = Op.getOperand(0);
3542 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3543 if (Idx == 0)
3544 return Op;
3545
3546 // UNPCKHPD the element to the lowest double word, then movsd.
3547 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3548 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3549 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003550 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003551 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003552 IdxVec.
3553 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003554 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3555 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003556 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3557 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3558 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003559 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003560 }
3561
3562 return SDOperand();
3563}
3564
3565SDOperand
3566X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00003567 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00003568 // as its second argument.
3569 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003570 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003571 SDOperand N0 = Op.getOperand(0);
3572 SDOperand N1 = Op.getOperand(1);
3573 SDOperand N2 = Op.getOperand(2);
3574 if (MVT::getSizeInBits(BaseVT) == 16) {
3575 if (N1.getValueType() != MVT::i32)
3576 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3577 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00003578 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003579 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3580 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3581 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3582 if (Idx == 0) {
3583 // Use a movss.
3584 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3585 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman51eaa862007-06-14 22:58:02 +00003586 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003587 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003588 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3589 for (unsigned i = 1; i <= 3; ++i)
3590 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3591 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003592 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3593 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003594 } else {
3595 // Use two pinsrw instructions to insert a 32 bit value.
3596 Idx <<= 1;
3597 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng4ebcc8c2007-07-31 06:21:44 +00003598 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3599 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3600 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3601 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003602 }
3603 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3604 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003605 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003606 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3607 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003608 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003609 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3610 }
3611 }
3612
3613 return SDOperand();
3614}
3615
3616SDOperand
3617X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3618 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3619 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3620}
3621
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003622// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003623// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3624// one of the above mentioned nodes. It has to be wrapped because otherwise
3625// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3626// be used to form addressing mode. These wrapped nodes will be selected
3627// into MOV32ri.
3628SDOperand
3629X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3630 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003631 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3632 getPointerTy(),
3633 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003634 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003635 // With PIC, the address is actually $g + Offset.
3636 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3637 !Subtarget->isPICStyleRIPRel()) {
3638 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3639 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3640 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003641 }
3642
3643 return Result;
3644}
3645
3646SDOperand
3647X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3648 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003649 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003650 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003651 // With PIC, the address is actually $g + Offset.
3652 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3653 !Subtarget->isPICStyleRIPRel()) {
3654 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3655 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3656 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003657 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003658
3659 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3660 // load the value at address GV, not the value of GV itself. This means that
3661 // the GlobalAddress must be in the base or index register of the address, not
3662 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003663 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003664 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3665 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003666
3667 return Result;
3668}
3669
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003670// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3671static SDOperand
3672LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3673 const MVT::ValueType PtrVT) {
3674 SDOperand InFlag;
3675 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3676 DAG.getNode(X86ISD::GlobalBaseReg,
3677 PtrVT), InFlag);
3678 InFlag = Chain.getValue(1);
3679
3680 // emit leal symbol@TLSGD(,%ebx,1), %eax
3681 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3682 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3683 GA->getValueType(0),
3684 GA->getOffset());
3685 SDOperand Ops[] = { Chain, TGA, InFlag };
3686 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3687 InFlag = Result.getValue(2);
3688 Chain = Result.getValue(1);
3689
3690 // call ___tls_get_addr. This function receives its argument in
3691 // the register EAX.
3692 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3693 InFlag = Chain.getValue(1);
3694
3695 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3696 SDOperand Ops1[] = { Chain,
3697 DAG.getTargetExternalSymbol("___tls_get_addr",
3698 PtrVT),
3699 DAG.getRegister(X86::EAX, PtrVT),
3700 DAG.getRegister(X86::EBX, PtrVT),
3701 InFlag };
3702 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3703 InFlag = Chain.getValue(1);
3704
3705 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3706}
3707
3708// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3709// "local exec" model.
3710static SDOperand
3711LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3712 const MVT::ValueType PtrVT) {
3713 // Get the Thread Pointer
3714 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3715 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3716 // exec)
3717 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3718 GA->getValueType(0),
3719 GA->getOffset());
3720 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003721
3722 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3723 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3724
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003725 // The address of the thread local variable is the add of the thread
3726 // pointer with the offset of the variable.
3727 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3728}
3729
3730SDOperand
3731X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3732 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003733 // TODO: implement the "initial exec"model for pic executables
3734 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3735 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003736 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3737 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3738 // otherwise use the "Local Exec"TLS Model
3739 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3740 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3741 else
3742 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3743}
3744
Evan Cheng0db9fe62006-04-25 20:13:52 +00003745SDOperand
3746X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3747 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003748 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003749 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003750 // With PIC, the address is actually $g + Offset.
3751 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3752 !Subtarget->isPICStyleRIPRel()) {
3753 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3754 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3755 Result);
3756 }
3757
3758 return Result;
3759}
3760
3761SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3762 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3763 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3764 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3765 // With PIC, the address is actually $g + Offset.
3766 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3767 !Subtarget->isPICStyleRIPRel()) {
3768 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3769 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3770 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771 }
3772
3773 return Result;
3774}
3775
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003776/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3777/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003778SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003779 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3780 "Not an i64 shift!");
3781 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3782 SDOperand ShOpLo = Op.getOperand(0);
3783 SDOperand ShOpHi = Op.getOperand(1);
3784 SDOperand ShAmt = Op.getOperand(2);
3785 SDOperand Tmp1 = isSRA ?
3786 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3787 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003788
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003789 SDOperand Tmp2, Tmp3;
3790 if (Op.getOpcode() == ISD::SHL_PARTS) {
3791 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3792 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3793 } else {
3794 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3795 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3796 }
Evan Chenge3413162006-01-09 18:33:28 +00003797
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003798 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3799 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3800 DAG.getConstant(32, MVT::i8));
3801 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3802 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00003803
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003804 SDOperand Hi, Lo;
3805 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3806 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3807 SmallVector<SDOperand, 4> Ops;
3808 if (Op.getOpcode() == ISD::SHL_PARTS) {
3809 Ops.push_back(Tmp2);
3810 Ops.push_back(Tmp3);
3811 Ops.push_back(CC);
3812 Ops.push_back(Cond);
3813 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003814
Evan Chenge3413162006-01-09 18:33:28 +00003815 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003816 Ops.push_back(Tmp3);
3817 Ops.push_back(Tmp1);
3818 Ops.push_back(CC);
3819 Ops.push_back(Cond);
3820 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3821 } else {
3822 Ops.push_back(Tmp2);
3823 Ops.push_back(Tmp3);
3824 Ops.push_back(CC);
3825 Ops.push_back(Cond);
3826 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3827
3828 Ops.clear();
3829 Ops.push_back(Tmp3);
3830 Ops.push_back(Tmp1);
3831 Ops.push_back(CC);
3832 Ops.push_back(Cond);
3833 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3834 }
3835
3836 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3837 Ops.clear();
3838 Ops.push_back(Lo);
3839 Ops.push_back(Hi);
3840 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003841}
Evan Chenga3195e82006-01-12 22:54:21 +00003842
Evan Cheng0db9fe62006-04-25 20:13:52 +00003843SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3844 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3845 Op.getOperand(0).getValueType() >= MVT::i16 &&
3846 "Unknown SINT_TO_FP to lower!");
3847
3848 SDOperand Result;
3849 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3850 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3851 MachineFunction &MF = DAG.getMachineFunction();
3852 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3853 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003854 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003855 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003856
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003857 // These are really Legal; caller falls through into that case.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003858 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3859 return Result;
3860 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003861 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003862 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3863 Subtarget->is64Bit())
3864 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003865
Evan Cheng0db9fe62006-04-25 20:13:52 +00003866 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003867 SDVTList Tys;
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003868 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3869 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003870 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00003871 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3872 else
Dale Johannesen849f2142007-07-03 00:53:03 +00003873 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003874 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003875 Ops.push_back(Chain);
3876 Ops.push_back(StackSlot);
3877 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003878 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003879 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003880
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003881 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003882 Chain = Result.getValue(1);
3883 SDOperand InFlag = Result.getValue(2);
3884
3885 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3886 // shouldn't be necessary except that RFP cannot be live across
3887 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003888 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003889 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003890 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00003891 Tys = DAG.getVTList(MVT::Other);
3892 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003893 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003894 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003895 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003896 Ops.push_back(DAG.getValueType(Op.getValueType()));
3897 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003898 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003899 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003900 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003901
Evan Cheng0db9fe62006-04-25 20:13:52 +00003902 return Result;
3903}
3904
3905SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3906 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3907 "Unknown FP_TO_SINT to lower!");
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003908 SDOperand Result;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003909
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003910 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003911 if (Op.getValueType() == MVT::i32 &&
3912 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
3913 return Result;
3914 if (Op.getValueType() == MVT::i32 &&
3915 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003916 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003917 if (Subtarget->is64Bit() &&
3918 Op.getValueType() == MVT::i64 &&
3919 Op.getOperand(0).getValueType() != MVT::f80)
3920 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003921
Evan Cheng87c89352007-10-15 20:11:21 +00003922 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3923 // stack slot.
3924 MachineFunction &MF = DAG.getMachineFunction();
3925 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3926 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3927 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003928 unsigned Opc;
3929 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003930 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3931 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3932 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3933 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003935
Evan Cheng0db9fe62006-04-25 20:13:52 +00003936 SDOperand Chain = DAG.getEntryNode();
3937 SDOperand Value = Op.getOperand(0);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003938 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3939 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003940 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003941 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00003942 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003943 SDOperand Ops[] = {
3944 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3945 };
3946 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 Chain = Value.getValue(1);
3948 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3949 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3950 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003951
Evan Cheng0db9fe62006-04-25 20:13:52 +00003952 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00003953 SDOperand Ops[] = { Chain, Value, StackSlot };
3954 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00003955
Chris Lattner7ef1a4b2007-10-17 06:17:29 +00003956 // Load the result. If this is an i64 load on an x86-32 host, expand the
3957 // load.
3958 if (Op.getValueType() != MVT::i64 || Subtarget->is64Bit())
3959 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
3960
3961 SDOperand Lo = DAG.getLoad(MVT::i32, FIST, StackSlot, NULL, 0);
3962 StackSlot = DAG.getNode(ISD::ADD, StackSlot.getValueType(), StackSlot,
3963 DAG.getConstant(StackSlot.getValueType(), 4));
3964 SDOperand Hi = DAG.getLoad(MVT::i32, FIST, StackSlot, NULL, 0);
3965
3966
3967 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003968}
3969
3970SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3971 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003972 MVT::ValueType EltVT = VT;
3973 if (MVT::isVector(VT))
3974 EltVT = MVT::getVectorElementType(VT);
3975 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003976 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00003977 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003978 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00003979 CV.push_back(C);
3980 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003981 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003982 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00003983 CV.push_back(C);
3984 CV.push_back(C);
3985 CV.push_back(C);
3986 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003987 }
Dan Gohmand3006222007-07-27 17:16:43 +00003988 Constant *C = ConstantVector::get(CV);
3989 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3990 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3991 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003992 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3993}
3994
3995SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3996 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003997 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00003998 unsigned EltNum = 1;
3999 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004000 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004001 EltNum = MVT::getVectorNumElements(VT);
4002 }
Dan Gohman20382522007-07-10 00:05:58 +00004003 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004004 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004005 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004006 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004007 CV.push_back(C);
4008 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004009 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004010 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004011 CV.push_back(C);
4012 CV.push_back(C);
4013 CV.push_back(C);
4014 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004015 }
Dan Gohmand3006222007-07-27 17:16:43 +00004016 Constant *C = ConstantVector::get(CV);
4017 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4018 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4019 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004020 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004021 return DAG.getNode(ISD::BIT_CONVERT, VT,
4022 DAG.getNode(ISD::XOR, MVT::v2i64,
4023 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4024 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4025 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004026 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4027 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004028}
4029
Evan Cheng68c47cb2007-01-05 07:55:56 +00004030SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004031 SDOperand Op0 = Op.getOperand(0);
4032 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004033 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004034 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004035 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004036
4037 // If second operand is smaller, extend it first.
4038 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4039 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4040 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004041 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004042 }
4043
Evan Cheng68c47cb2007-01-05 07:55:56 +00004044 // First get the sign bit of second operand.
4045 std::vector<Constant*> CV;
4046 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004047 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4048 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004049 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004050 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4051 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4052 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4053 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004054 }
Dan Gohmand3006222007-07-27 17:16:43 +00004055 Constant *C = ConstantVector::get(CV);
4056 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4057 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4058 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004059 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004060
4061 // Shift sign bit right or left if the two operands have different types.
4062 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4063 // Op0 is MVT::f32, Op1 is MVT::f64.
4064 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4065 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4066 DAG.getConstant(32, MVT::i32));
4067 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4068 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4069 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004070 }
4071
Evan Cheng73d6cf12007-01-05 21:37:56 +00004072 // Clear first operand sign bit.
4073 CV.clear();
4074 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004075 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4076 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004077 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004078 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4079 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4080 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4081 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004082 }
Dan Gohmand3006222007-07-27 17:16:43 +00004083 C = ConstantVector::get(CV);
4084 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4085 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4086 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004087 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4088
4089 // Or the value with the sign bit.
4090 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004091}
4092
Evan Chenge5f62042007-09-29 00:00:36 +00004093SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004094 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004095 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004096 SDOperand Op0 = Op.getOperand(0);
4097 SDOperand Op1 = Op.getOperand(1);
4098 SDOperand CC = Op.getOperand(2);
4099 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4100 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4101 unsigned X86CC;
4102
Evan Cheng0488db92007-09-25 01:57:46 +00004103 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004104 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004105 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4106 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004107 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004108 }
Evan Cheng0488db92007-09-25 01:57:46 +00004109
4110 assert(isFP && "Illegal integer SetCC!");
4111
Evan Chenge5f62042007-09-29 00:00:36 +00004112 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004113 switch (SetCCOpcode) {
4114 default: assert(false && "Illegal floating point SetCC!");
4115 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004116 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004117 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004118 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004119 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4120 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4121 }
4122 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004123 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004124 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004125 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004126 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4127 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4128 }
4129 }
4130}
4131
4132
Evan Cheng0db9fe62006-04-25 20:13:52 +00004133SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004134 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004135 SDOperand Cond = Op.getOperand(0);
4136 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004137
Evan Cheng734503b2006-09-11 02:19:56 +00004138 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004139 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004140
Evan Cheng3f41d662007-10-08 22:16:29 +00004141 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4142 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004143 if (Cond.getOpcode() == X86ISD::SETCC) {
4144 CC = Cond.getOperand(0);
4145
Evan Cheng734503b2006-09-11 02:19:56 +00004146 SDOperand Cmp = Cond.getOperand(1);
4147 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004148 MVT::ValueType VT = Op.getValueType();
4149 bool IllegalFPCMov = false;
4150 if (VT == MVT::f32 && !X86ScalarSSEf32)
4151 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4152 else if (VT == MVT::f64 && !X86ScalarSSEf64)
4153 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Dale Johannesenc274f542007-10-16 18:09:08 +00004154 else if (VT == MVT::f80)
4155 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chenge5f62042007-09-29 00:00:36 +00004156 if ((Opc == X86ISD::CMP ||
4157 Opc == X86ISD::COMI ||
4158 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004159 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004160 addTest = false;
4161 }
4162 }
4163
4164 if (addTest) {
4165 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004166 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004167 }
4168
4169 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4170 MVT::Flag);
4171 SmallVector<SDOperand, 4> Ops;
4172 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4173 // condition is true.
4174 Ops.push_back(Op.getOperand(2));
4175 Ops.push_back(Op.getOperand(1));
4176 Ops.push_back(CC);
4177 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004178 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004179}
4180
Evan Cheng0db9fe62006-04-25 20:13:52 +00004181SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004182 bool addTest = true;
4183 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004184 SDOperand Cond = Op.getOperand(1);
4185 SDOperand Dest = Op.getOperand(2);
4186 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004187
Evan Cheng0db9fe62006-04-25 20:13:52 +00004188 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004189 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004190
Evan Cheng3f41d662007-10-08 22:16:29 +00004191 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4192 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004193 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004194 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004195
Evan Cheng734503b2006-09-11 02:19:56 +00004196 SDOperand Cmp = Cond.getOperand(1);
4197 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004198 if (Opc == X86ISD::CMP ||
4199 Opc == X86ISD::COMI ||
4200 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004201 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004202 addTest = false;
4203 }
4204 }
4205
4206 if (addTest) {
4207 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004208 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004209 }
Evan Chenge5f62042007-09-29 00:00:36 +00004210 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004211 Chain, Op.getOperand(2), CC, Cond);
4212}
4213
Evan Cheng32fe1032006-05-25 00:59:30 +00004214SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004215 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4216 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004217
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004218 if (Subtarget->is64Bit())
4219 if(CallingConv==CallingConv::Fast && isTailCall && PerformTailCallOpt)
4220 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4221 else
4222 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00004223 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004224 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004225 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004226 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00004227 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004228 if (isTailCall && PerformTailCallOpt)
4229 return LowerX86_TailCallTo(Op, DAG, CallingConv);
4230 else
4231 return LowerCCCCallTo(Op,DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004232 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004233 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004234 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004235 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00004236 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004237 }
Evan Cheng32fe1032006-05-25 00:59:30 +00004238}
4239
Anton Korobeynikove060b532007-04-17 19:34:00 +00004240
4241// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4242// Calls to _alloca is needed to probe the stack when allocating more than 4k
4243// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4244// that the guard pages used by the OS virtual memory manager are allocated in
4245// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004246SDOperand
4247X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4248 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004249 assert(Subtarget->isTargetCygMing() &&
4250 "This should be used only on Cygwin/Mingw targets");
4251
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004252 // Get the inputs.
4253 SDOperand Chain = Op.getOperand(0);
4254 SDOperand Size = Op.getOperand(1);
4255 // FIXME: Ensure alignment here
4256
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004257 SDOperand Flag;
4258
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004259 MVT::ValueType IntPtr = getPointerTy();
4260 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004261
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004262 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4263 Flag = Chain.getValue(1);
4264
4265 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4266 SDOperand Ops[] = { Chain,
4267 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4268 DAG.getRegister(X86::EAX, IntPtr),
4269 Flag };
4270 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4271 Flag = Chain.getValue(1);
4272
4273 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004274
4275 std::vector<MVT::ValueType> Tys;
4276 Tys.push_back(SPTy);
4277 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004278 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4279 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004280}
4281
Evan Cheng1bc78042006-04-26 01:20:17 +00004282SDOperand
4283X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00004284 MachineFunction &MF = DAG.getMachineFunction();
4285 const Function* Fn = MF.getFunction();
4286 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00004287 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00004288 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00004289 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00004290
Evan Cheng25caf632006-05-23 21:06:34 +00004291 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00004292 if (Subtarget->is64Bit())
4293 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00004294 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004295 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004296 default:
4297 assert(0 && "Unsupported calling convention");
4298 case CallingConv::Fast:
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004299 return LowerCCCArguments(Op,DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004300 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00004301 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004302 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004303 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004304 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004305 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004306 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00004307 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00004308 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004309 }
Evan Cheng1bc78042006-04-26 01:20:17 +00004310}
4311
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4313 SDOperand InFlag(0, 0);
4314 SDOperand Chain = Op.getOperand(0);
4315 unsigned Align =
4316 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4317 if (Align == 0) Align = 1;
4318
4319 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004320 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004321 // The libc version is likely to be faster for these cases. It can use the
4322 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004323 if ((Align & 3) != 0 ||
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004324 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004325 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004326 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004327 TargetLowering::ArgListTy Args;
4328 TargetLowering::ArgListEntry Entry;
4329 Entry.Node = Op.getOperand(1);
4330 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004331 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004332 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004333 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4334 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004335 Args.push_back(Entry);
4336 Entry.Node = Op.getOperand(3);
4337 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004339 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004340 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4341 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004342 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004343
Evan Cheng0db9fe62006-04-25 20:13:52 +00004344 MVT::ValueType AVT;
4345 SDOperand Count;
4346 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4347 unsigned BytesLeft = 0;
4348 bool TwoRepStos = false;
4349 if (ValC) {
4350 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004351 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004352
Evan Cheng0db9fe62006-04-25 20:13:52 +00004353 // If the value is a constant, then we can potentially use larger sets.
4354 switch (Align & 3) {
4355 case 2: // WORD aligned
4356 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004358 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004359 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004360 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004361 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004362 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004363 Val = (Val << 8) | Val;
4364 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004365 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4366 AVT = MVT::i64;
4367 ValReg = X86::RAX;
4368 Val = (Val << 32) | Val;
4369 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 break;
4371 default: // Byte aligned
4372 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004373 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004374 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004375 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004376 }
4377
Evan Cheng25ab6902006-09-08 06:48:29 +00004378 if (AVT > MVT::i8) {
4379 if (I) {
4380 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4381 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4382 BytesLeft = I->getValue() % UBytes;
4383 } else {
4384 assert(AVT >= MVT::i32 &&
4385 "Do not use rep;stos if not at least DWORD aligned");
4386 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4387 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4388 TwoRepStos = true;
4389 }
4390 }
4391
Evan Cheng0db9fe62006-04-25 20:13:52 +00004392 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4393 InFlag);
4394 InFlag = Chain.getValue(1);
4395 } else {
4396 AVT = MVT::i8;
4397 Count = Op.getOperand(3);
4398 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4399 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004400 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004401
Evan Cheng25ab6902006-09-08 06:48:29 +00004402 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4403 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004405 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4406 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004407 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004408
Chris Lattnerd96d0722007-02-25 06:40:16 +00004409 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004410 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004411 Ops.push_back(Chain);
4412 Ops.push_back(DAG.getValueType(AVT));
4413 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004414 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004415
Evan Cheng0db9fe62006-04-25 20:13:52 +00004416 if (TwoRepStos) {
4417 InFlag = Chain.getValue(1);
4418 Count = Op.getOperand(3);
4419 MVT::ValueType CVT = Count.getValueType();
4420 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004421 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4422 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4423 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004424 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004425 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426 Ops.clear();
4427 Ops.push_back(Chain);
4428 Ops.push_back(DAG.getValueType(MVT::i8));
4429 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004430 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004431 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004432 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004433 SDOperand Value;
4434 unsigned Val = ValC->getValue() & 255;
4435 unsigned Offset = I->getValue() - BytesLeft;
4436 SDOperand DstAddr = Op.getOperand(1);
4437 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004438 if (BytesLeft >= 4) {
4439 Val = (Val << 8) | Val;
4440 Val = (Val << 16) | Val;
4441 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004442 Chain = DAG.getStore(Chain, Value,
4443 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4444 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004445 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004446 BytesLeft -= 4;
4447 Offset += 4;
4448 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004449 if (BytesLeft >= 2) {
4450 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004451 Chain = DAG.getStore(Chain, Value,
4452 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4453 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004454 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004455 BytesLeft -= 2;
4456 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004457 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004458 if (BytesLeft == 1) {
4459 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004460 Chain = DAG.getStore(Chain, Value,
4461 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4462 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004463 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004464 }
Evan Cheng386031a2006-03-24 07:29:27 +00004465 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004466
Evan Cheng0db9fe62006-04-25 20:13:52 +00004467 return Chain;
4468}
Evan Cheng11e15b32006-04-03 20:53:28 +00004469
Evan Cheng0db9fe62006-04-25 20:13:52 +00004470SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
Rafael Espindola068317b2007-09-28 12:53:01 +00004471 SDOperand ChainOp = Op.getOperand(0);
4472 SDOperand DestOp = Op.getOperand(1);
4473 SDOperand SourceOp = Op.getOperand(2);
4474 SDOperand CountOp = Op.getOperand(3);
4475 SDOperand AlignOp = Op.getOperand(4);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004476 SDOperand AlwaysInlineOp = Op.getOperand(5);
4477
4478 bool AlwaysInline = (bool)cast<ConstantSDNode>(AlwaysInlineOp)->getValue();
Rafael Espindola068317b2007-09-28 12:53:01 +00004479 unsigned Align = (unsigned)cast<ConstantSDNode>(AlignOp)->getValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004480 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00004481
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004482 // If size is unknown, call memcpy.
4483 ConstantSDNode *I = dyn_cast<ConstantSDNode>(CountOp);
4484 if (!I) {
4485 assert(!AlwaysInline && "Cannot inline copy of unknown size");
4486 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4487 }
4488 unsigned Size = I->getValue();
4489
4490 if (AlwaysInline)
4491 return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG);
4492
Rafael Espindola068317b2007-09-28 12:53:01 +00004493 // The libc version is likely to be faster for the following cases. It can
4494 // use the address value and run time information about the CPU.
Rafael Espindola44c82652007-08-27 17:48:26 +00004495 // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster
Rafael Espindola068317b2007-09-28 12:53:01 +00004496
4497 // If not DWORD aligned, call memcpy.
4498 if ((Align & 3) != 0)
4499 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4500
Rafael Espindola068317b2007-09-28 12:53:01 +00004501 // If size is more than the threshold, call memcpy.
Rafael Espindola068317b2007-09-28 12:53:01 +00004502 if (Size > Subtarget->getMinRepStrSizeThreshold())
4503 return LowerMEMCPYCall(ChainOp, DestOp, SourceOp, CountOp, DAG);
4504
4505 return LowerMEMCPYInline(ChainOp, DestOp, SourceOp, Size, Align, DAG);
4506}
4507
4508SDOperand X86TargetLowering::LowerMEMCPYCall(SDOperand Chain,
4509 SDOperand Dest,
4510 SDOperand Source,
4511 SDOperand Count,
4512 SelectionDAG &DAG) {
4513 MVT::ValueType IntPtr = getPointerTy();
4514 TargetLowering::ArgListTy Args;
4515 TargetLowering::ArgListEntry Entry;
4516 Entry.Ty = getTargetData()->getIntPtrType();
4517 Entry.Node = Dest; Args.push_back(Entry);
4518 Entry.Node = Source; Args.push_back(Entry);
4519 Entry.Node = Count; Args.push_back(Entry);
4520 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004521 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
Rafael Espindola068317b2007-09-28 12:53:01 +00004523 return CallResult.second;
4524}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004525
Rafael Espindola068317b2007-09-28 12:53:01 +00004526SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4527 SDOperand Dest,
4528 SDOperand Source,
4529 unsigned Size,
4530 unsigned Align,
4531 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004532 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004534 switch (Align & 3) {
4535 case 2: // WORD aligned
4536 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004537 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004538 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004539 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004540 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4541 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004542 break;
4543 default: // Byte aligned
4544 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004545 break;
4546 }
4547
Rafael Espindola068317b2007-09-28 12:53:01 +00004548 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4549 SDOperand Count = DAG.getConstant(Size / UBytes, getPointerTy());
4550 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004551
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004553 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4554 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004555 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004556 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004557 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004558 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004559 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004560 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561 InFlag = Chain.getValue(1);
4562
Chris Lattnerd96d0722007-02-25 06:40:16 +00004563 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004564 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004565 Ops.push_back(Chain);
4566 Ops.push_back(DAG.getValueType(AVT));
4567 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004568 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569
Rafael Espindola068317b2007-09-28 12:53:01 +00004570 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004571 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004572 unsigned Offset = Size - BytesLeft;
4573 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004574 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004575 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004576 MVT::ValueType SrcVT = SrcAddr.getValueType();
4577 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004578 if (BytesLeft >= 4) {
4579 Value = DAG.getLoad(MVT::i32, Chain,
4580 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4581 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004582 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004583 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004584 Chain = DAG.getStore(Chain, Value,
4585 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4586 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004587 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004588 BytesLeft -= 4;
4589 Offset += 4;
4590 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591 if (BytesLeft >= 2) {
4592 Value = DAG.getLoad(MVT::i16, Chain,
4593 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4594 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004595 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004596 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004597 Chain = DAG.getStore(Chain, Value,
4598 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4599 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004600 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 BytesLeft -= 2;
4602 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004603 }
4604
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 if (BytesLeft == 1) {
4606 Value = DAG.getLoad(MVT::i8, Chain,
4607 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4608 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004609 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004610 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004611 Chain = DAG.getStore(Chain, Value,
4612 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4613 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004614 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004615 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004616 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617
4618 return Chain;
4619}
4620
4621SDOperand
4622X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerd96d0722007-02-25 06:40:16 +00004623 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004624 SDOperand TheOp = Op.getOperand(0);
4625 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004626 if (Subtarget->is64Bit()) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004627 SDOperand Copy1 =
4628 DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004629 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4630 MVT::i64, Copy1.getValue(2));
4631 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4632 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004633 SDOperand Ops[] = {
4634 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4635 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004636
4637 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004638 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004639 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004640
4641 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4642 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4643 MVT::i32, Copy1.getValue(2));
4644 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4645 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4646 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647}
4648
4649SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004650 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4651
Evan Cheng25ab6902006-09-08 06:48:29 +00004652 if (!Subtarget->is64Bit()) {
4653 // vastart just stores the address of the VarArgsFrameIndex slot into the
4654 // memory location argument.
4655 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004656 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4657 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004658 }
4659
4660 // __va_list_tag:
4661 // gp_offset (0 - 6 * 8)
4662 // fp_offset (48 - 48 + 8 * 16)
4663 // overflow_arg_area (point to parameters coming in memory).
4664 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004665 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004666 SDOperand FIN = Op.getOperand(1);
4667 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004668 SDOperand Store = DAG.getStore(Op.getOperand(0),
4669 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004670 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004671 MemOps.push_back(Store);
4672
4673 // Store fp_offset
4674 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4675 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00004676 Store = DAG.getStore(Op.getOperand(0),
4677 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004678 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004679 MemOps.push_back(Store);
4680
4681 // Store ptr to overflow_arg_area
4682 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4683 DAG.getConstant(4, getPointerTy()));
4684 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004685 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4686 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004687 MemOps.push_back(Store);
4688
4689 // Store ptr to reg_save_area.
4690 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4691 DAG.getConstant(8, getPointerTy()));
4692 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004693 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4694 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004695 MemOps.push_back(Store);
4696 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004697}
4698
Evan Chengae642192007-03-02 23:16:35 +00004699SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4700 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4701 SDOperand Chain = Op.getOperand(0);
4702 SDOperand DstPtr = Op.getOperand(1);
4703 SDOperand SrcPtr = Op.getOperand(2);
4704 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4705 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4706
4707 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4708 SrcSV->getValue(), SrcSV->getOffset());
4709 Chain = SrcPtr.getValue(1);
4710 for (unsigned i = 0; i < 3; ++i) {
4711 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4712 SrcSV->getValue(), SrcSV->getOffset());
4713 Chain = Val.getValue(1);
4714 Chain = DAG.getStore(Chain, Val, DstPtr,
4715 DstSV->getValue(), DstSV->getOffset());
4716 if (i == 2)
4717 break;
4718 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4719 DAG.getConstant(8, getPointerTy()));
4720 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4721 DAG.getConstant(8, getPointerTy()));
4722 }
4723 return Chain;
4724}
4725
Evan Cheng0db9fe62006-04-25 20:13:52 +00004726SDOperand
4727X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4728 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4729 switch (IntNo) {
4730 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004731 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004732 case Intrinsic::x86_sse_comieq_ss:
4733 case Intrinsic::x86_sse_comilt_ss:
4734 case Intrinsic::x86_sse_comile_ss:
4735 case Intrinsic::x86_sse_comigt_ss:
4736 case Intrinsic::x86_sse_comige_ss:
4737 case Intrinsic::x86_sse_comineq_ss:
4738 case Intrinsic::x86_sse_ucomieq_ss:
4739 case Intrinsic::x86_sse_ucomilt_ss:
4740 case Intrinsic::x86_sse_ucomile_ss:
4741 case Intrinsic::x86_sse_ucomigt_ss:
4742 case Intrinsic::x86_sse_ucomige_ss:
4743 case Intrinsic::x86_sse_ucomineq_ss:
4744 case Intrinsic::x86_sse2_comieq_sd:
4745 case Intrinsic::x86_sse2_comilt_sd:
4746 case Intrinsic::x86_sse2_comile_sd:
4747 case Intrinsic::x86_sse2_comigt_sd:
4748 case Intrinsic::x86_sse2_comige_sd:
4749 case Intrinsic::x86_sse2_comineq_sd:
4750 case Intrinsic::x86_sse2_ucomieq_sd:
4751 case Intrinsic::x86_sse2_ucomilt_sd:
4752 case Intrinsic::x86_sse2_ucomile_sd:
4753 case Intrinsic::x86_sse2_ucomigt_sd:
4754 case Intrinsic::x86_sse2_ucomige_sd:
4755 case Intrinsic::x86_sse2_ucomineq_sd: {
4756 unsigned Opc = 0;
4757 ISD::CondCode CC = ISD::SETCC_INVALID;
4758 switch (IntNo) {
4759 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004760 case Intrinsic::x86_sse_comieq_ss:
4761 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004762 Opc = X86ISD::COMI;
4763 CC = ISD::SETEQ;
4764 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004765 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004766 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004767 Opc = X86ISD::COMI;
4768 CC = ISD::SETLT;
4769 break;
4770 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004771 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004772 Opc = X86ISD::COMI;
4773 CC = ISD::SETLE;
4774 break;
4775 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004776 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004777 Opc = X86ISD::COMI;
4778 CC = ISD::SETGT;
4779 break;
4780 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004781 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004782 Opc = X86ISD::COMI;
4783 CC = ISD::SETGE;
4784 break;
4785 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004786 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004787 Opc = X86ISD::COMI;
4788 CC = ISD::SETNE;
4789 break;
4790 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004791 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792 Opc = X86ISD::UCOMI;
4793 CC = ISD::SETEQ;
4794 break;
4795 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004796 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797 Opc = X86ISD::UCOMI;
4798 CC = ISD::SETLT;
4799 break;
4800 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004801 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802 Opc = X86ISD::UCOMI;
4803 CC = ISD::SETLE;
4804 break;
4805 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004806 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004807 Opc = X86ISD::UCOMI;
4808 CC = ISD::SETGT;
4809 break;
4810 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004811 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004812 Opc = X86ISD::UCOMI;
4813 CC = ISD::SETGE;
4814 break;
4815 case Intrinsic::x86_sse_ucomineq_ss:
4816 case Intrinsic::x86_sse2_ucomineq_sd:
4817 Opc = X86ISD::UCOMI;
4818 CC = ISD::SETNE;
4819 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004820 }
Evan Cheng734503b2006-09-11 02:19:56 +00004821
Evan Cheng0db9fe62006-04-25 20:13:52 +00004822 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004823 SDOperand LHS = Op.getOperand(1);
4824 SDOperand RHS = Op.getOperand(2);
4825 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004826
Evan Chenge5f62042007-09-29 00:00:36 +00004827 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4828 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4829 DAG.getConstant(X86CC, MVT::i8), Cond);
4830 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004831 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004832 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004833}
Evan Cheng72261582005-12-20 06:22:03 +00004834
Nate Begemanbcc5f362007-01-29 22:58:52 +00004835SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4836 // Depths > 0 not supported yet!
4837 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4838 return SDOperand();
4839
4840 // Just load the return address
4841 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4842 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4843}
4844
4845SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4846 // Depths > 0 not supported yet!
4847 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4848 return SDOperand();
4849
4850 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4851 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4852 DAG.getConstant(4, getPointerTy()));
4853}
4854
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004855SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4856 SelectionDAG &DAG) {
4857 // Is not yet supported on x86-64
4858 if (Subtarget->is64Bit())
4859 return SDOperand();
4860
4861 return DAG.getConstant(8, getPointerTy());
4862}
4863
4864SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4865{
4866 assert(!Subtarget->is64Bit() &&
4867 "Lowering of eh_return builtin is not supported yet on x86-64");
4868
4869 MachineFunction &MF = DAG.getMachineFunction();
4870 SDOperand Chain = Op.getOperand(0);
4871 SDOperand Offset = Op.getOperand(1);
4872 SDOperand Handler = Op.getOperand(2);
4873
4874 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4875 getPointerTy());
4876
4877 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4878 DAG.getConstant(-4UL, getPointerTy()));
4879 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4880 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4881 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4882 MF.addLiveOut(X86::ECX);
4883
4884 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4885 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4886}
4887
Duncan Sandsb116fac2007-07-27 20:02:49 +00004888SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4889 SelectionDAG &DAG) {
4890 SDOperand Root = Op.getOperand(0);
4891 SDOperand Trmp = Op.getOperand(1); // trampoline
4892 SDOperand FPtr = Op.getOperand(2); // nested function
4893 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4894
4895 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4896
4897 if (Subtarget->is64Bit()) {
4898 return SDOperand(); // not yet supported
4899 } else {
4900 Function *Func = (Function *)
4901 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4902 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00004903 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004904
4905 switch (CC) {
4906 default:
4907 assert(0 && "Unsupported calling convention");
4908 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00004909 case CallingConv::X86_StdCall: {
4910 // Pass 'nest' parameter in ECX.
4911 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004912 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004913
4914 // Check that ECX wasn't needed by an 'inreg' parameter.
4915 const FunctionType *FTy = Func->getFunctionType();
4916 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4917
4918 if (Attrs && !Func->isVarArg()) {
4919 unsigned InRegCount = 0;
4920 unsigned Idx = 1;
4921
4922 for (FunctionType::param_iterator I = FTy->param_begin(),
4923 E = FTy->param_end(); I != E; ++I, ++Idx)
4924 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4925 // FIXME: should only count parameters that are lowered to integers.
4926 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4927
4928 if (InRegCount > 2) {
4929 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4930 abort();
4931 }
4932 }
4933 break;
4934 }
4935 case CallingConv::X86_FastCall:
4936 // Pass 'nest' parameter in EAX.
4937 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004938 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004939 break;
4940 }
4941
Duncan Sandsee465742007-08-29 19:01:20 +00004942 const X86InstrInfo *TII =
4943 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4944
Duncan Sandsb116fac2007-07-27 20:02:49 +00004945 SDOperand OutChains[4];
4946 SDOperand Addr, Disp;
4947
4948 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4949 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4950
Duncan Sandsee465742007-08-29 19:01:20 +00004951 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4952 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4953 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00004954 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4955
4956 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4957 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4958 TrmpSV->getOffset() + 1, false, 1);
4959
Duncan Sandsee465742007-08-29 19:01:20 +00004960 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004961 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4962 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4963 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4964
4965 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4966 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4967 TrmpSV->getOffset() + 6, false, 1);
4968
Duncan Sandsf7331b32007-09-11 14:10:23 +00004969 SDOperand Ops[] =
4970 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4971 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004972 }
4973}
4974
Evan Cheng0db9fe62006-04-25 20:13:52 +00004975/// LowerOperation - Provide custom lowering hooks for some operations.
4976///
4977SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4978 switch (Op.getOpcode()) {
4979 default: assert(0 && "Should not custom lower this!");
4980 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4981 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4982 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4983 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4984 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4985 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4986 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004987 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004988 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4989 case ISD::SHL_PARTS:
4990 case ISD::SRA_PARTS:
4991 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4992 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4993 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4994 case ISD::FABS: return LowerFABS(Op, DAG);
4995 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004996 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00004997 case ISD::SETCC: return LowerSETCC(Op, DAG);
4998 case ISD::SELECT: return LowerSELECT(Op, DAG);
4999 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005000 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005001 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005002 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005003 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005004 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5005 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
5006 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
5007 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005008 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005009 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005010 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5011 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005012 case ISD::FRAME_TO_ARGS_OFFSET:
5013 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005014 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005015 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005016 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005017 }
Jim Laskey62819f32007-02-21 22:54:50 +00005018 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005019}
5020
Evan Cheng72261582005-12-20 06:22:03 +00005021const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5022 switch (Opcode) {
5023 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00005024 case X86ISD::SHLD: return "X86ISD::SHLD";
5025 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005026 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005027 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005028 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005029 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005030 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005031 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005032 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5033 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5034 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005035 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005036 case X86ISD::FST: return "X86ISD::FST";
5037 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00005038 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005039 case X86ISD::CALL: return "X86ISD::CALL";
5040 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5041 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5042 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005043 case X86ISD::COMI: return "X86ISD::COMI";
5044 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005045 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005046 case X86ISD::CMOV: return "X86ISD::CMOV";
5047 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005048 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005049 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5050 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005051 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005052 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00005053 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00005054 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00005055 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005056 case X86ISD::FMAX: return "X86ISD::FMAX";
5057 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005058 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5059 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005060 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5061 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005062 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005063 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Evan Cheng72261582005-12-20 06:22:03 +00005064 }
5065}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005066
Chris Lattnerc9addb72007-03-30 23:15:24 +00005067// isLegalAddressingMode - Return true if the addressing mode represented
5068// by AM is legal for this target, for a load/store of the specified type.
5069bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5070 const Type *Ty) const {
5071 // X86 supports extremely general addressing modes.
5072
5073 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5074 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5075 return false;
5076
5077 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005078 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005079 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5080 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005081
5082 // X86-64 only supports addr of globals in small code model.
5083 if (Subtarget->is64Bit()) {
5084 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5085 return false;
5086 // If lower 4G is not available, then we must use rip-relative addressing.
5087 if (AM.BaseOffs || AM.Scale > 1)
5088 return false;
5089 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005090 }
5091
5092 switch (AM.Scale) {
5093 case 0:
5094 case 1:
5095 case 2:
5096 case 4:
5097 case 8:
5098 // These scales always work.
5099 break;
5100 case 3:
5101 case 5:
5102 case 9:
5103 // These scales are formed with basereg+scalereg. Only accept if there is
5104 // no basereg yet.
5105 if (AM.HasBaseReg)
5106 return false;
5107 break;
5108 default: // Other stuff never works.
5109 return false;
5110 }
5111
5112 return true;
5113}
5114
5115
Evan Cheng60c07e12006-07-05 22:17:51 +00005116/// isShuffleMaskLegal - Targets can use this to indicate that they only
5117/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5118/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5119/// are assumed to be legal.
5120bool
5121X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5122 // Only do shuffles on 128-bit vector types for now.
5123 if (MVT::getSizeInBits(VT) == 64) return false;
5124 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005125 isIdentityMask(Mask.Val) ||
5126 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005127 isSplatMask(Mask.Val) ||
5128 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5129 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005130 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005131 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005132 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005133}
5134
5135bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5136 MVT::ValueType EVT,
5137 SelectionDAG &DAG) const {
5138 unsigned NumElts = BVOps.size();
5139 // Only do shuffles on 128-bit vector types for now.
5140 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5141 if (NumElts == 2) return true;
5142 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005143 return (isMOVLMask(&BVOps[0], 4) ||
5144 isCommutedMOVL(&BVOps[0], 4, true) ||
5145 isSHUFPMask(&BVOps[0], 4) ||
5146 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005147 }
5148 return false;
5149}
5150
5151//===----------------------------------------------------------------------===//
5152// X86 Scheduler Hooks
5153//===----------------------------------------------------------------------===//
5154
5155MachineBasicBlock *
5156X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
5157 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005159 switch (MI->getOpcode()) {
5160 default: assert(false && "Unexpected instr type to insert");
5161 case X86::CMOV_FR32:
5162 case X86::CMOV_FR64:
5163 case X86::CMOV_V4F32:
5164 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005165 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005166 // To "insert" a SELECT_CC instruction, we actually have to insert the
5167 // diamond control-flow pattern. The incoming instruction knows the
5168 // destination vreg to set, the condition code register to branch on, the
5169 // true/false values to select between, and a branch opcode to use.
5170 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5171 ilist<MachineBasicBlock>::iterator It = BB;
5172 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005173
Evan Cheng60c07e12006-07-05 22:17:51 +00005174 // thisMBB:
5175 // ...
5176 // TrueVal = ...
5177 // cmpTY ccX, r1, r2
5178 // bCC copy1MBB
5179 // fallthrough --> copy0MBB
5180 MachineBasicBlock *thisMBB = BB;
5181 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5182 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005183 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005184 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005185 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005186 MachineFunction *F = BB->getParent();
5187 F->getBasicBlockList().insert(It, copy0MBB);
5188 F->getBasicBlockList().insert(It, sinkMBB);
5189 // Update machine-CFG edges by first adding all successors of the current
5190 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005191 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005192 e = BB->succ_end(); i != e; ++i)
5193 sinkMBB->addSuccessor(*i);
5194 // Next, remove all successors of the current block, and add the true
5195 // and fallthrough blocks as its successors.
5196 while(!BB->succ_empty())
5197 BB->removeSuccessor(BB->succ_begin());
5198 BB->addSuccessor(copy0MBB);
5199 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005200
Evan Cheng60c07e12006-07-05 22:17:51 +00005201 // copy0MBB:
5202 // %FalseValue = ...
5203 // # fallthrough to sinkMBB
5204 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005205
Evan Cheng60c07e12006-07-05 22:17:51 +00005206 // Update machine-CFG edges
5207 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005208
Evan Cheng60c07e12006-07-05 22:17:51 +00005209 // sinkMBB:
5210 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5211 // ...
5212 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005213 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005214 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5215 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5216
5217 delete MI; // The pseudo instruction is gone now.
5218 return BB;
5219 }
5220
Dale Johannesen849f2142007-07-03 00:53:03 +00005221 case X86::FP32_TO_INT16_IN_MEM:
5222 case X86::FP32_TO_INT32_IN_MEM:
5223 case X86::FP32_TO_INT64_IN_MEM:
5224 case X86::FP64_TO_INT16_IN_MEM:
5225 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005226 case X86::FP64_TO_INT64_IN_MEM:
5227 case X86::FP80_TO_INT16_IN_MEM:
5228 case X86::FP80_TO_INT32_IN_MEM:
5229 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005230 // Change the floating point control register to use "round towards zero"
5231 // mode when truncating to an integer value.
5232 MachineFunction *F = BB->getParent();
5233 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005234 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005235
5236 // Load the old value of the high byte of the control word...
5237 unsigned OldCW =
5238 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005239 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005240
5241 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005242 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5243 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005244
5245 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005246 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005247
5248 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005249 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5250 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005251
5252 // Get the X86 opcode to use.
5253 unsigned Opc;
5254 switch (MI->getOpcode()) {
5255 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005256 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5257 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5258 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5259 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5260 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5261 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005262 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5263 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5264 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005265 }
5266
5267 X86AddressMode AM;
5268 MachineOperand &Op = MI->getOperand(0);
5269 if (Op.isRegister()) {
5270 AM.BaseType = X86AddressMode::RegBase;
5271 AM.Base.Reg = Op.getReg();
5272 } else {
5273 AM.BaseType = X86AddressMode::FrameIndexBase;
5274 AM.Base.FrameIndex = Op.getFrameIndex();
5275 }
5276 Op = MI->getOperand(1);
5277 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005278 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005279 Op = MI->getOperand(2);
5280 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005281 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005282 Op = MI->getOperand(3);
5283 if (Op.isGlobalAddress()) {
5284 AM.GV = Op.getGlobal();
5285 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005286 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005287 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005288 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5289 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005290
5291 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005292 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005293
5294 delete MI; // The pseudo instruction is gone now.
5295 return BB;
5296 }
5297 }
5298}
5299
5300//===----------------------------------------------------------------------===//
5301// X86 Optimization Hooks
5302//===----------------------------------------------------------------------===//
5303
Nate Begeman368e18d2006-02-16 21:11:51 +00005304void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5305 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005306 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005307 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005308 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005309 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005310 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005311 assert((Opc >= ISD::BUILTIN_OP_END ||
5312 Opc == ISD::INTRINSIC_WO_CHAIN ||
5313 Opc == ISD::INTRINSIC_W_CHAIN ||
5314 Opc == ISD::INTRINSIC_VOID) &&
5315 "Should use MaskedValueIsZero if you don't know whether Op"
5316 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005317
Evan Cheng865f0602006-04-05 06:11:20 +00005318 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005319 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005320 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005321 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00005322 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5323 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005324 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005325}
Chris Lattner259e97c2006-01-31 19:43:35 +00005326
Evan Cheng206ee9d2006-07-07 08:33:52 +00005327/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5328/// element of the result of the vector shuffle.
5329static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5330 MVT::ValueType VT = N->getValueType(0);
5331 SDOperand PermMask = N->getOperand(2);
5332 unsigned NumElems = PermMask.getNumOperands();
5333 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5334 i %= NumElems;
5335 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5336 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005337 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005338 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5339 SDOperand Idx = PermMask.getOperand(i);
5340 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005341 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005342 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5343 }
5344 return SDOperand();
5345}
5346
5347/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5348/// node is a GlobalAddress + an offset.
5349static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005350 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005351 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005352 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5353 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5354 return true;
5355 }
Evan Cheng0085a282006-11-30 21:55:46 +00005356 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005357 SDOperand N1 = N->getOperand(0);
5358 SDOperand N2 = N->getOperand(1);
5359 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5360 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5361 if (V) {
5362 Offset += V->getSignExtended();
5363 return true;
5364 }
5365 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5366 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5367 if (V) {
5368 Offset += V->getSignExtended();
5369 return true;
5370 }
5371 }
5372 }
5373 return false;
5374}
5375
5376/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5377/// + Dist * Size.
5378static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5379 MachineFrameInfo *MFI) {
5380 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5381 return false;
5382
5383 SDOperand Loc = N->getOperand(1);
5384 SDOperand BaseLoc = Base->getOperand(1);
5385 if (Loc.getOpcode() == ISD::FrameIndex) {
5386 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5387 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005388 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5389 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005390 int FS = MFI->getObjectSize(FI);
5391 int BFS = MFI->getObjectSize(BFI);
5392 if (FS != BFS || FS != Size) return false;
5393 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5394 } else {
5395 GlobalValue *GV1 = NULL;
5396 GlobalValue *GV2 = NULL;
5397 int64_t Offset1 = 0;
5398 int64_t Offset2 = 0;
5399 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5400 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5401 if (isGA1 && isGA2 && GV1 == GV2)
5402 return Offset1 == (Offset2 + Dist*Size);
5403 }
5404
5405 return false;
5406}
5407
Evan Cheng1e60c092006-07-10 21:37:44 +00005408static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5409 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005410 GlobalValue *GV;
5411 int64_t Offset;
5412 if (isGAPlusOffset(Base, GV, Offset))
5413 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5414 else {
5415 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00005416 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005417 if (BFI < 0)
5418 // Fixed objects do not specify alignment, however the offsets are known.
5419 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5420 (MFI->getObjectOffset(BFI) % 16) == 0);
5421 else
5422 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005423 }
5424 return false;
5425}
5426
5427
5428/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5429/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5430/// if the load addresses are consecutive, non-overlapping, and in the right
5431/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005432static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5433 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005434 MachineFunction &MF = DAG.getMachineFunction();
5435 MachineFrameInfo *MFI = MF.getFrameInfo();
5436 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005437 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005438 SDOperand PermMask = N->getOperand(2);
5439 int NumElems = (int)PermMask.getNumOperands();
5440 SDNode *Base = NULL;
5441 for (int i = 0; i < NumElems; ++i) {
5442 SDOperand Idx = PermMask.getOperand(i);
5443 if (Idx.getOpcode() == ISD::UNDEF) {
5444 if (!Base) return SDOperand();
5445 } else {
5446 SDOperand Arg =
5447 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005448 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005449 return SDOperand();
5450 if (!Base)
5451 Base = Arg.Val;
5452 else if (!isConsecutiveLoad(Arg.Val, Base,
5453 i, MVT::getSizeInBits(EVT)/8,MFI))
5454 return SDOperand();
5455 }
5456 }
5457
Evan Cheng1e60c092006-07-10 21:37:44 +00005458 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005459 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005460 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005461 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005462 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005463 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005464 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5465 LD->getSrcValueOffset(), LD->isVolatile(),
5466 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005467 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005468}
5469
Chris Lattner83e6c992006-10-04 06:57:07 +00005470/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5471static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5472 const X86Subtarget *Subtarget) {
5473 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005474
Chris Lattner83e6c992006-10-04 06:57:07 +00005475 // If we have SSE[12] support, try to form min/max nodes.
5476 if (Subtarget->hasSSE2() &&
5477 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5478 if (Cond.getOpcode() == ISD::SETCC) {
5479 // Get the LHS/RHS of the select.
5480 SDOperand LHS = N->getOperand(1);
5481 SDOperand RHS = N->getOperand(2);
5482 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005483
Evan Cheng8ca29322006-11-10 21:43:37 +00005484 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005485 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005486 switch (CC) {
5487 default: break;
5488 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5489 case ISD::SETULE:
5490 case ISD::SETLE:
5491 if (!UnsafeFPMath) break;
5492 // FALL THROUGH.
5493 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5494 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005495 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005496 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005497
Chris Lattner1907a7b2006-10-05 04:11:26 +00005498 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5499 case ISD::SETUGT:
5500 case ISD::SETGT:
5501 if (!UnsafeFPMath) break;
5502 // FALL THROUGH.
5503 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5504 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005505 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005506 break;
5507 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005508 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005509 switch (CC) {
5510 default: break;
5511 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5512 case ISD::SETUGT:
5513 case ISD::SETGT:
5514 if (!UnsafeFPMath) break;
5515 // FALL THROUGH.
5516 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5517 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005518 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005519 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005520
Chris Lattner1907a7b2006-10-05 04:11:26 +00005521 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5522 case ISD::SETULE:
5523 case ISD::SETLE:
5524 if (!UnsafeFPMath) break;
5525 // FALL THROUGH.
5526 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5527 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005528 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005529 break;
5530 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005531 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005532
Evan Cheng8ca29322006-11-10 21:43:37 +00005533 if (Opcode)
5534 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005535 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005536
Chris Lattner83e6c992006-10-04 06:57:07 +00005537 }
5538
5539 return SDOperand();
5540}
5541
5542
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005543SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005544 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005545 SelectionDAG &DAG = DCI.DAG;
5546 switch (N->getOpcode()) {
5547 default: break;
5548 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00005549 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00005550 case ISD::SELECT:
5551 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005552 }
5553
5554 return SDOperand();
5555}
5556
Evan Cheng60c07e12006-07-05 22:17:51 +00005557//===----------------------------------------------------------------------===//
5558// X86 Inline Assembly Support
5559//===----------------------------------------------------------------------===//
5560
Chris Lattnerf4dff842006-07-11 02:54:03 +00005561/// getConstraintType - Given a constraint letter, return the type of
5562/// constraint it is for this target.
5563X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005564X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5565 if (Constraint.size() == 1) {
5566 switch (Constraint[0]) {
5567 case 'A':
5568 case 'r':
5569 case 'R':
5570 case 'l':
5571 case 'q':
5572 case 'Q':
5573 case 'x':
5574 case 'Y':
5575 return C_RegisterClass;
5576 default:
5577 break;
5578 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00005579 }
Chris Lattner4234f572007-03-25 02:14:49 +00005580 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00005581}
5582
Chris Lattner48884cd2007-08-25 00:47:38 +00005583/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5584/// vector. If it is invalid, don't add anything to Ops.
5585void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5586 char Constraint,
5587 std::vector<SDOperand>&Ops,
5588 SelectionDAG &DAG) {
5589 SDOperand Result(0, 0);
5590
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005591 switch (Constraint) {
5592 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00005593 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00005594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005595 if (C->getValue() <= 31) {
5596 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5597 break;
5598 }
Devang Patel84f7fd22007-03-17 00:13:28 +00005599 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005600 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00005601 case 'N':
5602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005603 if (C->getValue() <= 255) {
5604 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5605 break;
5606 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00005607 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005608 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00005609 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005610 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00005611 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5612 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5613 break;
5614 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005615
Chris Lattnerdc43a882007-05-03 16:52:29 +00005616 // If we are in non-pic codegen mode, we allow the address of a global (with
5617 // an optional displacement) to be used with 'i'.
5618 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5619 int64_t Offset = 0;
5620
5621 // Match either (GA) or (GA+C)
5622 if (GA) {
5623 Offset = GA->getOffset();
5624 } else if (Op.getOpcode() == ISD::ADD) {
5625 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5626 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5627 if (C && GA) {
5628 Offset = GA->getOffset()+C->getValue();
5629 } else {
5630 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5631 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5632 if (C && GA)
5633 Offset = GA->getOffset()+C->getValue();
5634 else
5635 C = 0, GA = 0;
5636 }
5637 }
5638
5639 if (GA) {
5640 // If addressing this global requires a load (e.g. in PIC mode), we can't
5641 // match.
5642 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5643 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00005644 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005645
Chris Lattnerdc43a882007-05-03 16:52:29 +00005646 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5647 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00005648 Result = Op;
5649 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005650 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005651
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005652 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00005653 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005654 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00005655 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005656
5657 if (Result.Val) {
5658 Ops.push_back(Result);
5659 return;
5660 }
5661 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005662}
5663
Chris Lattner259e97c2006-01-31 19:43:35 +00005664std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005665getRegClassForInlineAsmConstraint(const std::string &Constraint,
5666 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005667 if (Constraint.size() == 1) {
5668 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00005669 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005670 default: break; // Unknown constraint letter
5671 case 'A': // EAX/EDX
5672 if (VT == MVT::i32 || VT == MVT::i64)
5673 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5674 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005675 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5676 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005677 if (VT == MVT::i32)
5678 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5679 else if (VT == MVT::i16)
5680 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5681 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00005682 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005683 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005684 }
5685 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005686
Chris Lattner1efa40f2006-02-22 00:56:39 +00005687 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005688}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005689
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005690std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005691X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5692 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00005693 // First, see if this is a constraint that directly corresponds to an LLVM
5694 // register class.
5695 if (Constraint.size() == 1) {
5696 // GCC Constraint Letters
5697 switch (Constraint[0]) {
5698 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005699 case 'r': // GENERAL_REGS
5700 case 'R': // LEGACY_REGS
5701 case 'l': // INDEX_REGS
5702 if (VT == MVT::i64 && Subtarget->is64Bit())
5703 return std::make_pair(0U, X86::GR64RegisterClass);
5704 if (VT == MVT::i32)
5705 return std::make_pair(0U, X86::GR32RegisterClass);
5706 else if (VT == MVT::i16)
5707 return std::make_pair(0U, X86::GR16RegisterClass);
5708 else if (VT == MVT::i8)
5709 return std::make_pair(0U, X86::GR8RegisterClass);
5710 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00005711 case 'y': // MMX_REGS if MMX allowed.
5712 if (!Subtarget->hasMMX()) break;
5713 return std::make_pair(0U, X86::VR64RegisterClass);
5714 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005715 case 'Y': // SSE_REGS if SSE2 allowed
5716 if (!Subtarget->hasSSE2()) break;
5717 // FALL THROUGH.
5718 case 'x': // SSE_REGS if SSE1 allowed
5719 if (!Subtarget->hasSSE1()) break;
5720
5721 switch (VT) {
5722 default: break;
5723 // Scalar SSE types.
5724 case MVT::f32:
5725 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00005726 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005727 case MVT::f64:
5728 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00005729 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005730 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00005731 case MVT::v16i8:
5732 case MVT::v8i16:
5733 case MVT::v4i32:
5734 case MVT::v2i64:
5735 case MVT::v4f32:
5736 case MVT::v2f64:
5737 return std::make_pair(0U, X86::VR128RegisterClass);
5738 }
Chris Lattnerad043e82007-04-09 05:11:28 +00005739 break;
5740 }
5741 }
5742
Chris Lattnerf76d1802006-07-31 23:26:50 +00005743 // Use the default implementation in TargetLowering to convert the register
5744 // constraint into a member of a register class.
5745 std::pair<unsigned, const TargetRegisterClass*> Res;
5746 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00005747
5748 // Not found as a standard register?
5749 if (Res.second == 0) {
5750 // GCC calls "st(0)" just plain "st".
5751 if (StringsEqualNoCase("{st}", Constraint)) {
5752 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00005753 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00005754 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005755
Chris Lattner1a60aa72006-10-31 19:42:44 +00005756 return Res;
5757 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005758
Chris Lattnerf76d1802006-07-31 23:26:50 +00005759 // Otherwise, check to see if this is a register class of the wrong value
5760 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5761 // turn into {ax},{dx}.
5762 if (Res.second->hasType(VT))
5763 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005764
Chris Lattnerf76d1802006-07-31 23:26:50 +00005765 // All of the single-register GCC register classes map their values onto
5766 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5767 // really want an 8-bit or 32-bit register, map to the appropriate register
5768 // class and return the appropriate register.
5769 if (Res.second != X86::GR16RegisterClass)
5770 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005771
Chris Lattnerf76d1802006-07-31 23:26:50 +00005772 if (VT == MVT::i8) {
5773 unsigned DestReg = 0;
5774 switch (Res.first) {
5775 default: break;
5776 case X86::AX: DestReg = X86::AL; break;
5777 case X86::DX: DestReg = X86::DL; break;
5778 case X86::CX: DestReg = X86::CL; break;
5779 case X86::BX: DestReg = X86::BL; break;
5780 }
5781 if (DestReg) {
5782 Res.first = DestReg;
5783 Res.second = Res.second = X86::GR8RegisterClass;
5784 }
5785 } else if (VT == MVT::i32) {
5786 unsigned DestReg = 0;
5787 switch (Res.first) {
5788 default: break;
5789 case X86::AX: DestReg = X86::EAX; break;
5790 case X86::DX: DestReg = X86::EDX; break;
5791 case X86::CX: DestReg = X86::ECX; break;
5792 case X86::BX: DestReg = X86::EBX; break;
5793 case X86::SI: DestReg = X86::ESI; break;
5794 case X86::DI: DestReg = X86::EDI; break;
5795 case X86::BP: DestReg = X86::EBP; break;
5796 case X86::SP: DestReg = X86::ESP; break;
5797 }
5798 if (DestReg) {
5799 Res.first = DestReg;
5800 Res.second = Res.second = X86::GR32RegisterClass;
5801 }
Evan Cheng25ab6902006-09-08 06:48:29 +00005802 } else if (VT == MVT::i64) {
5803 unsigned DestReg = 0;
5804 switch (Res.first) {
5805 default: break;
5806 case X86::AX: DestReg = X86::RAX; break;
5807 case X86::DX: DestReg = X86::RDX; break;
5808 case X86::CX: DestReg = X86::RCX; break;
5809 case X86::BX: DestReg = X86::RBX; break;
5810 case X86::SI: DestReg = X86::RSI; break;
5811 case X86::DI: DestReg = X86::RDI; break;
5812 case X86::BP: DestReg = X86::RBP; break;
5813 case X86::SP: DestReg = X86::RSP; break;
5814 }
5815 if (DestReg) {
5816 Res.first = DestReg;
5817 Res.second = Res.second = X86::GR64RegisterClass;
5818 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005819 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005820
Chris Lattnerf76d1802006-07-31 23:26:50 +00005821 return Res;
5822}