blob: 9ce1fa993d5f2269a58f65c3f026180496459dac [file] [log] [blame]
Chris Lattnerc6d05672006-05-23 23:20:42 +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"
Duncan Sandsb116fac2007-07-27 20:02:49 +000016#include "X86CodeEmitter.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000019#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
21#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000024#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000026#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000029#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000033#include "llvm/CodeGen/SelectionDAG.h"
34#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000035#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000037#include "llvm/ADT/StringExtras.h"
Duncan Sandsb116fac2007-07-27 20:02:49 +000038#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039using namespace llvm;
40
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000043 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
Evan Cheng25ab6902006-09-08 06:48:29 +000045 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Cheng559806f2006-01-27 08:10:46 +000046
Anton Korobeynikov2365f512007-07-14 14:06:15 +000047 RegInfo = TM.getRegisterInfo();
48
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 // Set up the TargetLowering object.
50
51 // X86 is weird, it always uses i8 for shift amounts and setcc results.
52 setShiftAmountType(MVT::i8);
53 setSetCCResultType(MVT::i8);
54 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000055 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000057 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000058
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000059 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000060 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000061 setUseUnderscoreSetJmp(false);
62 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000063 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000064 // MS runtime is weird: it exports _setjmp, but longjmp!
65 setUseUnderscoreSetJmp(true);
66 setUseUnderscoreLongJmp(false);
67 } else {
68 setUseUnderscoreSetJmp(true);
69 setUseUnderscoreLongJmp(true);
70 }
71
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000072 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000073 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
74 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
75 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000076 if (Subtarget->is64Bit())
77 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000078
Evan Chengc5484282006-10-04 00:56:09 +000079 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
80
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000081 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
82 // operation.
83 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
84 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000086
Evan Cheng25ab6902006-09-08 06:48:29 +000087 if (Subtarget->is64Bit()) {
88 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000089 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000090 } else {
91 if (X86ScalarSSE)
92 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
93 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
94 else
95 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
96 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000097
98 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
99 // this operation.
100 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
101 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000102 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +0000103 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +0000104 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000105 else {
106 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
107 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
108 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000109
Evan Cheng25ab6902006-09-08 06:48:29 +0000110 if (!Subtarget->is64Bit()) {
111 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
112 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
113 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
114 }
Evan Cheng6dab0532006-01-30 08:02:57 +0000115
Evan Cheng02568ff2006-01-30 22:13:22 +0000116 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
117 // this operation.
118 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
119 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
120
121 if (X86ScalarSSE) {
122 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
123 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000125 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126 }
127
128 // Handle FP_TO_UINT by promoting the destination to a larger signed
129 // conversion.
130 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
131 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
132 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
133
Evan Cheng25ab6902006-09-08 06:48:29 +0000134 if (Subtarget->is64Bit()) {
135 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000136 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000137 } else {
138 if (X86ScalarSSE && !Subtarget->hasSSE3())
139 // Expand FP_TO_UINT into a select.
140 // FIXME: We would like to use a Custom expander here eventually to do
141 // the optimal thing for SSE vs. the default expansion in the legalizer.
142 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
143 else
144 // With SSE3 we can use fisttpll to convert to a signed i64.
145 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
146 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000147
Chris Lattner399610a2006-12-05 18:22:22 +0000148 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Chris Lattnerf3597a12006-12-05 18:45:06 +0000149 if (!X86ScalarSSE) {
150 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
151 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
152 }
Chris Lattner21f66852005-12-23 05:15:23 +0000153
Evan Chengc35497f2006-10-30 08:02:39 +0000154 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000155 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000156 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
157 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000158 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000159 if (Subtarget->is64Bit())
160 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000162 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000163 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
164 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000166
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
168 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
169 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
170 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
171 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
172 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
173 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
174 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
175 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000176 if (Subtarget->is64Bit()) {
177 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
178 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
179 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
180 }
181
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000182 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000183 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000184
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000185 // These should be promoted to a larger select which is supported.
186 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
187 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000188 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000189 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
190 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
191 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
192 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
193 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
194 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
195 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
196 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
197 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 if (Subtarget->is64Bit()) {
199 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
200 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
201 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000202 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000203 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000204 if (!Subtarget->is64Bit())
205 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
206
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000207 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000208 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000209 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000210 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000211 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000212 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 if (Subtarget->is64Bit()) {
214 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
215 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
216 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
217 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
218 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000219 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000220 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
221 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
222 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000223 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000224 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
225 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226
Chris Lattnerf73bae12005-11-29 06:16:21 +0000227 // We don't have line number support yet.
228 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000229 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000230 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000231 if (!Subtarget->isTargetDarwin() &&
232 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000233 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000234 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000235
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000236 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
237 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
238 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
239 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
240 if (Subtarget->is64Bit()) {
241 // FIXME: Verify
242 setExceptionPointerRegister(X86::RAX);
243 setExceptionSelectorRegister(X86::RDX);
244 } else {
245 setExceptionPointerRegister(X86::EAX);
246 setExceptionSelectorRegister(X86::EDX);
247 }
248
Duncan Sandsb116fac2007-07-27 20:02:49 +0000249 setOperationAction(ISD::ADJUST_TRAMP, MVT::i32, Expand);
250 setOperationAction(ISD::ADJUST_TRAMP, MVT::i64, Expand);
251 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
252
Nate Begemanacc398c2006-01-25 18:21:52 +0000253 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
254 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000255 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000256 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000257 if (Subtarget->is64Bit())
258 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
259 else
260 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
261
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000262 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000263 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 if (Subtarget->is64Bit())
265 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000266 if (Subtarget->isTargetCygMing())
267 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
268 else
269 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000270
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000271 if (X86ScalarSSE) {
272 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000273 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
274 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000275
Evan Cheng223547a2006-01-31 22:28:30 +0000276 // Use ANDPD to simulate FABS.
277 setOperationAction(ISD::FABS , MVT::f64, Custom);
278 setOperationAction(ISD::FABS , MVT::f32, Custom);
279
280 // Use XORP to simulate FNEG.
281 setOperationAction(ISD::FNEG , MVT::f64, Custom);
282 setOperationAction(ISD::FNEG , MVT::f32, Custom);
283
Evan Cheng68c47cb2007-01-05 07:55:56 +0000284 // Use ANDPD and ORPD to simulate FCOPYSIGN.
285 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
286 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
287
Evan Chengd25e9e82006-02-02 00:28:23 +0000288 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000289 setOperationAction(ISD::FSIN , MVT::f64, Expand);
290 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000291 setOperationAction(ISD::FREM , MVT::f64, Expand);
292 setOperationAction(ISD::FSIN , MVT::f32, Expand);
293 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294 setOperationAction(ISD::FREM , MVT::f32, Expand);
295
Chris Lattnera54aa942006-01-29 06:26:08 +0000296 // Expand FP immediates into loads from the stack, except for the special
297 // cases we handle.
298 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
299 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000300 addLegalFPImmediate(+0.0); // xorps / xorpd
301 } else {
302 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000303 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
304 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000305
Evan Cheng68c47cb2007-01-05 07:55:56 +0000306 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000307 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000308 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
309 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000310 setOperationAction(ISD::FP_ROUND, MVT::f32, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000311
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312 if (!UnsafeFPMath) {
313 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
314 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
315 }
316
Chris Lattnera54aa942006-01-29 06:26:08 +0000317 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000318 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000319 addLegalFPImmediate(+0.0); // FLD0
320 addLegalFPImmediate(+1.0); // FLD1
321 addLegalFPImmediate(-0.0); // FLD0/FCHS
322 addLegalFPImmediate(-1.0); // FLD1/FCHS
323 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000324
Evan Chengd30bf012006-03-01 01:11:20 +0000325 // First set operation action for all vector types to expand. Then we
326 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000327 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
328 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000329 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
330 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000331 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000332 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000333 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000334 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000335 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
336 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
337 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
338 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
339 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
340 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000341 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000342 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000343 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000344 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000345 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
346 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
347 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
348 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
349 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
350 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
351 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000352 }
353
Evan Chenga88973f2006-03-22 19:22:18 +0000354 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000355 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
356 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
357 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000358 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000359
Evan Chengd30bf012006-03-01 01:11:20 +0000360 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000361
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000362 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
363 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
364 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000365 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000366
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000367 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
368 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
369 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
370
Bill Wendling74027e92007-03-15 21:24:36 +0000371 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
372 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
373
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000374 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000375 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000376 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000377 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
378 setOperationAction(ISD::AND, MVT::v2i32, Promote);
379 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
380 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000381
382 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000383 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000384 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000385 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
386 setOperationAction(ISD::OR, MVT::v2i32, Promote);
387 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
388 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000389
390 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000391 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000392 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000393 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
394 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
395 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
396 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000397
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000398 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000399 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000400 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000401 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
402 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
403 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
404 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000405
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000406 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
407 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
408 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
409 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000410
411 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
412 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
413 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000414 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000415
416 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
417 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000418 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
419 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000420 }
421
Evan Chenga88973f2006-03-22 19:22:18 +0000422 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000423 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
424
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000425 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
426 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
427 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
428 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000429 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
430 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000431 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
432 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
433 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000434 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000435 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000436 }
437
Evan Chenga88973f2006-03-22 19:22:18 +0000438 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000439 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
440 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
441 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
442 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
443 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
444
Evan Chengf7c378e2006-04-10 07:23:14 +0000445 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
446 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
447 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000448 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000449 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
450 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
451 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000452 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000453 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000454 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
455 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
456 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
457 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000458 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
459 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000460
Evan Chengf7c378e2006-04-10 07:23:14 +0000461 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
462 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000463 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000464 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
465 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
466 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000467
Evan Cheng2c3ae372006-04-12 21:21:57 +0000468 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
469 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
470 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
471 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
472 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
473 }
474 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
475 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
476 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
477 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
478 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
479 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
480
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000481 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000482 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
483 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
484 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
485 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
486 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
487 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
488 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000489 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
490 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000491 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
492 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000493 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000494
495 // Custom lower v2i64 and v2f64 selects.
496 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000497 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000498 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000499 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000500 }
501
Evan Cheng6be2c582006-04-05 23:38:46 +0000502 // We want to custom lower some of our intrinsics.
503 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
504
Evan Cheng206ee9d2006-07-07 08:33:52 +0000505 // We have target-specific dag combine patterns for the following nodes:
506 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000507 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000508
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000509 computeRegisterProperties();
510
Evan Cheng87ed7162006-02-14 08:25:08 +0000511 // FIXME: These should be based on subtarget info. Plus, the values should
512 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000513 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
514 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
515 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000516 allowUnalignedMemoryAccesses = true; // x86 supports it!
517}
518
Chris Lattner2b02a442007-02-25 08:29:00 +0000519
520//===----------------------------------------------------------------------===//
521// Return Value Calling Convention Implementation
522//===----------------------------------------------------------------------===//
523
Chris Lattner59ed56b2007-02-28 04:55:35 +0000524#include "X86GenCallingConv.inc"
Chris Lattner9774c912007-02-27 05:28:59 +0000525
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000526/// LowerRET - Lower an ISD::RET node.
527SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
528 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
529
Chris Lattner9774c912007-02-27 05:28:59 +0000530 SmallVector<CCValAssign, 16> RVLocs;
531 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000532 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
533 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000534 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000535
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000536
537 // If this is the first return lowered for this function, add the regs to the
538 // liveout set for the function.
539 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000540 for (unsigned i = 0; i != RVLocs.size(); ++i)
541 if (RVLocs[i].isRegLoc())
542 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000543 }
544
545 SDOperand Chain = Op.getOperand(0);
546 SDOperand Flag;
547
548 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000549 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
550 RVLocs[0].getLocReg() != X86::ST0) {
551 for (unsigned i = 0; i != RVLocs.size(); ++i) {
552 CCValAssign &VA = RVLocs[i];
553 assert(VA.isRegLoc() && "Can only return in registers!");
554 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
555 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000556 Flag = Chain.getValue(1);
557 }
558 } else {
559 // We need to handle a destination of ST0 specially, because it isn't really
560 // a register.
561 SDOperand Value = Op.getOperand(1);
562
563 // If this is an FP return with ScalarSSE, we need to move the value from
564 // an XMM register onto the fp-stack.
565 if (X86ScalarSSE) {
566 SDOperand MemLoc;
567
568 // If this is a load into a scalarsse value, don't store the loaded value
569 // back to the stack, only to reload it: just replace the scalar-sse load.
570 if (ISD::isNON_EXTLoad(Value.Val) &&
571 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
572 Chain = Value.getOperand(0);
573 MemLoc = Value.getOperand(1);
574 } else {
575 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000576 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000577 MachineFunction &MF = DAG.getMachineFunction();
578 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
579 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
580 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
581 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000582 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000583 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000584 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
585 Chain = Value.getValue(1);
586 }
587
588 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
589 SDOperand Ops[] = { Chain, Value };
590 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
591 Flag = Chain.getValue(1);
592 }
593
594 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
595 if (Flag.Val)
596 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
597 else
598 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
599}
600
601
Chris Lattner3085e152007-02-25 08:59:22 +0000602/// LowerCallResult - Lower the result values of an ISD::CALL into the
603/// appropriate copies out of appropriate physical registers. This assumes that
604/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
605/// being lowered. The returns a SDNode with the same number of values as the
606/// ISD::CALL.
607SDNode *X86TargetLowering::
608LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
609 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000610
611 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000612 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000613 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
614 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000615 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
616
Chris Lattner3085e152007-02-25 08:59:22 +0000617
Chris Lattnere32bbf62007-02-28 07:09:55 +0000618 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000619
620 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000621 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
622 for (unsigned i = 0; i != RVLocs.size(); ++i) {
623 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
624 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000625 InFlag = Chain.getValue(2);
626 ResultVals.push_back(Chain.getValue(0));
627 }
628 } else {
629 // Copies from the FP stack are special, as ST0 isn't a valid register
630 // before the fp stackifier runs.
631
632 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000633 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000634 SDOperand GROps[] = { Chain, InFlag };
635 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
636 Chain = RetVal.getValue(1);
637 InFlag = RetVal.getValue(2);
638
639 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
640 // an XMM register.
641 if (X86ScalarSSE) {
642 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
643 // shouldn't be necessary except that RFP cannot be live across
644 // multiple blocks. When stackifier is fixed, they can be uncoupled.
645 MachineFunction &MF = DAG.getMachineFunction();
646 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
647 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
648 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000649 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000650 };
651 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000652 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000653 Chain = RetVal.getValue(1);
654 }
Chris Lattner3085e152007-02-25 08:59:22 +0000655 ResultVals.push_back(RetVal);
656 }
657
658 // Merge everything together with a MERGE_VALUES node.
659 ResultVals.push_back(Chain);
660 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
661 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000662}
663
664
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000665//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000666// C & StdCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000667//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000668// StdCall calling convention seems to be standard for many Windows' API
669// routines and around. It differs from C calling convention just a little:
670// callee should clean up the stack, not caller. Symbols should be also
671// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000672
Evan Cheng85e38002006-04-27 05:35:28 +0000673/// AddLiveIn - This helper function adds the specified physical register to the
674/// MachineFunction as a live in value. It also creates a corresponding virtual
675/// register for it.
676static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000677 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000678 assert(RC->contains(PReg) && "Not the correct regclass!");
679 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
680 MF.addLiveIn(PReg, VReg);
681 return VReg;
682}
683
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000684SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
685 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000686 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000687 MachineFunction &MF = DAG.getMachineFunction();
688 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000689 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000690 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000691
Chris Lattner638402b2007-02-28 07:00:42 +0000692 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000693 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000694 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
695 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000696 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
697
Chris Lattnerf39f7712007-02-28 05:46:49 +0000698 SmallVector<SDOperand, 8> ArgValues;
699 unsigned LastVal = ~0U;
700 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
701 CCValAssign &VA = ArgLocs[i];
702 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
703 // places.
704 assert(VA.getValNo() != LastVal &&
705 "Don't support value assigned to multiple locs yet");
706 LastVal = VA.getValNo();
707
708 if (VA.isRegLoc()) {
709 MVT::ValueType RegVT = VA.getLocVT();
710 TargetRegisterClass *RC;
711 if (RegVT == MVT::i32)
712 RC = X86::GR32RegisterClass;
713 else {
714 assert(MVT::isVector(RegVT));
715 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000716 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000717
Chris Lattner82932a52007-03-02 05:12:29 +0000718 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
719 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000720
721 // If this is an 8 or 16-bit value, it is really passed promoted to 32
722 // bits. Insert an assert[sz]ext to capture this, then truncate to the
723 // right size.
724 if (VA.getLocInfo() == CCValAssign::SExt)
725 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
726 DAG.getValueType(VA.getValVT()));
727 else if (VA.getLocInfo() == CCValAssign::ZExt)
728 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
729 DAG.getValueType(VA.getValVT()));
730
731 if (VA.getLocInfo() != CCValAssign::Full)
732 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
733
734 ArgValues.push_back(ArgValue);
735 } else {
736 assert(VA.isMemLoc());
737
738 // Create the nodes corresponding to a load from this parameter slot.
739 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
740 VA.getLocMemOffset());
741 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
742 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
Evan Cheng1bc78042006-04-26 01:20:17 +0000743 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000744 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000745
746 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng1bc78042006-04-26 01:20:17 +0000747
Evan Cheng25caf632006-05-23 21:06:34 +0000748 ArgValues.push_back(Root);
749
Evan Cheng1bc78042006-04-26 01:20:17 +0000750 // If the function takes variable number of arguments, make a frame index for
751 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000752 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000753 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000754
755 if (isStdCall && !isVarArg) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000756 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000757 BytesCallerReserves = 0;
758 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000759 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000760
761 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000762 if (NumArgs &&
763 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000764 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000765 BytesToPopOnReturn = 4;
766
767 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000768 }
769
Evan Cheng25ab6902006-09-08 06:48:29 +0000770 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
771 ReturnAddrIndex = 0; // No return address slot generated yet.
Evan Cheng25caf632006-05-23 21:06:34 +0000772
Chris Lattnerd15dff22007-04-17 17:21:52 +0000773 MF.getInfo<X86MachineFunctionInfo>()
774 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000775
Evan Cheng25caf632006-05-23 21:06:34 +0000776 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000777 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000778 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000779}
780
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000781SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000782 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000783 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000784 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000785 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
786 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +0000787 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000788
Chris Lattner638402b2007-02-28 07:00:42 +0000789 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +0000790 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000791 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000792 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000793
Chris Lattner423c5f42007-02-28 05:31:48 +0000794 // Get a count of how many bytes are to be pushed on the stack.
795 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000796
Evan Cheng32fe1032006-05-25 00:59:30 +0000797 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000798
Chris Lattner5a88b832007-02-25 07:10:00 +0000799 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
800 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +0000801
Chris Lattner423c5f42007-02-28 05:31:48 +0000802 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +0000803
804 // Walk the register/memloc assignments, inserting copies/loads.
805 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
806 CCValAssign &VA = ArgLocs[i];
807 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000808
Chris Lattner423c5f42007-02-28 05:31:48 +0000809 // Promote the value if needed.
810 switch (VA.getLocInfo()) {
811 default: assert(0 && "Unknown loc info!");
812 case CCValAssign::Full: break;
813 case CCValAssign::SExt:
814 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
815 break;
816 case CCValAssign::ZExt:
817 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
818 break;
819 case CCValAssign::AExt:
820 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
821 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +0000822 }
Chris Lattner423c5f42007-02-28 05:31:48 +0000823
824 if (VA.isRegLoc()) {
825 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
826 } else {
827 assert(VA.isMemLoc());
828 if (StackPtr.Val == 0)
829 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
830 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000831 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
832 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000833 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000834 }
835
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000836 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000837 bool isSRet = NumOps &&
838 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000839 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000840
Evan Cheng32fe1032006-05-25 00:59:30 +0000841 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000842 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
843 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000844
Evan Cheng347d5f72006-04-28 21:29:37 +0000845 // Build a sequence of copy-to-reg nodes chained together with token chain
846 // and flag operands which copy the outgoing args into registers.
847 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000848 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
849 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
850 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000851 InFlag = Chain.getValue(1);
852 }
853
Evan Chengf4684712007-02-21 21:18:14 +0000854 // ELF / PIC requires GOT in the EBX register before function calls via PLT
855 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +0000856 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
857 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000858 Chain = DAG.getCopyToReg(Chain, X86::EBX,
859 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
860 InFlag);
861 InFlag = Chain.getValue(1);
862 }
863
Evan Cheng32fe1032006-05-25 00:59:30 +0000864 // If the callee is a GlobalAddress node (quite common, every direct call is)
865 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +0000866 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +0000867 // We should use extra load for direct calls to dllimported functions in
868 // non-JIT mode.
869 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
870 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +0000871 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
872 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +0000873 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
874
Chris Lattnerd96d0722007-02-25 06:40:16 +0000875 // Returns a chain & a flag for retval copy to use.
876 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +0000877 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000878 Ops.push_back(Chain);
879 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +0000880
881 // Add argument registers to the end of the list so that they are known live
882 // into the call.
883 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000884 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +0000885 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +0000886
887 // Add an implicit use GOT pointer in EBX.
888 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
889 Subtarget->isPICStyleGOT())
890 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000891
Evan Cheng347d5f72006-04-28 21:29:37 +0000892 if (InFlag.Val)
893 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000894
Evan Cheng32fe1032006-05-25 00:59:30 +0000895 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000896 NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +0000897 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000898
Chris Lattner2d297092006-05-23 18:50:38 +0000899 // Create the CALLSEQ_END node.
900 unsigned NumBytesForCalleeToPush = 0;
901
Chris Lattner09c75a42007-02-25 09:06:15 +0000902 if (CC == CallingConv::X86_StdCall) {
903 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000904 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +0000905 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000906 NumBytesForCalleeToPush = NumBytes;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000907 } else {
908 // If this is is a call to a struct-return function, the callee
909 // pops the hidden struct pointer, so we have to push it back.
910 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000911 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000912 }
913
Chris Lattner7d53a1c2007-02-25 07:18:38 +0000914 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000915 Ops.clear();
916 Ops.push_back(Chain);
917 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000918 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000919 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000920 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner3085e152007-02-25 08:59:22 +0000921 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000922
Chris Lattner3085e152007-02-25 08:59:22 +0000923 // Handle result values, copying them out of physregs into vregs that we
924 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +0000925 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000926}
927
Evan Cheng25ab6902006-09-08 06:48:29 +0000928
929//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +0000930// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000931//===----------------------------------------------------------------------===//
932//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000933// The X86 'fastcall' calling convention passes up to two integer arguments in
934// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
935// and requires that the callee pop its arguments off the stack (allowing proper
936// tail calls), and has the same return value conventions as C calling convs.
937//
938// This calling convention always arranges for the callee pop value to be 8n+4
939// bytes, which is needed for tail recursion elimination and stack alignment
940// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +0000941SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +0000942X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000943 MachineFunction &MF = DAG.getMachineFunction();
944 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000945 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +0000946 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000947
Chris Lattner638402b2007-02-28 07:00:42 +0000948 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +0000949 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000950 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
951 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000952 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +0000953
954 SmallVector<SDOperand, 8> ArgValues;
955 unsigned LastVal = ~0U;
956 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
957 CCValAssign &VA = ArgLocs[i];
958 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
959 // places.
960 assert(VA.getValNo() != LastVal &&
961 "Don't support value assigned to multiple locs yet");
962 LastVal = VA.getValNo();
963
964 if (VA.isRegLoc()) {
965 MVT::ValueType RegVT = VA.getLocVT();
966 TargetRegisterClass *RC;
967 if (RegVT == MVT::i32)
968 RC = X86::GR32RegisterClass;
969 else {
970 assert(MVT::isVector(RegVT));
971 RC = X86::VR128RegisterClass;
972 }
973
Chris Lattner82932a52007-03-02 05:12:29 +0000974 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
975 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +0000976
977 // If this is an 8 or 16-bit value, it is really passed promoted to 32
978 // bits. Insert an assert[sz]ext to capture this, then truncate to the
979 // right size.
980 if (VA.getLocInfo() == CCValAssign::SExt)
981 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
982 DAG.getValueType(VA.getValVT()));
983 else if (VA.getLocInfo() == CCValAssign::ZExt)
984 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
985 DAG.getValueType(VA.getValVT()));
986
987 if (VA.getLocInfo() != CCValAssign::Full)
988 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
989
990 ArgValues.push_back(ArgValue);
991 } else {
992 assert(VA.isMemLoc());
993
994 // Create the nodes corresponding to a load from this parameter slot.
995 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
996 VA.getLocMemOffset());
997 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
998 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
999 }
1000 }
1001
Evan Cheng25caf632006-05-23 21:06:34 +00001002 ArgValues.push_back(Root);
1003
Chris Lattnerfc664c12007-02-28 06:21:19 +00001004 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001005
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001006 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001007 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1008 // arguments and the arguments after the retaddr has been pushed are aligned.
1009 if ((StackSize & 7) == 0)
1010 StackSize += 4;
1011 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001012
1013 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001014 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001015 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001016 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001017 BytesCallerReserves = 0;
1018
Chris Lattnerd15dff22007-04-17 17:21:52 +00001019 MF.getInfo<X86MachineFunctionInfo>()
1020 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001021
Evan Cheng25caf632006-05-23 21:06:34 +00001022 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001023 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001024 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001025}
1026
Chris Lattnere87e1152006-09-26 03:57:53 +00001027SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001028 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001029 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001030 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001031 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001032 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001033
Chris Lattner638402b2007-02-28 07:00:42 +00001034 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001035 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001036 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001037 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001038
1039 // Get a count of how many bytes are to be pushed on the stack.
1040 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001041
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001042 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001043 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1044 // arguments and the arguments after the retaddr has been pushed are aligned.
1045 if ((NumBytes & 7) == 0)
1046 NumBytes += 4;
1047 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001048
Chris Lattner94dd2922006-02-13 09:00:43 +00001049 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001050
Chris Lattner5a88b832007-02-25 07:10:00 +00001051 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1052 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001053
1054 SDOperand StackPtr;
1055
1056 // Walk the register/memloc assignments, inserting copies/loads.
1057 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1058 CCValAssign &VA = ArgLocs[i];
1059 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1060
1061 // Promote the value if needed.
1062 switch (VA.getLocInfo()) {
1063 default: assert(0 && "Unknown loc info!");
1064 case CCValAssign::Full: break;
1065 case CCValAssign::SExt:
1066 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001067 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001068 case CCValAssign::ZExt:
1069 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1070 break;
1071 case CCValAssign::AExt:
1072 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1073 break;
1074 }
1075
1076 if (VA.isRegLoc()) {
1077 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1078 } else {
1079 assert(VA.isMemLoc());
1080 if (StackPtr.Val == 0)
1081 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1082 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001083 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001084 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng32fe1032006-05-25 00:59:30 +00001085 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001086 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001087
Evan Cheng32fe1032006-05-25 00:59:30 +00001088 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001089 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1090 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001091
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001092 // Build a sequence of copy-to-reg nodes chained together with token chain
1093 // and flag operands which copy the outgoing args into registers.
1094 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001095 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1096 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1097 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001098 InFlag = Chain.getValue(1);
1099 }
1100
Evan Cheng32fe1032006-05-25 00:59:30 +00001101 // If the callee is a GlobalAddress node (quite common, every direct call is)
1102 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001103 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001104 // We should use extra load for direct calls to dllimported functions in
1105 // non-JIT mode.
1106 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1107 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001108 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1109 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001110 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1111
Evan Chengf4684712007-02-21 21:18:14 +00001112 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1113 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001114 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1115 Subtarget->isPICStyleGOT()) {
1116 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1117 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1118 InFlag);
1119 InFlag = Chain.getValue(1);
1120 }
1121
Chris Lattnerd96d0722007-02-25 06:40:16 +00001122 // Returns a chain & a flag for retval copy to use.
1123 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001124 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001125 Ops.push_back(Chain);
1126 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001127
1128 // Add argument registers to the end of the list so that they are known live
1129 // into the call.
1130 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001131 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001132 RegsToPass[i].second.getValueType()));
1133
Evan Chengf4684712007-02-21 21:18:14 +00001134 // Add an implicit use GOT pointer in EBX.
1135 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1136 Subtarget->isPICStyleGOT())
1137 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1138
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001139 if (InFlag.Val)
1140 Ops.push_back(InFlag);
1141
1142 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001143 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001144 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001145 InFlag = Chain.getValue(1);
1146
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001147 // Returns a flag for retval copy to use.
1148 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001149 Ops.clear();
1150 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001151 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1152 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001153 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001154 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001155 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001156
Chris Lattner339b4392007-02-25 09:10:05 +00001157 // Handle result values, copying them out of physregs into vregs that we
1158 // return.
1159 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001160}
1161
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001162
1163//===----------------------------------------------------------------------===//
1164// X86-64 C Calling Convention implementation
1165//===----------------------------------------------------------------------===//
1166
1167SDOperand
1168X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001169 MachineFunction &MF = DAG.getMachineFunction();
1170 MachineFrameInfo *MFI = MF.getFrameInfo();
1171 SDOperand Root = Op.getOperand(0);
1172 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1173
1174 static const unsigned GPR64ArgRegs[] = {
1175 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1176 };
1177 static const unsigned XMMArgRegs[] = {
1178 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1179 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1180 };
1181
Chris Lattner638402b2007-02-28 07:00:42 +00001182
1183 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001184 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001185 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1186 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001187 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001188
1189 SmallVector<SDOperand, 8> ArgValues;
1190 unsigned LastVal = ~0U;
1191 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1192 CCValAssign &VA = ArgLocs[i];
1193 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1194 // places.
1195 assert(VA.getValNo() != LastVal &&
1196 "Don't support value assigned to multiple locs yet");
1197 LastVal = VA.getValNo();
1198
1199 if (VA.isRegLoc()) {
1200 MVT::ValueType RegVT = VA.getLocVT();
1201 TargetRegisterClass *RC;
1202 if (RegVT == MVT::i32)
1203 RC = X86::GR32RegisterClass;
1204 else if (RegVT == MVT::i64)
1205 RC = X86::GR64RegisterClass;
1206 else if (RegVT == MVT::f32)
1207 RC = X86::FR32RegisterClass;
1208 else if (RegVT == MVT::f64)
1209 RC = X86::FR64RegisterClass;
1210 else {
1211 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001212 if (MVT::getSizeInBits(RegVT) == 64) {
1213 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1214 RegVT = MVT::i64;
1215 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001216 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001217 }
Chris Lattner82932a52007-03-02 05:12:29 +00001218
1219 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1220 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001221
1222 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1223 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1224 // right size.
1225 if (VA.getLocInfo() == CCValAssign::SExt)
1226 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228 else if (VA.getLocInfo() == CCValAssign::ZExt)
1229 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1231
1232 if (VA.getLocInfo() != CCValAssign::Full)
1233 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1234
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001235 // Handle MMX values passed in GPRs.
1236 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1237 MVT::getSizeInBits(RegVT) == 64)
1238 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1239
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001240 ArgValues.push_back(ArgValue);
1241 } else {
1242 assert(VA.isMemLoc());
1243
1244 // Create the nodes corresponding to a load from this parameter slot.
1245 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1246 VA.getLocMemOffset());
1247 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1248 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1249 }
1250 }
1251
1252 unsigned StackSize = CCInfo.getNextStackOffset();
1253
1254 // If the function takes variable number of arguments, make a frame index for
1255 // the start of the first vararg value... for expansion of llvm.va_start.
1256 if (isVarArg) {
1257 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1258 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1259
1260 // For X86-64, if there are vararg parameters that are passed via
1261 // registers, then we must store them to their spots on the stack so they
1262 // may be loaded by deferencing the result of va_next.
1263 VarArgsGPOffset = NumIntRegs * 8;
1264 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1265 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1266 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1267
1268 // Store the integer parameter registers.
1269 SmallVector<SDOperand, 8> MemOps;
1270 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1271 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1272 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1273 for (; NumIntRegs != 6; ++NumIntRegs) {
1274 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1275 X86::GR64RegisterClass);
1276 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1277 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1278 MemOps.push_back(Store);
1279 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1280 DAG.getConstant(8, getPointerTy()));
1281 }
1282
1283 // Now store the XMM (fp + vector) parameter registers.
1284 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1285 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1286 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1287 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1288 X86::VR128RegisterClass);
1289 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1290 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1291 MemOps.push_back(Store);
1292 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1293 DAG.getConstant(16, getPointerTy()));
1294 }
1295 if (!MemOps.empty())
1296 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1297 &MemOps[0], MemOps.size());
1298 }
1299
1300 ArgValues.push_back(Root);
1301
1302 ReturnAddrIndex = 0; // No return address slot generated yet.
1303 BytesToPopOnReturn = 0; // Callee pops nothing.
1304 BytesCallerReserves = StackSize;
1305
1306 // Return the new list of results.
1307 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1308 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1309}
1310
1311SDOperand
1312X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1313 unsigned CC) {
1314 SDOperand Chain = Op.getOperand(0);
1315 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1316 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1317 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001318
1319 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001320 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001321 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001322 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001323
1324 // Get a count of how many bytes are to be pushed on the stack.
1325 unsigned NumBytes = CCInfo.getNextStackOffset();
1326 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1327
1328 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1329 SmallVector<SDOperand, 8> MemOpChains;
1330
1331 SDOperand StackPtr;
1332
1333 // Walk the register/memloc assignments, inserting copies/loads.
1334 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1335 CCValAssign &VA = ArgLocs[i];
1336 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1337
1338 // Promote the value if needed.
1339 switch (VA.getLocInfo()) {
1340 default: assert(0 && "Unknown loc info!");
1341 case CCValAssign::Full: break;
1342 case CCValAssign::SExt:
1343 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1344 break;
1345 case CCValAssign::ZExt:
1346 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1347 break;
1348 case CCValAssign::AExt:
1349 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1350 break;
1351 }
1352
1353 if (VA.isRegLoc()) {
1354 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1355 } else {
1356 assert(VA.isMemLoc());
1357 if (StackPtr.Val == 0)
1358 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1359 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1360 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1361 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1362 }
1363 }
1364
1365 if (!MemOpChains.empty())
1366 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1367 &MemOpChains[0], MemOpChains.size());
1368
1369 // Build a sequence of copy-to-reg nodes chained together with token chain
1370 // and flag operands which copy the outgoing args into registers.
1371 SDOperand InFlag;
1372 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1373 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1374 InFlag);
1375 InFlag = Chain.getValue(1);
1376 }
1377
1378 if (isVarArg) {
1379 // From AMD64 ABI document:
1380 // For calls that may call functions that use varargs or stdargs
1381 // (prototype-less calls or calls to functions containing ellipsis (...) in
1382 // the declaration) %al is used as hidden argument to specify the number
1383 // of SSE registers used. The contents of %al do not need to match exactly
1384 // the number of registers, but must be an ubound on the number of SSE
1385 // registers used and is in the range 0 - 8 inclusive.
1386
1387 // Count the number of XMM registers allocated.
1388 static const unsigned XMMArgRegs[] = {
1389 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1390 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1391 };
1392 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1393
1394 Chain = DAG.getCopyToReg(Chain, X86::AL,
1395 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1396 InFlag = Chain.getValue(1);
1397 }
1398
1399 // If the callee is a GlobalAddress node (quite common, every direct call is)
1400 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1401 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1402 // We should use extra load for direct calls to dllimported functions in
1403 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001404 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001405 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1406 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001407 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1408 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001409 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1410 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001411
1412 // Returns a chain & a flag for retval copy to use.
1413 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1414 SmallVector<SDOperand, 8> Ops;
1415 Ops.push_back(Chain);
1416 Ops.push_back(Callee);
1417
1418 // Add argument registers to the end of the list so that they are known live
1419 // into the call.
1420 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1421 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1422 RegsToPass[i].second.getValueType()));
1423
1424 if (InFlag.Val)
1425 Ops.push_back(InFlag);
1426
1427 // FIXME: Do not generate X86ISD::TAILCALL for now.
1428 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1429 NodeTys, &Ops[0], Ops.size());
1430 InFlag = Chain.getValue(1);
1431
1432 // Returns a flag for retval copy to use.
1433 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1434 Ops.clear();
1435 Ops.push_back(Chain);
1436 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1437 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1438 Ops.push_back(InFlag);
1439 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1440 InFlag = Chain.getValue(1);
1441
1442 // Handle result values, copying them out of physregs into vregs that we
1443 // return.
1444 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1445}
1446
1447
1448//===----------------------------------------------------------------------===//
1449// Other Lowering Hooks
1450//===----------------------------------------------------------------------===//
1451
1452
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001453SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1454 if (ReturnAddrIndex == 0) {
1455 // Set up a frame object for the return address.
1456 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng25ab6902006-09-08 06:48:29 +00001457 if (Subtarget->is64Bit())
1458 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1459 else
1460 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001461 }
1462
Evan Cheng25ab6902006-09-08 06:48:29 +00001463 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001464}
1465
1466
1467
Evan Cheng6dfa9992006-01-30 23:41:35 +00001468/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1469/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001470/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1471/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001472static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001473 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1474 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001475 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001476 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001477 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1478 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1479 // X > -1 -> X == 0, jump !sign.
1480 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001481 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001482 return true;
1483 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1484 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001485 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001486 return true;
1487 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001488 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001489
Evan Chengd9558e02006-01-06 00:43:03 +00001490 switch (SetCCOpcode) {
1491 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001492 case ISD::SETEQ: X86CC = X86::COND_E; break;
1493 case ISD::SETGT: X86CC = X86::COND_G; break;
1494 case ISD::SETGE: X86CC = X86::COND_GE; break;
1495 case ISD::SETLT: X86CC = X86::COND_L; break;
1496 case ISD::SETLE: X86CC = X86::COND_LE; break;
1497 case ISD::SETNE: X86CC = X86::COND_NE; break;
1498 case ISD::SETULT: X86CC = X86::COND_B; break;
1499 case ISD::SETUGT: X86CC = X86::COND_A; break;
1500 case ISD::SETULE: X86CC = X86::COND_BE; break;
1501 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001502 }
1503 } else {
1504 // On a floating point condition, the flags are set as follows:
1505 // ZF PF CF op
1506 // 0 | 0 | 0 | X > Y
1507 // 0 | 0 | 1 | X < Y
1508 // 1 | 0 | 0 | X == Y
1509 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001510 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001511 switch (SetCCOpcode) {
1512 default: break;
1513 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001514 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001515 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001516 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001517 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001518 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001519 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001520 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001521 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001522 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001523 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001524 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001525 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001526 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001527 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001528 case ISD::SETNE: X86CC = X86::COND_NE; break;
1529 case ISD::SETUO: X86CC = X86::COND_P; break;
1530 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001531 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001532 if (Flip)
1533 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001534 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001535
Chris Lattner7fbe9722006-10-20 17:42:20 +00001536 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001537}
1538
Evan Cheng4a460802006-01-11 00:33:36 +00001539/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1540/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001541/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001542static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001543 switch (X86CC) {
1544 default:
1545 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001546 case X86::COND_B:
1547 case X86::COND_BE:
1548 case X86::COND_E:
1549 case X86::COND_P:
1550 case X86::COND_A:
1551 case X86::COND_AE:
1552 case X86::COND_NE:
1553 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001554 return true;
1555 }
1556}
1557
Evan Cheng5ced1d82006-04-06 23:23:56 +00001558/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001559/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001560static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1561 if (Op.getOpcode() == ISD::UNDEF)
1562 return true;
1563
1564 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001565 return (Val >= Low && Val < Hi);
1566}
1567
1568/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1569/// true if Op is undef or if its value equal to the specified value.
1570static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1571 if (Op.getOpcode() == ISD::UNDEF)
1572 return true;
1573 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001574}
1575
Evan Cheng0188ecb2006-03-22 18:59:22 +00001576/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1577/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1578bool X86::isPSHUFDMask(SDNode *N) {
1579 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1580
1581 if (N->getNumOperands() != 4)
1582 return false;
1583
1584 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001585 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001586 SDOperand Arg = N->getOperand(i);
1587 if (Arg.getOpcode() == ISD::UNDEF) continue;
1588 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1589 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001590 return false;
1591 }
1592
1593 return true;
1594}
1595
1596/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001597/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001598bool X86::isPSHUFHWMask(SDNode *N) {
1599 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1600
1601 if (N->getNumOperands() != 8)
1602 return false;
1603
1604 // Lower quadword copied in order.
1605 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001606 SDOperand Arg = N->getOperand(i);
1607 if (Arg.getOpcode() == ISD::UNDEF) continue;
1608 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1609 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001610 return false;
1611 }
1612
1613 // Upper quadword shuffled.
1614 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001615 SDOperand Arg = N->getOperand(i);
1616 if (Arg.getOpcode() == ISD::UNDEF) continue;
1617 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1618 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001619 if (Val < 4 || Val > 7)
1620 return false;
1621 }
1622
1623 return true;
1624}
1625
1626/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001627/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001628bool X86::isPSHUFLWMask(SDNode *N) {
1629 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1630
1631 if (N->getNumOperands() != 8)
1632 return false;
1633
1634 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001635 for (unsigned i = 4; i != 8; ++i)
1636 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001637 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001638
1639 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001640 for (unsigned i = 0; i != 4; ++i)
1641 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001642 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001643
1644 return true;
1645}
1646
Evan Cheng14aed5e2006-03-24 01:18:28 +00001647/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1648/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00001649static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00001650 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001651
Evan Cheng39623da2006-04-20 08:58:49 +00001652 unsigned Half = NumElems / 2;
1653 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001654 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00001655 return false;
1656 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001657 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00001658 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001659
1660 return true;
1661}
1662
Evan Cheng39623da2006-04-20 08:58:49 +00001663bool X86::isSHUFPMask(SDNode *N) {
1664 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001665 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001666}
1667
Evan Cheng213d2cf2007-05-17 18:45:50 +00001668/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00001669/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1670/// half elements to come from vector 1 (which would equal the dest.) and
1671/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00001672static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1673 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001674
Chris Lattner5a88b832007-02-25 07:10:00 +00001675 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00001676 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001677 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00001678 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00001679 for (unsigned i = Half; i < NumOps; ++i)
1680 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00001681 return false;
1682 return true;
1683}
1684
1685static bool isCommutedSHUFP(SDNode *N) {
1686 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001687 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001688}
1689
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001690/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1691/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1692bool X86::isMOVHLPSMask(SDNode *N) {
1693 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1694
Evan Cheng2064a2b2006-03-28 06:50:32 +00001695 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001696 return false;
1697
Evan Cheng2064a2b2006-03-28 06:50:32 +00001698 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001699 return isUndefOrEqual(N->getOperand(0), 6) &&
1700 isUndefOrEqual(N->getOperand(1), 7) &&
1701 isUndefOrEqual(N->getOperand(2), 2) &&
1702 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001703}
1704
Evan Cheng6e56e2c2006-11-07 22:14:24 +00001705/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1706/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1707/// <2, 3, 2, 3>
1708bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1709 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1710
1711 if (N->getNumOperands() != 4)
1712 return false;
1713
1714 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1715 return isUndefOrEqual(N->getOperand(0), 2) &&
1716 isUndefOrEqual(N->getOperand(1), 3) &&
1717 isUndefOrEqual(N->getOperand(2), 2) &&
1718 isUndefOrEqual(N->getOperand(3), 3);
1719}
1720
Evan Cheng5ced1d82006-04-06 23:23:56 +00001721/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1722/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1723bool X86::isMOVLPMask(SDNode *N) {
1724 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1725
1726 unsigned NumElems = N->getNumOperands();
1727 if (NumElems != 2 && NumElems != 4)
1728 return false;
1729
Evan Chengc5cdff22006-04-07 21:53:05 +00001730 for (unsigned i = 0; i < NumElems/2; ++i)
1731 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1732 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001733
Evan Chengc5cdff22006-04-07 21:53:05 +00001734 for (unsigned i = NumElems/2; i < NumElems; ++i)
1735 if (!isUndefOrEqual(N->getOperand(i), i))
1736 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001737
1738 return true;
1739}
1740
1741/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001742/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1743/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001744bool X86::isMOVHPMask(SDNode *N) {
1745 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1746
1747 unsigned NumElems = N->getNumOperands();
1748 if (NumElems != 2 && NumElems != 4)
1749 return false;
1750
Evan Chengc5cdff22006-04-07 21:53:05 +00001751 for (unsigned i = 0; i < NumElems/2; ++i)
1752 if (!isUndefOrEqual(N->getOperand(i), i))
1753 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001754
1755 for (unsigned i = 0; i < NumElems/2; ++i) {
1756 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001757 if (!isUndefOrEqual(Arg, i + NumElems))
1758 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001759 }
1760
1761 return true;
1762}
1763
Evan Cheng0038e592006-03-28 00:39:58 +00001764/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1765/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00001766bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1767 bool V2IsSplat = false) {
1768 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00001769 return false;
1770
Chris Lattner5a88b832007-02-25 07:10:00 +00001771 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1772 SDOperand BitI = Elts[i];
1773 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001774 if (!isUndefOrEqual(BitI, j))
1775 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001776 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001777 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001778 return false;
1779 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00001780 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001781 return false;
1782 }
Evan Cheng0038e592006-03-28 00:39:58 +00001783 }
1784
1785 return true;
1786}
1787
Evan Cheng39623da2006-04-20 08:58:49 +00001788bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1789 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001790 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001791}
1792
Evan Cheng4fcb9222006-03-28 02:43:26 +00001793/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1794/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00001795bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1796 bool V2IsSplat = false) {
1797 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00001798 return false;
1799
Chris Lattner5a88b832007-02-25 07:10:00 +00001800 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1801 SDOperand BitI = Elts[i];
1802 SDOperand BitI1 = Elts[i+1];
1803 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00001804 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001805 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001806 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001807 return false;
1808 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00001809 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001810 return false;
1811 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001812 }
1813
1814 return true;
1815}
1816
Evan Cheng39623da2006-04-20 08:58:49 +00001817bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1818 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001819 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001820}
1821
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001822/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1823/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1824/// <0, 0, 1, 1>
1825bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1826 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1827
1828 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00001829 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001830 return false;
1831
1832 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1833 SDOperand BitI = N->getOperand(i);
1834 SDOperand BitI1 = N->getOperand(i+1);
1835
Evan Chengc5cdff22006-04-07 21:53:05 +00001836 if (!isUndefOrEqual(BitI, j))
1837 return false;
1838 if (!isUndefOrEqual(BitI1, j))
1839 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001840 }
1841
1842 return true;
1843}
1844
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00001845/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1846/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1847/// <2, 2, 3, 3>
1848bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1849 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1850
1851 unsigned NumElems = N->getNumOperands();
1852 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1853 return false;
1854
1855 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1856 SDOperand BitI = N->getOperand(i);
1857 SDOperand BitI1 = N->getOperand(i + 1);
1858
1859 if (!isUndefOrEqual(BitI, j))
1860 return false;
1861 if (!isUndefOrEqual(BitI1, j))
1862 return false;
1863 }
1864
1865 return true;
1866}
1867
Evan Cheng017dcc62006-04-21 01:05:10 +00001868/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1869/// specifies a shuffle of elements that is suitable for input to MOVSS,
1870/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00001871static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1872 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001873 return false;
1874
Chris Lattner5a88b832007-02-25 07:10:00 +00001875 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001876 return false;
1877
Chris Lattner5a88b832007-02-25 07:10:00 +00001878 for (unsigned i = 1; i < NumElts; ++i) {
1879 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001880 return false;
1881 }
1882
1883 return true;
1884}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001885
Evan Cheng017dcc62006-04-21 01:05:10 +00001886bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001887 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001888 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001889}
1890
Evan Cheng017dcc62006-04-21 01:05:10 +00001891/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1892/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00001893/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00001894static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1895 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00001896 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001897 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00001898 return false;
1899
1900 if (!isUndefOrEqual(Ops[0], 0))
1901 return false;
1902
Chris Lattner5a88b832007-02-25 07:10:00 +00001903 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00001904 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00001905 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1906 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1907 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00001908 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001909 }
1910
1911 return true;
1912}
1913
Evan Cheng8cf723d2006-09-08 01:50:06 +00001914static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1915 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001916 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001917 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1918 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00001919}
1920
Evan Chengd9539472006-04-14 21:59:03 +00001921/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1922/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1923bool X86::isMOVSHDUPMask(SDNode *N) {
1924 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1925
1926 if (N->getNumOperands() != 4)
1927 return false;
1928
1929 // Expect 1, 1, 3, 3
1930 for (unsigned i = 0; i < 2; ++i) {
1931 SDOperand Arg = N->getOperand(i);
1932 if (Arg.getOpcode() == ISD::UNDEF) continue;
1933 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1934 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1935 if (Val != 1) return false;
1936 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001937
1938 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001939 for (unsigned i = 2; i < 4; ++i) {
1940 SDOperand Arg = N->getOperand(i);
1941 if (Arg.getOpcode() == ISD::UNDEF) continue;
1942 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1943 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1944 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001945 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001946 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001947
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001948 // Don't use movshdup if it can be done with a shufps.
1949 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001950}
1951
1952/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1953/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1954bool X86::isMOVSLDUPMask(SDNode *N) {
1955 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1956
1957 if (N->getNumOperands() != 4)
1958 return false;
1959
1960 // Expect 0, 0, 2, 2
1961 for (unsigned i = 0; i < 2; ++i) {
1962 SDOperand Arg = N->getOperand(i);
1963 if (Arg.getOpcode() == ISD::UNDEF) continue;
1964 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1965 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1966 if (Val != 0) return false;
1967 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001968
1969 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001970 for (unsigned i = 2; i < 4; ++i) {
1971 SDOperand Arg = N->getOperand(i);
1972 if (Arg.getOpcode() == ISD::UNDEF) continue;
1973 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1974 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1975 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001976 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001977 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001978
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001979 // Don't use movshdup if it can be done with a shufps.
1980 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001981}
1982
Evan Cheng49892af2007-06-19 00:02:56 +00001983/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
1984/// specifies a identity operation on the LHS or RHS.
1985static bool isIdentityMask(SDNode *N, bool RHS = false) {
1986 unsigned NumElems = N->getNumOperands();
1987 for (unsigned i = 0; i < NumElems; ++i)
1988 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
1989 return false;
1990 return true;
1991}
1992
Evan Chengb9df0ca2006-03-22 02:53:00 +00001993/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1994/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001995static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001996 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1997
Evan Chengb9df0ca2006-03-22 02:53:00 +00001998 // This is a splat operation if each element of the permute is the same, and
1999 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002000 unsigned NumElems = N->getNumOperands();
2001 SDOperand ElementBase;
2002 unsigned i = 0;
2003 for (; i != NumElems; ++i) {
2004 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002005 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002006 ElementBase = Elt;
2007 break;
2008 }
2009 }
2010
2011 if (!ElementBase.Val)
2012 return false;
2013
2014 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002015 SDOperand Arg = N->getOperand(i);
2016 if (Arg.getOpcode() == ISD::UNDEF) continue;
2017 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002018 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002019 }
2020
2021 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002022 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002023}
2024
Evan Chengc575ca22006-04-17 20:43:08 +00002025/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2026/// a splat of a single element and it's a 2 or 4 element mask.
2027bool X86::isSplatMask(SDNode *N) {
2028 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2029
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002030 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002031 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2032 return false;
2033 return ::isSplatMask(N);
2034}
2035
Evan Chengf686d9b2006-10-27 21:08:32 +00002036/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2037/// specifies a splat of zero element.
2038bool X86::isSplatLoMask(SDNode *N) {
2039 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2040
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002041 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002042 if (!isUndefOrEqual(N->getOperand(i), 0))
2043 return false;
2044 return true;
2045}
2046
Evan Cheng63d33002006-03-22 08:01:21 +00002047/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2048/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2049/// instructions.
2050unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002051 unsigned NumOperands = N->getNumOperands();
2052 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2053 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002054 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002055 unsigned Val = 0;
2056 SDOperand Arg = N->getOperand(NumOperands-i-1);
2057 if (Arg.getOpcode() != ISD::UNDEF)
2058 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002059 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002060 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002061 if (i != NumOperands - 1)
2062 Mask <<= Shift;
2063 }
Evan Cheng63d33002006-03-22 08:01:21 +00002064
2065 return Mask;
2066}
2067
Evan Cheng506d3df2006-03-29 23:07:14 +00002068/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2069/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2070/// instructions.
2071unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2072 unsigned Mask = 0;
2073 // 8 nodes, but we only care about the last 4.
2074 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002075 unsigned Val = 0;
2076 SDOperand Arg = N->getOperand(i);
2077 if (Arg.getOpcode() != ISD::UNDEF)
2078 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002079 Mask |= (Val - 4);
2080 if (i != 4)
2081 Mask <<= 2;
2082 }
2083
2084 return Mask;
2085}
2086
2087/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2088/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2089/// instructions.
2090unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2091 unsigned Mask = 0;
2092 // 8 nodes, but we only care about the first 4.
2093 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002094 unsigned Val = 0;
2095 SDOperand Arg = N->getOperand(i);
2096 if (Arg.getOpcode() != ISD::UNDEF)
2097 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002098 Mask |= Val;
2099 if (i != 0)
2100 Mask <<= 2;
2101 }
2102
2103 return Mask;
2104}
2105
Evan Chengc21a0532006-04-05 01:47:37 +00002106/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2107/// specifies a 8 element shuffle that can be broken into a pair of
2108/// PSHUFHW and PSHUFLW.
2109static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2110 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2111
2112 if (N->getNumOperands() != 8)
2113 return false;
2114
2115 // Lower quadword shuffled.
2116 for (unsigned i = 0; i != 4; ++i) {
2117 SDOperand Arg = N->getOperand(i);
2118 if (Arg.getOpcode() == ISD::UNDEF) continue;
2119 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2120 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2121 if (Val > 4)
2122 return false;
2123 }
2124
2125 // Upper quadword shuffled.
2126 for (unsigned i = 4; i != 8; ++i) {
2127 SDOperand Arg = N->getOperand(i);
2128 if (Arg.getOpcode() == ISD::UNDEF) continue;
2129 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2130 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2131 if (Val < 4 || Val > 7)
2132 return false;
2133 }
2134
2135 return true;
2136}
2137
Evan Cheng5ced1d82006-04-06 23:23:56 +00002138/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2139/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002140static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2141 SDOperand &V2, SDOperand &Mask,
2142 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002143 MVT::ValueType VT = Op.getValueType();
2144 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002145 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002146 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002147 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002148
2149 for (unsigned i = 0; i != NumElems; ++i) {
2150 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002151 if (Arg.getOpcode() == ISD::UNDEF) {
2152 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2153 continue;
2154 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002155 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2156 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2157 if (Val < NumElems)
2158 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2159 else
2160 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2161 }
2162
Evan Cheng9eca5e82006-10-25 21:49:50 +00002163 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002164 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002165 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002166}
2167
Evan Cheng533a0aa2006-04-19 20:35:22 +00002168/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2169/// match movhlps. The lower half elements should come from upper half of
2170/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002171/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002172static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2173 unsigned NumElems = Mask->getNumOperands();
2174 if (NumElems != 4)
2175 return false;
2176 for (unsigned i = 0, e = 2; i != e; ++i)
2177 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2178 return false;
2179 for (unsigned i = 2; i != 4; ++i)
2180 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2181 return false;
2182 return true;
2183}
2184
Evan Cheng5ced1d82006-04-06 23:23:56 +00002185/// isScalarLoadToVector - Returns true if the node is a scalar load that
2186/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002187static inline bool isScalarLoadToVector(SDNode *N) {
2188 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2189 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002190 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002191 }
2192 return false;
2193}
2194
Evan Cheng533a0aa2006-04-19 20:35:22 +00002195/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2196/// match movlp{s|d}. The lower half elements should come from lower half of
2197/// V1 (and in order), and the upper half elements should come from the upper
2198/// half of V2 (and in order). And since V1 will become the source of the
2199/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002200static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002201 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002202 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002203 // Is V2 is a vector load, don't do this transformation. We will try to use
2204 // load folding shufps op.
2205 if (ISD::isNON_EXTLoad(V2))
2206 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002207
Evan Cheng533a0aa2006-04-19 20:35:22 +00002208 unsigned NumElems = Mask->getNumOperands();
2209 if (NumElems != 2 && NumElems != 4)
2210 return false;
2211 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2212 if (!isUndefOrEqual(Mask->getOperand(i), i))
2213 return false;
2214 for (unsigned i = NumElems/2; i != NumElems; ++i)
2215 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2216 return false;
2217 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002218}
2219
Evan Cheng39623da2006-04-20 08:58:49 +00002220/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2221/// all the same.
2222static bool isSplatVector(SDNode *N) {
2223 if (N->getOpcode() != ISD::BUILD_VECTOR)
2224 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002225
Evan Cheng39623da2006-04-20 08:58:49 +00002226 SDOperand SplatValue = N->getOperand(0);
2227 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2228 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002229 return false;
2230 return true;
2231}
2232
Evan Cheng8cf723d2006-09-08 01:50:06 +00002233/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2234/// to an undef.
2235static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002236 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002237 return false;
2238
2239 SDOperand V1 = N->getOperand(0);
2240 SDOperand V2 = N->getOperand(1);
2241 SDOperand Mask = N->getOperand(2);
2242 unsigned NumElems = Mask.getNumOperands();
2243 for (unsigned i = 0; i != NumElems; ++i) {
2244 SDOperand Arg = Mask.getOperand(i);
2245 if (Arg.getOpcode() != ISD::UNDEF) {
2246 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2247 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2248 return false;
2249 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2250 return false;
2251 }
2252 }
2253 return true;
2254}
2255
Evan Cheng213d2cf2007-05-17 18:45:50 +00002256/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2257/// constant +0.0.
2258static inline bool isZeroNode(SDOperand Elt) {
2259 return ((isa<ConstantSDNode>(Elt) &&
2260 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2261 (isa<ConstantFPSDNode>(Elt) &&
2262 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2263}
2264
2265/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2266/// to an zero vector.
2267static bool isZeroShuffle(SDNode *N) {
2268 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2269 return false;
2270
2271 SDOperand V1 = N->getOperand(0);
2272 SDOperand V2 = N->getOperand(1);
2273 SDOperand Mask = N->getOperand(2);
2274 unsigned NumElems = Mask.getNumOperands();
2275 for (unsigned i = 0; i != NumElems; ++i) {
2276 SDOperand Arg = Mask.getOperand(i);
2277 if (Arg.getOpcode() != ISD::UNDEF) {
2278 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2279 if (Idx < NumElems) {
2280 unsigned Opc = V1.Val->getOpcode();
2281 if (Opc == ISD::UNDEF)
2282 continue;
2283 if (Opc != ISD::BUILD_VECTOR ||
2284 !isZeroNode(V1.Val->getOperand(Idx)))
2285 return false;
2286 } else if (Idx >= NumElems) {
2287 unsigned Opc = V2.Val->getOpcode();
2288 if (Opc == ISD::UNDEF)
2289 continue;
2290 if (Opc != ISD::BUILD_VECTOR ||
2291 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2292 return false;
2293 }
2294 }
2295 }
2296 return true;
2297}
2298
2299/// getZeroVector - Returns a vector of specified type with all zero elements.
2300///
2301static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2302 assert(MVT::isVector(VT) && "Expected a vector type");
Dan Gohman237898a2007-05-24 14:33:05 +00002303 unsigned NumElems = MVT::getVectorNumElements(VT);
Dan Gohman51eaa862007-06-14 22:58:02 +00002304 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002305 bool isFP = MVT::isFloatingPoint(EVT);
2306 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2307 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2308 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2309}
2310
Evan Cheng39623da2006-04-20 08:58:49 +00002311/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2312/// that point to V2 points to its first element.
2313static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2314 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2315
2316 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002317 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002318 unsigned NumElems = Mask.getNumOperands();
2319 for (unsigned i = 0; i != NumElems; ++i) {
2320 SDOperand Arg = Mask.getOperand(i);
2321 if (Arg.getOpcode() != ISD::UNDEF) {
2322 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2323 if (Val > NumElems) {
2324 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2325 Changed = true;
2326 }
2327 }
2328 MaskVec.push_back(Arg);
2329 }
2330
2331 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002332 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2333 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002334 return Mask;
2335}
2336
Evan Cheng017dcc62006-04-21 01:05:10 +00002337/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2338/// operation of specified width.
2339static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002340 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002341 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002342
Chris Lattner5a88b832007-02-25 07:10:00 +00002343 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002344 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2345 for (unsigned i = 1; i != NumElems; ++i)
2346 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002347 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002348}
2349
Evan Chengc575ca22006-04-17 20:43:08 +00002350/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2351/// of specified width.
2352static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2353 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002354 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002355 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002356 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2357 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2358 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2359 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002360 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002361}
2362
Evan Cheng39623da2006-04-20 08:58:49 +00002363/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2364/// of specified width.
2365static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2366 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002367 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002368 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002369 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002370 for (unsigned i = 0; i != Half; ++i) {
2371 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2372 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2373 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002374 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002375}
2376
Evan Chengc575ca22006-04-17 20:43:08 +00002377/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2378///
2379static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2380 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002381 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002382 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002383 unsigned NumElems = Mask.getNumOperands();
2384 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002385 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002386 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002387 NumElems >>= 1;
2388 }
2389 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2390
2391 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002392 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002393 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002394 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002395 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2396}
2397
Evan Chengba05f722006-04-21 23:03:30 +00002398/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Evan Cheng213d2cf2007-05-17 18:45:50 +00002399/// vector of zero or undef vector.
Evan Chengba05f722006-04-21 23:03:30 +00002400static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002401 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002402 bool isZero, SelectionDAG &DAG) {
2403 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002404 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002405 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002406 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002407 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Cheng017dcc62006-04-21 01:05:10 +00002408 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002409 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2410 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002411 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002412}
2413
Evan Chengc78d3b42006-04-24 18:01:45 +00002414/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2415///
2416static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2417 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002418 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002419 if (NumNonZero > 8)
2420 return SDOperand();
2421
2422 SDOperand V(0, 0);
2423 bool First = true;
2424 for (unsigned i = 0; i < 16; ++i) {
2425 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2426 if (ThisIsNonZero && First) {
2427 if (NumZero)
2428 V = getZeroVector(MVT::v8i16, DAG);
2429 else
2430 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2431 First = false;
2432 }
2433
2434 if ((i & 1) != 0) {
2435 SDOperand ThisElt(0, 0), LastElt(0, 0);
2436 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2437 if (LastIsNonZero) {
2438 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2439 }
2440 if (ThisIsNonZero) {
2441 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2442 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2443 ThisElt, DAG.getConstant(8, MVT::i8));
2444 if (LastIsNonZero)
2445 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2446 } else
2447 ThisElt = LastElt;
2448
2449 if (ThisElt.Val)
2450 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00002451 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002452 }
2453 }
2454
2455 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2456}
2457
Bill Wendlinga348c562007-03-22 18:42:45 +00002458/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002459///
2460static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2461 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002462 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002463 if (NumNonZero > 4)
2464 return SDOperand();
2465
2466 SDOperand V(0, 0);
2467 bool First = true;
2468 for (unsigned i = 0; i < 8; ++i) {
2469 bool isNonZero = (NonZeros & (1 << i)) != 0;
2470 if (isNonZero) {
2471 if (First) {
2472 if (NumZero)
2473 V = getZeroVector(MVT::v8i16, DAG);
2474 else
2475 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2476 First = false;
2477 }
2478 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00002479 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002480 }
2481 }
2482
2483 return V;
2484}
2485
Evan Cheng0db9fe62006-04-25 20:13:52 +00002486SDOperand
2487X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2488 // All zero's are handled with pxor.
2489 if (ISD::isBuildVectorAllZeros(Op.Val))
2490 return Op;
2491
2492 // All one's are handled with pcmpeqd.
2493 if (ISD::isBuildVectorAllOnes(Op.Val))
2494 return Op;
2495
2496 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002497 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002498 unsigned EVTBits = MVT::getSizeInBits(EVT);
2499
2500 unsigned NumElems = Op.getNumOperands();
2501 unsigned NumZero = 0;
2502 unsigned NumNonZero = 0;
2503 unsigned NonZeros = 0;
Dan Gohmana3941172007-07-24 22:55:08 +00002504 unsigned NumNonZeroImms = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002505 std::set<SDOperand> Values;
2506 for (unsigned i = 0; i < NumElems; ++i) {
2507 SDOperand Elt = Op.getOperand(i);
2508 if (Elt.getOpcode() != ISD::UNDEF) {
2509 Values.insert(Elt);
2510 if (isZeroNode(Elt))
2511 NumZero++;
2512 else {
2513 NonZeros |= (1 << i);
2514 NumNonZero++;
Dan Gohmana3941172007-07-24 22:55:08 +00002515 if (Elt.getOpcode() == ISD::Constant ||
2516 Elt.getOpcode() == ISD::ConstantFP)
2517 NumNonZeroImms++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002518 }
2519 }
2520 }
2521
Dan Gohman7f321562007-06-25 16:23:39 +00002522 if (NumNonZero == 0) {
2523 if (NumZero == 0)
2524 // All undef vector. Return an UNDEF.
2525 return DAG.getNode(ISD::UNDEF, VT);
2526 else
2527 // A mix of zero and undef. Return a zero vector.
2528 return getZeroVector(VT, DAG);
2529 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002530
2531 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2532 if (Values.size() == 1)
2533 return SDOperand();
2534
2535 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002536 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002537 unsigned Idx = CountTrailingZeros_32(NonZeros);
2538 SDOperand Item = Op.getOperand(Idx);
2539 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2540 if (Idx == 0)
2541 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2542 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2543 NumZero > 0, DAG);
2544
2545 if (EVTBits == 32) {
2546 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2547 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2548 DAG);
2549 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002550 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002551 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002552 for (unsigned i = 0; i < NumElems; i++)
2553 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002554 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2555 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002556 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2557 DAG.getNode(ISD::UNDEF, VT), Mask);
2558 }
2559 }
2560
Dan Gohmana3941172007-07-24 22:55:08 +00002561 // A vector full of immediates; various special cases are already
2562 // handled, so this is best done with a single constant-pool load.
2563 if (NumNonZero == NumNonZeroImms)
2564 return SDOperand();
2565
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002566 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002567 if (EVTBits == 64)
2568 return SDOperand();
2569
2570 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00002571 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002572 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2573 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002574 if (V.Val) return V;
2575 }
2576
Bill Wendling826f36f2007-03-28 00:57:11 +00002577 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002578 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2579 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002580 if (V.Val) return V;
2581 }
2582
2583 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00002584 SmallVector<SDOperand, 8> V;
2585 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002586 if (NumElems == 4 && NumZero > 0) {
2587 for (unsigned i = 0; i < 4; ++i) {
2588 bool isZero = !(NonZeros & (1 << i));
2589 if (isZero)
2590 V[i] = getZeroVector(VT, DAG);
2591 else
2592 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2593 }
2594
2595 for (unsigned i = 0; i < 2; ++i) {
2596 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2597 default: break;
2598 case 0:
2599 V[i] = V[i*2]; // Must be a zero vector.
2600 break;
2601 case 1:
2602 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2603 getMOVLMask(NumElems, DAG));
2604 break;
2605 case 2:
2606 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2607 getMOVLMask(NumElems, DAG));
2608 break;
2609 case 3:
2610 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2611 getUnpacklMask(NumElems, DAG));
2612 break;
2613 }
2614 }
2615
Evan Cheng069287d2006-05-16 07:21:53 +00002616 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002617 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002618 // FIXME: we can do the same for v4f32 case when we know both parts of
2619 // the lower half come from scalar_to_vector (loadf32). We should do
2620 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002621 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002622 return V[0];
2623 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002624 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002625 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002626 bool Reverse = (NonZeros & 0x3) == 2;
2627 for (unsigned i = 0; i < 2; ++i)
2628 if (Reverse)
2629 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2630 else
2631 MaskVec.push_back(DAG.getConstant(i, EVT));
2632 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2633 for (unsigned i = 0; i < 2; ++i)
2634 if (Reverse)
2635 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2636 else
2637 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002638 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2639 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002640 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2641 }
2642
2643 if (Values.size() > 2) {
2644 // Expand into a number of unpckl*.
2645 // e.g. for v4f32
2646 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2647 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2648 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2649 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2650 for (unsigned i = 0; i < NumElems; ++i)
2651 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2652 NumElems >>= 1;
2653 while (NumElems != 0) {
2654 for (unsigned i = 0; i < NumElems; ++i)
2655 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2656 UnpckMask);
2657 NumElems >>= 1;
2658 }
2659 return V[0];
2660 }
2661
2662 return SDOperand();
2663}
2664
2665SDOperand
2666X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2667 SDOperand V1 = Op.getOperand(0);
2668 SDOperand V2 = Op.getOperand(1);
2669 SDOperand PermMask = Op.getOperand(2);
2670 MVT::ValueType VT = Op.getValueType();
2671 unsigned NumElems = PermMask.getNumOperands();
2672 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2673 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00002674 bool V1IsSplat = false;
2675 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002676
Evan Cheng8cf723d2006-09-08 01:50:06 +00002677 if (isUndefShuffle(Op.Val))
2678 return DAG.getNode(ISD::UNDEF, VT);
2679
Evan Cheng213d2cf2007-05-17 18:45:50 +00002680 if (isZeroShuffle(Op.Val))
2681 return getZeroVector(VT, DAG);
2682
Evan Cheng49892af2007-06-19 00:02:56 +00002683 if (isIdentityMask(PermMask.Val))
2684 return V1;
2685 else if (isIdentityMask(PermMask.Val, true))
2686 return V2;
2687
Evan Cheng0db9fe62006-04-25 20:13:52 +00002688 if (isSplatMask(PermMask.Val)) {
2689 if (NumElems <= 4) return Op;
2690 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002691 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002692 }
2693
Evan Cheng9bbbb982006-10-25 20:48:19 +00002694 if (X86::isMOVLMask(PermMask.Val))
2695 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002696
Evan Cheng9bbbb982006-10-25 20:48:19 +00002697 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2698 X86::isMOVSLDUPMask(PermMask.Val) ||
2699 X86::isMOVHLPSMask(PermMask.Val) ||
2700 X86::isMOVHPMask(PermMask.Val) ||
2701 X86::isMOVLPMask(PermMask.Val))
2702 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002703
Evan Cheng9bbbb982006-10-25 20:48:19 +00002704 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2705 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00002706 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002707
Evan Cheng9eca5e82006-10-25 21:49:50 +00002708 bool Commuted = false;
Evan Cheng9bbbb982006-10-25 20:48:19 +00002709 V1IsSplat = isSplatVector(V1.Val);
2710 V2IsSplat = isSplatVector(V2.Val);
2711 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00002712 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00002713 std::swap(V1IsSplat, V2IsSplat);
2714 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002715 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00002716 }
2717
2718 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2719 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00002720 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00002721 if (V2IsSplat) {
2722 // V2 is a splat, so the mask may be malformed. That is, it may point
2723 // to any V2 element. The instruction selectior won't like this. Get
2724 // a corrected mask and commute to form a proper MOVS{S|D}.
2725 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2726 if (NewMask.Val != PermMask.Val)
2727 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002728 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00002729 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00002730 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002731
Evan Chengd9b8e402006-10-16 06:36:00 +00002732 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002733 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00002734 X86::isUNPCKLMask(PermMask.Val) ||
2735 X86::isUNPCKHMask(PermMask.Val))
2736 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00002737
Evan Cheng9bbbb982006-10-25 20:48:19 +00002738 if (V2IsSplat) {
2739 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002740 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00002741 // new vector_shuffle with the corrected mask.
2742 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2743 if (NewMask.Val != PermMask.Val) {
2744 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2745 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2746 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2747 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2748 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2749 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002750 }
2751 }
2752 }
2753
2754 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00002755 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2756 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2757
2758 if (Commuted) {
2759 // Commute is back and try unpck* again.
2760 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2761 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002762 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00002763 X86::isUNPCKLMask(PermMask.Val) ||
2764 X86::isUNPCKHMask(PermMask.Val))
2765 return Op;
2766 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002767
2768 // If VT is integer, try PSHUF* first, then SHUFP*.
2769 if (MVT::isInteger(VT)) {
2770 if (X86::isPSHUFDMask(PermMask.Val) ||
2771 X86::isPSHUFHWMask(PermMask.Val) ||
2772 X86::isPSHUFLWMask(PermMask.Val)) {
2773 if (V2.getOpcode() != ISD::UNDEF)
2774 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2775 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2776 return Op;
2777 }
2778
Chris Lattner07c70cd2007-05-17 17:13:13 +00002779 if (X86::isSHUFPMask(PermMask.Val) &&
2780 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002781 return Op;
2782
2783 // Handle v8i16 shuffle high / low shuffle node pair.
2784 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2785 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002786 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002787 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002788 for (unsigned i = 0; i != 4; ++i)
2789 MaskVec.push_back(PermMask.getOperand(i));
2790 for (unsigned i = 4; i != 8; ++i)
2791 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002792 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2793 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002794 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2795 MaskVec.clear();
2796 for (unsigned i = 0; i != 4; ++i)
2797 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2798 for (unsigned i = 4; i != 8; ++i)
2799 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00002800 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002801 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2802 }
2803 } else {
2804 // Floating point cases in the other order.
2805 if (X86::isSHUFPMask(PermMask.Val))
2806 return Op;
2807 if (X86::isPSHUFDMask(PermMask.Val) ||
2808 X86::isPSHUFHWMask(PermMask.Val) ||
2809 X86::isPSHUFLWMask(PermMask.Val)) {
2810 if (V2.getOpcode() != ISD::UNDEF)
2811 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2812 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2813 return Op;
2814 }
2815 }
2816
Chris Lattner07c70cd2007-05-17 17:13:13 +00002817 if (NumElems == 4 &&
2818 // Don't do this for MMX.
2819 MVT::getSizeInBits(VT) != 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002820 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002821 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002822 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00002823 Locs.reserve(NumElems);
Chris Lattner5a88b832007-02-25 07:10:00 +00002824 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2825 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002826 unsigned NumHi = 0;
2827 unsigned NumLo = 0;
2828 // If no more than two elements come from either vector. This can be
2829 // implemented with two shuffles. First shuffle gather the elements.
2830 // The second shuffle, which takes the first shuffle as both of its
2831 // vector operands, put the elements into the right order.
2832 for (unsigned i = 0; i != NumElems; ++i) {
2833 SDOperand Elt = PermMask.getOperand(i);
2834 if (Elt.getOpcode() == ISD::UNDEF) {
2835 Locs[i] = std::make_pair(-1, -1);
2836 } else {
2837 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2838 if (Val < NumElems) {
2839 Locs[i] = std::make_pair(0, NumLo);
2840 Mask1[NumLo] = Elt;
2841 NumLo++;
2842 } else {
2843 Locs[i] = std::make_pair(1, NumHi);
2844 if (2+NumHi < NumElems)
2845 Mask1[2+NumHi] = Elt;
2846 NumHi++;
2847 }
2848 }
2849 }
2850 if (NumLo <= 2 && NumHi <= 2) {
2851 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002852 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2853 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002854 for (unsigned i = 0; i != NumElems; ++i) {
2855 if (Locs[i].first == -1)
2856 continue;
2857 else {
2858 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2859 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2860 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2861 }
2862 }
2863
2864 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00002865 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2866 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002867 }
2868
2869 // Break it into (shuffle shuffle_hi, shuffle_lo).
2870 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00002871 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2872 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2873 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002874 unsigned MaskIdx = 0;
2875 unsigned LoIdx = 0;
2876 unsigned HiIdx = NumElems/2;
2877 for (unsigned i = 0; i != NumElems; ++i) {
2878 if (i == NumElems/2) {
2879 MaskPtr = &HiMask;
2880 MaskIdx = 1;
2881 LoIdx = 0;
2882 HiIdx = NumElems/2;
2883 }
2884 SDOperand Elt = PermMask.getOperand(i);
2885 if (Elt.getOpcode() == ISD::UNDEF) {
2886 Locs[i] = std::make_pair(-1, -1);
2887 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2888 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2889 (*MaskPtr)[LoIdx] = Elt;
2890 LoIdx++;
2891 } else {
2892 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2893 (*MaskPtr)[HiIdx] = Elt;
2894 HiIdx++;
2895 }
2896 }
2897
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002898 SDOperand LoShuffle =
2899 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002900 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2901 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002902 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002903 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002904 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2905 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00002906 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002907 for (unsigned i = 0; i != NumElems; ++i) {
2908 if (Locs[i].first == -1) {
2909 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2910 } else {
2911 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2912 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2913 }
2914 }
2915 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00002916 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2917 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002918 }
2919
2920 return SDOperand();
2921}
2922
2923SDOperand
2924X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2925 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2926 return SDOperand();
2927
2928 MVT::ValueType VT = Op.getValueType();
2929 // TODO: handle v16i8.
2930 if (MVT::getSizeInBits(VT) == 16) {
2931 // Transform it so it match pextrw which produces a 32-bit result.
2932 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2933 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2934 Op.getOperand(0), Op.getOperand(1));
2935 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2936 DAG.getValueType(VT));
2937 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2938 } else if (MVT::getSizeInBits(VT) == 32) {
2939 SDOperand Vec = Op.getOperand(0);
2940 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2941 if (Idx == 0)
2942 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002943 // SHUFPS the element to the lowest double word, then movss.
2944 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00002945 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00002946 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
2947 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2948 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2949 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00002950 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2951 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002952 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002953 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002954 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00002955 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002956 } else if (MVT::getSizeInBits(VT) == 64) {
2957 SDOperand Vec = Op.getOperand(0);
2958 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2959 if (Idx == 0)
2960 return Op;
2961
2962 // UNPCKHPD the element to the lowest double word, then movsd.
2963 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2964 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2965 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00002966 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00002967 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
2968 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00002969 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2970 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002971 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2972 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2973 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00002974 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002975 }
2976
2977 return SDOperand();
2978}
2979
2980SDOperand
2981X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00002982 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00002983 // as its second argument.
2984 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002985 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002986 SDOperand N0 = Op.getOperand(0);
2987 SDOperand N1 = Op.getOperand(1);
2988 SDOperand N2 = Op.getOperand(2);
2989 if (MVT::getSizeInBits(BaseVT) == 16) {
2990 if (N1.getValueType() != MVT::i32)
2991 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2992 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00002993 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002994 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2995 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2996 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2997 if (Idx == 0) {
2998 // Use a movss.
2999 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3000 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman51eaa862007-06-14 22:58:02 +00003001 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003002 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003003 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3004 for (unsigned i = 1; i <= 3; ++i)
3005 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3006 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003007 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3008 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003009 } else {
3010 // Use two pinsrw instructions to insert a 32 bit value.
3011 Idx <<= 1;
3012 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng466685d2006-10-09 20:57:25 +00003013 if (ISD::isNON_EXTLoad(N1.Val)) {
Evan Cheng069287d2006-05-16 07:21:53 +00003014 // Just load directly from f32mem to GR32.
Evan Cheng466685d2006-10-09 20:57:25 +00003015 LoadSDNode *LD = cast<LoadSDNode>(N1);
3016 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3017 LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003018 } else {
3019 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3020 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3021 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003022 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003023 }
3024 }
3025 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3026 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003027 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003028 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3029 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003030 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003031 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3032 }
3033 }
3034
3035 return SDOperand();
3036}
3037
3038SDOperand
3039X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3040 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3041 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3042}
3043
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003044// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003045// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3046// one of the above mentioned nodes. It has to be wrapped because otherwise
3047// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3048// be used to form addressing mode. These wrapped nodes will be selected
3049// into MOV32ri.
3050SDOperand
3051X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3052 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003053 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3054 getPointerTy(),
3055 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003056 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003057 // With PIC, the address is actually $g + Offset.
3058 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3059 !Subtarget->isPICStyleRIPRel()) {
3060 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3061 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3062 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003063 }
3064
3065 return Result;
3066}
3067
3068SDOperand
3069X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3070 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003071 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003072 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003073 // With PIC, the address is actually $g + Offset.
3074 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3075 !Subtarget->isPICStyleRIPRel()) {
3076 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3077 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3078 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003079 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003080
3081 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3082 // load the value at address GV, not the value of GV itself. This means that
3083 // the GlobalAddress must be in the base or index register of the address, not
3084 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003085 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003086 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3087 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003088
3089 return Result;
3090}
3091
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003092// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3093static SDOperand
3094LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3095 const MVT::ValueType PtrVT) {
3096 SDOperand InFlag;
3097 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3098 DAG.getNode(X86ISD::GlobalBaseReg,
3099 PtrVT), InFlag);
3100 InFlag = Chain.getValue(1);
3101
3102 // emit leal symbol@TLSGD(,%ebx,1), %eax
3103 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3104 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3105 GA->getValueType(0),
3106 GA->getOffset());
3107 SDOperand Ops[] = { Chain, TGA, InFlag };
3108 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3109 InFlag = Result.getValue(2);
3110 Chain = Result.getValue(1);
3111
3112 // call ___tls_get_addr. This function receives its argument in
3113 // the register EAX.
3114 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3115 InFlag = Chain.getValue(1);
3116
3117 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3118 SDOperand Ops1[] = { Chain,
3119 DAG.getTargetExternalSymbol("___tls_get_addr",
3120 PtrVT),
3121 DAG.getRegister(X86::EAX, PtrVT),
3122 DAG.getRegister(X86::EBX, PtrVT),
3123 InFlag };
3124 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3125 InFlag = Chain.getValue(1);
3126
3127 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3128}
3129
3130// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3131// "local exec" model.
3132static SDOperand
3133LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3134 const MVT::ValueType PtrVT) {
3135 // Get the Thread Pointer
3136 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3137 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3138 // exec)
3139 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3140 GA->getValueType(0),
3141 GA->getOffset());
3142 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003143
3144 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3145 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3146
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003147 // The address of the thread local variable is the add of the thread
3148 // pointer with the offset of the variable.
3149 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3150}
3151
3152SDOperand
3153X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3154 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003155 // TODO: implement the "initial exec"model for pic executables
3156 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3157 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003158 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3159 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3160 // otherwise use the "Local Exec"TLS Model
3161 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3162 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3163 else
3164 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3165}
3166
Evan Cheng0db9fe62006-04-25 20:13:52 +00003167SDOperand
3168X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3169 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003170 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003171 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003172 // With PIC, the address is actually $g + Offset.
3173 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3174 !Subtarget->isPICStyleRIPRel()) {
3175 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3176 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3177 Result);
3178 }
3179
3180 return Result;
3181}
3182
3183SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3184 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3185 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3186 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3187 // With PIC, the address is actually $g + Offset.
3188 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3189 !Subtarget->isPICStyleRIPRel()) {
3190 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3191 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3192 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003193 }
3194
3195 return Result;
3196}
3197
3198SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00003199 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3200 "Not an i64 shift!");
3201 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3202 SDOperand ShOpLo = Op.getOperand(0);
3203 SDOperand ShOpHi = Op.getOperand(1);
3204 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng734503b2006-09-11 02:19:56 +00003205 SDOperand Tmp1 = isSRA ?
3206 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3207 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003208
3209 SDOperand Tmp2, Tmp3;
3210 if (Op.getOpcode() == ISD::SHL_PARTS) {
3211 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3212 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3213 } else {
3214 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00003215 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00003216 }
3217
Evan Cheng734503b2006-09-11 02:19:56 +00003218 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3219 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3220 DAG.getConstant(32, MVT::i8));
3221 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3222 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Chenge3413162006-01-09 18:33:28 +00003223
3224 SDOperand Hi, Lo;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003225 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00003226
Evan Cheng734503b2006-09-11 02:19:56 +00003227 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3228 SmallVector<SDOperand, 4> Ops;
Evan Chenge3413162006-01-09 18:33:28 +00003229 if (Op.getOpcode() == ISD::SHL_PARTS) {
3230 Ops.push_back(Tmp2);
3231 Ops.push_back(Tmp3);
3232 Ops.push_back(CC);
3233 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003234 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003235 InFlag = Hi.getValue(1);
3236
3237 Ops.clear();
3238 Ops.push_back(Tmp3);
3239 Ops.push_back(Tmp1);
3240 Ops.push_back(CC);
3241 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003242 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003243 } else {
3244 Ops.push_back(Tmp2);
3245 Ops.push_back(Tmp3);
3246 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00003247 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003248 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003249 InFlag = Lo.getValue(1);
3250
3251 Ops.clear();
3252 Ops.push_back(Tmp3);
3253 Ops.push_back(Tmp1);
3254 Ops.push_back(CC);
3255 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003256 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003257 }
3258
Evan Cheng734503b2006-09-11 02:19:56 +00003259 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003260 Ops.clear();
3261 Ops.push_back(Lo);
3262 Ops.push_back(Hi);
Evan Cheng734503b2006-09-11 02:19:56 +00003263 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003264}
Evan Chenga3195e82006-01-12 22:54:21 +00003265
Evan Cheng0db9fe62006-04-25 20:13:52 +00003266SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3267 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3268 Op.getOperand(0).getValueType() >= MVT::i16 &&
3269 "Unknown SINT_TO_FP to lower!");
3270
3271 SDOperand Result;
3272 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3273 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3274 MachineFunction &MF = DAG.getMachineFunction();
3275 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3276 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003277 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003278 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003279
3280 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003281 SDVTList Tys;
3282 if (X86ScalarSSE)
3283 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3284 else
Dale Johannesen849f2142007-07-03 00:53:03 +00003285 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003286 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003287 Ops.push_back(Chain);
3288 Ops.push_back(StackSlot);
3289 Ops.push_back(DAG.getValueType(SrcVT));
3290 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003291 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003292
3293 if (X86ScalarSSE) {
3294 Chain = Result.getValue(1);
3295 SDOperand InFlag = Result.getValue(2);
3296
3297 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3298 // shouldn't be necessary except that RFP cannot be live across
3299 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003300 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003301 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003302 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00003303 Tys = DAG.getVTList(MVT::Other);
3304 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003305 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003306 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003307 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003308 Ops.push_back(DAG.getValueType(Op.getValueType()));
3309 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003310 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003311 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003312 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003313
Evan Cheng0db9fe62006-04-25 20:13:52 +00003314 return Result;
3315}
3316
3317SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3318 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3319 "Unknown FP_TO_SINT to lower!");
3320 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3321 // stack slot.
3322 MachineFunction &MF = DAG.getMachineFunction();
3323 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3324 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3325 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3326
3327 unsigned Opc;
3328 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003329 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3330 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3331 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3332 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003333 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003334
Evan Cheng0db9fe62006-04-25 20:13:52 +00003335 SDOperand Chain = DAG.getEntryNode();
3336 SDOperand Value = Op.getOperand(0);
3337 if (X86ScalarSSE) {
3338 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003339 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00003340 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003341 SDOperand Ops[] = {
3342 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3343 };
3344 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003345 Chain = Value.getValue(1);
3346 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3347 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3348 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003349
Evan Cheng0db9fe62006-04-25 20:13:52 +00003350 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00003351 SDOperand Ops[] = { Chain, Value, StackSlot };
3352 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00003353
Evan Cheng0db9fe62006-04-25 20:13:52 +00003354 // Load the result.
Evan Cheng466685d2006-10-09 20:57:25 +00003355 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003356}
3357
3358SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3359 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003360 MVT::ValueType EltVT = VT;
3361 if (MVT::isVector(VT))
3362 EltVT = MVT::getVectorElementType(VT);
3363 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003364 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00003365 if (EltVT == MVT::f64) {
3366 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63)));
3367 CV.push_back(C);
3368 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003369 } else {
Dan Gohman20382522007-07-10 00:05:58 +00003370 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31)));
3371 CV.push_back(C);
3372 CV.push_back(C);
3373 CV.push_back(C);
3374 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003375 }
Dan Gohmand3006222007-07-27 17:16:43 +00003376 Constant *C = ConstantVector::get(CV);
3377 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3378 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3379 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003380 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3381}
3382
3383SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3384 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003385 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00003386 unsigned EltNum = 1;
3387 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00003388 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00003389 EltNum = MVT::getVectorNumElements(VT);
3390 }
Dan Gohman20382522007-07-10 00:05:58 +00003391 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003392 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00003393 if (EltVT == MVT::f64) {
3394 Constant *C = ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63));
3395 CV.push_back(C);
3396 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003397 } else {
Dan Gohman20382522007-07-10 00:05:58 +00003398 Constant *C = ConstantFP::get(OpNTy, BitsToFloat(1U << 31));
3399 CV.push_back(C);
3400 CV.push_back(C);
3401 CV.push_back(C);
3402 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003403 }
Dan Gohmand3006222007-07-27 17:16:43 +00003404 Constant *C = ConstantVector::get(CV);
3405 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3406 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3407 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00003408 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00003409 return DAG.getNode(ISD::BIT_CONVERT, VT,
3410 DAG.getNode(ISD::XOR, MVT::v2i64,
3411 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3412 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3413 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00003414 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3415 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003416}
3417
Evan Cheng68c47cb2007-01-05 07:55:56 +00003418SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00003419 SDOperand Op0 = Op.getOperand(0);
3420 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003421 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00003422 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00003423 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003424
3425 // If second operand is smaller, extend it first.
3426 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3427 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3428 SrcVT = VT;
3429 }
3430
Evan Cheng68c47cb2007-01-05 07:55:56 +00003431 // First get the sign bit of second operand.
3432 std::vector<Constant*> CV;
3433 if (SrcVT == MVT::f64) {
3434 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3435 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3436 } else {
3437 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3438 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3439 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3440 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3441 }
Dan Gohmand3006222007-07-27 17:16:43 +00003442 Constant *C = ConstantVector::get(CV);
3443 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3444 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3445 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003446 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003447
3448 // Shift sign bit right or left if the two operands have different types.
3449 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3450 // Op0 is MVT::f32, Op1 is MVT::f64.
3451 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3452 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3453 DAG.getConstant(32, MVT::i32));
3454 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3455 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3456 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00003457 }
3458
Evan Cheng73d6cf12007-01-05 21:37:56 +00003459 // Clear first operand sign bit.
3460 CV.clear();
3461 if (VT == MVT::f64) {
3462 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3463 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3464 } else {
3465 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3466 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3467 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3468 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3469 }
Dan Gohmand3006222007-07-27 17:16:43 +00003470 C = ConstantVector::get(CV);
3471 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3472 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3473 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003474 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3475
3476 // Or the value with the sign bit.
3477 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003478}
3479
Evan Cheng734503b2006-09-11 02:19:56 +00003480SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3481 SDOperand Chain) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003482 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3483 SDOperand Cond;
Evan Cheng734503b2006-09-11 02:19:56 +00003484 SDOperand Op0 = Op.getOperand(0);
3485 SDOperand Op1 = Op.getOperand(1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003486 SDOperand CC = Op.getOperand(2);
3487 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Chengcf12ec42006-10-12 19:12:56 +00003488 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3489 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003490 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003491 unsigned X86CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003492
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003493 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattnerf9570512006-09-13 03:22:10 +00003494 Op0, Op1, DAG)) {
Evan Cheng734503b2006-09-11 02:19:56 +00003495 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003496 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003497 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003498 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003499 }
3500
3501 assert(isFP && "Illegal integer SetCC!");
3502
3503 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003504 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003505
3506 switch (SetCCOpcode) {
3507 default: assert(false && "Illegal floating point SetCC!");
3508 case ISD::SETOEQ: { // !PF & ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003509 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003510 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003511 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003512 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003513 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003514 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3515 }
3516 case ISD::SETUNE: { // PF | !ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003517 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003518 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003519 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003520 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003521 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003522 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3523 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003524 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003525}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003526
Evan Cheng0db9fe62006-04-25 20:13:52 +00003527SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003528 bool addTest = true;
3529 SDOperand Chain = DAG.getEntryNode();
3530 SDOperand Cond = Op.getOperand(0);
3531 SDOperand CC;
3532 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng9bba8942006-01-26 02:13:10 +00003533
Evan Cheng734503b2006-09-11 02:19:56 +00003534 if (Cond.getOpcode() == ISD::SETCC)
3535 Cond = LowerSETCC(Cond, DAG, Chain);
3536
3537 if (Cond.getOpcode() == X86ISD::SETCC) {
3538 CC = Cond.getOperand(0);
3539
Evan Cheng0db9fe62006-04-25 20:13:52 +00003540 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng734503b2006-09-11 02:19:56 +00003541 // (since flag operand cannot be shared). Use it as the condition setting
3542 // operand in place of the X86ISD::SETCC.
3543 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Cheng0db9fe62006-04-25 20:13:52 +00003544 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng734503b2006-09-11 02:19:56 +00003545 // pressure reason)?
3546 SDOperand Cmp = Cond.getOperand(1);
3547 unsigned Opc = Cmp.getOpcode();
3548 bool IllegalFPCMov = !X86ScalarSSE &&
3549 MVT::isFloatingPoint(Op.getValueType()) &&
3550 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3551 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3552 !IllegalFPCMov) {
3553 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3554 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3555 addTest = false;
3556 }
3557 }
Evan Chengaaca22c2006-01-10 20:26:56 +00003558
Evan Cheng0db9fe62006-04-25 20:13:52 +00003559 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003560 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003561 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3562 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng7df96d62005-12-17 01:21:05 +00003563 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003564
Evan Cheng734503b2006-09-11 02:19:56 +00003565 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3566 SmallVector<SDOperand, 4> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003567 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3568 // condition is true.
3569 Ops.push_back(Op.getOperand(2));
3570 Ops.push_back(Op.getOperand(1));
3571 Ops.push_back(CC);
Evan Cheng734503b2006-09-11 02:19:56 +00003572 Ops.push_back(Cond.getValue(1));
3573 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003574}
Evan Cheng9bba8942006-01-26 02:13:10 +00003575
Evan Cheng0db9fe62006-04-25 20:13:52 +00003576SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003577 bool addTest = true;
3578 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003579 SDOperand Cond = Op.getOperand(1);
3580 SDOperand Dest = Op.getOperand(2);
3581 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00003582 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3583
Evan Cheng0db9fe62006-04-25 20:13:52 +00003584 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng734503b2006-09-11 02:19:56 +00003585 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003586
3587 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00003588 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003589
Evan Cheng734503b2006-09-11 02:19:56 +00003590 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3591 // (since flag operand cannot be shared). Use it as the condition setting
3592 // operand in place of the X86ISD::SETCC.
3593 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3594 // to use a test instead of duplicating the X86ISD::CMP (for register
3595 // pressure reason)?
3596 SDOperand Cmp = Cond.getOperand(1);
3597 unsigned Opc = Cmp.getOpcode();
3598 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3599 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3600 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3601 addTest = false;
3602 }
3603 }
Evan Cheng1bcee362006-01-13 01:03:02 +00003604
Evan Cheng0db9fe62006-04-25 20:13:52 +00003605 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003606 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003607 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3608 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng898101c2005-12-19 23:12:38 +00003609 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003610 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng734503b2006-09-11 02:19:56 +00003611 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003612}
Evan Cheng67f92a72006-01-11 22:15:48 +00003613
Evan Cheng32fe1032006-05-25 00:59:30 +00003614SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3615 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003616
Evan Cheng25ab6902006-09-08 06:48:29 +00003617 if (Subtarget->is64Bit())
Chris Lattner09c75a42007-02-25 09:06:15 +00003618 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00003619 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003620 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003621 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003622 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00003623 case CallingConv::Fast:
Chris Lattner2db39b82007-02-28 06:05:16 +00003624 // TODO: Implement fastcc
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003625 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003626 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003627 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00003628 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003629 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00003630 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003631 }
Evan Cheng32fe1032006-05-25 00:59:30 +00003632}
3633
Anton Korobeynikove060b532007-04-17 19:34:00 +00003634
3635// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3636// Calls to _alloca is needed to probe the stack when allocating more than 4k
3637// bytes in one go. Touching the stack at 4K increments is necessary to ensure
3638// that the guard pages used by the OS virtual memory manager are allocated in
3639// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003640SDOperand
3641X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3642 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00003643 assert(Subtarget->isTargetCygMing() &&
3644 "This should be used only on Cygwin/Mingw targets");
3645
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003646 // Get the inputs.
3647 SDOperand Chain = Op.getOperand(0);
3648 SDOperand Size = Op.getOperand(1);
3649 // FIXME: Ensure alignment here
3650
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003651 SDOperand Flag;
3652
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003653 MVT::ValueType IntPtr = getPointerTy();
3654 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003655
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003656 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3657 Flag = Chain.getValue(1);
3658
3659 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3660 SDOperand Ops[] = { Chain,
3661 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3662 DAG.getRegister(X86::EAX, IntPtr),
3663 Flag };
3664 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3665 Flag = Chain.getValue(1);
3666
3667 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003668
3669 std::vector<MVT::ValueType> Tys;
3670 Tys.push_back(SPTy);
3671 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003672 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3673 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003674}
3675
Evan Cheng1bc78042006-04-26 01:20:17 +00003676SDOperand
3677X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00003678 MachineFunction &MF = DAG.getMachineFunction();
3679 const Function* Fn = MF.getFunction();
3680 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00003681 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00003682 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00003683 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00003684
Evan Cheng25caf632006-05-23 21:06:34 +00003685 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00003686 if (Subtarget->is64Bit())
3687 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00003688 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003689 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003690 default:
3691 assert(0 && "Unsupported calling convention");
3692 case CallingConv::Fast:
Chris Lattner2db39b82007-02-28 06:05:16 +00003693 // TODO: implement fastcc.
3694
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003695 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003696 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003697 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003698 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00003699 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003700 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003701 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00003702 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00003703 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003704 }
Evan Cheng1bc78042006-04-26 01:20:17 +00003705}
3706
Evan Cheng0db9fe62006-04-25 20:13:52 +00003707SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3708 SDOperand InFlag(0, 0);
3709 SDOperand Chain = Op.getOperand(0);
3710 unsigned Align =
3711 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3712 if (Align == 0) Align = 1;
3713
3714 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3715 // If not DWORD aligned, call memset if size is less than the threshold.
3716 // It knows how to align to the right boundary first.
3717 if ((Align & 3) != 0 ||
3718 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3719 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003720 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00003721 TargetLowering::ArgListTy Args;
3722 TargetLowering::ArgListEntry Entry;
3723 Entry.Node = Op.getOperand(1);
3724 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00003725 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00003726 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00003727 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3728 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00003729 Args.push_back(Entry);
3730 Entry.Node = Op.getOperand(3);
3731 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003732 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00003733 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00003734 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3735 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003736 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003737
Evan Cheng0db9fe62006-04-25 20:13:52 +00003738 MVT::ValueType AVT;
3739 SDOperand Count;
3740 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3741 unsigned BytesLeft = 0;
3742 bool TwoRepStos = false;
3743 if (ValC) {
3744 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00003745 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003746
Evan Cheng0db9fe62006-04-25 20:13:52 +00003747 // If the value is a constant, then we can potentially use larger sets.
3748 switch (Align & 3) {
3749 case 2: // WORD aligned
3750 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003751 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00003752 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003753 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00003754 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00003755 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00003756 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003757 Val = (Val << 8) | Val;
3758 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00003759 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3760 AVT = MVT::i64;
3761 ValReg = X86::RAX;
3762 Val = (Val << 32) | Val;
3763 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003764 break;
3765 default: // Byte aligned
3766 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00003768 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003769 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003770 }
3771
Evan Cheng25ab6902006-09-08 06:48:29 +00003772 if (AVT > MVT::i8) {
3773 if (I) {
3774 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3775 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3776 BytesLeft = I->getValue() % UBytes;
3777 } else {
3778 assert(AVT >= MVT::i32 &&
3779 "Do not use rep;stos if not at least DWORD aligned");
3780 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3781 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3782 TwoRepStos = true;
3783 }
3784 }
3785
Evan Cheng0db9fe62006-04-25 20:13:52 +00003786 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3787 InFlag);
3788 InFlag = Chain.getValue(1);
3789 } else {
3790 AVT = MVT::i8;
3791 Count = Op.getOperand(3);
3792 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3793 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00003794 }
Evan Chengc78d3b42006-04-24 18:01:45 +00003795
Evan Cheng25ab6902006-09-08 06:48:29 +00003796 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3797 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003798 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00003799 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3800 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003801 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00003802
Chris Lattnerd96d0722007-02-25 06:40:16 +00003803 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00003804 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003805 Ops.push_back(Chain);
3806 Ops.push_back(DAG.getValueType(AVT));
3807 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003808 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00003809
Evan Cheng0db9fe62006-04-25 20:13:52 +00003810 if (TwoRepStos) {
3811 InFlag = Chain.getValue(1);
3812 Count = Op.getOperand(3);
3813 MVT::ValueType CVT = Count.getValueType();
3814 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00003815 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3816 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3817 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003818 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00003819 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003820 Ops.clear();
3821 Ops.push_back(Chain);
3822 Ops.push_back(DAG.getValueType(MVT::i8));
3823 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003824 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003825 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003826 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003827 SDOperand Value;
3828 unsigned Val = ValC->getValue() & 255;
3829 unsigned Offset = I->getValue() - BytesLeft;
3830 SDOperand DstAddr = Op.getOperand(1);
3831 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00003832 if (BytesLeft >= 4) {
3833 Val = (Val << 8) | Val;
3834 Val = (Val << 16) | Val;
3835 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00003836 Chain = DAG.getStore(Chain, Value,
3837 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3838 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003839 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003840 BytesLeft -= 4;
3841 Offset += 4;
3842 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003843 if (BytesLeft >= 2) {
3844 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00003845 Chain = DAG.getStore(Chain, Value,
3846 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3847 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003848 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849 BytesLeft -= 2;
3850 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00003851 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003852 if (BytesLeft == 1) {
3853 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00003854 Chain = DAG.getStore(Chain, Value,
3855 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3856 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003857 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00003858 }
Evan Cheng386031a2006-03-24 07:29:27 +00003859 }
Evan Cheng11e15b32006-04-03 20:53:28 +00003860
Evan Cheng0db9fe62006-04-25 20:13:52 +00003861 return Chain;
3862}
Evan Cheng11e15b32006-04-03 20:53:28 +00003863
Evan Cheng0db9fe62006-04-25 20:13:52 +00003864SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3865 SDOperand Chain = Op.getOperand(0);
3866 unsigned Align =
3867 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3868 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00003869
Evan Cheng0db9fe62006-04-25 20:13:52 +00003870 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3871 // If not DWORD aligned, call memcpy if size is less than the threshold.
3872 // It knows how to align to the right boundary first.
3873 if ((Align & 3) != 0 ||
3874 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3875 MVT::ValueType IntPtr = getPointerTy();
Reid Spencer47857812006-12-31 05:55:36 +00003876 TargetLowering::ArgListTy Args;
3877 TargetLowering::ArgListEntry Entry;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003878 Entry.Ty = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00003879 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3880 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3881 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003882 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00003883 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00003884 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3885 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00003886 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003887
3888 MVT::ValueType AVT;
3889 SDOperand Count;
3890 unsigned BytesLeft = 0;
3891 bool TwoRepMovs = false;
3892 switch (Align & 3) {
3893 case 2: // WORD aligned
3894 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003895 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00003896 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00003897 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00003898 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3899 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003900 break;
3901 default: // Byte aligned
3902 AVT = MVT::i8;
3903 Count = Op.getOperand(3);
3904 break;
3905 }
3906
Evan Cheng25ab6902006-09-08 06:48:29 +00003907 if (AVT > MVT::i8) {
3908 if (I) {
3909 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3910 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3911 BytesLeft = I->getValue() % UBytes;
3912 } else {
3913 assert(AVT >= MVT::i32 &&
3914 "Do not use rep;movs if not at least DWORD aligned");
3915 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3916 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3917 TwoRepMovs = true;
3918 }
3919 }
3920
Evan Cheng0db9fe62006-04-25 20:13:52 +00003921 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003922 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3923 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003924 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00003925 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3926 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003927 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00003928 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3929 Op.getOperand(2), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003930 InFlag = Chain.getValue(1);
3931
Chris Lattnerd96d0722007-02-25 06:40:16 +00003932 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00003933 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 Ops.push_back(Chain);
3935 Ops.push_back(DAG.getValueType(AVT));
3936 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003937 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003938
3939 if (TwoRepMovs) {
3940 InFlag = Chain.getValue(1);
3941 Count = Op.getOperand(3);
3942 MVT::ValueType CVT = Count.getValueType();
3943 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00003944 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3945 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3946 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00003948 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 Ops.clear();
3950 Ops.push_back(Chain);
3951 Ops.push_back(DAG.getValueType(MVT::i8));
3952 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003953 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003955 // Issue loads and stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003956 unsigned Offset = I->getValue() - BytesLeft;
3957 SDOperand DstAddr = Op.getOperand(1);
3958 MVT::ValueType DstVT = DstAddr.getValueType();
3959 SDOperand SrcAddr = Op.getOperand(2);
3960 MVT::ValueType SrcVT = SrcAddr.getValueType();
3961 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00003962 if (BytesLeft >= 4) {
3963 Value = DAG.getLoad(MVT::i32, Chain,
3964 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3965 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00003966 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003967 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00003968 Chain = DAG.getStore(Chain, Value,
3969 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3970 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003971 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003972 BytesLeft -= 4;
3973 Offset += 4;
3974 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 if (BytesLeft >= 2) {
3976 Value = DAG.getLoad(MVT::i16, Chain,
3977 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3978 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00003979 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003980 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00003981 Chain = DAG.getStore(Chain, Value,
3982 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3983 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003984 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003985 BytesLeft -= 2;
3986 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00003987 }
3988
Evan Cheng0db9fe62006-04-25 20:13:52 +00003989 if (BytesLeft == 1) {
3990 Value = DAG.getLoad(MVT::i8, Chain,
3991 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3992 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00003993 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003994 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00003995 Chain = DAG.getStore(Chain, Value,
3996 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3997 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003998 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003999 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004000 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004001
4002 return Chain;
4003}
4004
4005SDOperand
4006X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerd96d0722007-02-25 06:40:16 +00004007 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004008 SDOperand TheOp = Op.getOperand(0);
4009 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004010 if (Subtarget->is64Bit()) {
4011 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4012 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4013 MVT::i64, Copy1.getValue(2));
4014 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4015 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004016 SDOperand Ops[] = {
4017 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4018 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004019
4020 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004021 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004022 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004023
4024 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4025 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4026 MVT::i32, Copy1.getValue(2));
4027 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4028 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4029 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004030}
4031
4032SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004033 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4034
Evan Cheng25ab6902006-09-08 06:48:29 +00004035 if (!Subtarget->is64Bit()) {
4036 // vastart just stores the address of the VarArgsFrameIndex slot into the
4037 // memory location argument.
4038 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004039 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4040 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004041 }
4042
4043 // __va_list_tag:
4044 // gp_offset (0 - 6 * 8)
4045 // fp_offset (48 - 48 + 8 * 16)
4046 // overflow_arg_area (point to parameters coming in memory).
4047 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004048 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004049 SDOperand FIN = Op.getOperand(1);
4050 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004051 SDOperand Store = DAG.getStore(Op.getOperand(0),
4052 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004053 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004054 MemOps.push_back(Store);
4055
4056 // Store fp_offset
4057 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4058 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00004059 Store = DAG.getStore(Op.getOperand(0),
4060 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004061 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004062 MemOps.push_back(Store);
4063
4064 // Store ptr to overflow_arg_area
4065 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4066 DAG.getConstant(4, getPointerTy()));
4067 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004068 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4069 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004070 MemOps.push_back(Store);
4071
4072 // Store ptr to reg_save_area.
4073 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4074 DAG.getConstant(8, getPointerTy()));
4075 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004076 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4077 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004078 MemOps.push_back(Store);
4079 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004080}
4081
Evan Chengae642192007-03-02 23:16:35 +00004082SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4083 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4084 SDOperand Chain = Op.getOperand(0);
4085 SDOperand DstPtr = Op.getOperand(1);
4086 SDOperand SrcPtr = Op.getOperand(2);
4087 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4088 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4089
4090 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4091 SrcSV->getValue(), SrcSV->getOffset());
4092 Chain = SrcPtr.getValue(1);
4093 for (unsigned i = 0; i < 3; ++i) {
4094 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4095 SrcSV->getValue(), SrcSV->getOffset());
4096 Chain = Val.getValue(1);
4097 Chain = DAG.getStore(Chain, Val, DstPtr,
4098 DstSV->getValue(), DstSV->getOffset());
4099 if (i == 2)
4100 break;
4101 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4102 DAG.getConstant(8, getPointerTy()));
4103 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4104 DAG.getConstant(8, getPointerTy()));
4105 }
4106 return Chain;
4107}
4108
Evan Cheng0db9fe62006-04-25 20:13:52 +00004109SDOperand
4110X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4111 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4112 switch (IntNo) {
4113 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004114 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004115 case Intrinsic::x86_sse_comieq_ss:
4116 case Intrinsic::x86_sse_comilt_ss:
4117 case Intrinsic::x86_sse_comile_ss:
4118 case Intrinsic::x86_sse_comigt_ss:
4119 case Intrinsic::x86_sse_comige_ss:
4120 case Intrinsic::x86_sse_comineq_ss:
4121 case Intrinsic::x86_sse_ucomieq_ss:
4122 case Intrinsic::x86_sse_ucomilt_ss:
4123 case Intrinsic::x86_sse_ucomile_ss:
4124 case Intrinsic::x86_sse_ucomigt_ss:
4125 case Intrinsic::x86_sse_ucomige_ss:
4126 case Intrinsic::x86_sse_ucomineq_ss:
4127 case Intrinsic::x86_sse2_comieq_sd:
4128 case Intrinsic::x86_sse2_comilt_sd:
4129 case Intrinsic::x86_sse2_comile_sd:
4130 case Intrinsic::x86_sse2_comigt_sd:
4131 case Intrinsic::x86_sse2_comige_sd:
4132 case Intrinsic::x86_sse2_comineq_sd:
4133 case Intrinsic::x86_sse2_ucomieq_sd:
4134 case Intrinsic::x86_sse2_ucomilt_sd:
4135 case Intrinsic::x86_sse2_ucomile_sd:
4136 case Intrinsic::x86_sse2_ucomigt_sd:
4137 case Intrinsic::x86_sse2_ucomige_sd:
4138 case Intrinsic::x86_sse2_ucomineq_sd: {
4139 unsigned Opc = 0;
4140 ISD::CondCode CC = ISD::SETCC_INVALID;
4141 switch (IntNo) {
4142 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004143 case Intrinsic::x86_sse_comieq_ss:
4144 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004145 Opc = X86ISD::COMI;
4146 CC = ISD::SETEQ;
4147 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004148 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004149 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004150 Opc = X86ISD::COMI;
4151 CC = ISD::SETLT;
4152 break;
4153 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004154 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155 Opc = X86ISD::COMI;
4156 CC = ISD::SETLE;
4157 break;
4158 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004159 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004160 Opc = X86ISD::COMI;
4161 CC = ISD::SETGT;
4162 break;
4163 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004164 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004165 Opc = X86ISD::COMI;
4166 CC = ISD::SETGE;
4167 break;
4168 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004169 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170 Opc = X86ISD::COMI;
4171 CC = ISD::SETNE;
4172 break;
4173 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004174 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004175 Opc = X86ISD::UCOMI;
4176 CC = ISD::SETEQ;
4177 break;
4178 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004179 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004180 Opc = X86ISD::UCOMI;
4181 CC = ISD::SETLT;
4182 break;
4183 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004184 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004185 Opc = X86ISD::UCOMI;
4186 CC = ISD::SETLE;
4187 break;
4188 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004189 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004190 Opc = X86ISD::UCOMI;
4191 CC = ISD::SETGT;
4192 break;
4193 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004194 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004195 Opc = X86ISD::UCOMI;
4196 CC = ISD::SETGE;
4197 break;
4198 case Intrinsic::x86_sse_ucomineq_ss:
4199 case Intrinsic::x86_sse2_ucomineq_sd:
4200 Opc = X86ISD::UCOMI;
4201 CC = ISD::SETNE;
4202 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004203 }
Evan Cheng734503b2006-09-11 02:19:56 +00004204
Evan Cheng0db9fe62006-04-25 20:13:52 +00004205 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004206 SDOperand LHS = Op.getOperand(1);
4207 SDOperand RHS = Op.getOperand(2);
4208 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004209
4210 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattnerf9570512006-09-13 03:22:10 +00004211 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng734503b2006-09-11 02:19:56 +00004212 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4213 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4214 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4215 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004216 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004217 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004218 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004219}
Evan Cheng72261582005-12-20 06:22:03 +00004220
Nate Begemanbcc5f362007-01-29 22:58:52 +00004221SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4222 // Depths > 0 not supported yet!
4223 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4224 return SDOperand();
4225
4226 // Just load the return address
4227 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4228 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4229}
4230
4231SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4232 // Depths > 0 not supported yet!
4233 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4234 return SDOperand();
4235
4236 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4237 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4238 DAG.getConstant(4, getPointerTy()));
4239}
4240
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004241SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4242 SelectionDAG &DAG) {
4243 // Is not yet supported on x86-64
4244 if (Subtarget->is64Bit())
4245 return SDOperand();
4246
4247 return DAG.getConstant(8, getPointerTy());
4248}
4249
4250SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4251{
4252 assert(!Subtarget->is64Bit() &&
4253 "Lowering of eh_return builtin is not supported yet on x86-64");
4254
4255 MachineFunction &MF = DAG.getMachineFunction();
4256 SDOperand Chain = Op.getOperand(0);
4257 SDOperand Offset = Op.getOperand(1);
4258 SDOperand Handler = Op.getOperand(2);
4259
4260 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4261 getPointerTy());
4262
4263 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4264 DAG.getConstant(-4UL, getPointerTy()));
4265 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4266 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4267 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4268 MF.addLiveOut(X86::ECX);
4269
4270 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4271 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4272}
4273
Duncan Sandsb116fac2007-07-27 20:02:49 +00004274SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4275 SelectionDAG &DAG) {
4276 SDOperand Root = Op.getOperand(0);
4277 SDOperand Trmp = Op.getOperand(1); // trampoline
4278 SDOperand FPtr = Op.getOperand(2); // nested function
4279 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4280
4281 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4282
4283 if (Subtarget->is64Bit()) {
4284 return SDOperand(); // not yet supported
4285 } else {
4286 Function *Func = (Function *)
4287 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4288 unsigned CC = Func->getCallingConv();
4289 unsigned char NestReg;
4290
4291 switch (CC) {
4292 default:
4293 assert(0 && "Unsupported calling convention");
4294 case CallingConv::C:
4295 case CallingConv::Fast:
4296 case CallingConv::X86_StdCall: {
4297 // Pass 'nest' parameter in ECX.
4298 // Must be kept in sync with X86CallingConv.td
4299 NestReg = N86::ECX;
4300
4301 // Check that ECX wasn't needed by an 'inreg' parameter.
4302 const FunctionType *FTy = Func->getFunctionType();
4303 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4304
4305 if (Attrs && !Func->isVarArg()) {
4306 unsigned InRegCount = 0;
4307 unsigned Idx = 1;
4308
4309 for (FunctionType::param_iterator I = FTy->param_begin(),
4310 E = FTy->param_end(); I != E; ++I, ++Idx)
4311 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4312 // FIXME: should only count parameters that are lowered to integers.
4313 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4314
4315 if (InRegCount > 2) {
4316 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4317 abort();
4318 }
4319 }
4320 break;
4321 }
4322 case CallingConv::X86_FastCall:
4323 // Pass 'nest' parameter in EAX.
4324 // Must be kept in sync with X86CallingConv.td
4325 NestReg = N86::EAX;
4326 break;
4327 }
4328
4329 SDOperand OutChains[4];
4330 SDOperand Addr, Disp;
4331
4332 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4333 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4334
4335 const unsigned char MOV32ri = 0xB8;
4336 const unsigned char JMP = 0xE9;
4337
4338 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|NestReg, MVT::i8),
4339 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4340
4341 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4342 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4343 TrmpSV->getOffset() + 1, false, 1);
4344
4345 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4346 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4347 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4348
4349 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4350 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4351 TrmpSV->getOffset() + 6, false, 1);
4352
4353 return DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4);
4354 }
4355}
4356
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357/// LowerOperation - Provide custom lowering hooks for some operations.
4358///
4359SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4360 switch (Op.getOpcode()) {
4361 default: assert(0 && "Should not custom lower this!");
4362 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4363 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4364 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4365 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4366 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4367 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4368 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004369 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4371 case ISD::SHL_PARTS:
4372 case ISD::SRA_PARTS:
4373 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4374 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4375 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4376 case ISD::FABS: return LowerFABS(Op, DAG);
4377 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004378 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004379 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004380 case ISD::SELECT: return LowerSELECT(Op, DAG);
4381 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4382 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00004383 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00004385 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004386 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4387 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4388 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4389 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00004390 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004391 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00004392 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4393 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004394 case ISD::FRAME_TO_ARGS_OFFSET:
4395 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004396 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004397 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004398 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 }
Jim Laskey62819f32007-02-21 22:54:50 +00004400 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401}
4402
Evan Cheng72261582005-12-20 06:22:03 +00004403const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4404 switch (Opcode) {
4405 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00004406 case X86ISD::SHLD: return "X86ISD::SHLD";
4407 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00004408 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004409 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00004410 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004411 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00004412 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00004413 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00004414 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4415 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4416 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00004417 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00004418 case X86ISD::FST: return "X86ISD::FST";
4419 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00004420 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00004421 case X86ISD::CALL: return "X86ISD::CALL";
4422 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4423 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4424 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00004425 case X86ISD::COMI: return "X86ISD::COMI";
4426 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00004427 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00004428 case X86ISD::CMOV: return "X86ISD::CMOV";
4429 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00004430 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00004431 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4432 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00004433 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00004434 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00004435 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00004436 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00004437 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00004438 case X86ISD::FMAX: return "X86ISD::FMAX";
4439 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00004440 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4441 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004442 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4443 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004444 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Evan Cheng72261582005-12-20 06:22:03 +00004445 }
4446}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004447
Chris Lattnerc9addb72007-03-30 23:15:24 +00004448// isLegalAddressingMode - Return true if the addressing mode represented
4449// by AM is legal for this target, for a load/store of the specified type.
4450bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4451 const Type *Ty) const {
4452 // X86 supports extremely general addressing modes.
4453
4454 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4455 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4456 return false;
4457
4458 if (AM.BaseGV) {
4459 // X86-64 only supports addr of globals in small code model.
4460 if (Subtarget->is64Bit() &&
4461 getTargetMachine().getCodeModel() != CodeModel::Small)
4462 return false;
4463
4464 // We can only fold this if we don't need a load either.
4465 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4466 return false;
4467 }
4468
4469 switch (AM.Scale) {
4470 case 0:
4471 case 1:
4472 case 2:
4473 case 4:
4474 case 8:
4475 // These scales always work.
4476 break;
4477 case 3:
4478 case 5:
4479 case 9:
4480 // These scales are formed with basereg+scalereg. Only accept if there is
4481 // no basereg yet.
4482 if (AM.HasBaseReg)
4483 return false;
4484 break;
4485 default: // Other stuff never works.
4486 return false;
4487 }
4488
4489 return true;
4490}
4491
4492
Evan Cheng60c07e12006-07-05 22:17:51 +00004493/// isShuffleMaskLegal - Targets can use this to indicate that they only
4494/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4495/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4496/// are assumed to be legal.
4497bool
4498X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4499 // Only do shuffles on 128-bit vector types for now.
4500 if (MVT::getSizeInBits(VT) == 64) return false;
4501 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00004502 isIdentityMask(Mask.Val) ||
4503 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00004504 isSplatMask(Mask.Val) ||
4505 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4506 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00004507 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00004508 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00004509 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00004510}
4511
4512bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4513 MVT::ValueType EVT,
4514 SelectionDAG &DAG) const {
4515 unsigned NumElts = BVOps.size();
4516 // Only do shuffles on 128-bit vector types for now.
4517 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4518 if (NumElts == 2) return true;
4519 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00004520 return (isMOVLMask(&BVOps[0], 4) ||
4521 isCommutedMOVL(&BVOps[0], 4, true) ||
4522 isSHUFPMask(&BVOps[0], 4) ||
4523 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00004524 }
4525 return false;
4526}
4527
4528//===----------------------------------------------------------------------===//
4529// X86 Scheduler Hooks
4530//===----------------------------------------------------------------------===//
4531
4532MachineBasicBlock *
4533X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4534 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004535 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00004536 switch (MI->getOpcode()) {
4537 default: assert(false && "Unexpected instr type to insert");
4538 case X86::CMOV_FR32:
4539 case X86::CMOV_FR64:
4540 case X86::CMOV_V4F32:
4541 case X86::CMOV_V2F64:
4542 case X86::CMOV_V2I64: {
4543 // To "insert" a SELECT_CC instruction, we actually have to insert the
4544 // diamond control-flow pattern. The incoming instruction knows the
4545 // destination vreg to set, the condition code register to branch on, the
4546 // true/false values to select between, and a branch opcode to use.
4547 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4548 ilist<MachineBasicBlock>::iterator It = BB;
4549 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004550
Evan Cheng60c07e12006-07-05 22:17:51 +00004551 // thisMBB:
4552 // ...
4553 // TrueVal = ...
4554 // cmpTY ccX, r1, r2
4555 // bCC copy1MBB
4556 // fallthrough --> copy0MBB
4557 MachineBasicBlock *thisMBB = BB;
4558 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4559 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004560 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00004561 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00004562 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00004563 MachineFunction *F = BB->getParent();
4564 F->getBasicBlockList().insert(It, copy0MBB);
4565 F->getBasicBlockList().insert(It, sinkMBB);
4566 // Update machine-CFG edges by first adding all successors of the current
4567 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004568 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00004569 e = BB->succ_end(); i != e; ++i)
4570 sinkMBB->addSuccessor(*i);
4571 // Next, remove all successors of the current block, and add the true
4572 // and fallthrough blocks as its successors.
4573 while(!BB->succ_empty())
4574 BB->removeSuccessor(BB->succ_begin());
4575 BB->addSuccessor(copy0MBB);
4576 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004577
Evan Cheng60c07e12006-07-05 22:17:51 +00004578 // copy0MBB:
4579 // %FalseValue = ...
4580 // # fallthrough to sinkMBB
4581 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004582
Evan Cheng60c07e12006-07-05 22:17:51 +00004583 // Update machine-CFG edges
4584 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004585
Evan Cheng60c07e12006-07-05 22:17:51 +00004586 // sinkMBB:
4587 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4588 // ...
4589 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00004590 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00004591 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4592 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4593
4594 delete MI; // The pseudo instruction is gone now.
4595 return BB;
4596 }
4597
Dale Johannesen849f2142007-07-03 00:53:03 +00004598 case X86::FP32_TO_INT16_IN_MEM:
4599 case X86::FP32_TO_INT32_IN_MEM:
4600 case X86::FP32_TO_INT64_IN_MEM:
4601 case X86::FP64_TO_INT16_IN_MEM:
4602 case X86::FP64_TO_INT32_IN_MEM:
4603 case X86::FP64_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00004604 // Change the floating point control register to use "round towards zero"
4605 // mode when truncating to an integer value.
4606 MachineFunction *F = BB->getParent();
4607 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004608 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004609
4610 // Load the old value of the high byte of the control word...
4611 unsigned OldCW =
4612 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004613 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004614
4615 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004616 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4617 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00004618
4619 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004620 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004621
4622 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00004623 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4624 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00004625
4626 // Get the X86 opcode to use.
4627 unsigned Opc;
4628 switch (MI->getOpcode()) {
4629 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00004630 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4631 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4632 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4633 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4634 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4635 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00004636 }
4637
4638 X86AddressMode AM;
4639 MachineOperand &Op = MI->getOperand(0);
4640 if (Op.isRegister()) {
4641 AM.BaseType = X86AddressMode::RegBase;
4642 AM.Base.Reg = Op.getReg();
4643 } else {
4644 AM.BaseType = X86AddressMode::FrameIndexBase;
4645 AM.Base.FrameIndex = Op.getFrameIndex();
4646 }
4647 Op = MI->getOperand(1);
4648 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004649 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004650 Op = MI->getOperand(2);
4651 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004652 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004653 Op = MI->getOperand(3);
4654 if (Op.isGlobalAddress()) {
4655 AM.GV = Op.getGlobal();
4656 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00004657 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004658 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00004659 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4660 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00004661
4662 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00004663 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004664
4665 delete MI; // The pseudo instruction is gone now.
4666 return BB;
4667 }
4668 }
4669}
4670
4671//===----------------------------------------------------------------------===//
4672// X86 Optimization Hooks
4673//===----------------------------------------------------------------------===//
4674
Nate Begeman368e18d2006-02-16 21:11:51 +00004675void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4676 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004677 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00004678 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00004679 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00004680 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004681 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00004682 assert((Opc >= ISD::BUILTIN_OP_END ||
4683 Opc == ISD::INTRINSIC_WO_CHAIN ||
4684 Opc == ISD::INTRINSIC_W_CHAIN ||
4685 Opc == ISD::INTRINSIC_VOID) &&
4686 "Should use MaskedValueIsZero if you don't know whether Op"
4687 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004688
Evan Cheng865f0602006-04-05 06:11:20 +00004689 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004690 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00004691 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004692 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00004693 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4694 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004695 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004696}
Chris Lattner259e97c2006-01-31 19:43:35 +00004697
Evan Cheng206ee9d2006-07-07 08:33:52 +00004698/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4699/// element of the result of the vector shuffle.
4700static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4701 MVT::ValueType VT = N->getValueType(0);
4702 SDOperand PermMask = N->getOperand(2);
4703 unsigned NumElems = PermMask.getNumOperands();
4704 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4705 i %= NumElems;
4706 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4707 return (i == 0)
Dan Gohman51eaa862007-06-14 22:58:02 +00004708 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00004709 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4710 SDOperand Idx = PermMask.getOperand(i);
4711 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00004712 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00004713 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4714 }
4715 return SDOperand();
4716}
4717
4718/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4719/// node is a GlobalAddress + an offset.
4720static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00004721 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004722 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004723 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4724 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4725 return true;
4726 }
Evan Cheng0085a282006-11-30 21:55:46 +00004727 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004728 SDOperand N1 = N->getOperand(0);
4729 SDOperand N2 = N->getOperand(1);
4730 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4731 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4732 if (V) {
4733 Offset += V->getSignExtended();
4734 return true;
4735 }
4736 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4737 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4738 if (V) {
4739 Offset += V->getSignExtended();
4740 return true;
4741 }
4742 }
4743 }
4744 return false;
4745}
4746
4747/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4748/// + Dist * Size.
4749static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4750 MachineFrameInfo *MFI) {
4751 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4752 return false;
4753
4754 SDOperand Loc = N->getOperand(1);
4755 SDOperand BaseLoc = Base->getOperand(1);
4756 if (Loc.getOpcode() == ISD::FrameIndex) {
4757 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4758 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00004759 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
4760 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00004761 int FS = MFI->getObjectSize(FI);
4762 int BFS = MFI->getObjectSize(BFI);
4763 if (FS != BFS || FS != Size) return false;
4764 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4765 } else {
4766 GlobalValue *GV1 = NULL;
4767 GlobalValue *GV2 = NULL;
4768 int64_t Offset1 = 0;
4769 int64_t Offset2 = 0;
4770 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4771 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4772 if (isGA1 && isGA2 && GV1 == GV2)
4773 return Offset1 == (Offset2 + Dist*Size);
4774 }
4775
4776 return false;
4777}
4778
Evan Cheng1e60c092006-07-10 21:37:44 +00004779static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4780 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004781 GlobalValue *GV;
4782 int64_t Offset;
4783 if (isGAPlusOffset(Base, GV, Offset))
4784 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4785 else {
4786 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00004787 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00004788 if (BFI < 0)
4789 // Fixed objects do not specify alignment, however the offsets are known.
4790 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4791 (MFI->getObjectOffset(BFI) % 16) == 0);
4792 else
4793 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00004794 }
4795 return false;
4796}
4797
4798
4799/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4800/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4801/// if the load addresses are consecutive, non-overlapping, and in the right
4802/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00004803static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4804 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004805 MachineFunction &MF = DAG.getMachineFunction();
4806 MachineFrameInfo *MFI = MF.getFrameInfo();
4807 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00004808 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00004809 SDOperand PermMask = N->getOperand(2);
4810 int NumElems = (int)PermMask.getNumOperands();
4811 SDNode *Base = NULL;
4812 for (int i = 0; i < NumElems; ++i) {
4813 SDOperand Idx = PermMask.getOperand(i);
4814 if (Idx.getOpcode() == ISD::UNDEF) {
4815 if (!Base) return SDOperand();
4816 } else {
4817 SDOperand Arg =
4818 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00004819 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00004820 return SDOperand();
4821 if (!Base)
4822 Base = Arg.Val;
4823 else if (!isConsecutiveLoad(Arg.Val, Base,
4824 i, MVT::getSizeInBits(EVT)/8,MFI))
4825 return SDOperand();
4826 }
4827 }
4828
Evan Cheng1e60c092006-07-10 21:37:44 +00004829 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00004830 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00004831 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00004832 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00004833 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00004834 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00004835 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4836 LD->getSrcValueOffset(), LD->isVolatile(),
4837 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00004838 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00004839}
4840
Chris Lattner83e6c992006-10-04 06:57:07 +00004841/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4842static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4843 const X86Subtarget *Subtarget) {
4844 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004845
Chris Lattner83e6c992006-10-04 06:57:07 +00004846 // If we have SSE[12] support, try to form min/max nodes.
4847 if (Subtarget->hasSSE2() &&
4848 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4849 if (Cond.getOpcode() == ISD::SETCC) {
4850 // Get the LHS/RHS of the select.
4851 SDOperand LHS = N->getOperand(1);
4852 SDOperand RHS = N->getOperand(2);
4853 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004854
Evan Cheng8ca29322006-11-10 21:43:37 +00004855 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00004856 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00004857 switch (CC) {
4858 default: break;
4859 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4860 case ISD::SETULE:
4861 case ISD::SETLE:
4862 if (!UnsafeFPMath) break;
4863 // FALL THROUGH.
4864 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4865 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00004866 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004867 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004868
Chris Lattner1907a7b2006-10-05 04:11:26 +00004869 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4870 case ISD::SETUGT:
4871 case ISD::SETGT:
4872 if (!UnsafeFPMath) break;
4873 // FALL THROUGH.
4874 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4875 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00004876 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004877 break;
4878 }
Chris Lattner83e6c992006-10-04 06:57:07 +00004879 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00004880 switch (CC) {
4881 default: break;
4882 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4883 case ISD::SETUGT:
4884 case ISD::SETGT:
4885 if (!UnsafeFPMath) break;
4886 // FALL THROUGH.
4887 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4888 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00004889 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004890 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004891
Chris Lattner1907a7b2006-10-05 04:11:26 +00004892 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4893 case ISD::SETULE:
4894 case ISD::SETLE:
4895 if (!UnsafeFPMath) break;
4896 // FALL THROUGH.
4897 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4898 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00004899 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004900 break;
4901 }
Chris Lattner83e6c992006-10-04 06:57:07 +00004902 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004903
Evan Cheng8ca29322006-11-10 21:43:37 +00004904 if (Opcode)
4905 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00004906 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004907
Chris Lattner83e6c992006-10-04 06:57:07 +00004908 }
4909
4910 return SDOperand();
4911}
4912
4913
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004914SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00004915 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004916 SelectionDAG &DAG = DCI.DAG;
4917 switch (N->getOpcode()) {
4918 default: break;
4919 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00004920 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00004921 case ISD::SELECT:
4922 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00004923 }
4924
4925 return SDOperand();
4926}
4927
Evan Cheng60c07e12006-07-05 22:17:51 +00004928//===----------------------------------------------------------------------===//
4929// X86 Inline Assembly Support
4930//===----------------------------------------------------------------------===//
4931
Chris Lattnerf4dff842006-07-11 02:54:03 +00004932/// getConstraintType - Given a constraint letter, return the type of
4933/// constraint it is for this target.
4934X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004935X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4936 if (Constraint.size() == 1) {
4937 switch (Constraint[0]) {
4938 case 'A':
4939 case 'r':
4940 case 'R':
4941 case 'l':
4942 case 'q':
4943 case 'Q':
4944 case 'x':
4945 case 'Y':
4946 return C_RegisterClass;
4947 default:
4948 break;
4949 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00004950 }
Chris Lattner4234f572007-03-25 02:14:49 +00004951 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00004952}
4953
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004954/// isOperandValidForConstraint - Return the specified operand (possibly
4955/// modified) if the specified SDOperand is valid for the specified target
4956/// constraint letter, otherwise return null.
4957SDOperand X86TargetLowering::
4958isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4959 switch (Constraint) {
4960 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00004961 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00004962 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4963 if (C->getValue() <= 31)
Chris Lattner709fd412007-05-15 01:28:08 +00004964 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
Devang Patel84f7fd22007-03-17 00:13:28 +00004965 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00004966 return SDOperand(0,0);
4967 case 'N':
4968 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4969 if (C->getValue() <= 255)
Chris Lattner709fd412007-05-15 01:28:08 +00004970 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
Chris Lattner188b9fe2007-03-25 01:57:35 +00004971 }
4972 return SDOperand(0,0);
Chris Lattnerdc43a882007-05-03 16:52:29 +00004973 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004974 // Literal immediates are always ok.
Chris Lattner709fd412007-05-15 01:28:08 +00004975 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op))
4976 return DAG.getTargetConstant(CST->getValue(), Op.getValueType());
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004977
Chris Lattnerdc43a882007-05-03 16:52:29 +00004978 // If we are in non-pic codegen mode, we allow the address of a global (with
4979 // an optional displacement) to be used with 'i'.
4980 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
4981 int64_t Offset = 0;
4982
4983 // Match either (GA) or (GA+C)
4984 if (GA) {
4985 Offset = GA->getOffset();
4986 } else if (Op.getOpcode() == ISD::ADD) {
4987 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4988 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4989 if (C && GA) {
4990 Offset = GA->getOffset()+C->getValue();
4991 } else {
4992 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4993 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4994 if (C && GA)
4995 Offset = GA->getOffset()+C->getValue();
4996 else
4997 C = 0, GA = 0;
4998 }
4999 }
5000
5001 if (GA) {
5002 // If addressing this global requires a load (e.g. in PIC mode), we can't
5003 // match.
5004 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5005 false))
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005006 return SDOperand(0, 0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005007
Chris Lattnerdc43a882007-05-03 16:52:29 +00005008 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5009 Offset);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005010 return Op;
5011 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005012
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005013 // Otherwise, not valid for this mode.
5014 return SDOperand(0, 0);
5015 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00005016 }
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005017 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5018}
5019
Chris Lattner259e97c2006-01-31 19:43:35 +00005020std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005021getRegClassForInlineAsmConstraint(const std::string &Constraint,
5022 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005023 if (Constraint.size() == 1) {
5024 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00005025 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005026 default: break; // Unknown constraint letter
5027 case 'A': // EAX/EDX
5028 if (VT == MVT::i32 || VT == MVT::i64)
5029 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5030 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005031 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5032 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005033 if (VT == MVT::i32)
5034 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5035 else if (VT == MVT::i16)
5036 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5037 else if (VT == MVT::i8)
5038 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5039 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005040 }
5041 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005042
Chris Lattner1efa40f2006-02-22 00:56:39 +00005043 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005044}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005045
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005046std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005047X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5048 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00005049 // First, see if this is a constraint that directly corresponds to an LLVM
5050 // register class.
5051 if (Constraint.size() == 1) {
5052 // GCC Constraint Letters
5053 switch (Constraint[0]) {
5054 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005055 case 'r': // GENERAL_REGS
5056 case 'R': // LEGACY_REGS
5057 case 'l': // INDEX_REGS
5058 if (VT == MVT::i64 && Subtarget->is64Bit())
5059 return std::make_pair(0U, X86::GR64RegisterClass);
5060 if (VT == MVT::i32)
5061 return std::make_pair(0U, X86::GR32RegisterClass);
5062 else if (VT == MVT::i16)
5063 return std::make_pair(0U, X86::GR16RegisterClass);
5064 else if (VT == MVT::i8)
5065 return std::make_pair(0U, X86::GR8RegisterClass);
5066 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00005067 case 'y': // MMX_REGS if MMX allowed.
5068 if (!Subtarget->hasMMX()) break;
5069 return std::make_pair(0U, X86::VR64RegisterClass);
5070 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005071 case 'Y': // SSE_REGS if SSE2 allowed
5072 if (!Subtarget->hasSSE2()) break;
5073 // FALL THROUGH.
5074 case 'x': // SSE_REGS if SSE1 allowed
5075 if (!Subtarget->hasSSE1()) break;
5076
5077 switch (VT) {
5078 default: break;
5079 // Scalar SSE types.
5080 case MVT::f32:
5081 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00005082 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005083 case MVT::f64:
5084 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00005085 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005086 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00005087 case MVT::v16i8:
5088 case MVT::v8i16:
5089 case MVT::v4i32:
5090 case MVT::v2i64:
5091 case MVT::v4f32:
5092 case MVT::v2f64:
5093 return std::make_pair(0U, X86::VR128RegisterClass);
5094 }
Chris Lattnerad043e82007-04-09 05:11:28 +00005095 break;
5096 }
5097 }
5098
Chris Lattnerf76d1802006-07-31 23:26:50 +00005099 // Use the default implementation in TargetLowering to convert the register
5100 // constraint into a member of a register class.
5101 std::pair<unsigned, const TargetRegisterClass*> Res;
5102 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00005103
5104 // Not found as a standard register?
5105 if (Res.second == 0) {
5106 // GCC calls "st(0)" just plain "st".
5107 if (StringsEqualNoCase("{st}", Constraint)) {
5108 Res.first = X86::ST0;
5109 Res.second = X86::RSTRegisterClass;
5110 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005111
Chris Lattner1a60aa72006-10-31 19:42:44 +00005112 return Res;
5113 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005114
Chris Lattnerf76d1802006-07-31 23:26:50 +00005115 // Otherwise, check to see if this is a register class of the wrong value
5116 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5117 // turn into {ax},{dx}.
5118 if (Res.second->hasType(VT))
5119 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005120
Chris Lattnerf76d1802006-07-31 23:26:50 +00005121 // All of the single-register GCC register classes map their values onto
5122 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5123 // really want an 8-bit or 32-bit register, map to the appropriate register
5124 // class and return the appropriate register.
5125 if (Res.second != X86::GR16RegisterClass)
5126 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005127
Chris Lattnerf76d1802006-07-31 23:26:50 +00005128 if (VT == MVT::i8) {
5129 unsigned DestReg = 0;
5130 switch (Res.first) {
5131 default: break;
5132 case X86::AX: DestReg = X86::AL; break;
5133 case X86::DX: DestReg = X86::DL; break;
5134 case X86::CX: DestReg = X86::CL; break;
5135 case X86::BX: DestReg = X86::BL; break;
5136 }
5137 if (DestReg) {
5138 Res.first = DestReg;
5139 Res.second = Res.second = X86::GR8RegisterClass;
5140 }
5141 } else if (VT == MVT::i32) {
5142 unsigned DestReg = 0;
5143 switch (Res.first) {
5144 default: break;
5145 case X86::AX: DestReg = X86::EAX; break;
5146 case X86::DX: DestReg = X86::EDX; break;
5147 case X86::CX: DestReg = X86::ECX; break;
5148 case X86::BX: DestReg = X86::EBX; break;
5149 case X86::SI: DestReg = X86::ESI; break;
5150 case X86::DI: DestReg = X86::EDI; break;
5151 case X86::BP: DestReg = X86::EBP; break;
5152 case X86::SP: DestReg = X86::ESP; break;
5153 }
5154 if (DestReg) {
5155 Res.first = DestReg;
5156 Res.second = Res.second = X86::GR32RegisterClass;
5157 }
Evan Cheng25ab6902006-09-08 06:48:29 +00005158 } else if (VT == MVT::i64) {
5159 unsigned DestReg = 0;
5160 switch (Res.first) {
5161 default: break;
5162 case X86::AX: DestReg = X86::RAX; break;
5163 case X86::DX: DestReg = X86::RDX; break;
5164 case X86::CX: DestReg = X86::RCX; break;
5165 case X86::BX: DestReg = X86::RBX; break;
5166 case X86::SI: DestReg = X86::RSI; break;
5167 case X86::DI: DestReg = X86::RDI; break;
5168 case X86::BP: DestReg = X86::RBP; break;
5169 case X86::SP: DestReg = X86::RSP; break;
5170 }
5171 if (DestReg) {
5172 Res.first = DestReg;
5173 Res.second = Res.second = X86::GR64RegisterClass;
5174 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005175 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005176
Chris Lattnerf76d1802006-07-31 23:26:50 +00005177 return Res;
5178}