blob: e0ffb70fc4faa45af4bfde0484b243a813b498bf [file] [log] [blame]
Chris Lattnera58f5592006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattner76ac0682005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chengdc614c12006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng88decde2006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venancio25188892007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000026#include "llvm/ADT/VectorExtras.h"
27#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdc3adc82007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000032#include "llvm/CodeGen/SelectionDAG.h"
33#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000034#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnerf6a69662006-10-31 19:42:44 +000036#include "llvm/ADT/StringExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000037using namespace llvm;
38
Chris Lattner76ac0682005-11-15 00:40:23 +000039X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000041 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000043 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Chengcde9e302006-01-27 08:10:46 +000044
Chris Lattner76ac0682005-11-15 00:40:23 +000045 // Set up the TargetLowering object.
46
47 // X86 is weird, it always uses i8 for shift amounts and setcc results.
48 setShiftAmountType(MVT::i8);
49 setSetCCResultType(MVT::i8);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000051 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000052 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng11b0a5d2006-09-08 06:48:29 +000053 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng20931a72006-03-16 21:47:42 +000054
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000055 if (Subtarget->isTargetDarwin()) {
Evan Chengb09a56f2006-03-17 20:31:41 +000056 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000057 setUseUnderscoreSetJmp(false);
58 setUseUnderscoreLongJmp(false);
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +000059 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikov3b7c2572006-12-10 23:12:42 +000060 // MS runtime is weird: it exports _setjmp, but longjmp!
61 setUseUnderscoreSetJmp(true);
62 setUseUnderscoreLongJmp(false);
63 } else {
64 setUseUnderscoreSetJmp(true);
65 setUseUnderscoreLongJmp(true);
66 }
67
Chris Lattner76ac0682005-11-15 00:40:23 +000068 // Set up the register classes.
Evan Cheng9fee4422006-05-16 07:21:53 +000069 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
70 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
71 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000072 if (Subtarget->is64Bit())
73 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +000074
Evan Cheng5d9fd972006-10-04 00:56:09 +000075 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
76
Chris Lattner76ac0682005-11-15 00:40:23 +000077 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
78 // operation.
79 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
80 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
81 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000082
Evan Cheng11b0a5d2006-09-08 06:48:29 +000083 if (Subtarget->is64Bit()) {
84 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000085 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +000086 } else {
87 if (X86ScalarSSE)
88 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
89 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
90 else
91 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
92 }
Chris Lattner76ac0682005-11-15 00:40:23 +000093
94 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
95 // this operation.
96 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
97 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000098 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000099 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +0000100 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +0000101 else {
102 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
103 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
104 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000105
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000106 if (!Subtarget->is64Bit()) {
107 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
108 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
109 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
110 }
Evan Cheng5b97fcf2006-01-30 08:02:57 +0000111
Evan Cheng08390f62006-01-30 22:13:22 +0000112 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
113 // this operation.
114 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
115 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
116
117 if (X86ScalarSSE) {
118 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
119 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000120 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000121 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000122 }
123
124 // Handle FP_TO_UINT by promoting the destination to a larger signed
125 // conversion.
126 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
127 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
128 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
129
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000130 if (Subtarget->is64Bit()) {
131 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000132 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000133 } else {
134 if (X86ScalarSSE && !Subtarget->hasSSE3())
135 // Expand FP_TO_UINT into a select.
136 // FIXME: We would like to use a Custom expander here eventually to do
137 // the optimal thing for SSE vs. the default expansion in the legalizer.
138 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
139 else
140 // With SSE3 we can use fisttpll to convert to a signed i64.
141 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
142 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000143
Chris Lattner55c17f92006-12-05 18:22:22 +0000144 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Chris Lattnerc20b7e82006-12-05 18:45:06 +0000145 if (!X86ScalarSSE) {
146 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
147 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
148 }
Chris Lattner30107e62005-12-23 05:15:23 +0000149
Evan Cheng0d41d192006-10-30 08:02:39 +0000150 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng593bea72006-02-17 07:01:52 +0000151 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000152 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
153 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000154 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000155 if (Subtarget->is64Bit())
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
160 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000161 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000162
Chris Lattner76ac0682005-11-15 00:40:23 +0000163 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
164 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
165 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
166 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
167 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
168 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
169 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
170 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
171 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000172 if (Subtarget->is64Bit()) {
173 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
174 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
175 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
176 }
177
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000178 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000179 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000180
Chris Lattner76ac0682005-11-15 00:40:23 +0000181 // These should be promoted to a larger select which is supported.
182 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
183 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000184 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000185 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
186 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
187 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
188 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
189 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
190 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
191 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
192 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
193 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000194 if (Subtarget->is64Bit()) {
195 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
196 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
197 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000198 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000199 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000200 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000201 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000202 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000203 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000204 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000205 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000206 if (Subtarget->is64Bit()) {
207 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
208 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
209 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
210 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
211 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000212 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000213 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
214 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
215 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000216 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000217 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
218 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000219
Chris Lattner9c415362005-11-29 06:16:21 +0000220 // We don't have line number support yet.
221 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000222 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000223 // FIXME - use subtarget debug flags
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000224 if (!Subtarget->isTargetDarwin() &&
225 !Subtarget->isTargetELF() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +0000226 !Subtarget->isTargetCygMing())
Jim Laskeyf9e54452007-01-26 14:34:52 +0000227 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000228
Anton Korobeynikovf1dcf692007-05-02 19:53:33 +0000229 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
230 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
231 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
232 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
233 if (Subtarget->is64Bit()) {
234 // FIXME: Verify
235 setExceptionPointerRegister(X86::RAX);
236 setExceptionSelectorRegister(X86::RDX);
237 } else {
238 setExceptionPointerRegister(X86::EAX);
239 setExceptionSelectorRegister(X86::EDX);
240 }
241
Nate Begemane74795c2006-01-25 18:21:52 +0000242 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
243 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemane74795c2006-01-25 18:21:52 +0000244 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemane74795c2006-01-25 18:21:52 +0000245 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengdeaea252007-03-02 23:16:35 +0000246 if (Subtarget->is64Bit())
247 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
248 else
249 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
250
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000251 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000252 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit())
254 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +0000255 if (Subtarget->isTargetCygMing())
256 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
257 else
258 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000259
Chris Lattner76ac0682005-11-15 00:40:23 +0000260 if (X86ScalarSSE) {
261 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000262 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
263 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000264
Evan Cheng72d5c252006-01-31 22:28:30 +0000265 // Use ANDPD to simulate FABS.
266 setOperationAction(ISD::FABS , MVT::f64, Custom);
267 setOperationAction(ISD::FABS , MVT::f32, Custom);
268
269 // Use XORP to simulate FNEG.
270 setOperationAction(ISD::FNEG , MVT::f64, Custom);
271 setOperationAction(ISD::FNEG , MVT::f32, Custom);
272
Evan Cheng4363e882007-01-05 07:55:56 +0000273 // Use ANDPD and ORPD to simulate FCOPYSIGN.
274 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
275 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
276
Evan Chengd8fba3a2006-02-02 00:28:23 +0000277 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000278 setOperationAction(ISD::FSIN , MVT::f64, Expand);
279 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000280 setOperationAction(ISD::FREM , MVT::f64, Expand);
281 setOperationAction(ISD::FSIN , MVT::f32, Expand);
282 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000283 setOperationAction(ISD::FREM , MVT::f32, Expand);
284
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000285 // Expand FP immediates into loads from the stack, except for the special
286 // cases we handle.
287 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
288 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000289 addLegalFPImmediate(+0.0); // xorps / xorpd
290 } else {
291 // Set up the FP register classes.
292 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000293
Evan Cheng4363e882007-01-05 07:55:56 +0000294 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
295 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
296 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000297
Chris Lattner76ac0682005-11-15 00:40:23 +0000298 if (!UnsafeFPMath) {
299 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
300 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
301 }
302
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000303 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000304 addLegalFPImmediate(+0.0); // FLD0
305 addLegalFPImmediate(+1.0); // FLD1
306 addLegalFPImmediate(-0.0); // FLD0/FCHS
307 addLegalFPImmediate(-1.0); // FLD1/FCHS
308 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000309
Evan Cheng19264272006-03-01 01:11:20 +0000310 // First set operation action for all vector types to expand. Then we
311 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmaneefa83e2007-05-18 18:44:07 +0000312 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
313 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Cheng19264272006-03-01 01:11:20 +0000314 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
315 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000316 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
317 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000318 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Chengbf3df772006-10-27 18:49:08 +0000319 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
320 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
321 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
322 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
323 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
324 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000325 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000326 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000327 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000328 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000329 }
330
Evan Chengbc047222006-03-22 19:22:18 +0000331 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000332 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
333 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
334 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendling98d21042007-03-26 07:53:08 +0000335 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng9e252e32006-02-22 02:26:30 +0000336
Evan Cheng19264272006-03-01 01:11:20 +0000337 // FIXME: add MMX packed arithmetics
Bill Wendling97905b42007-03-07 05:43:18 +0000338
Bill Wendling6092ce22007-03-08 22:09:11 +0000339 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
340 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
341 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner2805bce2007-04-12 04:14:49 +0000342 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000343
Bill Wendlinge9b81f52007-03-10 09:57:05 +0000344 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
345 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
346 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
347
Bill Wendlinge3103412007-03-15 21:24:36 +0000348 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
349 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
350
Bill Wendling144b8bb2007-03-16 09:44:46 +0000351 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000352 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000353 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000354 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
355 setOperationAction(ISD::AND, MVT::v2i32, Promote);
356 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
357 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000358
359 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000360 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000361 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000362 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
363 setOperationAction(ISD::OR, MVT::v2i32, Promote);
364 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
365 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000366
367 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000368 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000369 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendling158f6092007-03-26 08:03:33 +0000370 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
371 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
372 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
373 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling144b8bb2007-03-16 09:44:46 +0000374
Bill Wendling6092ce22007-03-08 22:09:11 +0000375 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000376 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling6092ce22007-03-08 22:09:11 +0000377 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendling98d21042007-03-26 07:53:08 +0000378 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
379 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
380 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
381 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling6092ce22007-03-08 22:09:11 +0000382
Bill Wendling6dff51a2007-03-27 20:22:40 +0000383 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
384 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
385 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
386 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlingd551a182007-03-22 18:42:45 +0000387
388 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
389 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
390 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendling6dff51a2007-03-27 20:22:40 +0000391 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendlingad2db4a2007-03-28 00:57:11 +0000392
393 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
394 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling591eab82007-04-24 21:16:55 +0000395 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
396 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000397 }
398
Evan Chengbc047222006-03-22 19:22:18 +0000399 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000400 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
401
Evan Chengbf3df772006-10-27 18:49:08 +0000402 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
403 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
404 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
405 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000406 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
407 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
408 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000409 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000410 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000411 }
412
Evan Chengbc047222006-03-22 19:22:18 +0000413 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000414 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
415 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
416 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
417 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
418 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
419
Evan Cheng617a6a82006-04-10 07:23:14 +0000420 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
421 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
422 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000423 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000424 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
425 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
426 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng57f261b2007-03-12 22:58:52 +0000427 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000428 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengbf3df772006-10-27 18:49:08 +0000429 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
430 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
431 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
432 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Evan Cheng92232302006-04-12 21:21:57 +0000433
Evan Cheng617a6a82006-04-10 07:23:14 +0000434 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
435 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000436 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000437 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
438 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
439 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000440
Evan Cheng92232302006-04-12 21:21:57 +0000441 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
442 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
443 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
444 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
445 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
446 }
447 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
448 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
449 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
450 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
451 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
452 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
453
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000454 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng92232302006-04-12 21:21:57 +0000455 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
456 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
457 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
458 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
459 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
460 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
461 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000462 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
463 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000464 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
465 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000466 }
Evan Cheng92232302006-04-12 21:21:57 +0000467
468 // Custom lower v2i64 and v2f64 selects.
469 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000470 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000471 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000472 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000473 }
474
Evan Cheng78038292006-04-05 23:38:46 +0000475 // We want to custom lower some of our intrinsics.
476 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
477
Evan Cheng5987cfb2006-07-07 08:33:52 +0000478 // We have target-specific dag combine patterns for the following nodes:
479 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner9259b1e2006-10-04 06:57:07 +0000480 setTargetDAGCombine(ISD::SELECT);
Evan Cheng5987cfb2006-07-07 08:33:52 +0000481
Chris Lattner76ac0682005-11-15 00:40:23 +0000482 computeRegisterProperties();
483
Evan Cheng6a374562006-02-14 08:25:08 +0000484 // FIXME: These should be based on subtarget info. Plus, the values should
485 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000486 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
487 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
488 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000489 allowUnalignedMemoryAccesses = true; // x86 supports it!
490}
491
Chris Lattner3c763092007-02-25 08:29:00 +0000492
493//===----------------------------------------------------------------------===//
494// Return Value Calling Convention Implementation
495//===----------------------------------------------------------------------===//
496
Chris Lattnerba3d2732007-02-28 04:55:35 +0000497#include "X86GenCallingConv.inc"
Chris Lattnerc9eed392007-02-27 05:28:59 +0000498
Chris Lattner2fc0d702007-02-25 09:12:39 +0000499/// LowerRET - Lower an ISD::RET node.
500SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
501 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
502
Chris Lattnerc9eed392007-02-27 05:28:59 +0000503 SmallVector<CCValAssign, 16> RVLocs;
504 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner944200b2007-06-19 00:13:10 +0000505 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
506 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000507 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000508
Chris Lattner2fc0d702007-02-25 09:12:39 +0000509
510 // If this is the first return lowered for this function, add the regs to the
511 // liveout set for the function.
512 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000513 for (unsigned i = 0; i != RVLocs.size(); ++i)
514 if (RVLocs[i].isRegLoc())
515 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2fc0d702007-02-25 09:12:39 +0000516 }
517
518 SDOperand Chain = Op.getOperand(0);
519 SDOperand Flag;
520
521 // Copy the result values into the output registers.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000522 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
523 RVLocs[0].getLocReg() != X86::ST0) {
524 for (unsigned i = 0; i != RVLocs.size(); ++i) {
525 CCValAssign &VA = RVLocs[i];
526 assert(VA.isRegLoc() && "Can only return in registers!");
527 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
528 Flag);
Chris Lattner2fc0d702007-02-25 09:12:39 +0000529 Flag = Chain.getValue(1);
530 }
531 } else {
532 // We need to handle a destination of ST0 specially, because it isn't really
533 // a register.
534 SDOperand Value = Op.getOperand(1);
535
536 // If this is an FP return with ScalarSSE, we need to move the value from
537 // an XMM register onto the fp-stack.
538 if (X86ScalarSSE) {
539 SDOperand MemLoc;
540
541 // If this is a load into a scalarsse value, don't store the loaded value
542 // back to the stack, only to reload it: just replace the scalar-sse load.
543 if (ISD::isNON_EXTLoad(Value.Val) &&
544 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
545 Chain = Value.getOperand(0);
546 MemLoc = Value.getOperand(1);
547 } else {
548 // Spill the value to memory and reload it into top of stack.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000549 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2fc0d702007-02-25 09:12:39 +0000550 MachineFunction &MF = DAG.getMachineFunction();
551 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
552 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
553 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
554 }
555 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000556 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2fc0d702007-02-25 09:12:39 +0000557 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
558 Chain = Value.getValue(1);
559 }
560
561 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
562 SDOperand Ops[] = { Chain, Value };
563 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
564 Flag = Chain.getValue(1);
565 }
566
567 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
568 if (Flag.Val)
569 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
570 else
571 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
572}
573
574
Chris Lattner0cd99602007-02-25 08:59:22 +0000575/// LowerCallResult - Lower the result values of an ISD::CALL into the
576/// appropriate copies out of appropriate physical registers. This assumes that
577/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
578/// being lowered. The returns a SDNode with the same number of values as the
579/// ISD::CALL.
580SDNode *X86TargetLowering::
581LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
582 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattner152bfa12007-02-28 07:09:55 +0000583
584 // Assign locations to each value returned by this call.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000585 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner944200b2007-06-19 00:13:10 +0000586 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
587 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattner152bfa12007-02-28 07:09:55 +0000588 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
589
Chris Lattner0cd99602007-02-25 08:59:22 +0000590
Chris Lattner152bfa12007-02-28 07:09:55 +0000591 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner0cd99602007-02-25 08:59:22 +0000592
593 // Copy all of the result registers out of their specified physreg.
Chris Lattnerc9eed392007-02-27 05:28:59 +0000594 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
595 for (unsigned i = 0; i != RVLocs.size(); ++i) {
596 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
597 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner0cd99602007-02-25 08:59:22 +0000598 InFlag = Chain.getValue(2);
599 ResultVals.push_back(Chain.getValue(0));
600 }
601 } else {
602 // Copies from the FP stack are special, as ST0 isn't a valid register
603 // before the fp stackifier runs.
604
605 // Copy ST0 into an RFP register with FP_GET_RESULT.
606 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
607 SDOperand GROps[] = { Chain, InFlag };
608 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
609 Chain = RetVal.getValue(1);
610 InFlag = RetVal.getValue(2);
611
612 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
613 // an XMM register.
614 if (X86ScalarSSE) {
615 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
616 // shouldn't be necessary except that RFP cannot be live across
617 // multiple blocks. When stackifier is fixed, they can be uncoupled.
618 MachineFunction &MF = DAG.getMachineFunction();
619 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
620 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
621 SDOperand Ops[] = {
Chris Lattnerc9eed392007-02-27 05:28:59 +0000622 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner0cd99602007-02-25 08:59:22 +0000623 };
624 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattnerc9eed392007-02-27 05:28:59 +0000625 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner0cd99602007-02-25 08:59:22 +0000626 Chain = RetVal.getValue(1);
627 }
628
Chris Lattnerc9eed392007-02-27 05:28:59 +0000629 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
Chris Lattner0cd99602007-02-25 08:59:22 +0000630 // FIXME: we would really like to remember that this FP_ROUND
631 // operation is okay to eliminate if we allow excess FP precision.
632 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
633 ResultVals.push_back(RetVal);
634 }
635
636 // Merge everything together with a MERGE_VALUES node.
637 ResultVals.push_back(Chain);
638 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
639 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner3c763092007-02-25 08:29:00 +0000640}
641
642
Chris Lattner76ac0682005-11-15 00:40:23 +0000643//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000644// C & StdCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000645//===----------------------------------------------------------------------===//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000646// StdCall calling convention seems to be standard for many Windows' API
647// routines and around. It differs from C calling convention just a little:
648// callee should clean up the stack, not caller. Symbols should be also
649// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +0000650
Evan Cheng24eb3f42006-04-27 05:35:28 +0000651/// AddLiveIn - This helper function adds the specified physical register to the
652/// MachineFunction as a live in value. It also creates a corresponding virtual
653/// register for it.
654static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000655 const TargetRegisterClass *RC) {
Evan Cheng24eb3f42006-04-27 05:35:28 +0000656 assert(RC->contains(PReg) && "Not the correct regclass!");
657 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
658 MF.addLiveIn(PReg, VReg);
659 return VReg;
660}
661
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000662SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
663 bool isStdCall) {
Evan Cheng17e734f2006-05-23 21:06:34 +0000664 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000665 MachineFunction &MF = DAG.getMachineFunction();
666 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000667 SDOperand Root = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000668 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000669
Chris Lattner227b6c52007-02-28 07:00:42 +0000670 // Assign locations to all of the incoming arguments.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000671 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +0000672 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
673 getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000674 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
675
Chris Lattnerb9db2252007-02-28 05:46:49 +0000676 SmallVector<SDOperand, 8> ArgValues;
677 unsigned LastVal = ~0U;
678 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
679 CCValAssign &VA = ArgLocs[i];
680 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
681 // places.
682 assert(VA.getValNo() != LastVal &&
683 "Don't support value assigned to multiple locs yet");
684 LastVal = VA.getValNo();
685
686 if (VA.isRegLoc()) {
687 MVT::ValueType RegVT = VA.getLocVT();
688 TargetRegisterClass *RC;
689 if (RegVT == MVT::i32)
690 RC = X86::GR32RegisterClass;
691 else {
692 assert(MVT::isVector(RegVT));
693 RC = X86::VR128RegisterClass;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000694 }
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000695
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000696 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
697 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerb9db2252007-02-28 05:46:49 +0000698
699 // If this is an 8 or 16-bit value, it is really passed promoted to 32
700 // bits. Insert an assert[sz]ext to capture this, then truncate to the
701 // right size.
702 if (VA.getLocInfo() == CCValAssign::SExt)
703 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
704 DAG.getValueType(VA.getValVT()));
705 else if (VA.getLocInfo() == CCValAssign::ZExt)
706 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
707 DAG.getValueType(VA.getValVT()));
708
709 if (VA.getLocInfo() != CCValAssign::Full)
710 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
711
712 ArgValues.push_back(ArgValue);
713 } else {
714 assert(VA.isMemLoc());
715
716 // Create the nodes corresponding to a load from this parameter slot.
717 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
718 VA.getLocMemOffset());
719 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
720 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000721 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000722 }
Chris Lattnerb9db2252007-02-28 05:46:49 +0000723
724 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000725
Evan Cheng17e734f2006-05-23 21:06:34 +0000726 ArgValues.push_back(Root);
727
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000728 // If the function takes variable number of arguments, make a frame index for
729 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng7068a932006-05-23 21:08:24 +0000730 if (isVarArg)
Chris Lattnerb9db2252007-02-28 05:46:49 +0000731 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000732
733 if (isStdCall && !isVarArg) {
Chris Lattnerb9db2252007-02-28 05:46:49 +0000734 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000735 BytesCallerReserves = 0;
736 } else {
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000737 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerb9db2252007-02-28 05:46:49 +0000738
739 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000740 if (NumArgs &&
741 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000742 ISD::ParamFlags::StructReturn))
Chris Lattnerb9db2252007-02-28 05:46:49 +0000743 BytesToPopOnReturn = 4;
744
745 BytesCallerReserves = StackSize;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000746 }
747
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000748 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
749 ReturnAddrIndex = 0; // No return address slot generated yet.
Evan Cheng17e734f2006-05-23 21:06:34 +0000750
Chris Lattnerff0598d2007-04-17 17:21:52 +0000751 MF.getInfo<X86MachineFunctionInfo>()
752 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000753
Evan Cheng17e734f2006-05-23 21:06:34 +0000754 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +0000755 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +0000756 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000757}
758
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000759SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +0000760 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +0000761 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000762 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng2a330942006-05-25 00:59:30 +0000763 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
764 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +0000765 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000766
Chris Lattner227b6c52007-02-28 07:00:42 +0000767 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerbe799592007-02-28 05:31:48 +0000768 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +0000769 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000770 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000771
Chris Lattnerbe799592007-02-28 05:31:48 +0000772 // Get a count of how many bytes are to be pushed on the stack.
773 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +0000774
Evan Cheng2a330942006-05-25 00:59:30 +0000775 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000776
Chris Lattner35a08552007-02-25 07:10:00 +0000777 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
778 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng2a330942006-05-25 00:59:30 +0000779
Chris Lattnerbe799592007-02-28 05:31:48 +0000780 SDOperand StackPtr;
Chris Lattnerbe799592007-02-28 05:31:48 +0000781
782 // Walk the register/memloc assignments, inserting copies/loads.
783 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
784 CCValAssign &VA = ArgLocs[i];
785 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000786
Chris Lattnerbe799592007-02-28 05:31:48 +0000787 // Promote the value if needed.
788 switch (VA.getLocInfo()) {
789 default: assert(0 && "Unknown loc info!");
790 case CCValAssign::Full: break;
791 case CCValAssign::SExt:
792 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
793 break;
794 case CCValAssign::ZExt:
795 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
796 break;
797 case CCValAssign::AExt:
798 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
799 break;
Evan Cheng5ee96892006-05-25 18:56:34 +0000800 }
Chris Lattnerbe799592007-02-28 05:31:48 +0000801
802 if (VA.isRegLoc()) {
803 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
804 } else {
805 assert(VA.isMemLoc());
806 if (StackPtr.Val == 0)
807 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
808 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000809 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
810 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattner76ac0682005-11-15 00:40:23 +0000811 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000812 }
813
Chris Lattner5958b172007-02-28 05:39:26 +0000814 // If the first argument is an sret pointer, remember it.
Anton Korobeynikove7ec3bc2007-03-06 08:12:33 +0000815 bool isSRet = NumOps &&
816 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikoved4b3032007-03-07 16:25:09 +0000817 ISD::ParamFlags::StructReturn);
Chris Lattner5958b172007-02-28 05:39:26 +0000818
Evan Cheng2a330942006-05-25 00:59:30 +0000819 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000820 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
821 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +0000822
Evan Cheng88decde2006-04-28 21:29:37 +0000823 // Build a sequence of copy-to-reg nodes chained together with token chain
824 // and flag operands which copy the outgoing args into registers.
825 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +0000826 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
827 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
828 InFlag);
Evan Cheng88decde2006-04-28 21:29:37 +0000829 InFlag = Chain.getValue(1);
830 }
831
Evan Cheng84a041e2007-02-21 21:18:14 +0000832 // ELF / PIC requires GOT in the EBX register before function calls via PLT
833 // GOT pointer.
Evan Cheng1281dc32007-01-22 21:34:25 +0000834 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
835 Subtarget->isPICStyleGOT()) {
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000836 Chain = DAG.getCopyToReg(Chain, X86::EBX,
837 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
838 InFlag);
839 InFlag = Chain.getValue(1);
840 }
841
Evan Cheng2a330942006-05-25 00:59:30 +0000842 // If the callee is a GlobalAddress node (quite common, every direct call is)
843 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +0000844 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +0000845 // We should use extra load for direct calls to dllimported functions in
846 // non-JIT mode.
847 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
848 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +0000849 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
850 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +0000851 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
852
Chris Lattnere56fef92007-02-25 06:40:16 +0000853 // Returns a chain & a flag for retval copy to use.
854 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +0000855 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000856 Ops.push_back(Chain);
857 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +0000858
859 // Add argument registers to the end of the list so that they are known live
860 // into the call.
861 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000862 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +0000863 RegsToPass[i].second.getValueType()));
Evan Cheng84a041e2007-02-21 21:18:14 +0000864
865 // Add an implicit use GOT pointer in EBX.
866 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
867 Subtarget->isPICStyleGOT())
868 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikova0554d92007-01-12 19:20:47 +0000869
Evan Cheng88decde2006-04-28 21:29:37 +0000870 if (InFlag.Val)
871 Ops.push_back(InFlag);
Evan Cheng45e190982006-01-05 00:27:02 +0000872
Evan Cheng2a330942006-05-25 00:59:30 +0000873 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000874 NodeTys, &Ops[0], Ops.size());
Evan Cheng88decde2006-04-28 21:29:37 +0000875 InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000876
Chris Lattner8be5be82006-05-23 18:50:38 +0000877 // Create the CALLSEQ_END node.
878 unsigned NumBytesForCalleeToPush = 0;
879
Chris Lattner7802f3e2007-02-25 09:06:15 +0000880 if (CC == CallingConv::X86_StdCall) {
881 if (isVarArg)
Chris Lattner5958b172007-02-28 05:39:26 +0000882 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner7802f3e2007-02-25 09:06:15 +0000883 else
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000884 NumBytesForCalleeToPush = NumBytes;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000885 } else {
886 // If this is is a call to a struct-return function, the callee
887 // pops the hidden struct pointer, so we have to push it back.
888 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattner5958b172007-02-28 05:39:26 +0000889 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000890 }
891
Chris Lattnerd6b853ad2007-02-25 07:18:38 +0000892 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000893 Ops.clear();
894 Ops.push_back(Chain);
895 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8be5be82006-05-23 18:50:38 +0000896 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +0000897 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +0000898 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner0cd99602007-02-25 08:59:22 +0000899 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +0000900
Chris Lattner0cd99602007-02-25 08:59:22 +0000901 // Handle result values, copying them out of physregs into vregs that we
902 // return.
Chris Lattner7802f3e2007-02-25 09:06:15 +0000903 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +0000904}
905
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000906
907//===----------------------------------------------------------------------===//
Chris Lattner3066bec2007-02-28 06:10:12 +0000908// FastCall Calling Convention implementation
Chris Lattner76ac0682005-11-15 00:40:23 +0000909//===----------------------------------------------------------------------===//
910//
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000911// The X86 'fastcall' calling convention passes up to two integer arguments in
912// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
913// and requires that the callee pop its arguments off the stack (allowing proper
914// tail calls), and has the same return value conventions as C calling convs.
915//
916// This calling convention always arranges for the callee pop value to be 8n+4
917// bytes, which is needed for tail recursion elimination and stack alignment
918// reasons.
Evan Cheng17e734f2006-05-23 21:06:34 +0000919SDOperand
Chris Lattner3ed3be32007-02-28 06:05:16 +0000920X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000921 MachineFunction &MF = DAG.getMachineFunction();
922 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng17e734f2006-05-23 21:06:34 +0000923 SDOperand Root = Op.getOperand(0);
Chris Lattner944200b2007-06-19 00:13:10 +0000924 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattner76ac0682005-11-15 00:40:23 +0000925
Chris Lattner227b6c52007-02-28 07:00:42 +0000926 // Assign locations to all of the incoming arguments.
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000927 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +0000928 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
929 getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +0000930 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000931
932 SmallVector<SDOperand, 8> ArgValues;
933 unsigned LastVal = ~0U;
934 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
935 CCValAssign &VA = ArgLocs[i];
936 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
937 // places.
938 assert(VA.getValNo() != LastVal &&
939 "Don't support value assigned to multiple locs yet");
940 LastVal = VA.getValNo();
941
942 if (VA.isRegLoc()) {
943 MVT::ValueType RegVT = VA.getLocVT();
944 TargetRegisterClass *RC;
945 if (RegVT == MVT::i32)
946 RC = X86::GR32RegisterClass;
947 else {
948 assert(MVT::isVector(RegVT));
949 RC = X86::VR128RegisterClass;
950 }
951
Chris Lattner9c7e5e32007-03-02 05:12:29 +0000952 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
953 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000954
955 // If this is an 8 or 16-bit value, it is really passed promoted to 32
956 // bits. Insert an assert[sz]ext to capture this, then truncate to the
957 // right size.
958 if (VA.getLocInfo() == CCValAssign::SExt)
959 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
960 DAG.getValueType(VA.getValVT()));
961 else if (VA.getLocInfo() == CCValAssign::ZExt)
962 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
963 DAG.getValueType(VA.getValVT()));
964
965 if (VA.getLocInfo() != CCValAssign::Full)
966 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
967
968 ArgValues.push_back(ArgValue);
969 } else {
970 assert(VA.isMemLoc());
971
972 // Create the nodes corresponding to a load from this parameter slot.
973 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
974 VA.getLocMemOffset());
975 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
976 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
977 }
978 }
979
Evan Cheng17e734f2006-05-23 21:06:34 +0000980 ArgValues.push_back(Root);
981
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000982 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000983
Anton Korobeynikov57af2a42007-03-02 21:50:27 +0000984 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +0000985 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
986 // arguments and the arguments after the retaddr has been pushed are aligned.
987 if ((StackSize & 7) == 0)
988 StackSize += 4;
989 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000990
991 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000992 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattner76ac0682005-11-15 00:40:23 +0000993 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner66e1d1d2007-02-28 06:21:19 +0000994 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattner76ac0682005-11-15 00:40:23 +0000995 BytesCallerReserves = 0;
996
Chris Lattnerff0598d2007-04-17 17:21:52 +0000997 MF.getInfo<X86MachineFunctionInfo>()
998 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikov037c8672007-01-28 13:31:35 +0000999
Evan Cheng17e734f2006-05-23 21:06:34 +00001000 // Return the new list of results.
Chris Lattner35a08552007-02-25 07:10:00 +00001001 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner29478082007-02-26 07:50:02 +00001002 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001003}
1004
Chris Lattner104aa5d2006-09-26 03:57:53 +00001005SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner7802f3e2007-02-25 09:06:15 +00001006 unsigned CC) {
Evan Cheng2a330942006-05-25 00:59:30 +00001007 SDOperand Chain = Op.getOperand(0);
Evan Cheng2a330942006-05-25 00:59:30 +00001008 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner944200b2007-06-19 00:13:10 +00001009 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng2a330942006-05-25 00:59:30 +00001010 SDOperand Callee = Op.getOperand(4);
Evan Cheng2a330942006-05-25 00:59:30 +00001011
Chris Lattner227b6c52007-02-28 07:00:42 +00001012 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerd439e862007-02-28 06:26:33 +00001013 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001014 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001015 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerd439e862007-02-28 06:26:33 +00001016
1017 // Get a count of how many bytes are to be pushed on the stack.
1018 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattner76ac0682005-11-15 00:40:23 +00001019
Anton Korobeynikov57af2a42007-03-02 21:50:27 +00001020 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikovaf8be442007-03-01 16:29:22 +00001021 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1022 // arguments and the arguments after the retaddr has been pushed are aligned.
1023 if ((NumBytes & 7) == 0)
1024 NumBytes += 4;
1025 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001026
Chris Lattner62c34842006-02-13 09:00:43 +00001027 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerd439e862007-02-28 06:26:33 +00001028
Chris Lattner35a08552007-02-25 07:10:00 +00001029 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1030 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerd439e862007-02-28 06:26:33 +00001031
1032 SDOperand StackPtr;
1033
1034 // Walk the register/memloc assignments, inserting copies/loads.
1035 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1036 CCValAssign &VA = ArgLocs[i];
1037 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1038
1039 // Promote the value if needed.
1040 switch (VA.getLocInfo()) {
1041 default: assert(0 && "Unknown loc info!");
1042 case CCValAssign::Full: break;
1043 case CCValAssign::SExt:
1044 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner3ed3be32007-02-28 06:05:16 +00001045 break;
Chris Lattnerd439e862007-02-28 06:26:33 +00001046 case CCValAssign::ZExt:
1047 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1048 break;
1049 case CCValAssign::AExt:
1050 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1051 break;
1052 }
1053
1054 if (VA.isRegLoc()) {
1055 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1056 } else {
1057 assert(VA.isMemLoc());
1058 if (StackPtr.Val == 0)
1059 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1060 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Evan Cheng2a330942006-05-25 00:59:30 +00001061 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Chengab51cf22006-10-13 21:14:26 +00001062 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng2a330942006-05-25 00:59:30 +00001063 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001064 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001065
Evan Cheng2a330942006-05-25 00:59:30 +00001066 if (!MemOpChains.empty())
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001067 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1068 &MemOpChains[0], MemOpChains.size());
Chris Lattner76ac0682005-11-15 00:40:23 +00001069
Nate Begeman7e5496d2006-02-17 00:03:04 +00001070 // Build a sequence of copy-to-reg nodes chained together with token chain
1071 // and flag operands which copy the outgoing args into registers.
1072 SDOperand InFlag;
Evan Cheng2a330942006-05-25 00:59:30 +00001073 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1074 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1075 InFlag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001076 InFlag = Chain.getValue(1);
1077 }
1078
Evan Cheng2a330942006-05-25 00:59:30 +00001079 // If the callee is a GlobalAddress node (quite common, every direct call is)
1080 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001081 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00001082 // We should use extra load for direct calls to dllimported functions in
1083 // non-JIT mode.
1084 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1085 getTargetMachine(), true))
Anton Korobeynikov37d080b2006-11-20 10:46:14 +00001086 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1087 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng2a330942006-05-25 00:59:30 +00001088 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1089
Evan Cheng84a041e2007-02-21 21:18:14 +00001090 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1091 // GOT pointer.
Anton Korobeynikov037c8672007-01-28 13:31:35 +00001092 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1093 Subtarget->isPICStyleGOT()) {
1094 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1095 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1096 InFlag);
1097 InFlag = Chain.getValue(1);
1098 }
1099
Chris Lattnere56fef92007-02-25 06:40:16 +00001100 // Returns a chain & a flag for retval copy to use.
1101 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00001102 SmallVector<SDOperand, 8> Ops;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001103 Ops.push_back(Chain);
1104 Ops.push_back(Callee);
Evan Chengca254862006-06-14 18:17:40 +00001105
1106 // Add argument registers to the end of the list so that they are known live
1107 // into the call.
1108 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001109 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengca254862006-06-14 18:17:40 +00001110 RegsToPass[i].second.getValueType()));
1111
Evan Cheng84a041e2007-02-21 21:18:14 +00001112 // Add an implicit use GOT pointer in EBX.
1113 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1114 Subtarget->isPICStyleGOT())
1115 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1116
Nate Begeman7e5496d2006-02-17 00:03:04 +00001117 if (InFlag.Val)
1118 Ops.push_back(InFlag);
1119
1120 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner3d826992006-05-16 06:45:34 +00001121 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001122 NodeTys, &Ops[0], Ops.size());
Nate Begeman7e5496d2006-02-17 00:03:04 +00001123 InFlag = Chain.getValue(1);
1124
Chris Lattnerd6b853ad2007-02-25 07:18:38 +00001125 // Returns a flag for retval copy to use.
1126 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman7e5496d2006-02-17 00:03:04 +00001127 Ops.clear();
1128 Ops.push_back(Chain);
Evan Cheng2a330942006-05-25 00:59:30 +00001129 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1130 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman7e5496d2006-02-17 00:03:04 +00001131 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00001132 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattnerba474f52007-02-25 09:10:05 +00001133 InFlag = Chain.getValue(1);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001134
Chris Lattnerba474f52007-02-25 09:10:05 +00001135 // Handle result values, copying them out of physregs into vregs that we
1136 // return.
1137 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattner76ac0682005-11-15 00:40:23 +00001138}
1139
Chris Lattner3066bec2007-02-28 06:10:12 +00001140
1141//===----------------------------------------------------------------------===//
1142// X86-64 C Calling Convention implementation
1143//===----------------------------------------------------------------------===//
1144
1145SDOperand
1146X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner3066bec2007-02-28 06:10:12 +00001147 MachineFunction &MF = DAG.getMachineFunction();
1148 MachineFrameInfo *MFI = MF.getFrameInfo();
1149 SDOperand Root = Op.getOperand(0);
1150 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1151
1152 static const unsigned GPR64ArgRegs[] = {
1153 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1154 };
1155 static const unsigned XMMArgRegs[] = {
1156 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1157 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1158 };
1159
Chris Lattner227b6c52007-02-28 07:00:42 +00001160
1161 // Assign locations to all of the incoming arguments.
Chris Lattner3066bec2007-02-28 06:10:12 +00001162 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001163 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1164 getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001165 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001166
1167 SmallVector<SDOperand, 8> ArgValues;
1168 unsigned LastVal = ~0U;
1169 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1170 CCValAssign &VA = ArgLocs[i];
1171 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1172 // places.
1173 assert(VA.getValNo() != LastVal &&
1174 "Don't support value assigned to multiple locs yet");
1175 LastVal = VA.getValNo();
1176
1177 if (VA.isRegLoc()) {
1178 MVT::ValueType RegVT = VA.getLocVT();
1179 TargetRegisterClass *RC;
1180 if (RegVT == MVT::i32)
1181 RC = X86::GR32RegisterClass;
1182 else if (RegVT == MVT::i64)
1183 RC = X86::GR64RegisterClass;
1184 else if (RegVT == MVT::f32)
1185 RC = X86::FR32RegisterClass;
1186 else if (RegVT == MVT::f64)
1187 RC = X86::FR64RegisterClass;
1188 else {
1189 assert(MVT::isVector(RegVT));
Chris Lattner75372ad2007-06-09 05:08:10 +00001190 if (MVT::getSizeInBits(RegVT) == 64) {
1191 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1192 RegVT = MVT::i64;
1193 } else
Chris Lattnera4a49e32007-06-09 05:01:50 +00001194 RC = X86::VR128RegisterClass;
Chris Lattner3066bec2007-02-28 06:10:12 +00001195 }
Chris Lattner9c7e5e32007-03-02 05:12:29 +00001196
1197 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1198 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattner3066bec2007-02-28 06:10:12 +00001199
1200 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1201 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1202 // right size.
1203 if (VA.getLocInfo() == CCValAssign::SExt)
1204 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1205 DAG.getValueType(VA.getValVT()));
1206 else if (VA.getLocInfo() == CCValAssign::ZExt)
1207 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1208 DAG.getValueType(VA.getValVT()));
1209
1210 if (VA.getLocInfo() != CCValAssign::Full)
1211 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1212
Chris Lattner75372ad2007-06-09 05:08:10 +00001213 // Handle MMX values passed in GPRs.
1214 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1215 MVT::getSizeInBits(RegVT) == 64)
1216 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1217
Chris Lattner3066bec2007-02-28 06:10:12 +00001218 ArgValues.push_back(ArgValue);
1219 } else {
1220 assert(VA.isMemLoc());
1221
1222 // Create the nodes corresponding to a load from this parameter slot.
1223 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1224 VA.getLocMemOffset());
1225 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1226 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1227 }
1228 }
1229
1230 unsigned StackSize = CCInfo.getNextStackOffset();
1231
1232 // If the function takes variable number of arguments, make a frame index for
1233 // the start of the first vararg value... for expansion of llvm.va_start.
1234 if (isVarArg) {
1235 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1236 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1237
1238 // For X86-64, if there are vararg parameters that are passed via
1239 // registers, then we must store them to their spots on the stack so they
1240 // may be loaded by deferencing the result of va_next.
1241 VarArgsGPOffset = NumIntRegs * 8;
1242 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1243 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1244 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1245
1246 // Store the integer parameter registers.
1247 SmallVector<SDOperand, 8> MemOps;
1248 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1249 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1250 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1251 for (; NumIntRegs != 6; ++NumIntRegs) {
1252 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1253 X86::GR64RegisterClass);
1254 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1255 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1256 MemOps.push_back(Store);
1257 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1258 DAG.getConstant(8, getPointerTy()));
1259 }
1260
1261 // Now store the XMM (fp + vector) parameter registers.
1262 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1263 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1264 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1265 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1266 X86::VR128RegisterClass);
1267 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1268 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1269 MemOps.push_back(Store);
1270 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1271 DAG.getConstant(16, getPointerTy()));
1272 }
1273 if (!MemOps.empty())
1274 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1275 &MemOps[0], MemOps.size());
1276 }
1277
1278 ArgValues.push_back(Root);
1279
1280 ReturnAddrIndex = 0; // No return address slot generated yet.
1281 BytesToPopOnReturn = 0; // Callee pops nothing.
1282 BytesCallerReserves = StackSize;
1283
1284 // Return the new list of results.
1285 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1286 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1287}
1288
1289SDOperand
1290X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1291 unsigned CC) {
1292 SDOperand Chain = Op.getOperand(0);
1293 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1294 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1295 SDOperand Callee = Op.getOperand(4);
Chris Lattner227b6c52007-02-28 07:00:42 +00001296
1297 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner3066bec2007-02-28 06:10:12 +00001298 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner944200b2007-06-19 00:13:10 +00001299 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner227b6c52007-02-28 07:00:42 +00001300 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattner3066bec2007-02-28 06:10:12 +00001301
1302 // Get a count of how many bytes are to be pushed on the stack.
1303 unsigned NumBytes = CCInfo.getNextStackOffset();
1304 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1305
1306 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1307 SmallVector<SDOperand, 8> MemOpChains;
1308
1309 SDOperand StackPtr;
1310
1311 // Walk the register/memloc assignments, inserting copies/loads.
1312 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1313 CCValAssign &VA = ArgLocs[i];
1314 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1315
1316 // Promote the value if needed.
1317 switch (VA.getLocInfo()) {
1318 default: assert(0 && "Unknown loc info!");
1319 case CCValAssign::Full: break;
1320 case CCValAssign::SExt:
1321 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1322 break;
1323 case CCValAssign::ZExt:
1324 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1325 break;
1326 case CCValAssign::AExt:
1327 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1328 break;
1329 }
1330
1331 if (VA.isRegLoc()) {
1332 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1333 } else {
1334 assert(VA.isMemLoc());
1335 if (StackPtr.Val == 0)
1336 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1337 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1338 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1339 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1340 }
1341 }
1342
1343 if (!MemOpChains.empty())
1344 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1345 &MemOpChains[0], MemOpChains.size());
1346
1347 // Build a sequence of copy-to-reg nodes chained together with token chain
1348 // and flag operands which copy the outgoing args into registers.
1349 SDOperand InFlag;
1350 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1351 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1352 InFlag);
1353 InFlag = Chain.getValue(1);
1354 }
1355
1356 if (isVarArg) {
1357 // From AMD64 ABI document:
1358 // For calls that may call functions that use varargs or stdargs
1359 // (prototype-less calls or calls to functions containing ellipsis (...) in
1360 // the declaration) %al is used as hidden argument to specify the number
1361 // of SSE registers used. The contents of %al do not need to match exactly
1362 // the number of registers, but must be an ubound on the number of SSE
1363 // registers used and is in the range 0 - 8 inclusive.
1364
1365 // Count the number of XMM registers allocated.
1366 static const unsigned XMMArgRegs[] = {
1367 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1368 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1369 };
1370 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1371
1372 Chain = DAG.getCopyToReg(Chain, X86::AL,
1373 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1374 InFlag = Chain.getValue(1);
1375 }
1376
1377 // If the callee is a GlobalAddress node (quite common, every direct call is)
1378 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1379 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1380 // We should use extra load for direct calls to dllimported functions in
1381 // non-JIT mode.
Evan Chenga1779b92007-03-14 22:11:11 +00001382 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001383 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1384 getTargetMachine(), true))
Chris Lattner3066bec2007-02-28 06:10:12 +00001385 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1386 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chenga1779b92007-03-14 22:11:11 +00001387 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1388 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattner3066bec2007-02-28 06:10:12 +00001389
1390 // Returns a chain & a flag for retval copy to use.
1391 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1392 SmallVector<SDOperand, 8> Ops;
1393 Ops.push_back(Chain);
1394 Ops.push_back(Callee);
1395
1396 // Add argument registers to the end of the list so that they are known live
1397 // into the call.
1398 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1399 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1400 RegsToPass[i].second.getValueType()));
1401
1402 if (InFlag.Val)
1403 Ops.push_back(InFlag);
1404
1405 // FIXME: Do not generate X86ISD::TAILCALL for now.
1406 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1407 NodeTys, &Ops[0], Ops.size());
1408 InFlag = Chain.getValue(1);
1409
1410 // Returns a flag for retval copy to use.
1411 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1412 Ops.clear();
1413 Ops.push_back(Chain);
1414 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1415 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1416 Ops.push_back(InFlag);
1417 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1418 InFlag = Chain.getValue(1);
1419
1420 // Handle result values, copying them out of physregs into vregs that we
1421 // return.
1422 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1423}
1424
1425
1426//===----------------------------------------------------------------------===//
1427// Other Lowering Hooks
1428//===----------------------------------------------------------------------===//
1429
1430
Chris Lattner76ac0682005-11-15 00:40:23 +00001431SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1432 if (ReturnAddrIndex == 0) {
1433 // Set up a frame object for the return address.
1434 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001435 if (Subtarget->is64Bit())
1436 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1437 else
1438 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattner76ac0682005-11-15 00:40:23 +00001439 }
1440
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001441 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattner76ac0682005-11-15 00:40:23 +00001442}
1443
1444
1445
Evan Cheng45df7f82006-01-30 23:41:35 +00001446/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1447/// specific condition code. It returns a false if it cannot do a direct
Chris Lattner7a627672006-09-13 03:22:10 +00001448/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1449/// needed.
Evan Cheng78038292006-04-05 23:38:46 +00001450static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattner7a627672006-09-13 03:22:10 +00001451 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1452 SelectionDAG &DAG) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001453 X86CC = X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001454 if (!isFP) {
Chris Lattner971e3392006-09-13 17:04:54 +00001455 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1456 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1457 // X > -1 -> X == 0, jump !sign.
1458 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001459 X86CC = X86::COND_NS;
Chris Lattner971e3392006-09-13 17:04:54 +00001460 return true;
1461 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1462 // X < 0 -> X == 0, jump on sign.
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001463 X86CC = X86::COND_S;
Chris Lattner971e3392006-09-13 17:04:54 +00001464 return true;
1465 }
Chris Lattner7a627672006-09-13 03:22:10 +00001466 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00001467
Evan Cheng172fce72006-01-06 00:43:03 +00001468 switch (SetCCOpcode) {
1469 default: break;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001470 case ISD::SETEQ: X86CC = X86::COND_E; break;
1471 case ISD::SETGT: X86CC = X86::COND_G; break;
1472 case ISD::SETGE: X86CC = X86::COND_GE; break;
1473 case ISD::SETLT: X86CC = X86::COND_L; break;
1474 case ISD::SETLE: X86CC = X86::COND_LE; break;
1475 case ISD::SETNE: X86CC = X86::COND_NE; break;
1476 case ISD::SETULT: X86CC = X86::COND_B; break;
1477 case ISD::SETUGT: X86CC = X86::COND_A; break;
1478 case ISD::SETULE: X86CC = X86::COND_BE; break;
1479 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001480 }
1481 } else {
1482 // On a floating point condition, the flags are set as follows:
1483 // ZF PF CF op
1484 // 0 | 0 | 0 | X > Y
1485 // 0 | 0 | 1 | X < Y
1486 // 1 | 0 | 0 | X == Y
1487 // 1 | 1 | 1 | unordered
Chris Lattner7a627672006-09-13 03:22:10 +00001488 bool Flip = false;
Evan Cheng172fce72006-01-06 00:43:03 +00001489 switch (SetCCOpcode) {
1490 default: break;
1491 case ISD::SETUEQ:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001492 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001493 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001494 case ISD::SETOGT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001495 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001496 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001497 case ISD::SETOGE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001498 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001499 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001500 case ISD::SETULT:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001501 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001502 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001503 case ISD::SETULE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001504 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001505 case ISD::SETONE:
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001506 case ISD::SETNE: X86CC = X86::COND_NE; break;
1507 case ISD::SETUO: X86CC = X86::COND_P; break;
1508 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Cheng172fce72006-01-06 00:43:03 +00001509 }
Chris Lattner7a627672006-09-13 03:22:10 +00001510 if (Flip)
1511 std::swap(LHS, RHS);
Evan Cheng172fce72006-01-06 00:43:03 +00001512 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001513
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001514 return X86CC != X86::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001515}
1516
Evan Cheng339edad2006-01-11 00:33:36 +00001517/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1518/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001519/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001520static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001521 switch (X86CC) {
1522 default:
1523 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001524 case X86::COND_B:
1525 case X86::COND_BE:
1526 case X86::COND_E:
1527 case X86::COND_P:
1528 case X86::COND_A:
1529 case X86::COND_AE:
1530 case X86::COND_NE:
1531 case X86::COND_NP:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001532 return true;
1533 }
1534}
1535
Evan Chengc995b452006-04-06 23:23:56 +00001536/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001537/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001538static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1539 if (Op.getOpcode() == ISD::UNDEF)
1540 return true;
1541
1542 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001543 return (Val >= Low && Val < Hi);
1544}
1545
1546/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1547/// true if Op is undef or if its value equal to the specified value.
1548static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1549 if (Op.getOpcode() == ISD::UNDEF)
1550 return true;
1551 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001552}
1553
Evan Cheng68ad48b2006-03-22 18:59:22 +00001554/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1555/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1556bool X86::isPSHUFDMask(SDNode *N) {
1557 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1558
1559 if (N->getNumOperands() != 4)
1560 return false;
1561
1562 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001563 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001564 SDOperand Arg = N->getOperand(i);
1565 if (Arg.getOpcode() == ISD::UNDEF) continue;
1566 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1567 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001568 return false;
1569 }
1570
1571 return true;
1572}
1573
1574/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001575/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001576bool X86::isPSHUFHWMask(SDNode *N) {
1577 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1578
1579 if (N->getNumOperands() != 8)
1580 return false;
1581
1582 // Lower quadword copied in order.
1583 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001584 SDOperand Arg = N->getOperand(i);
1585 if (Arg.getOpcode() == ISD::UNDEF) continue;
1586 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1587 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001588 return false;
1589 }
1590
1591 // Upper quadword shuffled.
1592 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001593 SDOperand Arg = N->getOperand(i);
1594 if (Arg.getOpcode() == ISD::UNDEF) continue;
1595 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1596 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001597 if (Val < 4 || Val > 7)
1598 return false;
1599 }
1600
1601 return true;
1602}
1603
1604/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001605/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001606bool X86::isPSHUFLWMask(SDNode *N) {
1607 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1608
1609 if (N->getNumOperands() != 8)
1610 return false;
1611
1612 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001613 for (unsigned i = 4; i != 8; ++i)
1614 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001615 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001616
1617 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001618 for (unsigned i = 0; i != 4; ++i)
1619 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001620 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001621
1622 return true;
1623}
1624
Evan Chengd27fb3e2006-03-24 01:18:28 +00001625/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1626/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner35a08552007-02-25 07:10:00 +00001627static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001628 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001629
Evan Cheng60f0b892006-04-20 08:58:49 +00001630 unsigned Half = NumElems / 2;
1631 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001632 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng60f0b892006-04-20 08:58:49 +00001633 return false;
1634 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001635 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00001636 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001637
1638 return true;
1639}
1640
Evan Cheng60f0b892006-04-20 08:58:49 +00001641bool X86::isSHUFPMask(SDNode *N) {
1642 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001643 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001644}
1645
Evan Chengafa1cb62007-05-17 18:45:50 +00001646/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng60f0b892006-04-20 08:58:49 +00001647/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1648/// half elements to come from vector 1 (which would equal the dest.) and
1649/// the upper half to come from vector 2.
Chris Lattner35a08552007-02-25 07:10:00 +00001650static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1651 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001652
Chris Lattner35a08552007-02-25 07:10:00 +00001653 unsigned Half = NumOps / 2;
Evan Cheng60f0b892006-04-20 08:58:49 +00001654 for (unsigned i = 0; i < Half; ++i)
Chris Lattner35a08552007-02-25 07:10:00 +00001655 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng60f0b892006-04-20 08:58:49 +00001656 return false;
Chris Lattner35a08552007-02-25 07:10:00 +00001657 for (unsigned i = Half; i < NumOps; ++i)
1658 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng60f0b892006-04-20 08:58:49 +00001659 return false;
1660 return true;
1661}
1662
1663static bool isCommutedSHUFP(SDNode *N) {
1664 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001665 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001666}
1667
Evan Cheng2595a682006-03-24 02:58:06 +00001668/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1669/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1670bool X86::isMOVHLPSMask(SDNode *N) {
1671 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1672
Evan Cheng1a194a52006-03-28 06:50:32 +00001673 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001674 return false;
1675
Evan Cheng1a194a52006-03-28 06:50:32 +00001676 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001677 return isUndefOrEqual(N->getOperand(0), 6) &&
1678 isUndefOrEqual(N->getOperand(1), 7) &&
1679 isUndefOrEqual(N->getOperand(2), 2) &&
1680 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001681}
1682
Evan Cheng922e1912006-11-07 22:14:24 +00001683/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1684/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1685/// <2, 3, 2, 3>
1686bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1687 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1688
1689 if (N->getNumOperands() != 4)
1690 return false;
1691
1692 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1693 return isUndefOrEqual(N->getOperand(0), 2) &&
1694 isUndefOrEqual(N->getOperand(1), 3) &&
1695 isUndefOrEqual(N->getOperand(2), 2) &&
1696 isUndefOrEqual(N->getOperand(3), 3);
1697}
1698
Evan Chengc995b452006-04-06 23:23:56 +00001699/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1700/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1701bool X86::isMOVLPMask(SDNode *N) {
1702 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1703
1704 unsigned NumElems = N->getNumOperands();
1705 if (NumElems != 2 && NumElems != 4)
1706 return false;
1707
Evan Chengac847262006-04-07 21:53:05 +00001708 for (unsigned i = 0; i < NumElems/2; ++i)
1709 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1710 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001711
Evan Chengac847262006-04-07 21:53:05 +00001712 for (unsigned i = NumElems/2; i < NumElems; ++i)
1713 if (!isUndefOrEqual(N->getOperand(i), i))
1714 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001715
1716 return true;
1717}
1718
1719/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001720/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1721/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001722bool X86::isMOVHPMask(SDNode *N) {
1723 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1724
1725 unsigned NumElems = N->getNumOperands();
1726 if (NumElems != 2 && NumElems != 4)
1727 return false;
1728
Evan Chengac847262006-04-07 21:53:05 +00001729 for (unsigned i = 0; i < NumElems/2; ++i)
1730 if (!isUndefOrEqual(N->getOperand(i), i))
1731 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001732
1733 for (unsigned i = 0; i < NumElems/2; ++i) {
1734 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001735 if (!isUndefOrEqual(Arg, i + NumElems))
1736 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001737 }
1738
1739 return true;
1740}
1741
Evan Cheng5df75882006-03-28 00:39:58 +00001742/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1743/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner35a08552007-02-25 07:10:00 +00001744bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1745 bool V2IsSplat = false) {
1746 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng5df75882006-03-28 00:39:58 +00001747 return false;
1748
Chris Lattner35a08552007-02-25 07:10:00 +00001749 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1750 SDOperand BitI = Elts[i];
1751 SDOperand BitI1 = Elts[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001752 if (!isUndefOrEqual(BitI, j))
1753 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001754 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00001755 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001756 return false;
1757 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00001758 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001759 return false;
1760 }
Evan Cheng5df75882006-03-28 00:39:58 +00001761 }
1762
1763 return true;
1764}
1765
Evan Cheng60f0b892006-04-20 08:58:49 +00001766bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1767 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001768 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001769}
1770
Evan Cheng2bc32802006-03-28 02:43:26 +00001771/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1772/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner35a08552007-02-25 07:10:00 +00001773bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1774 bool V2IsSplat = false) {
1775 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng2bc32802006-03-28 02:43:26 +00001776 return false;
1777
Chris Lattner35a08552007-02-25 07:10:00 +00001778 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1779 SDOperand BitI = Elts[i];
1780 SDOperand BitI1 = Elts[i+1];
1781 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengac847262006-04-07 21:53:05 +00001782 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001783 if (V2IsSplat) {
Chris Lattner35a08552007-02-25 07:10:00 +00001784 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001785 return false;
1786 } else {
Chris Lattner35a08552007-02-25 07:10:00 +00001787 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng60f0b892006-04-20 08:58:49 +00001788 return false;
1789 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001790 }
1791
1792 return true;
1793}
1794
Evan Cheng60f0b892006-04-20 08:58:49 +00001795bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1796 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001797 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001798}
1799
Evan Chengf3b52c82006-04-05 07:20:06 +00001800/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1801/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1802/// <0, 0, 1, 1>
1803bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1804 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1805
1806 unsigned NumElems = N->getNumOperands();
Bill Wendling591eab82007-04-24 21:16:55 +00001807 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengf3b52c82006-04-05 07:20:06 +00001808 return false;
1809
1810 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1811 SDOperand BitI = N->getOperand(i);
1812 SDOperand BitI1 = N->getOperand(i+1);
1813
Evan Chengac847262006-04-07 21:53:05 +00001814 if (!isUndefOrEqual(BitI, j))
1815 return false;
1816 if (!isUndefOrEqual(BitI1, j))
1817 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001818 }
1819
1820 return true;
1821}
1822
Bill Wendling591eab82007-04-24 21:16:55 +00001823/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1824/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1825/// <2, 2, 3, 3>
1826bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1827 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1828
1829 unsigned NumElems = N->getNumOperands();
1830 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1831 return false;
1832
1833 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1834 SDOperand BitI = N->getOperand(i);
1835 SDOperand BitI1 = N->getOperand(i + 1);
1836
1837 if (!isUndefOrEqual(BitI, j))
1838 return false;
1839 if (!isUndefOrEqual(BitI1, j))
1840 return false;
1841 }
1842
1843 return true;
1844}
1845
Evan Chenge8b51802006-04-21 01:05:10 +00001846/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1847/// specifies a shuffle of elements that is suitable for input to MOVSS,
1848/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner35a08552007-02-25 07:10:00 +00001849static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1850 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001851 return false;
1852
Chris Lattner35a08552007-02-25 07:10:00 +00001853 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001854 return false;
1855
Chris Lattner35a08552007-02-25 07:10:00 +00001856 for (unsigned i = 1; i < NumElts; ++i) {
1857 if (!isUndefOrEqual(Elts[i], i))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001858 return false;
1859 }
1860
1861 return true;
1862}
Evan Chengf3b52c82006-04-05 07:20:06 +00001863
Evan Chenge8b51802006-04-21 01:05:10 +00001864bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001865 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001866 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng60f0b892006-04-20 08:58:49 +00001867}
1868
Evan Chenge8b51802006-04-21 01:05:10 +00001869/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1870/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001871/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner35a08552007-02-25 07:10:00 +00001872static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1873 bool V2IsSplat = false,
Evan Cheng89c5d042006-09-08 01:50:06 +00001874 bool V2IsUndef = false) {
Chris Lattner35a08552007-02-25 07:10:00 +00001875 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001876 return false;
1877
1878 if (!isUndefOrEqual(Ops[0], 0))
1879 return false;
1880
Chris Lattner35a08552007-02-25 07:10:00 +00001881 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001882 SDOperand Arg = Ops[i];
Chris Lattner35a08552007-02-25 07:10:00 +00001883 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1884 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1885 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng89c5d042006-09-08 01:50:06 +00001886 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001887 }
1888
1889 return true;
1890}
1891
Evan Cheng89c5d042006-09-08 01:50:06 +00001892static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1893 bool V2IsUndef = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001894 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner35a08552007-02-25 07:10:00 +00001895 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1896 V2IsSplat, V2IsUndef);
Evan Cheng60f0b892006-04-20 08:58:49 +00001897}
1898
Evan Cheng5d247f82006-04-14 21:59:03 +00001899/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1900/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1901bool X86::isMOVSHDUPMask(SDNode *N) {
1902 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1903
1904 if (N->getNumOperands() != 4)
1905 return false;
1906
1907 // Expect 1, 1, 3, 3
1908 for (unsigned i = 0; i < 2; ++i) {
1909 SDOperand Arg = N->getOperand(i);
1910 if (Arg.getOpcode() == ISD::UNDEF) continue;
1911 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1912 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1913 if (Val != 1) return false;
1914 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001915
1916 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001917 for (unsigned i = 2; i < 4; ++i) {
1918 SDOperand Arg = N->getOperand(i);
1919 if (Arg.getOpcode() == ISD::UNDEF) continue;
1920 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1921 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1922 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001923 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001924 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001925
Evan Cheng6222cf22006-04-15 05:37:34 +00001926 // Don't use movshdup if it can be done with a shufps.
1927 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001928}
1929
1930/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1931/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1932bool X86::isMOVSLDUPMask(SDNode *N) {
1933 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1934
1935 if (N->getNumOperands() != 4)
1936 return false;
1937
1938 // Expect 0, 0, 2, 2
1939 for (unsigned i = 0; i < 2; ++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 != 0) return false;
1945 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001946
1947 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001948 for (unsigned i = 2; i < 4; ++i) {
1949 SDOperand Arg = N->getOperand(i);
1950 if (Arg.getOpcode() == ISD::UNDEF) continue;
1951 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1952 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1953 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001954 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001955 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001956
Evan Cheng6222cf22006-04-15 05:37:34 +00001957 // Don't use movshdup if it can be done with a shufps.
1958 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001959}
1960
Evan Chengcea02ff2007-06-19 00:02:56 +00001961/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
1962/// specifies a identity operation on the LHS or RHS.
1963static bool isIdentityMask(SDNode *N, bool RHS = false) {
1964 unsigned NumElems = N->getNumOperands();
1965 for (unsigned i = 0; i < NumElems; ++i)
1966 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
1967 return false;
1968 return true;
1969}
1970
Evan Chengd097e672006-03-22 02:53:00 +00001971/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1972/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001973static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001974 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1975
Evan Chengd097e672006-03-22 02:53:00 +00001976 // This is a splat operation if each element of the permute is the same, and
1977 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001978 unsigned NumElems = N->getNumOperands();
1979 SDOperand ElementBase;
1980 unsigned i = 0;
1981 for (; i != NumElems; ++i) {
1982 SDOperand Elt = N->getOperand(i);
Reid Spencerde46e482006-11-02 20:25:50 +00001983 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001984 ElementBase = Elt;
1985 break;
1986 }
1987 }
1988
1989 if (!ElementBase.Val)
1990 return false;
1991
1992 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001993 SDOperand Arg = N->getOperand(i);
1994 if (Arg.getOpcode() == ISD::UNDEF) continue;
1995 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001996 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001997 }
1998
1999 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002000 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00002001}
2002
Evan Cheng5022b342006-04-17 20:43:08 +00002003/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2004/// a splat of a single element and it's a 2 or 4 element mask.
2005bool X86::isSplatMask(SDNode *N) {
2006 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2007
Evan Cheng4a1b0d32006-04-19 23:28:59 +00002008 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00002009 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2010 return false;
2011 return ::isSplatMask(N);
2012}
2013
Evan Chenge056dd52006-10-27 21:08:32 +00002014/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2015/// specifies a splat of zero element.
2016bool X86::isSplatLoMask(SDNode *N) {
2017 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2018
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002019 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chenge056dd52006-10-27 21:08:32 +00002020 if (!isUndefOrEqual(N->getOperand(i), 0))
2021 return false;
2022 return true;
2023}
2024
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002025/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2026/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2027/// instructions.
2028unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00002029 unsigned NumOperands = N->getNumOperands();
2030 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2031 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00002032 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002033 unsigned Val = 0;
2034 SDOperand Arg = N->getOperand(NumOperands-i-1);
2035 if (Arg.getOpcode() != ISD::UNDEF)
2036 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00002037 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002038 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00002039 if (i != NumOperands - 1)
2040 Mask <<= Shift;
2041 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00002042
2043 return Mask;
2044}
2045
Evan Chengb7fedff2006-03-29 23:07:14 +00002046/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2047/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2048/// instructions.
2049unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2050 unsigned Mask = 0;
2051 // 8 nodes, but we only care about the last 4.
2052 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002053 unsigned Val = 0;
2054 SDOperand Arg = N->getOperand(i);
2055 if (Arg.getOpcode() != ISD::UNDEF)
2056 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002057 Mask |= (Val - 4);
2058 if (i != 4)
2059 Mask <<= 2;
2060 }
2061
2062 return Mask;
2063}
2064
2065/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2066/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2067/// instructions.
2068unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2069 unsigned Mask = 0;
2070 // 8 nodes, but we only care about the first 4.
2071 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00002072 unsigned Val = 0;
2073 SDOperand Arg = N->getOperand(i);
2074 if (Arg.getOpcode() != ISD::UNDEF)
2075 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00002076 Mask |= Val;
2077 if (i != 0)
2078 Mask <<= 2;
2079 }
2080
2081 return Mask;
2082}
2083
Evan Cheng59a63552006-04-05 01:47:37 +00002084/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2085/// specifies a 8 element shuffle that can be broken into a pair of
2086/// PSHUFHW and PSHUFLW.
2087static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2088 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2089
2090 if (N->getNumOperands() != 8)
2091 return false;
2092
2093 // Lower quadword shuffled.
2094 for (unsigned i = 0; i != 4; ++i) {
2095 SDOperand Arg = N->getOperand(i);
2096 if (Arg.getOpcode() == ISD::UNDEF) continue;
2097 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2098 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2099 if (Val > 4)
2100 return false;
2101 }
2102
2103 // Upper quadword shuffled.
2104 for (unsigned i = 4; i != 8; ++i) {
2105 SDOperand Arg = N->getOperand(i);
2106 if (Arg.getOpcode() == ISD::UNDEF) continue;
2107 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2108 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2109 if (Val < 4 || Val > 7)
2110 return false;
2111 }
2112
2113 return true;
2114}
2115
Evan Chengc995b452006-04-06 23:23:56 +00002116/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2117/// values in ther permute mask.
Evan Chengc415c5b2006-10-25 21:49:50 +00002118static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2119 SDOperand &V2, SDOperand &Mask,
2120 SelectionDAG &DAG) {
Evan Chengc995b452006-04-06 23:23:56 +00002121 MVT::ValueType VT = Op.getValueType();
2122 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002123 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Chengc995b452006-04-06 23:23:56 +00002124 unsigned NumElems = Mask.getNumOperands();
Chris Lattner35a08552007-02-25 07:10:00 +00002125 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc995b452006-04-06 23:23:56 +00002126
2127 for (unsigned i = 0; i != NumElems; ++i) {
2128 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002129 if (Arg.getOpcode() == ISD::UNDEF) {
2130 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2131 continue;
2132 }
Evan Chengc995b452006-04-06 23:23:56 +00002133 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2134 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2135 if (Val < NumElems)
2136 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2137 else
2138 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2139 }
2140
Evan Chengc415c5b2006-10-25 21:49:50 +00002141 std::swap(V1, V2);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002142 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc415c5b2006-10-25 21:49:50 +00002143 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chengc995b452006-04-06 23:23:56 +00002144}
2145
Evan Cheng7855e4d2006-04-19 20:35:22 +00002146/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2147/// match movhlps. The lower half elements should come from upper half of
2148/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002149/// half of V2 (and in order).
Evan Cheng7855e4d2006-04-19 20:35:22 +00002150static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2151 unsigned NumElems = Mask->getNumOperands();
2152 if (NumElems != 4)
2153 return false;
2154 for (unsigned i = 0, e = 2; i != e; ++i)
2155 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2156 return false;
2157 for (unsigned i = 2; i != 4; ++i)
2158 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2159 return false;
2160 return true;
2161}
2162
Evan Chengc995b452006-04-06 23:23:56 +00002163/// isScalarLoadToVector - Returns true if the node is a scalar load that
2164/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002165static inline bool isScalarLoadToVector(SDNode *N) {
2166 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2167 N = N->getOperand(0).Val;
Evan Chenge71fe34d2006-10-09 20:57:25 +00002168 return ISD::isNON_EXTLoad(N);
Evan Chengc995b452006-04-06 23:23:56 +00002169 }
2170 return false;
2171}
2172
Evan Cheng7855e4d2006-04-19 20:35:22 +00002173/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2174/// match movlp{s|d}. The lower half elements should come from lower half of
2175/// V1 (and in order), and the upper half elements should come from the upper
2176/// half of V2 (and in order). And since V1 will become the source of the
2177/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Chenge646abb2006-10-09 21:39:25 +00002178static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002179 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng7855e4d2006-04-19 20:35:22 +00002180 return false;
Evan Chenge646abb2006-10-09 21:39:25 +00002181 // Is V2 is a vector load, don't do this transformation. We will try to use
2182 // load folding shufps op.
2183 if (ISD::isNON_EXTLoad(V2))
2184 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002185
Evan Cheng7855e4d2006-04-19 20:35:22 +00002186 unsigned NumElems = Mask->getNumOperands();
2187 if (NumElems != 2 && NumElems != 4)
2188 return false;
2189 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2190 if (!isUndefOrEqual(Mask->getOperand(i), i))
2191 return false;
2192 for (unsigned i = NumElems/2; i != NumElems; ++i)
2193 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2194 return false;
2195 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002196}
2197
Evan Cheng60f0b892006-04-20 08:58:49 +00002198/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2199/// all the same.
2200static bool isSplatVector(SDNode *N) {
2201 if (N->getOpcode() != ISD::BUILD_VECTOR)
2202 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002203
Evan Cheng60f0b892006-04-20 08:58:49 +00002204 SDOperand SplatValue = N->getOperand(0);
2205 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2206 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002207 return false;
2208 return true;
2209}
2210
Evan Cheng89c5d042006-09-08 01:50:06 +00002211/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2212/// to an undef.
2213static bool isUndefShuffle(SDNode *N) {
Evan Chengafa1cb62007-05-17 18:45:50 +00002214 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng89c5d042006-09-08 01:50:06 +00002215 return false;
2216
2217 SDOperand V1 = N->getOperand(0);
2218 SDOperand V2 = N->getOperand(1);
2219 SDOperand Mask = N->getOperand(2);
2220 unsigned NumElems = Mask.getNumOperands();
2221 for (unsigned i = 0; i != NumElems; ++i) {
2222 SDOperand Arg = Mask.getOperand(i);
2223 if (Arg.getOpcode() != ISD::UNDEF) {
2224 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2225 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2226 return false;
2227 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2228 return false;
2229 }
2230 }
2231 return true;
2232}
2233
Evan Chengafa1cb62007-05-17 18:45:50 +00002234/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2235/// constant +0.0.
2236static inline bool isZeroNode(SDOperand Elt) {
2237 return ((isa<ConstantSDNode>(Elt) &&
2238 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2239 (isa<ConstantFPSDNode>(Elt) &&
2240 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2241}
2242
2243/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2244/// to an zero vector.
2245static bool isZeroShuffle(SDNode *N) {
2246 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2247 return false;
2248
2249 SDOperand V1 = N->getOperand(0);
2250 SDOperand V2 = N->getOperand(1);
2251 SDOperand Mask = N->getOperand(2);
2252 unsigned NumElems = Mask.getNumOperands();
2253 for (unsigned i = 0; i != NumElems; ++i) {
2254 SDOperand Arg = Mask.getOperand(i);
2255 if (Arg.getOpcode() != ISD::UNDEF) {
2256 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2257 if (Idx < NumElems) {
2258 unsigned Opc = V1.Val->getOpcode();
2259 if (Opc == ISD::UNDEF)
2260 continue;
2261 if (Opc != ISD::BUILD_VECTOR ||
2262 !isZeroNode(V1.Val->getOperand(Idx)))
2263 return false;
2264 } else if (Idx >= NumElems) {
2265 unsigned Opc = V2.Val->getOpcode();
2266 if (Opc == ISD::UNDEF)
2267 continue;
2268 if (Opc != ISD::BUILD_VECTOR ||
2269 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2270 return false;
2271 }
2272 }
2273 }
2274 return true;
2275}
2276
2277/// getZeroVector - Returns a vector of specified type with all zero elements.
2278///
2279static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2280 assert(MVT::isVector(VT) && "Expected a vector type");
Dan Gohman703e0f82007-05-24 14:33:05 +00002281 unsigned NumElems = MVT::getVectorNumElements(VT);
Dan Gohman5c441312007-06-14 22:58:02 +00002282 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Chengafa1cb62007-05-17 18:45:50 +00002283 bool isFP = MVT::isFloatingPoint(EVT);
2284 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2285 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2286 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2287}
2288
Evan Cheng60f0b892006-04-20 08:58:49 +00002289/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2290/// that point to V2 points to its first element.
2291static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2292 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2293
2294 bool Changed = false;
Chris Lattner35a08552007-02-25 07:10:00 +00002295 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002296 unsigned NumElems = Mask.getNumOperands();
2297 for (unsigned i = 0; i != NumElems; ++i) {
2298 SDOperand Arg = Mask.getOperand(i);
2299 if (Arg.getOpcode() != ISD::UNDEF) {
2300 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2301 if (Val > NumElems) {
2302 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2303 Changed = true;
2304 }
2305 }
2306 MaskVec.push_back(Arg);
2307 }
2308
2309 if (Changed)
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002310 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2311 &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002312 return Mask;
2313}
2314
Evan Chenge8b51802006-04-21 01:05:10 +00002315/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2316/// operation of specified width.
2317static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002318 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002319 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng60f0b892006-04-20 08:58:49 +00002320
Chris Lattner35a08552007-02-25 07:10:00 +00002321 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002322 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2323 for (unsigned i = 1; i != NumElems; ++i)
2324 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002325 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002326}
2327
Evan Cheng5022b342006-04-17 20:43:08 +00002328/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2329/// of specified width.
2330static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2331 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002332 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002333 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5022b342006-04-17 20:43:08 +00002334 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2335 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2336 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2337 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002338 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng5022b342006-04-17 20:43:08 +00002339}
2340
Evan Cheng60f0b892006-04-20 08:58:49 +00002341/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2342/// of specified width.
2343static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2344 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002345 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng60f0b892006-04-20 08:58:49 +00002346 unsigned Half = NumElems/2;
Chris Lattner35a08552007-02-25 07:10:00 +00002347 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng60f0b892006-04-20 08:58:49 +00002348 for (unsigned i = 0; i != Half; ++i) {
2349 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2350 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2351 }
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002352 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng60f0b892006-04-20 08:58:49 +00002353}
2354
Evan Cheng5022b342006-04-17 20:43:08 +00002355/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2356///
2357static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2358 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002359 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002360 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002361 unsigned NumElems = Mask.getNumOperands();
2362 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002363 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002364 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002365 NumElems >>= 1;
2366 }
2367 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2368
2369 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002370 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002371 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002372 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002373 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2374}
2375
Evan Cheng14215c32006-04-21 23:03:30 +00002376/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Evan Chengafa1cb62007-05-17 18:45:50 +00002377/// vector of zero or undef vector.
Evan Cheng14215c32006-04-21 23:03:30 +00002378static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002379 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002380 bool isZero, SelectionDAG &DAG) {
2381 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002382 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002383 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Evan Chenge8b51802006-04-21 01:05:10 +00002384 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002385 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Chenge8b51802006-04-21 01:05:10 +00002386 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002387 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2388 &MaskVec[0], MaskVec.size());
Evan Cheng14215c32006-04-21 23:03:30 +00002389 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002390}
2391
Evan Chengb0461082006-04-24 18:01:45 +00002392/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2393///
2394static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2395 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002396 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002397 if (NumNonZero > 8)
2398 return SDOperand();
2399
2400 SDOperand V(0, 0);
2401 bool First = true;
2402 for (unsigned i = 0; i < 16; ++i) {
2403 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2404 if (ThisIsNonZero && First) {
2405 if (NumZero)
2406 V = getZeroVector(MVT::v8i16, DAG);
2407 else
2408 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2409 First = false;
2410 }
2411
2412 if ((i & 1) != 0) {
2413 SDOperand ThisElt(0, 0), LastElt(0, 0);
2414 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2415 if (LastIsNonZero) {
2416 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2417 }
2418 if (ThisIsNonZero) {
2419 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2420 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2421 ThisElt, DAG.getConstant(8, MVT::i8));
2422 if (LastIsNonZero)
2423 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2424 } else
2425 ThisElt = LastElt;
2426
2427 if (ThisElt.Val)
2428 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002429 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00002430 }
2431 }
2432
2433 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2434}
2435
Bill Wendlingd551a182007-03-22 18:42:45 +00002436/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengb0461082006-04-24 18:01:45 +00002437///
2438static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2439 unsigned NumNonZero, unsigned NumZero,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002440 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengb0461082006-04-24 18:01:45 +00002441 if (NumNonZero > 4)
2442 return SDOperand();
2443
2444 SDOperand V(0, 0);
2445 bool First = true;
2446 for (unsigned i = 0; i < 8; ++i) {
2447 bool isNonZero = (NonZeros & (1 << i)) != 0;
2448 if (isNonZero) {
2449 if (First) {
2450 if (NumZero)
2451 V = getZeroVector(MVT::v8i16, DAG);
2452 else
2453 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2454 First = false;
2455 }
2456 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002457 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengb0461082006-04-24 18:01:45 +00002458 }
2459 }
2460
2461 return V;
2462}
2463
Evan Chenga9467aa2006-04-25 20:13:52 +00002464SDOperand
2465X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2466 // All zero's are handled with pxor.
2467 if (ISD::isBuildVectorAllZeros(Op.Val))
2468 return Op;
2469
2470 // All one's are handled with pcmpeqd.
2471 if (ISD::isBuildVectorAllOnes(Op.Val))
2472 return Op;
2473
2474 MVT::ValueType VT = Op.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002475 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Chenga9467aa2006-04-25 20:13:52 +00002476 unsigned EVTBits = MVT::getSizeInBits(EVT);
2477
2478 unsigned NumElems = Op.getNumOperands();
2479 unsigned NumZero = 0;
2480 unsigned NumNonZero = 0;
2481 unsigned NonZeros = 0;
2482 std::set<SDOperand> Values;
2483 for (unsigned i = 0; i < NumElems; ++i) {
2484 SDOperand Elt = Op.getOperand(i);
2485 if (Elt.getOpcode() != ISD::UNDEF) {
2486 Values.insert(Elt);
2487 if (isZeroNode(Elt))
2488 NumZero++;
2489 else {
2490 NonZeros |= (1 << i);
2491 NumNonZero++;
2492 }
2493 }
2494 }
2495
2496 if (NumNonZero == 0)
2497 // Must be a mix of zero and undef. Return a zero vector.
2498 return getZeroVector(VT, DAG);
2499
2500 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2501 if (Values.size() == 1)
2502 return SDOperand();
2503
2504 // Special case for single non-zero element.
Evan Cheng798b3062006-10-25 20:48:19 +00002505 if (NumNonZero == 1) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002506 unsigned Idx = CountTrailingZeros_32(NonZeros);
2507 SDOperand Item = Op.getOperand(Idx);
2508 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2509 if (Idx == 0)
2510 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2511 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2512 NumZero > 0, DAG);
2513
2514 if (EVTBits == 32) {
2515 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2516 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2517 DAG);
2518 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002519 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002520 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002521 for (unsigned i = 0; i < NumElems; i++)
2522 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerc24a1d32006-08-08 02:23:42 +00002523 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2524 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002525 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2526 DAG.getNode(ISD::UNDEF, VT), Mask);
2527 }
2528 }
2529
Bill Wendling591eab82007-04-24 21:16:55 +00002530 // Let legalizer expand 2-wide build_vectors.
Evan Chenga9467aa2006-04-25 20:13:52 +00002531 if (EVTBits == 64)
2532 return SDOperand();
2533
2534 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002535 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002536 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2537 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00002538 if (V.Val) return V;
2539 }
2540
Bill Wendlingad2db4a2007-03-28 00:57:11 +00002541 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00002542 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2543 *this);
Evan Chenga9467aa2006-04-25 20:13:52 +00002544 if (V.Val) return V;
2545 }
2546
2547 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner35a08552007-02-25 07:10:00 +00002548 SmallVector<SDOperand, 8> V;
2549 V.resize(NumElems);
Evan Chenga9467aa2006-04-25 20:13:52 +00002550 if (NumElems == 4 && NumZero > 0) {
2551 for (unsigned i = 0; i < 4; ++i) {
2552 bool isZero = !(NonZeros & (1 << i));
2553 if (isZero)
2554 V[i] = getZeroVector(VT, DAG);
2555 else
2556 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2557 }
2558
2559 for (unsigned i = 0; i < 2; ++i) {
2560 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2561 default: break;
2562 case 0:
2563 V[i] = V[i*2]; // Must be a zero vector.
2564 break;
2565 case 1:
2566 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2567 getMOVLMask(NumElems, DAG));
2568 break;
2569 case 2:
2570 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2571 getMOVLMask(NumElems, DAG));
2572 break;
2573 case 3:
2574 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2575 getUnpacklMask(NumElems, DAG));
2576 break;
2577 }
2578 }
2579
Evan Cheng9fee4422006-05-16 07:21:53 +00002580 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002581 // clears the upper bits.
Evan Chenga9467aa2006-04-25 20:13:52 +00002582 // FIXME: we can do the same for v4f32 case when we know both parts of
2583 // the lower half come from scalar_to_vector (loadf32). We should do
2584 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng798b3062006-10-25 20:48:19 +00002585 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Chenga9467aa2006-04-25 20:13:52 +00002586 return V[0];
2587 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002588 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002589 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002590 bool Reverse = (NonZeros & 0x3) == 2;
2591 for (unsigned i = 0; i < 2; ++i)
2592 if (Reverse)
2593 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2594 else
2595 MaskVec.push_back(DAG.getConstant(i, EVT));
2596 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2597 for (unsigned i = 0; i < 2; ++i)
2598 if (Reverse)
2599 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2600 else
2601 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnered728e82006-08-11 17:38:39 +00002602 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2603 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002604 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2605 }
2606
2607 if (Values.size() > 2) {
2608 // Expand into a number of unpckl*.
2609 // e.g. for v4f32
2610 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2611 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2612 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2613 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2614 for (unsigned i = 0; i < NumElems; ++i)
2615 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2616 NumElems >>= 1;
2617 while (NumElems != 0) {
2618 for (unsigned i = 0; i < NumElems; ++i)
2619 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2620 UnpckMask);
2621 NumElems >>= 1;
2622 }
2623 return V[0];
2624 }
2625
2626 return SDOperand();
2627}
2628
2629SDOperand
2630X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2631 SDOperand V1 = Op.getOperand(0);
2632 SDOperand V2 = Op.getOperand(1);
2633 SDOperand PermMask = Op.getOperand(2);
2634 MVT::ValueType VT = Op.getValueType();
2635 unsigned NumElems = PermMask.getNumOperands();
2636 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2637 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Cheng949bcc92006-10-16 06:36:00 +00002638 bool V1IsSplat = false;
2639 bool V2IsSplat = false;
Evan Chenga9467aa2006-04-25 20:13:52 +00002640
Evan Cheng89c5d042006-09-08 01:50:06 +00002641 if (isUndefShuffle(Op.Val))
2642 return DAG.getNode(ISD::UNDEF, VT);
2643
Evan Chengafa1cb62007-05-17 18:45:50 +00002644 if (isZeroShuffle(Op.Val))
2645 return getZeroVector(VT, DAG);
2646
Evan Chengcea02ff2007-06-19 00:02:56 +00002647 if (isIdentityMask(PermMask.Val))
2648 return V1;
2649 else if (isIdentityMask(PermMask.Val, true))
2650 return V2;
2651
Evan Chenga9467aa2006-04-25 20:13:52 +00002652 if (isSplatMask(PermMask.Val)) {
2653 if (NumElems <= 4) return Op;
2654 // Promote it to a v4i32 splat.
Evan Cheng798b3062006-10-25 20:48:19 +00002655 return PromoteSplat(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00002656 }
2657
Evan Cheng798b3062006-10-25 20:48:19 +00002658 if (X86::isMOVLMask(PermMask.Val))
2659 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002660
Evan Cheng798b3062006-10-25 20:48:19 +00002661 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2662 X86::isMOVSLDUPMask(PermMask.Val) ||
2663 X86::isMOVHLPSMask(PermMask.Val) ||
2664 X86::isMOVHPMask(PermMask.Val) ||
2665 X86::isMOVLPMask(PermMask.Val))
2666 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002667
Evan Cheng798b3062006-10-25 20:48:19 +00002668 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2669 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Chengc415c5b2006-10-25 21:49:50 +00002670 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00002671
Evan Chengc415c5b2006-10-25 21:49:50 +00002672 bool Commuted = false;
Evan Cheng798b3062006-10-25 20:48:19 +00002673 V1IsSplat = isSplatVector(V1.Val);
2674 V2IsSplat = isSplatVector(V2.Val);
2675 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Chengc415c5b2006-10-25 21:49:50 +00002676 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00002677 std::swap(V1IsSplat, V2IsSplat);
2678 std::swap(V1IsUndef, V2IsUndef);
Evan Chengc415c5b2006-10-25 21:49:50 +00002679 Commuted = true;
Evan Cheng798b3062006-10-25 20:48:19 +00002680 }
2681
2682 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2683 if (V2IsUndef) return V1;
Evan Chengc415c5b2006-10-25 21:49:50 +00002684 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng798b3062006-10-25 20:48:19 +00002685 if (V2IsSplat) {
2686 // V2 is a splat, so the mask may be malformed. That is, it may point
2687 // to any V2 element. The instruction selectior won't like this. Get
2688 // a corrected mask and commute to form a proper MOVS{S|D}.
2689 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2690 if (NewMask.Val != PermMask.Val)
2691 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002692 }
Evan Cheng798b3062006-10-25 20:48:19 +00002693 return Op;
Evan Cheng949bcc92006-10-16 06:36:00 +00002694 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002695
Evan Cheng949bcc92006-10-16 06:36:00 +00002696 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling591eab82007-04-24 21:16:55 +00002697 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng949bcc92006-10-16 06:36:00 +00002698 X86::isUNPCKLMask(PermMask.Val) ||
2699 X86::isUNPCKHMask(PermMask.Val))
2700 return Op;
Evan Cheng8c5766e2006-10-04 18:33:38 +00002701
Evan Cheng798b3062006-10-25 20:48:19 +00002702 if (V2IsSplat) {
2703 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002704 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng798b3062006-10-25 20:48:19 +00002705 // new vector_shuffle with the corrected mask.
2706 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2707 if (NewMask.Val != PermMask.Val) {
2708 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2709 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2710 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2711 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2712 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2713 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002714 }
2715 }
2716 }
2717
2718 // Normalize the node to match x86 shuffle ops if needed
Evan Chengc415c5b2006-10-25 21:49:50 +00002719 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2720 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2721
2722 if (Commuted) {
2723 // Commute is back and try unpck* again.
2724 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2725 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling591eab82007-04-24 21:16:55 +00002726 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengc415c5b2006-10-25 21:49:50 +00002727 X86::isUNPCKLMask(PermMask.Val) ||
2728 X86::isUNPCKHMask(PermMask.Val))
2729 return Op;
2730 }
Evan Chenga9467aa2006-04-25 20:13:52 +00002731
2732 // If VT is integer, try PSHUF* first, then SHUFP*.
2733 if (MVT::isInteger(VT)) {
2734 if (X86::isPSHUFDMask(PermMask.Val) ||
2735 X86::isPSHUFHWMask(PermMask.Val) ||
2736 X86::isPSHUFLWMask(PermMask.Val)) {
2737 if (V2.getOpcode() != ISD::UNDEF)
2738 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2739 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2740 return Op;
2741 }
2742
Chris Lattnerdade6072007-05-17 17:13:13 +00002743 if (X86::isSHUFPMask(PermMask.Val) &&
2744 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Chenga9467aa2006-04-25 20:13:52 +00002745 return Op;
2746
2747 // Handle v8i16 shuffle high / low shuffle node pair.
2748 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2749 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman5c441312007-06-14 22:58:02 +00002750 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002751 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002752 for (unsigned i = 0; i != 4; ++i)
2753 MaskVec.push_back(PermMask.getOperand(i));
2754 for (unsigned i = 4; i != 8; ++i)
2755 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnered728e82006-08-11 17:38:39 +00002756 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2757 &MaskVec[0], MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002758 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2759 MaskVec.clear();
2760 for (unsigned i = 0; i != 4; ++i)
2761 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2762 for (unsigned i = 4; i != 8; ++i)
2763 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnered728e82006-08-11 17:38:39 +00002764 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002765 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2766 }
2767 } else {
2768 // Floating point cases in the other order.
2769 if (X86::isSHUFPMask(PermMask.Val))
2770 return Op;
2771 if (X86::isPSHUFDMask(PermMask.Val) ||
2772 X86::isPSHUFHWMask(PermMask.Val) ||
2773 X86::isPSHUFLWMask(PermMask.Val)) {
2774 if (V2.getOpcode() != ISD::UNDEF)
2775 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2776 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2777 return Op;
2778 }
2779 }
2780
Chris Lattnerdade6072007-05-17 17:13:13 +00002781 if (NumElems == 4 &&
2782 // Don't do this for MMX.
2783 MVT::getSizeInBits(VT) != 64) {
Evan Chenga9467aa2006-04-25 20:13:52 +00002784 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002785 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002786 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng3cd43622006-04-28 07:03:38 +00002787 Locs.reserve(NumElems);
Chris Lattner35a08552007-02-25 07:10:00 +00002788 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2789 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng3cd43622006-04-28 07:03:38 +00002790 unsigned NumHi = 0;
2791 unsigned NumLo = 0;
2792 // If no more than two elements come from either vector. This can be
2793 // implemented with two shuffles. First shuffle gather the elements.
2794 // The second shuffle, which takes the first shuffle as both of its
2795 // vector operands, put the elements into the right order.
2796 for (unsigned i = 0; i != NumElems; ++i) {
2797 SDOperand Elt = PermMask.getOperand(i);
2798 if (Elt.getOpcode() == ISD::UNDEF) {
2799 Locs[i] = std::make_pair(-1, -1);
2800 } else {
2801 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2802 if (Val < NumElems) {
2803 Locs[i] = std::make_pair(0, NumLo);
2804 Mask1[NumLo] = Elt;
2805 NumLo++;
2806 } else {
2807 Locs[i] = std::make_pair(1, NumHi);
2808 if (2+NumHi < NumElems)
2809 Mask1[2+NumHi] = Elt;
2810 NumHi++;
2811 }
2812 }
2813 }
2814 if (NumLo <= 2 && NumHi <= 2) {
2815 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002816 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2817 &Mask1[0], Mask1.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00002818 for (unsigned i = 0; i != NumElems; ++i) {
2819 if (Locs[i].first == -1)
2820 continue;
2821 else {
2822 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2823 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2824 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2825 }
2826 }
2827
2828 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnered728e82006-08-11 17:38:39 +00002829 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2830 &Mask2[0], Mask2.size()));
Evan Cheng3cd43622006-04-28 07:03:38 +00002831 }
2832
2833 // Break it into (shuffle shuffle_hi, shuffle_lo).
2834 Locs.clear();
Chris Lattner35a08552007-02-25 07:10:00 +00002835 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2836 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2837 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Chenga9467aa2006-04-25 20:13:52 +00002838 unsigned MaskIdx = 0;
2839 unsigned LoIdx = 0;
2840 unsigned HiIdx = NumElems/2;
2841 for (unsigned i = 0; i != NumElems; ++i) {
2842 if (i == NumElems/2) {
2843 MaskPtr = &HiMask;
2844 MaskIdx = 1;
2845 LoIdx = 0;
2846 HiIdx = NumElems/2;
2847 }
2848 SDOperand Elt = PermMask.getOperand(i);
2849 if (Elt.getOpcode() == ISD::UNDEF) {
2850 Locs[i] = std::make_pair(-1, -1);
2851 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2852 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2853 (*MaskPtr)[LoIdx] = Elt;
2854 LoIdx++;
2855 } else {
2856 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2857 (*MaskPtr)[HiIdx] = Elt;
2858 HiIdx++;
2859 }
2860 }
2861
Chris Lattner3d826992006-05-16 06:45:34 +00002862 SDOperand LoShuffle =
2863 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002864 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2865 &LoMask[0], LoMask.size()));
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00002866 SDOperand HiShuffle =
Chris Lattner3d826992006-05-16 06:45:34 +00002867 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnered728e82006-08-11 17:38:39 +00002868 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2869 &HiMask[0], HiMask.size()));
Chris Lattner35a08552007-02-25 07:10:00 +00002870 SmallVector<SDOperand, 8> MaskOps;
Evan Chenga9467aa2006-04-25 20:13:52 +00002871 for (unsigned i = 0; i != NumElems; ++i) {
2872 if (Locs[i].first == -1) {
2873 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2874 } else {
2875 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2876 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2877 }
2878 }
2879 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnered728e82006-08-11 17:38:39 +00002880 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2881 &MaskOps[0], MaskOps.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002882 }
2883
2884 return SDOperand();
2885}
2886
2887SDOperand
2888X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2889 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2890 return SDOperand();
2891
2892 MVT::ValueType VT = Op.getValueType();
2893 // TODO: handle v16i8.
2894 if (MVT::getSizeInBits(VT) == 16) {
2895 // Transform it so it match pextrw which produces a 32-bit result.
2896 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2897 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2898 Op.getOperand(0), Op.getOperand(1));
2899 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2900 DAG.getValueType(VT));
2901 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2902 } else if (MVT::getSizeInBits(VT) == 32) {
2903 SDOperand Vec = Op.getOperand(0);
2904 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2905 if (Idx == 0)
2906 return Op;
Evan Chenga9467aa2006-04-25 20:13:52 +00002907 // SHUFPS the element to the lowest double word, then movss.
2908 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00002909 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman5c441312007-06-14 22:58:02 +00002910 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
2911 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2912 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
2913 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00002914 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2915 &IdxVec[0], IdxVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002916 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng922e1912006-11-07 22:14:24 +00002917 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Chenga9467aa2006-04-25 20:13:52 +00002918 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00002919 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002920 } else if (MVT::getSizeInBits(VT) == 64) {
2921 SDOperand Vec = Op.getOperand(0);
2922 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2923 if (Idx == 0)
2924 return Op;
2925
2926 // UNPCKHPD the element to the lowest double word, then movsd.
2927 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2928 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2929 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner35a08552007-02-25 07:10:00 +00002930 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman5c441312007-06-14 22:58:02 +00002931 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
2932 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnered728e82006-08-11 17:38:39 +00002933 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2934 &IdxVec[0], IdxVec.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00002935 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2936 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2937 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Chengde7156f2006-06-15 08:14:54 +00002938 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002939 }
2940
2941 return SDOperand();
2942}
2943
2944SDOperand
2945X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002946 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Chenga9467aa2006-04-25 20:13:52 +00002947 // as its second argument.
2948 MVT::ValueType VT = Op.getValueType();
Dan Gohman5c441312007-06-14 22:58:02 +00002949 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Chenga9467aa2006-04-25 20:13:52 +00002950 SDOperand N0 = Op.getOperand(0);
2951 SDOperand N1 = Op.getOperand(1);
2952 SDOperand N2 = Op.getOperand(2);
2953 if (MVT::getSizeInBits(BaseVT) == 16) {
2954 if (N1.getValueType() != MVT::i32)
2955 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2956 if (N2.getValueType() != MVT::i32)
2957 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2958 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2959 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2960 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2961 if (Idx == 0) {
2962 // Use a movss.
2963 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2964 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman5c441312007-06-14 22:58:02 +00002965 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner35a08552007-02-25 07:10:00 +00002966 SmallVector<SDOperand, 8> MaskVec;
Evan Chenga9467aa2006-04-25 20:13:52 +00002967 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2968 for (unsigned i = 1; i <= 3; ++i)
2969 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2970 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnered728e82006-08-11 17:38:39 +00002971 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2972 &MaskVec[0], MaskVec.size()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002973 } else {
2974 // Use two pinsrw instructions to insert a 32 bit value.
2975 Idx <<= 1;
2976 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002977 if (ISD::isNON_EXTLoad(N1.Val)) {
Evan Cheng9fee4422006-05-16 07:21:53 +00002978 // Just load directly from f32mem to GR32.
Evan Chenge71fe34d2006-10-09 20:57:25 +00002979 LoadSDNode *LD = cast<LoadSDNode>(N1);
2980 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2981 LD->getSrcValue(), LD->getSrcValueOffset());
Evan Chenga9467aa2006-04-25 20:13:52 +00002982 } else {
2983 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2984 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2985 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002986 DAG.getConstant(0, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002987 }
2988 }
2989 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2990 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002991 DAG.getConstant(Idx, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002992 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2993 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Chengde7156f2006-06-15 08:14:54 +00002994 DAG.getConstant(Idx+1, getPointerTy()));
Evan Chenga9467aa2006-04-25 20:13:52 +00002995 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2996 }
2997 }
2998
2999 return SDOperand();
3000}
3001
3002SDOperand
3003X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3004 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3005 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3006}
3007
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003008// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Chenga9467aa2006-04-25 20:13:52 +00003009// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3010// one of the above mentioned nodes. It has to be wrapped because otherwise
3011// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3012// be used to form addressing mode. These wrapped nodes will be selected
3013// into MOV32ri.
3014SDOperand
3015X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3016 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng0b169222006-11-29 23:19:46 +00003017 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3018 getPointerTy(),
3019 CP->getAlignment());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003020 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003021 // With PIC, the address is actually $g + Offset.
3022 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3023 !Subtarget->isPICStyleRIPRel()) {
3024 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3025 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3026 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003027 }
3028
3029 return Result;
3030}
3031
3032SDOperand
3033X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3034 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng0b169222006-11-29 23:19:46 +00003035 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003036 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003037 // With PIC, the address is actually $g + Offset.
3038 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3039 !Subtarget->isPICStyleRIPRel()) {
3040 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3041 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3042 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003043 }
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00003044
3045 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3046 // load the value at address GV, not the value of GV itself. This means that
3047 // the GlobalAddress must be in the base or index register of the address, not
3048 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003049 // The same applies for external symbols during PIC codegen
Anton Korobeynikov430e68a12006-12-22 22:29:05 +00003050 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3051 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003052
3053 return Result;
3054}
3055
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00003056// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3057static SDOperand
3058LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3059 const MVT::ValueType PtrVT) {
3060 SDOperand InFlag;
3061 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3062 DAG.getNode(X86ISD::GlobalBaseReg,
3063 PtrVT), InFlag);
3064 InFlag = Chain.getValue(1);
3065
3066 // emit leal symbol@TLSGD(,%ebx,1), %eax
3067 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3068 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3069 GA->getValueType(0),
3070 GA->getOffset());
3071 SDOperand Ops[] = { Chain, TGA, InFlag };
3072 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3073 InFlag = Result.getValue(2);
3074 Chain = Result.getValue(1);
3075
3076 // call ___tls_get_addr. This function receives its argument in
3077 // the register EAX.
3078 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3079 InFlag = Chain.getValue(1);
3080
3081 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3082 SDOperand Ops1[] = { Chain,
3083 DAG.getTargetExternalSymbol("___tls_get_addr",
3084 PtrVT),
3085 DAG.getRegister(X86::EAX, PtrVT),
3086 DAG.getRegister(X86::EBX, PtrVT),
3087 InFlag };
3088 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3089 InFlag = Chain.getValue(1);
3090
3091 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3092}
3093
3094// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3095// "local exec" model.
3096static SDOperand
3097LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3098 const MVT::ValueType PtrVT) {
3099 // Get the Thread Pointer
3100 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3101 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3102 // exec)
3103 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3104 GA->getValueType(0),
3105 GA->getOffset());
3106 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancioefb80772007-04-22 22:50:52 +00003107
3108 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3109 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3110
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00003111 // The address of the thread local variable is the add of the thread
3112 // pointer with the offset of the variable.
3113 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3114}
3115
3116SDOperand
3117X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3118 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00003119 // TODO: implement the "initial exec"model for pic executables
3120 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3121 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00003122 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3123 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3124 // otherwise use the "Local Exec"TLS Model
3125 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3126 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3127 else
3128 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3129}
3130
Evan Chenga9467aa2006-04-25 20:13:52 +00003131SDOperand
3132X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3133 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Cheng0b169222006-11-29 23:19:46 +00003134 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng62cdc3f2006-12-05 04:01:03 +00003135 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikova0554d92007-01-12 19:20:47 +00003136 // With PIC, the address is actually $g + Offset.
3137 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3138 !Subtarget->isPICStyleRIPRel()) {
3139 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3140 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3141 Result);
3142 }
3143
3144 return Result;
3145}
3146
3147SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3148 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3149 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3150 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3151 // With PIC, the address is actually $g + Offset.
3152 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3153 !Subtarget->isPICStyleRIPRel()) {
3154 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3155 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3156 Result);
Evan Chenga9467aa2006-04-25 20:13:52 +00003157 }
3158
3159 return Result;
3160}
3161
3162SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00003163 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3164 "Not an i64 shift!");
3165 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3166 SDOperand ShOpLo = Op.getOperand(0);
3167 SDOperand ShOpHi = Op.getOperand(1);
3168 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003169 SDOperand Tmp1 = isSRA ?
3170 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3171 DAG.getConstant(0, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00003172
3173 SDOperand Tmp2, Tmp3;
3174 if (Op.getOpcode() == ISD::SHL_PARTS) {
3175 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3176 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3177 } else {
3178 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00003179 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00003180 }
3181
Evan Cheng4259a0f2006-09-11 02:19:56 +00003182 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3183 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3184 DAG.getConstant(32, MVT::i8));
3185 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3186 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Cheng9c249c32006-01-09 18:33:28 +00003187
3188 SDOperand Hi, Lo;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003189 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00003190
Evan Cheng4259a0f2006-09-11 02:19:56 +00003191 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3192 SmallVector<SDOperand, 4> Ops;
Evan Cheng9c249c32006-01-09 18:33:28 +00003193 if (Op.getOpcode() == ISD::SHL_PARTS) {
3194 Ops.push_back(Tmp2);
3195 Ops.push_back(Tmp3);
3196 Ops.push_back(CC);
3197 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003198 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003199 InFlag = Hi.getValue(1);
3200
3201 Ops.clear();
3202 Ops.push_back(Tmp3);
3203 Ops.push_back(Tmp1);
3204 Ops.push_back(CC);
3205 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003206 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003207 } else {
3208 Ops.push_back(Tmp2);
3209 Ops.push_back(Tmp3);
3210 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00003211 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003212 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003213 InFlag = Lo.getValue(1);
3214
3215 Ops.clear();
3216 Ops.push_back(Tmp3);
3217 Ops.push_back(Tmp1);
3218 Ops.push_back(CC);
3219 Ops.push_back(InFlag);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003220 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng9c249c32006-01-09 18:33:28 +00003221 }
3222
Evan Cheng4259a0f2006-09-11 02:19:56 +00003223 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Cheng9c249c32006-01-09 18:33:28 +00003224 Ops.clear();
3225 Ops.push_back(Lo);
3226 Ops.push_back(Hi);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003227 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003228}
Evan Cheng6305e502006-01-12 22:54:21 +00003229
Evan Chenga9467aa2006-04-25 20:13:52 +00003230SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3231 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3232 Op.getOperand(0).getValueType() >= MVT::i16 &&
3233 "Unknown SINT_TO_FP to lower!");
3234
3235 SDOperand Result;
3236 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3237 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3238 MachineFunction &MF = DAG.getMachineFunction();
3239 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3240 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chengdf9ac472006-10-05 23:01:46 +00003241 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Chengab51cf22006-10-13 21:14:26 +00003242 StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003243
3244 // Build the FILD
Chris Lattner35a08552007-02-25 07:10:00 +00003245 SDVTList Tys;
3246 if (X86ScalarSSE)
3247 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3248 else
3249 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3250 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003251 Ops.push_back(Chain);
3252 Ops.push_back(StackSlot);
3253 Ops.push_back(DAG.getValueType(SrcVT));
3254 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003255 Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003256
3257 if (X86ScalarSSE) {
3258 Chain = Result.getValue(1);
3259 SDOperand InFlag = Result.getValue(2);
3260
3261 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3262 // shouldn't be necessary except that RFP cannot be live across
3263 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00003264 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00003265 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00003266 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner35a08552007-02-25 07:10:00 +00003267 Tys = DAG.getVTList(MVT::Other);
3268 SmallVector<SDOperand, 8> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00003269 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003270 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00003271 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00003272 Ops.push_back(DAG.getValueType(Op.getValueType()));
3273 Ops.push_back(InFlag);
Chris Lattnerc24a1d32006-08-08 02:23:42 +00003274 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Chenge71fe34d2006-10-09 20:57:25 +00003275 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattner76ac0682005-11-15 00:40:23 +00003276 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003277
Evan Chenga9467aa2006-04-25 20:13:52 +00003278 return Result;
3279}
3280
3281SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3282 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3283 "Unknown FP_TO_SINT to lower!");
3284 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3285 // stack slot.
3286 MachineFunction &MF = DAG.getMachineFunction();
3287 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3288 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3289 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3290
3291 unsigned Opc;
3292 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00003293 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3294 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3295 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3296 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00003297 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003298
Evan Chenga9467aa2006-04-25 20:13:52 +00003299 SDOperand Chain = DAG.getEntryNode();
3300 SDOperand Value = Op.getOperand(0);
3301 if (X86ScalarSSE) {
3302 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Chengab51cf22006-10-13 21:14:26 +00003303 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Chris Lattner35a08552007-02-25 07:10:00 +00003304 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3305 SDOperand Ops[] = {
3306 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3307 };
3308 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003309 Chain = Value.getValue(1);
3310 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3311 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3312 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003313
Evan Chenga9467aa2006-04-25 20:13:52 +00003314 // Build the FP_TO_INT*_IN_MEM
Chris Lattner35a08552007-02-25 07:10:00 +00003315 SDOperand Ops[] = { Chain, Value, StackSlot };
3316 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Cheng172fce72006-01-06 00:43:03 +00003317
Evan Chenga9467aa2006-04-25 20:13:52 +00003318 // Load the result.
Evan Chenge71fe34d2006-10-09 20:57:25 +00003319 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003320}
3321
3322SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3323 MVT::ValueType VT = Op.getValueType();
3324 const Type *OpNTy = MVT::getTypeForValueType(VT);
3325 std::vector<Constant*> CV;
3326 if (VT == MVT::f64) {
3327 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3328 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3329 } else {
3330 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3331 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3332 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3333 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3334 }
3335 Constant *CS = ConstantStruct::get(CV);
3336 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner35a08552007-02-25 07:10:00 +00003337 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00003338 SmallVector<SDOperand, 3> Ops;
3339 Ops.push_back(DAG.getEntryNode());
3340 Ops.push_back(CPIdx);
3341 Ops.push_back(DAG.getSrcValue(NULL));
3342 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003343 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3344}
3345
3346SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3347 MVT::ValueType VT = Op.getValueType();
3348 const Type *OpNTy = MVT::getTypeForValueType(VT);
3349 std::vector<Constant*> CV;
3350 if (VT == MVT::f64) {
3351 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3352 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3353 } else {
3354 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3355 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3356 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3357 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3358 }
3359 Constant *CS = ConstantStruct::get(CV);
3360 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner35a08552007-02-25 07:10:00 +00003361 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00003362 SmallVector<SDOperand, 3> Ops;
3363 Ops.push_back(DAG.getEntryNode());
3364 Ops.push_back(CPIdx);
3365 Ops.push_back(DAG.getSrcValue(NULL));
3366 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003367 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3368}
3369
Evan Cheng4363e882007-01-05 07:55:56 +00003370SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng82241c82007-01-05 21:37:56 +00003371 SDOperand Op0 = Op.getOperand(0);
3372 SDOperand Op1 = Op.getOperand(1);
Evan Cheng4363e882007-01-05 07:55:56 +00003373 MVT::ValueType VT = Op.getValueType();
Evan Cheng82241c82007-01-05 21:37:56 +00003374 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng4363e882007-01-05 07:55:56 +00003375 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng82241c82007-01-05 21:37:56 +00003376
3377 // If second operand is smaller, extend it first.
3378 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3379 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3380 SrcVT = VT;
3381 }
3382
Evan Cheng4363e882007-01-05 07:55:56 +00003383 // First get the sign bit of second operand.
3384 std::vector<Constant*> CV;
3385 if (SrcVT == MVT::f64) {
3386 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3387 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3388 } else {
3389 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3390 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3391 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3392 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3393 }
3394 Constant *CS = ConstantStruct::get(CV);
3395 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnere56fef92007-02-25 06:40:16 +00003396 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
Evan Cheng4363e882007-01-05 07:55:56 +00003397 SmallVector<SDOperand, 3> Ops;
3398 Ops.push_back(DAG.getEntryNode());
3399 Ops.push_back(CPIdx);
3400 Ops.push_back(DAG.getSrcValue(NULL));
Evan Cheng82241c82007-01-05 21:37:56 +00003401 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3402 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng4363e882007-01-05 07:55:56 +00003403
3404 // Shift sign bit right or left if the two operands have different types.
3405 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3406 // Op0 is MVT::f32, Op1 is MVT::f64.
3407 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3408 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3409 DAG.getConstant(32, MVT::i32));
3410 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3411 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3412 DAG.getConstant(0, getPointerTy()));
Evan Cheng4363e882007-01-05 07:55:56 +00003413 }
3414
Evan Cheng82241c82007-01-05 21:37:56 +00003415 // Clear first operand sign bit.
3416 CV.clear();
3417 if (VT == MVT::f64) {
3418 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3419 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3420 } else {
3421 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3422 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3423 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3424 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3425 }
3426 CS = ConstantStruct::get(CV);
3427 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnere56fef92007-02-25 06:40:16 +00003428 Tys = DAG.getVTList(VT, MVT::Other);
Evan Cheng82241c82007-01-05 21:37:56 +00003429 Ops.clear();
3430 Ops.push_back(DAG.getEntryNode());
3431 Ops.push_back(CPIdx);
3432 Ops.push_back(DAG.getSrcValue(NULL));
3433 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3434 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3435
3436 // Or the value with the sign bit.
3437 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng4363e882007-01-05 07:55:56 +00003438}
3439
Evan Cheng4259a0f2006-09-11 02:19:56 +00003440SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3441 SDOperand Chain) {
Evan Chenga9467aa2006-04-25 20:13:52 +00003442 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3443 SDOperand Cond;
Evan Cheng4259a0f2006-09-11 02:19:56 +00003444 SDOperand Op0 = Op.getOperand(0);
3445 SDOperand Op1 = Op.getOperand(1);
Evan Chenga9467aa2006-04-25 20:13:52 +00003446 SDOperand CC = Op.getOperand(2);
3447 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng694810c2006-10-12 19:12:56 +00003448 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3449 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003450 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Chenga9467aa2006-04-25 20:13:52 +00003451 unsigned X86CC;
Evan Chenga9467aa2006-04-25 20:13:52 +00003452
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003453 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattner7a627672006-09-13 03:22:10 +00003454 Op0, Op1, DAG)) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003455 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Cheng694810c2006-10-12 19:12:56 +00003456 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003457 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003458 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003459 }
3460
3461 assert(isFP && "Illegal integer SetCC!");
3462
3463 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Cheng694810c2006-10-12 19:12:56 +00003464 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003465
3466 switch (SetCCOpcode) {
3467 default: assert(false && "Illegal floating point SetCC!");
3468 case ISD::SETOEQ: { // !PF & ZF
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003469 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003470 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003471 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003472 Tmp1.getValue(1) };
Evan Cheng694810c2006-10-12 19:12:56 +00003473 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003474 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3475 }
3476 case ISD::SETUNE: { // PF | !ZF
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003477 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Cheng694810c2006-10-12 19:12:56 +00003478 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003479 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003480 Tmp1.getValue(1) };
Evan Cheng694810c2006-10-12 19:12:56 +00003481 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003482 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3483 }
Evan Chengc1583db2005-12-21 20:21:51 +00003484 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003485}
Evan Cheng45df7f82006-01-30 23:41:35 +00003486
Evan Chenga9467aa2006-04-25 20:13:52 +00003487SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003488 bool addTest = true;
3489 SDOperand Chain = DAG.getEntryNode();
3490 SDOperand Cond = Op.getOperand(0);
3491 SDOperand CC;
3492 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng944d1e92006-01-26 02:13:10 +00003493
Evan Cheng4259a0f2006-09-11 02:19:56 +00003494 if (Cond.getOpcode() == ISD::SETCC)
3495 Cond = LowerSETCC(Cond, DAG, Chain);
3496
3497 if (Cond.getOpcode() == X86ISD::SETCC) {
3498 CC = Cond.getOperand(0);
3499
Evan Chenga9467aa2006-04-25 20:13:52 +00003500 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng4259a0f2006-09-11 02:19:56 +00003501 // (since flag operand cannot be shared). Use it as the condition setting
3502 // operand in place of the X86ISD::SETCC.
3503 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Chenga9467aa2006-04-25 20:13:52 +00003504 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng4259a0f2006-09-11 02:19:56 +00003505 // pressure reason)?
3506 SDOperand Cmp = Cond.getOperand(1);
3507 unsigned Opc = Cmp.getOpcode();
3508 bool IllegalFPCMov = !X86ScalarSSE &&
3509 MVT::isFloatingPoint(Op.getValueType()) &&
3510 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3511 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3512 !IllegalFPCMov) {
3513 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3514 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3515 addTest = false;
3516 }
3517 }
Evan Cheng73a1ad92006-01-10 20:26:56 +00003518
Evan Chenga9467aa2006-04-25 20:13:52 +00003519 if (addTest) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003520 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003521 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3522 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng225a4d02005-12-17 01:21:05 +00003523 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003524
Evan Cheng4259a0f2006-09-11 02:19:56 +00003525 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3526 SmallVector<SDOperand, 4> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003527 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3528 // condition is true.
3529 Ops.push_back(Op.getOperand(2));
3530 Ops.push_back(Op.getOperand(1));
3531 Ops.push_back(CC);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003532 Ops.push_back(Cond.getValue(1));
3533 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003534}
Evan Cheng944d1e92006-01-26 02:13:10 +00003535
Evan Chenga9467aa2006-04-25 20:13:52 +00003536SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003537 bool addTest = true;
3538 SDOperand Chain = Op.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003539 SDOperand Cond = Op.getOperand(1);
3540 SDOperand Dest = Op.getOperand(2);
3541 SDOperand CC;
Evan Cheng4259a0f2006-09-11 02:19:56 +00003542 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3543
Evan Chenga9467aa2006-04-25 20:13:52 +00003544 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng4259a0f2006-09-11 02:19:56 +00003545 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00003546
3547 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng4259a0f2006-09-11 02:19:56 +00003548 CC = Cond.getOperand(0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003549
Evan Cheng4259a0f2006-09-11 02:19:56 +00003550 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3551 // (since flag operand cannot be shared). Use it as the condition setting
3552 // operand in place of the X86ISD::SETCC.
3553 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3554 // to use a test instead of duplicating the X86ISD::CMP (for register
3555 // pressure reason)?
3556 SDOperand Cmp = Cond.getOperand(1);
3557 unsigned Opc = Cmp.getOpcode();
3558 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3559 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3560 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3561 addTest = false;
3562 }
3563 }
Evan Chengfb22e862006-01-13 01:03:02 +00003564
Evan Chenga9467aa2006-04-25 20:13:52 +00003565 if (addTest) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00003566 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng4259a0f2006-09-11 02:19:56 +00003567 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3568 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng6fc31042005-12-19 23:12:38 +00003569 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003570 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng4259a0f2006-09-11 02:19:56 +00003571 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003572}
Evan Chengae986f12006-01-11 22:15:48 +00003573
Evan Cheng2a330942006-05-25 00:59:30 +00003574SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3575 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003576
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003577 if (Subtarget->is64Bit())
Chris Lattner7802f3e2007-02-25 09:06:15 +00003578 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng2a330942006-05-25 00:59:30 +00003579 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003580 switch (CallingConv) {
Chris Lattnerfc360392006-09-27 18:29:38 +00003581 default:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003582 assert(0 && "Unsupported calling convention");
Chris Lattnerfc360392006-09-27 18:29:38 +00003583 case CallingConv::Fast:
Chris Lattner3ed3be32007-02-28 06:05:16 +00003584 // TODO: Implement fastcc
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003585 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00003586 case CallingConv::C:
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00003587 case CallingConv::X86_StdCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00003588 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerfc360392006-09-27 18:29:38 +00003589 case CallingConv::X86_FastCall:
Chris Lattner7802f3e2007-02-25 09:06:15 +00003590 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003591 }
Evan Cheng2a330942006-05-25 00:59:30 +00003592}
3593
Anton Korobeynikov9b91d982007-04-17 19:34:00 +00003594
3595// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3596// Calls to _alloca is needed to probe the stack when allocating more than 4k
3597// bytes in one go. Touching the stack at 4K increments is necessary to ensure
3598// that the guard pages used by the OS virtual memory manager are allocated in
3599// correct sequence.
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00003600SDOperand X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3601 SelectionDAG &DAG) {
Anton Korobeynikov9b91d982007-04-17 19:34:00 +00003602 assert(Subtarget->isTargetCygMing() &&
3603 "This should be used only on Cygwin/Mingw targets");
3604
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00003605 // Get the inputs.
3606 SDOperand Chain = Op.getOperand(0);
3607 SDOperand Size = Op.getOperand(1);
3608 // FIXME: Ensure alignment here
3609
3610 TargetLowering::ArgListTy Args;
3611 TargetLowering::ArgListEntry Entry;
3612 MVT::ValueType IntPtr = getPointerTy();
3613 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3614 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3615
3616 Entry.Node = Size;
3617 Entry.Ty = IntPtrTy;
3618 Entry.isInReg = true; // Should pass in EAX
3619 Args.push_back(Entry);
3620 std::pair<SDOperand, SDOperand> CallResult =
3621 LowerCallTo(Chain, IntPtrTy, false, false, CallingConv::C, false,
3622 DAG.getExternalSymbol("_alloca", IntPtr), Args, DAG);
3623
3624 SDOperand SP = DAG.getCopyFromReg(CallResult.second, X86StackPtr, SPTy);
3625
3626 std::vector<MVT::ValueType> Tys;
3627 Tys.push_back(SPTy);
3628 Tys.push_back(MVT::Other);
3629 SDOperand Ops[2] = { SP, CallResult.second };
3630 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3631}
3632
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003633SDOperand
3634X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chengdc614c12006-06-06 23:30:24 +00003635 MachineFunction &MF = DAG.getMachineFunction();
3636 const Function* Fn = MF.getFunction();
3637 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov4efbbc92007-01-03 11:43:14 +00003638 Subtarget->isTargetCygMing() &&
Evan Cheng0e14a562006-06-09 06:24:42 +00003639 Fn->getName() == "main")
Chris Lattnerff0598d2007-04-17 17:21:52 +00003640 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chengdc614c12006-06-06 23:30:24 +00003641
Evan Cheng17e734f2006-05-23 21:06:34 +00003642 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003643 if (Subtarget->is64Bit())
3644 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng17e734f2006-05-23 21:06:34 +00003645 else
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003646 switch(CC) {
Chris Lattnerfc360392006-09-27 18:29:38 +00003647 default:
3648 assert(0 && "Unsupported calling convention");
3649 case CallingConv::Fast:
Chris Lattner3ed3be32007-02-28 06:05:16 +00003650 // TODO: implement fastcc.
3651
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003652 // Falls through
Chris Lattnerfc360392006-09-27 18:29:38 +00003653 case CallingConv::C:
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003654 return LowerCCCArguments(Op, DAG);
Chris Lattnerfc360392006-09-27 18:29:38 +00003655 case CallingConv::X86_StdCall:
Chris Lattnerff0598d2007-04-17 17:21:52 +00003656 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikov037c8672007-01-28 13:31:35 +00003657 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerfc360392006-09-27 18:29:38 +00003658 case CallingConv::X86_FastCall:
Chris Lattnerff0598d2007-04-17 17:21:52 +00003659 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner3ed3be32007-02-28 06:05:16 +00003660 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00003661 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003662}
3663
Evan Chenga9467aa2006-04-25 20:13:52 +00003664SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3665 SDOperand InFlag(0, 0);
3666 SDOperand Chain = Op.getOperand(0);
3667 unsigned Align =
3668 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3669 if (Align == 0) Align = 1;
3670
3671 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3672 // If not DWORD aligned, call memset if size is less than the threshold.
3673 // It knows how to align to the right boundary first.
3674 if ((Align & 3) != 0 ||
3675 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3676 MVT::ValueType IntPtr = getPointerTy();
Owen Anderson20a631f2006-05-03 01:29:57 +00003677 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00003678 TargetLowering::ArgListTy Args;
3679 TargetLowering::ArgListEntry Entry;
3680 Entry.Node = Op.getOperand(1);
3681 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00003682 Args.push_back(Entry);
Reid Spencere87b5e92007-01-03 17:24:59 +00003683 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencere63b6512006-12-31 05:55:36 +00003684 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3685 Entry.Ty = IntPtrTy;
Reid Spencere63b6512006-12-31 05:55:36 +00003686 Args.push_back(Entry);
3687 Entry.Node = Op.getOperand(3);
3688 Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00003689 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00003690 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00003691 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3692 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003693 }
Evan Chengd097e672006-03-22 02:53:00 +00003694
Evan Chenga9467aa2006-04-25 20:13:52 +00003695 MVT::ValueType AVT;
3696 SDOperand Count;
3697 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3698 unsigned BytesLeft = 0;
3699 bool TwoRepStos = false;
3700 if (ValC) {
3701 unsigned ValReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003702 uint64_t Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003703
Evan Chenga9467aa2006-04-25 20:13:52 +00003704 // If the value is a constant, then we can potentially use larger sets.
3705 switch (Align & 3) {
3706 case 2: // WORD aligned
3707 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00003708 ValReg = X86::AX;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003709 Val = (Val << 8) | Val;
Evan Chenga9467aa2006-04-25 20:13:52 +00003710 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003711 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00003712 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003713 ValReg = X86::EAX;
Evan Chenga9467aa2006-04-25 20:13:52 +00003714 Val = (Val << 8) | Val;
3715 Val = (Val << 16) | Val;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003716 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3717 AVT = MVT::i64;
3718 ValReg = X86::RAX;
3719 Val = (Val << 32) | Val;
3720 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003721 break;
3722 default: // Byte aligned
3723 AVT = MVT::i8;
Evan Chenga9467aa2006-04-25 20:13:52 +00003724 ValReg = X86::AL;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003725 Count = Op.getOperand(3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003726 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003727 }
3728
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003729 if (AVT > MVT::i8) {
3730 if (I) {
3731 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3732 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3733 BytesLeft = I->getValue() % UBytes;
3734 } else {
3735 assert(AVT >= MVT::i32 &&
3736 "Do not use rep;stos if not at least DWORD aligned");
3737 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3738 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3739 TwoRepStos = true;
3740 }
3741 }
3742
Evan Chenga9467aa2006-04-25 20:13:52 +00003743 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3744 InFlag);
3745 InFlag = Chain.getValue(1);
3746 } else {
3747 AVT = MVT::i8;
3748 Count = Op.getOperand(3);
3749 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3750 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003751 }
Evan Chengb0461082006-04-24 18:01:45 +00003752
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003753 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3754 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003755 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003756 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3757 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003758 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003759
Chris Lattnere56fef92007-02-25 06:40:16 +00003760 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003761 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003762 Ops.push_back(Chain);
3763 Ops.push_back(DAG.getValueType(AVT));
3764 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003765 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengb0461082006-04-24 18:01:45 +00003766
Evan Chenga9467aa2006-04-25 20:13:52 +00003767 if (TwoRepStos) {
3768 InFlag = Chain.getValue(1);
3769 Count = Op.getOperand(3);
3770 MVT::ValueType CVT = Count.getValueType();
3771 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003772 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3773 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3774 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003775 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00003776 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003777 Ops.clear();
3778 Ops.push_back(Chain);
3779 Ops.push_back(DAG.getValueType(MVT::i8));
3780 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003781 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003782 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003783 // Issue stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00003784 SDOperand Value;
3785 unsigned Val = ValC->getValue() & 255;
3786 unsigned Offset = I->getValue() - BytesLeft;
3787 SDOperand DstAddr = Op.getOperand(1);
3788 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003789 if (BytesLeft >= 4) {
3790 Val = (Val << 8) | Val;
3791 Val = (Val << 16) | Val;
3792 Value = DAG.getConstant(Val, MVT::i32);
Evan Chengdf9ac472006-10-05 23:01:46 +00003793 Chain = DAG.getStore(Chain, Value,
3794 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3795 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003796 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003797 BytesLeft -= 4;
3798 Offset += 4;
3799 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003800 if (BytesLeft >= 2) {
3801 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Chengdf9ac472006-10-05 23:01:46 +00003802 Chain = DAG.getStore(Chain, Value,
3803 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3804 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003805 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003806 BytesLeft -= 2;
3807 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003808 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003809 if (BytesLeft == 1) {
3810 Value = DAG.getConstant(Val, MVT::i8);
Evan Chengdf9ac472006-10-05 23:01:46 +00003811 Chain = DAG.getStore(Chain, Value,
3812 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3813 DAG.getConstant(Offset, AddrVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003814 NULL, 0);
Evan Cheng14215c32006-04-21 23:03:30 +00003815 }
Evan Cheng082c8782006-03-24 07:29:27 +00003816 }
Evan Chengebf10062006-04-03 20:53:28 +00003817
Evan Chenga9467aa2006-04-25 20:13:52 +00003818 return Chain;
3819}
Evan Chengebf10062006-04-03 20:53:28 +00003820
Evan Chenga9467aa2006-04-25 20:13:52 +00003821SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3822 SDOperand Chain = Op.getOperand(0);
3823 unsigned Align =
3824 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3825 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003826
Evan Chenga9467aa2006-04-25 20:13:52 +00003827 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3828 // If not DWORD aligned, call memcpy if size is less than the threshold.
3829 // It knows how to align to the right boundary first.
3830 if ((Align & 3) != 0 ||
3831 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3832 MVT::ValueType IntPtr = getPointerTy();
Reid Spencere63b6512006-12-31 05:55:36 +00003833 TargetLowering::ArgListTy Args;
3834 TargetLowering::ArgListEntry Entry;
Anton Korobeynikov037c8672007-01-28 13:31:35 +00003835 Entry.Ty = getTargetData()->getIntPtrType();
Reid Spencere63b6512006-12-31 05:55:36 +00003836 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3837 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3838 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Chenga9467aa2006-04-25 20:13:52 +00003839 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencere63b6512006-12-31 05:55:36 +00003840 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Chenga9467aa2006-04-25 20:13:52 +00003841 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3842 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003843 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003844
3845 MVT::ValueType AVT;
3846 SDOperand Count;
3847 unsigned BytesLeft = 0;
3848 bool TwoRepMovs = false;
3849 switch (Align & 3) {
3850 case 2: // WORD aligned
3851 AVT = MVT::i16;
Evan Chenga9467aa2006-04-25 20:13:52 +00003852 break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003853 case 0: // DWORD aligned
Evan Chenga9467aa2006-04-25 20:13:52 +00003854 AVT = MVT::i32;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003855 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3856 AVT = MVT::i64;
Evan Chenga9467aa2006-04-25 20:13:52 +00003857 break;
3858 default: // Byte aligned
3859 AVT = MVT::i8;
3860 Count = Op.getOperand(3);
3861 break;
3862 }
3863
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003864 if (AVT > MVT::i8) {
3865 if (I) {
3866 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3867 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3868 BytesLeft = I->getValue() % UBytes;
3869 } else {
3870 assert(AVT >= MVT::i32 &&
3871 "Do not use rep;movs if not at least DWORD aligned");
3872 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3873 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3874 TwoRepMovs = true;
3875 }
3876 }
3877
Evan Chenga9467aa2006-04-25 20:13:52 +00003878 SDOperand InFlag(0, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003879 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3880 Count, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003881 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003882 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3883 Op.getOperand(1), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003884 InFlag = Chain.getValue(1);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003885 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3886 Op.getOperand(2), InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003887 InFlag = Chain.getValue(1);
3888
Chris Lattnere56fef92007-02-25 06:40:16 +00003889 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003890 SmallVector<SDOperand, 8> Ops;
Evan Chenga9467aa2006-04-25 20:13:52 +00003891 Ops.push_back(Chain);
3892 Ops.push_back(DAG.getValueType(AVT));
3893 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003894 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003895
3896 if (TwoRepMovs) {
3897 InFlag = Chain.getValue(1);
3898 Count = Op.getOperand(3);
3899 MVT::ValueType CVT = Count.getValueType();
3900 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003901 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3902 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3903 Left, InFlag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003904 InFlag = Chain.getValue(1);
Chris Lattnere56fef92007-02-25 06:40:16 +00003905 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Chenga9467aa2006-04-25 20:13:52 +00003906 Ops.clear();
3907 Ops.push_back(Chain);
3908 Ops.push_back(DAG.getValueType(MVT::i8));
3909 Ops.push_back(InFlag);
Evan Cheng5c68bba2006-08-11 07:35:45 +00003910 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00003911 } else if (BytesLeft) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003912 // Issue loads and stores for the last 1 - 7 bytes.
Evan Chenga9467aa2006-04-25 20:13:52 +00003913 unsigned Offset = I->getValue() - BytesLeft;
3914 SDOperand DstAddr = Op.getOperand(1);
3915 MVT::ValueType DstVT = DstAddr.getValueType();
3916 SDOperand SrcAddr = Op.getOperand(2);
3917 MVT::ValueType SrcVT = SrcAddr.getValueType();
3918 SDOperand Value;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003919 if (BytesLeft >= 4) {
3920 Value = DAG.getLoad(MVT::i32, Chain,
3921 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3922 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003923 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003924 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003925 Chain = DAG.getStore(Chain, Value,
3926 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3927 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003928 NULL, 0);
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003929 BytesLeft -= 4;
3930 Offset += 4;
3931 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003932 if (BytesLeft >= 2) {
3933 Value = DAG.getLoad(MVT::i16, Chain,
3934 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3935 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003936 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003937 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003938 Chain = DAG.getStore(Chain, Value,
3939 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3940 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003941 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003942 BytesLeft -= 2;
3943 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003944 }
3945
Evan Chenga9467aa2006-04-25 20:13:52 +00003946 if (BytesLeft == 1) {
3947 Value = DAG.getLoad(MVT::i8, Chain,
3948 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3949 DAG.getConstant(Offset, SrcVT)),
Evan Chenge71fe34d2006-10-09 20:57:25 +00003950 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003951 Chain = Value.getValue(1);
Evan Chengdf9ac472006-10-05 23:01:46 +00003952 Chain = DAG.getStore(Chain, Value,
3953 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3954 DAG.getConstant(Offset, DstVT)),
Evan Chengab51cf22006-10-13 21:14:26 +00003955 NULL, 0);
Evan Chenga9467aa2006-04-25 20:13:52 +00003956 }
Evan Chengcbffa462006-03-31 19:22:53 +00003957 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003958
3959 return Chain;
3960}
3961
3962SDOperand
3963X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere56fef92007-02-25 06:40:16 +00003964 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner35a08552007-02-25 07:10:00 +00003965 SDOperand TheOp = Op.getOperand(0);
3966 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00003967 if (Subtarget->is64Bit()) {
3968 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3969 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3970 MVT::i64, Copy1.getValue(2));
3971 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3972 DAG.getConstant(32, MVT::i8));
Chris Lattner35a08552007-02-25 07:10:00 +00003973 SDOperand Ops[] = {
3974 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3975 };
Chris Lattnere56fef92007-02-25 06:40:16 +00003976
3977 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner35a08552007-02-25 07:10:00 +00003978 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng28a9e9b2006-11-29 08:28:13 +00003979 }
Chris Lattner35a08552007-02-25 07:10:00 +00003980
3981 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3982 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3983 MVT::i32, Copy1.getValue(2));
3984 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3985 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3986 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Chenga9467aa2006-04-25 20:13:52 +00003987}
3988
3989SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Chengab51cf22006-10-13 21:14:26 +00003990 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3991
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003992 if (!Subtarget->is64Bit()) {
3993 // vastart just stores the address of the VarArgsFrameIndex slot into the
3994 // memory location argument.
3995 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00003996 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3997 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00003998 }
3999
4000 // __va_list_tag:
4001 // gp_offset (0 - 6 * 8)
4002 // fp_offset (48 - 48 + 8 * 16)
4003 // overflow_arg_area (point to parameters coming in memory).
4004 // reg_save_area
Chris Lattner35a08552007-02-25 07:10:00 +00004005 SmallVector<SDOperand, 8> MemOps;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004006 SDOperand FIN = Op.getOperand(1);
4007 // Store gp_offset
Evan Chengdf9ac472006-10-05 23:01:46 +00004008 SDOperand Store = DAG.getStore(Op.getOperand(0),
4009 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00004010 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004011 MemOps.push_back(Store);
4012
4013 // Store fp_offset
4014 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4015 DAG.getConstant(4, getPointerTy()));
Evan Chengdf9ac472006-10-05 23:01:46 +00004016 Store = DAG.getStore(Op.getOperand(0),
4017 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Chengab51cf22006-10-13 21:14:26 +00004018 FIN, SV->getValue(), SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004019 MemOps.push_back(Store);
4020
4021 // Store ptr to overflow_arg_area
4022 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4023 DAG.getConstant(4, getPointerTy()));
4024 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00004025 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4026 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004027 MemOps.push_back(Store);
4028
4029 // Store ptr to reg_save_area.
4030 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4031 DAG.getConstant(8, getPointerTy()));
4032 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Chengab51cf22006-10-13 21:14:26 +00004033 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4034 SV->getOffset());
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004035 MemOps.push_back(Store);
4036 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Chenga9467aa2006-04-25 20:13:52 +00004037}
4038
Evan Chengdeaea252007-03-02 23:16:35 +00004039SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4040 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4041 SDOperand Chain = Op.getOperand(0);
4042 SDOperand DstPtr = Op.getOperand(1);
4043 SDOperand SrcPtr = Op.getOperand(2);
4044 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4045 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4046
4047 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4048 SrcSV->getValue(), SrcSV->getOffset());
4049 Chain = SrcPtr.getValue(1);
4050 for (unsigned i = 0; i < 3; ++i) {
4051 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4052 SrcSV->getValue(), SrcSV->getOffset());
4053 Chain = Val.getValue(1);
4054 Chain = DAG.getStore(Chain, Val, DstPtr,
4055 DstSV->getValue(), DstSV->getOffset());
4056 if (i == 2)
4057 break;
4058 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4059 DAG.getConstant(8, getPointerTy()));
4060 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4061 DAG.getConstant(8, getPointerTy()));
4062 }
4063 return Chain;
4064}
4065
Evan Chenga9467aa2006-04-25 20:13:52 +00004066SDOperand
4067X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4068 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4069 switch (IntNo) {
4070 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00004071 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00004072 case Intrinsic::x86_sse_comieq_ss:
4073 case Intrinsic::x86_sse_comilt_ss:
4074 case Intrinsic::x86_sse_comile_ss:
4075 case Intrinsic::x86_sse_comigt_ss:
4076 case Intrinsic::x86_sse_comige_ss:
4077 case Intrinsic::x86_sse_comineq_ss:
4078 case Intrinsic::x86_sse_ucomieq_ss:
4079 case Intrinsic::x86_sse_ucomilt_ss:
4080 case Intrinsic::x86_sse_ucomile_ss:
4081 case Intrinsic::x86_sse_ucomigt_ss:
4082 case Intrinsic::x86_sse_ucomige_ss:
4083 case Intrinsic::x86_sse_ucomineq_ss:
4084 case Intrinsic::x86_sse2_comieq_sd:
4085 case Intrinsic::x86_sse2_comilt_sd:
4086 case Intrinsic::x86_sse2_comile_sd:
4087 case Intrinsic::x86_sse2_comigt_sd:
4088 case Intrinsic::x86_sse2_comige_sd:
4089 case Intrinsic::x86_sse2_comineq_sd:
4090 case Intrinsic::x86_sse2_ucomieq_sd:
4091 case Intrinsic::x86_sse2_ucomilt_sd:
4092 case Intrinsic::x86_sse2_ucomile_sd:
4093 case Intrinsic::x86_sse2_ucomigt_sd:
4094 case Intrinsic::x86_sse2_ucomige_sd:
4095 case Intrinsic::x86_sse2_ucomineq_sd: {
4096 unsigned Opc = 0;
4097 ISD::CondCode CC = ISD::SETCC_INVALID;
4098 switch (IntNo) {
4099 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004100 case Intrinsic::x86_sse_comieq_ss:
4101 case Intrinsic::x86_sse2_comieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004102 Opc = X86ISD::COMI;
4103 CC = ISD::SETEQ;
4104 break;
Evan Cheng78038292006-04-05 23:38:46 +00004105 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004106 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004107 Opc = X86ISD::COMI;
4108 CC = ISD::SETLT;
4109 break;
4110 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004111 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004112 Opc = X86ISD::COMI;
4113 CC = ISD::SETLE;
4114 break;
4115 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004116 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004117 Opc = X86ISD::COMI;
4118 CC = ISD::SETGT;
4119 break;
4120 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004121 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004122 Opc = X86ISD::COMI;
4123 CC = ISD::SETGE;
4124 break;
4125 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004126 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004127 Opc = X86ISD::COMI;
4128 CC = ISD::SETNE;
4129 break;
4130 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004131 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004132 Opc = X86ISD::UCOMI;
4133 CC = ISD::SETEQ;
4134 break;
4135 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004136 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004137 Opc = X86ISD::UCOMI;
4138 CC = ISD::SETLT;
4139 break;
4140 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004141 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004142 Opc = X86ISD::UCOMI;
4143 CC = ISD::SETLE;
4144 break;
4145 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004146 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004147 Opc = X86ISD::UCOMI;
4148 CC = ISD::SETGT;
4149 break;
4150 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00004151 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00004152 Opc = X86ISD::UCOMI;
4153 CC = ISD::SETGE;
4154 break;
4155 case Intrinsic::x86_sse_ucomineq_ss:
4156 case Intrinsic::x86_sse2_ucomineq_sd:
4157 Opc = X86ISD::UCOMI;
4158 CC = ISD::SETNE;
4159 break;
Evan Cheng78038292006-04-05 23:38:46 +00004160 }
Evan Cheng4259a0f2006-09-11 02:19:56 +00004161
Evan Chenga9467aa2006-04-25 20:13:52 +00004162 unsigned X86CC;
Chris Lattner7a627672006-09-13 03:22:10 +00004163 SDOperand LHS = Op.getOperand(1);
4164 SDOperand RHS = Op.getOperand(2);
4165 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004166
4167 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattner7a627672006-09-13 03:22:10 +00004168 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng4259a0f2006-09-11 02:19:56 +00004169 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4170 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4171 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4172 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Chenga9467aa2006-04-25 20:13:52 +00004173 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00004174 }
Evan Cheng5c59d492005-12-23 07:31:11 +00004175 }
Chris Lattner76ac0682005-11-15 00:40:23 +00004176}
Evan Cheng6af02632005-12-20 06:22:03 +00004177
Nate Begemaneda59972007-01-29 22:58:52 +00004178SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4179 // Depths > 0 not supported yet!
4180 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4181 return SDOperand();
4182
4183 // Just load the return address
4184 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4185 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4186}
4187
4188SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4189 // Depths > 0 not supported yet!
4190 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4191 return SDOperand();
4192
4193 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4194 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4195 DAG.getConstant(4, getPointerTy()));
4196}
4197
Evan Chenga9467aa2006-04-25 20:13:52 +00004198/// LowerOperation - Provide custom lowering hooks for some operations.
4199///
4200SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4201 switch (Op.getOpcode()) {
4202 default: assert(0 && "Should not custom lower this!");
4203 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4204 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4205 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4206 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4207 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4208 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4209 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00004210 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004211 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4212 case ISD::SHL_PARTS:
4213 case ISD::SRA_PARTS:
4214 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4215 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4216 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4217 case ISD::FABS: return LowerFABS(Op, DAG);
4218 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng4363e882007-01-05 07:55:56 +00004219 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng4259a0f2006-09-11 02:19:56 +00004220 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Chenga9467aa2006-04-25 20:13:52 +00004221 case ISD::SELECT: return LowerSELECT(Op, DAG);
4222 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4223 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng2a330942006-05-25 00:59:30 +00004224 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004225 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00004226 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004227 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4228 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4229 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4230 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengdeaea252007-03-02 23:16:35 +00004231 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004232 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemaneda59972007-01-29 22:58:52 +00004233 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4234 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov8b7aab02007-04-17 09:20:00 +00004235 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00004236 }
Jim Laskey3796abe2007-02-21 22:54:50 +00004237 return SDOperand();
Evan Chenga9467aa2006-04-25 20:13:52 +00004238}
4239
Evan Cheng6af02632005-12-20 06:22:03 +00004240const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4241 switch (Opcode) {
4242 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00004243 case X86ISD::SHLD: return "X86ISD::SHLD";
4244 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00004245 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng4363e882007-01-05 07:55:56 +00004246 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng72d5c252006-01-31 22:28:30 +00004247 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng4363e882007-01-05 07:55:56 +00004248 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Cheng6305e502006-01-12 22:54:21 +00004249 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00004250 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00004251 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4252 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4253 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00004254 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00004255 case X86ISD::FST: return "X86ISD::FST";
4256 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00004257 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00004258 case X86ISD::CALL: return "X86ISD::CALL";
4259 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4260 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4261 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng78038292006-04-05 23:38:46 +00004262 case X86ISD::COMI: return "X86ISD::COMI";
4263 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00004264 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00004265 case X86ISD::CMOV: return "X86ISD::CMOV";
4266 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00004267 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00004268 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4269 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00004270 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5987cfb2006-07-07 08:33:52 +00004271 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
Evan Cheng5588de92006-02-18 00:15:05 +00004272 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00004273 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00004274 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00004275 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00004276 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng49683ba2006-11-10 21:43:37 +00004277 case X86ISD::FMAX: return "X86ISD::FMAX";
4278 case X86ISD::FMIN: return "X86ISD::FMIN";
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00004279 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4280 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Evan Cheng6af02632005-12-20 06:22:03 +00004281 }
4282}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004283
Chris Lattner1eb94d92007-03-30 23:15:24 +00004284// isLegalAddressingMode - Return true if the addressing mode represented
4285// by AM is legal for this target, for a load/store of the specified type.
4286bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4287 const Type *Ty) const {
4288 // X86 supports extremely general addressing modes.
4289
4290 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4291 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4292 return false;
4293
4294 if (AM.BaseGV) {
4295 // X86-64 only supports addr of globals in small code model.
4296 if (Subtarget->is64Bit() &&
4297 getTargetMachine().getCodeModel() != CodeModel::Small)
4298 return false;
4299
4300 // We can only fold this if we don't need a load either.
4301 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4302 return false;
4303 }
4304
4305 switch (AM.Scale) {
4306 case 0:
4307 case 1:
4308 case 2:
4309 case 4:
4310 case 8:
4311 // These scales always work.
4312 break;
4313 case 3:
4314 case 5:
4315 case 9:
4316 // These scales are formed with basereg+scalereg. Only accept if there is
4317 // no basereg yet.
4318 if (AM.HasBaseReg)
4319 return false;
4320 break;
4321 default: // Other stuff never works.
4322 return false;
4323 }
4324
4325 return true;
4326}
4327
4328
Evan Cheng02612422006-07-05 22:17:51 +00004329/// isShuffleMaskLegal - Targets can use this to indicate that they only
4330/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4331/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4332/// are assumed to be legal.
4333bool
4334X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4335 // Only do shuffles on 128-bit vector types for now.
4336 if (MVT::getSizeInBits(VT) == 64) return false;
4337 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengcea02ff2007-06-19 00:02:56 +00004338 isIdentityMask(Mask.Val) ||
4339 isIdentityMask(Mask.Val, true) ||
Evan Cheng02612422006-07-05 22:17:51 +00004340 isSplatMask(Mask.Val) ||
4341 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4342 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengcea02ff2007-06-19 00:02:56 +00004343 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng02612422006-07-05 22:17:51 +00004344 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Chengcea02ff2007-06-19 00:02:56 +00004345 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng02612422006-07-05 22:17:51 +00004346}
4347
4348bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4349 MVT::ValueType EVT,
4350 SelectionDAG &DAG) const {
4351 unsigned NumElts = BVOps.size();
4352 // Only do shuffles on 128-bit vector types for now.
4353 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4354 if (NumElts == 2) return true;
4355 if (NumElts == 4) {
Chris Lattner35a08552007-02-25 07:10:00 +00004356 return (isMOVLMask(&BVOps[0], 4) ||
4357 isCommutedMOVL(&BVOps[0], 4, true) ||
4358 isSHUFPMask(&BVOps[0], 4) ||
4359 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng02612422006-07-05 22:17:51 +00004360 }
4361 return false;
4362}
4363
4364//===----------------------------------------------------------------------===//
4365// X86 Scheduler Hooks
4366//===----------------------------------------------------------------------===//
4367
4368MachineBasicBlock *
4369X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4370 MachineBasicBlock *BB) {
Evan Cheng20350c42006-11-27 23:37:22 +00004371 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng02612422006-07-05 22:17:51 +00004372 switch (MI->getOpcode()) {
4373 default: assert(false && "Unexpected instr type to insert");
4374 case X86::CMOV_FR32:
4375 case X86::CMOV_FR64:
4376 case X86::CMOV_V4F32:
4377 case X86::CMOV_V2F64:
4378 case X86::CMOV_V2I64: {
4379 // To "insert" a SELECT_CC instruction, we actually have to insert the
4380 // diamond control-flow pattern. The incoming instruction knows the
4381 // destination vreg to set, the condition code register to branch on, the
4382 // true/false values to select between, and a branch opcode to use.
4383 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4384 ilist<MachineBasicBlock>::iterator It = BB;
4385 ++It;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004386
Evan Cheng02612422006-07-05 22:17:51 +00004387 // thisMBB:
4388 // ...
4389 // TrueVal = ...
4390 // cmpTY ccX, r1, r2
4391 // bCC copy1MBB
4392 // fallthrough --> copy0MBB
4393 MachineBasicBlock *thisMBB = BB;
4394 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4395 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004396 unsigned Opc =
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004397 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Cheng20350c42006-11-27 23:37:22 +00004398 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng02612422006-07-05 22:17:51 +00004399 MachineFunction *F = BB->getParent();
4400 F->getBasicBlockList().insert(It, copy0MBB);
4401 F->getBasicBlockList().insert(It, sinkMBB);
4402 // Update machine-CFG edges by first adding all successors of the current
4403 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004404 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng02612422006-07-05 22:17:51 +00004405 e = BB->succ_end(); i != e; ++i)
4406 sinkMBB->addSuccessor(*i);
4407 // Next, remove all successors of the current block, and add the true
4408 // and fallthrough blocks as its successors.
4409 while(!BB->succ_empty())
4410 BB->removeSuccessor(BB->succ_begin());
4411 BB->addSuccessor(copy0MBB);
4412 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004413
Evan Cheng02612422006-07-05 22:17:51 +00004414 // copy0MBB:
4415 // %FalseValue = ...
4416 // # fallthrough to sinkMBB
4417 BB = copy0MBB;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004418
Evan Cheng02612422006-07-05 22:17:51 +00004419 // Update machine-CFG edges
4420 BB->addSuccessor(sinkMBB);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004421
Evan Cheng02612422006-07-05 22:17:51 +00004422 // sinkMBB:
4423 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4424 // ...
4425 BB = sinkMBB;
Evan Cheng20350c42006-11-27 23:37:22 +00004426 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng02612422006-07-05 22:17:51 +00004427 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4428 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4429
4430 delete MI; // The pseudo instruction is gone now.
4431 return BB;
4432 }
4433
4434 case X86::FP_TO_INT16_IN_MEM:
4435 case X86::FP_TO_INT32_IN_MEM:
4436 case X86::FP_TO_INT64_IN_MEM: {
4437 // Change the floating point control register to use "round towards zero"
4438 // mode when truncating to an integer value.
4439 MachineFunction *F = BB->getParent();
4440 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Cheng20350c42006-11-27 23:37:22 +00004441 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004442
4443 // Load the old value of the high byte of the control word...
4444 unsigned OldCW =
4445 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng20350c42006-11-27 23:37:22 +00004446 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004447
4448 // Set the high part to be round to zero...
Evan Cheng20350c42006-11-27 23:37:22 +00004449 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4450 .addImm(0xC7F);
Evan Cheng02612422006-07-05 22:17:51 +00004451
4452 // Reload the modified control word now...
Evan Cheng20350c42006-11-27 23:37:22 +00004453 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004454
4455 // Restore the memory image of control word to original value
Evan Cheng20350c42006-11-27 23:37:22 +00004456 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4457 .addReg(OldCW);
Evan Cheng02612422006-07-05 22:17:51 +00004458
4459 // Get the X86 opcode to use.
4460 unsigned Opc;
4461 switch (MI->getOpcode()) {
4462 default: assert(0 && "illegal opcode!");
4463 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4464 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4465 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4466 }
4467
4468 X86AddressMode AM;
4469 MachineOperand &Op = MI->getOperand(0);
4470 if (Op.isRegister()) {
4471 AM.BaseType = X86AddressMode::RegBase;
4472 AM.Base.Reg = Op.getReg();
4473 } else {
4474 AM.BaseType = X86AddressMode::FrameIndexBase;
4475 AM.Base.FrameIndex = Op.getFrameIndex();
4476 }
4477 Op = MI->getOperand(1);
4478 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004479 AM.Scale = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004480 Op = MI->getOperand(2);
4481 if (Op.isImmediate())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004482 AM.IndexReg = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004483 Op = MI->getOperand(3);
4484 if (Op.isGlobalAddress()) {
4485 AM.GV = Op.getGlobal();
4486 } else {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00004487 AM.Disp = Op.getImm();
Evan Cheng02612422006-07-05 22:17:51 +00004488 }
Evan Cheng20350c42006-11-27 23:37:22 +00004489 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4490 .addReg(MI->getOperand(4).getReg());
Evan Cheng02612422006-07-05 22:17:51 +00004491
4492 // Reload the original control word now.
Evan Cheng20350c42006-11-27 23:37:22 +00004493 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng02612422006-07-05 22:17:51 +00004494
4495 delete MI; // The pseudo instruction is gone now.
4496 return BB;
4497 }
4498 }
4499}
4500
4501//===----------------------------------------------------------------------===//
4502// X86 Optimization Hooks
4503//===----------------------------------------------------------------------===//
4504
Nate Begeman8a77efe2006-02-16 21:11:51 +00004505void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4506 uint64_t Mask,
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004507 uint64_t &KnownZero,
Nate Begeman8a77efe2006-02-16 21:11:51 +00004508 uint64_t &KnownOne,
4509 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004510 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00004511 assert((Opc >= ISD::BUILTIN_OP_END ||
4512 Opc == ISD::INTRINSIC_WO_CHAIN ||
4513 Opc == ISD::INTRINSIC_W_CHAIN ||
4514 Opc == ISD::INTRINSIC_VOID) &&
4515 "Should use MaskedValueIsZero if you don't know whether Op"
4516 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004517
Evan Cheng6d196db2006-04-05 06:11:20 +00004518 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004519 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00004520 default: break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004521 case X86ISD::SETCC:
Nate Begeman8a77efe2006-02-16 21:11:51 +00004522 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4523 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004524 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00004525}
Chris Lattnerc642aa52006-01-31 19:43:35 +00004526
Evan Cheng5987cfb2006-07-07 08:33:52 +00004527/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4528/// element of the result of the vector shuffle.
4529static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4530 MVT::ValueType VT = N->getValueType(0);
4531 SDOperand PermMask = N->getOperand(2);
4532 unsigned NumElems = PermMask.getNumOperands();
4533 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4534 i %= NumElems;
4535 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4536 return (i == 0)
Dan Gohman5c441312007-06-14 22:58:02 +00004537 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng5987cfb2006-07-07 08:33:52 +00004538 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4539 SDOperand Idx = PermMask.getOperand(i);
4540 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman5c441312007-06-14 22:58:02 +00004541 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng5987cfb2006-07-07 08:33:52 +00004542 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4543 }
4544 return SDOperand();
4545}
4546
4547/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4548/// node is a GlobalAddress + an offset.
4549static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Chengae1cd752006-11-30 21:55:46 +00004550 unsigned Opc = N->getOpcode();
Evan Cheng62cdc3f2006-12-05 04:01:03 +00004551 if (Opc == X86ISD::Wrapper) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004552 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4553 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4554 return true;
4555 }
Evan Chengae1cd752006-11-30 21:55:46 +00004556 } else if (Opc == ISD::ADD) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004557 SDOperand N1 = N->getOperand(0);
4558 SDOperand N2 = N->getOperand(1);
4559 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4560 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4561 if (V) {
4562 Offset += V->getSignExtended();
4563 return true;
4564 }
4565 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4566 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4567 if (V) {
4568 Offset += V->getSignExtended();
4569 return true;
4570 }
4571 }
4572 }
4573 return false;
4574}
4575
4576/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4577/// + Dist * Size.
4578static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4579 MachineFrameInfo *MFI) {
4580 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4581 return false;
4582
4583 SDOperand Loc = N->getOperand(1);
4584 SDOperand BaseLoc = Base->getOperand(1);
4585 if (Loc.getOpcode() == ISD::FrameIndex) {
4586 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4587 return false;
4588 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4589 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4590 int FS = MFI->getObjectSize(FI);
4591 int BFS = MFI->getObjectSize(BFI);
4592 if (FS != BFS || FS != Size) return false;
4593 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4594 } else {
4595 GlobalValue *GV1 = NULL;
4596 GlobalValue *GV2 = NULL;
4597 int64_t Offset1 = 0;
4598 int64_t Offset2 = 0;
4599 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4600 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4601 if (isGA1 && isGA2 && GV1 == GV2)
4602 return Offset1 == (Offset2 + Dist*Size);
4603 }
4604
4605 return false;
4606}
4607
Evan Cheng79cf9a52006-07-10 21:37:44 +00004608static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4609 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004610 GlobalValue *GV;
4611 int64_t Offset;
4612 if (isGAPlusOffset(Base, GV, Offset))
4613 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4614 else {
4615 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4616 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng79cf9a52006-07-10 21:37:44 +00004617 if (BFI < 0)
4618 // Fixed objects do not specify alignment, however the offsets are known.
4619 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4620 (MFI->getObjectOffset(BFI) % 16) == 0);
4621 else
4622 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng5987cfb2006-07-07 08:33:52 +00004623 }
4624 return false;
4625}
4626
4627
4628/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4629/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4630/// if the load addresses are consecutive, non-overlapping, and in the right
4631/// order.
Evan Cheng79cf9a52006-07-10 21:37:44 +00004632static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4633 const X86Subtarget *Subtarget) {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004634 MachineFunction &MF = DAG.getMachineFunction();
4635 MachineFrameInfo *MFI = MF.getFrameInfo();
4636 MVT::ValueType VT = N->getValueType(0);
Dan Gohman5c441312007-06-14 22:58:02 +00004637 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng5987cfb2006-07-07 08:33:52 +00004638 SDOperand PermMask = N->getOperand(2);
4639 int NumElems = (int)PermMask.getNumOperands();
4640 SDNode *Base = NULL;
4641 for (int i = 0; i < NumElems; ++i) {
4642 SDOperand Idx = PermMask.getOperand(i);
4643 if (Idx.getOpcode() == ISD::UNDEF) {
4644 if (!Base) return SDOperand();
4645 } else {
4646 SDOperand Arg =
4647 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004648 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng5987cfb2006-07-07 08:33:52 +00004649 return SDOperand();
4650 if (!Base)
4651 Base = Arg.Val;
4652 else if (!isConsecutiveLoad(Arg.Val, Base,
4653 i, MVT::getSizeInBits(EVT)/8,MFI))
4654 return SDOperand();
4655 }
4656 }
4657
Evan Cheng79cf9a52006-07-10 21:37:44 +00004658 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Evan Chenge71fe34d2006-10-09 20:57:25 +00004659 if (isAlign16) {
4660 LoadSDNode *LD = cast<LoadSDNode>(Base);
4661 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4662 LD->getSrcValueOffset());
4663 } else {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004664 // Just use movups, it's shorter.
Chris Lattnere56fef92007-02-25 06:40:16 +00004665 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
Evan Chengbd1c5a82006-08-11 09:08:15 +00004666 SmallVector<SDOperand, 3> Ops;
4667 Ops.push_back(Base->getOperand(0));
4668 Ops.push_back(Base->getOperand(1));
4669 Ops.push_back(Base->getOperand(2));
Evan Cheng5987cfb2006-07-07 08:33:52 +00004670 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengbd1c5a82006-08-11 09:08:15 +00004671 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
Evan Cheng5c68bba2006-08-11 07:35:45 +00004672 }
Evan Cheng5987cfb2006-07-07 08:33:52 +00004673}
4674
Chris Lattner9259b1e2006-10-04 06:57:07 +00004675/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4676static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4677 const X86Subtarget *Subtarget) {
4678 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004679
Chris Lattner9259b1e2006-10-04 06:57:07 +00004680 // If we have SSE[12] support, try to form min/max nodes.
4681 if (Subtarget->hasSSE2() &&
4682 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4683 if (Cond.getOpcode() == ISD::SETCC) {
4684 // Get the LHS/RHS of the select.
4685 SDOperand LHS = N->getOperand(1);
4686 SDOperand RHS = N->getOperand(2);
4687 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004688
Evan Cheng49683ba2006-11-10 21:43:37 +00004689 unsigned Opcode = 0;
Chris Lattner9259b1e2006-10-04 06:57:07 +00004690 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004691 switch (CC) {
4692 default: break;
4693 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4694 case ISD::SETULE:
4695 case ISD::SETLE:
4696 if (!UnsafeFPMath) break;
4697 // FALL THROUGH.
4698 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4699 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00004700 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004701 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004702
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004703 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4704 case ISD::SETUGT:
4705 case ISD::SETGT:
4706 if (!UnsafeFPMath) break;
4707 // FALL THROUGH.
4708 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4709 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00004710 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004711 break;
4712 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00004713 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004714 switch (CC) {
4715 default: break;
4716 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4717 case ISD::SETUGT:
4718 case ISD::SETGT:
4719 if (!UnsafeFPMath) break;
4720 // FALL THROUGH.
4721 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4722 case ISD::SETGE:
Evan Cheng49683ba2006-11-10 21:43:37 +00004723 Opcode = X86ISD::FMIN;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004724 break;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004725
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004726 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4727 case ISD::SETULE:
4728 case ISD::SETLE:
4729 if (!UnsafeFPMath) break;
4730 // FALL THROUGH.
4731 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4732 case ISD::SETLT:
Evan Cheng49683ba2006-11-10 21:43:37 +00004733 Opcode = X86ISD::FMAX;
Chris Lattnerf2ef2432006-10-05 04:11:26 +00004734 break;
4735 }
Chris Lattner9259b1e2006-10-04 06:57:07 +00004736 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004737
Evan Cheng49683ba2006-11-10 21:43:37 +00004738 if (Opcode)
4739 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner9259b1e2006-10-04 06:57:07 +00004740 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004741
Chris Lattner9259b1e2006-10-04 06:57:07 +00004742 }
4743
4744 return SDOperand();
4745}
4746
4747
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004748SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng5987cfb2006-07-07 08:33:52 +00004749 DAGCombinerInfo &DCI) const {
Evan Cheng5987cfb2006-07-07 08:33:52 +00004750 SelectionDAG &DAG = DCI.DAG;
4751 switch (N->getOpcode()) {
4752 default: break;
4753 case ISD::VECTOR_SHUFFLE:
Evan Cheng79cf9a52006-07-10 21:37:44 +00004754 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner9259b1e2006-10-04 06:57:07 +00004755 case ISD::SELECT:
4756 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng5987cfb2006-07-07 08:33:52 +00004757 }
4758
4759 return SDOperand();
4760}
4761
Evan Cheng02612422006-07-05 22:17:51 +00004762//===----------------------------------------------------------------------===//
4763// X86 Inline Assembly Support
4764//===----------------------------------------------------------------------===//
4765
Chris Lattner298ef372006-07-11 02:54:03 +00004766/// getConstraintType - Given a constraint letter, return the type of
4767/// constraint it is for this target.
4768X86TargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +00004769X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4770 if (Constraint.size() == 1) {
4771 switch (Constraint[0]) {
4772 case 'A':
4773 case 'r':
4774 case 'R':
4775 case 'l':
4776 case 'q':
4777 case 'Q':
4778 case 'x':
4779 case 'Y':
4780 return C_RegisterClass;
4781 default:
4782 break;
4783 }
Chris Lattner298ef372006-07-11 02:54:03 +00004784 }
Chris Lattnerd6855142007-03-25 02:14:49 +00004785 return TargetLowering::getConstraintType(Constraint);
Chris Lattner298ef372006-07-11 02:54:03 +00004786}
4787
Chris Lattner44daa502006-10-31 20:13:11 +00004788/// isOperandValidForConstraint - Return the specified operand (possibly
4789/// modified) if the specified SDOperand is valid for the specified target
4790/// constraint letter, otherwise return null.
4791SDOperand X86TargetLowering::
4792isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4793 switch (Constraint) {
4794 default: break;
Devang Patelb38c2ec2007-03-17 00:13:28 +00004795 case 'I':
Chris Lattner03a643a2007-03-25 01:57:35 +00004796 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4797 if (C->getValue() <= 31)
Chris Lattnerc8798d02007-05-15 01:28:08 +00004798 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
Devang Patelb38c2ec2007-03-17 00:13:28 +00004799 }
Chris Lattner03a643a2007-03-25 01:57:35 +00004800 return SDOperand(0,0);
4801 case 'N':
4802 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4803 if (C->getValue() <= 255)
Chris Lattnerc8798d02007-05-15 01:28:08 +00004804 return DAG.getTargetConstant(C->getValue(), Op.getValueType());
Chris Lattner03a643a2007-03-25 01:57:35 +00004805 }
4806 return SDOperand(0,0);
Chris Lattner83df45a2007-05-03 16:52:29 +00004807 case 'i': {
Chris Lattner44daa502006-10-31 20:13:11 +00004808 // Literal immediates are always ok.
Chris Lattnerc8798d02007-05-15 01:28:08 +00004809 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op))
4810 return DAG.getTargetConstant(CST->getValue(), Op.getValueType());
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004811
Chris Lattner83df45a2007-05-03 16:52:29 +00004812 // If we are in non-pic codegen mode, we allow the address of a global (with
4813 // an optional displacement) to be used with 'i'.
4814 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
4815 int64_t Offset = 0;
4816
4817 // Match either (GA) or (GA+C)
4818 if (GA) {
4819 Offset = GA->getOffset();
4820 } else if (Op.getOpcode() == ISD::ADD) {
4821 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4822 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4823 if (C && GA) {
4824 Offset = GA->getOffset()+C->getValue();
4825 } else {
4826 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4827 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
4828 if (C && GA)
4829 Offset = GA->getOffset()+C->getValue();
4830 else
4831 C = 0, GA = 0;
4832 }
4833 }
4834
4835 if (GA) {
4836 // If addressing this global requires a load (e.g. in PIC mode), we can't
4837 // match.
4838 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
4839 false))
Chris Lattner44daa502006-10-31 20:13:11 +00004840 return SDOperand(0, 0);
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004841
Chris Lattner83df45a2007-05-03 16:52:29 +00004842 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4843 Offset);
Chris Lattner44daa502006-10-31 20:13:11 +00004844 return Op;
4845 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004846
Chris Lattner44daa502006-10-31 20:13:11 +00004847 // Otherwise, not valid for this mode.
4848 return SDOperand(0, 0);
4849 }
Chris Lattner83df45a2007-05-03 16:52:29 +00004850 }
Chris Lattner44daa502006-10-31 20:13:11 +00004851 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4852}
4853
Chris Lattnerc642aa52006-01-31 19:43:35 +00004854std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00004855getRegClassForInlineAsmConstraint(const std::string &Constraint,
4856 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00004857 if (Constraint.size() == 1) {
4858 // FIXME: not handling fp-stack yet!
Chris Lattnerc642aa52006-01-31 19:43:35 +00004859 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattner298ef372006-07-11 02:54:03 +00004860 default: break; // Unknown constraint letter
4861 case 'A': // EAX/EDX
4862 if (VT == MVT::i32 || VT == MVT::i64)
4863 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4864 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004865 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4866 case 'Q': // Q_REGS
Chris Lattner6d4a2dc2006-05-06 00:29:37 +00004867 if (VT == MVT::i32)
4868 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4869 else if (VT == MVT::i16)
4870 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4871 else if (VT == MVT::i8)
4872 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4873 break;
Chris Lattnerc642aa52006-01-31 19:43:35 +00004874 }
4875 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004876
Chris Lattner7ad77df2006-02-22 00:56:39 +00004877 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00004878}
Chris Lattner524129d2006-07-31 23:26:50 +00004879
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004880std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner524129d2006-07-31 23:26:50 +00004881X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4882 MVT::ValueType VT) const {
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004883 // First, see if this is a constraint that directly corresponds to an LLVM
4884 // register class.
4885 if (Constraint.size() == 1) {
4886 // GCC Constraint Letters
4887 switch (Constraint[0]) {
4888 default: break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00004889 case 'r': // GENERAL_REGS
4890 case 'R': // LEGACY_REGS
4891 case 'l': // INDEX_REGS
4892 if (VT == MVT::i64 && Subtarget->is64Bit())
4893 return std::make_pair(0U, X86::GR64RegisterClass);
4894 if (VT == MVT::i32)
4895 return std::make_pair(0U, X86::GR32RegisterClass);
4896 else if (VT == MVT::i16)
4897 return std::make_pair(0U, X86::GR16RegisterClass);
4898 else if (VT == MVT::i8)
4899 return std::make_pair(0U, X86::GR8RegisterClass);
4900 break;
Chris Lattner2805bce2007-04-12 04:14:49 +00004901 case 'y': // MMX_REGS if MMX allowed.
4902 if (!Subtarget->hasMMX()) break;
4903 return std::make_pair(0U, X86::VR64RegisterClass);
4904 break;
Chris Lattner7451e4d2007-04-09 05:49:22 +00004905 case 'Y': // SSE_REGS if SSE2 allowed
4906 if (!Subtarget->hasSSE2()) break;
4907 // FALL THROUGH.
4908 case 'x': // SSE_REGS if SSE1 allowed
4909 if (!Subtarget->hasSSE1()) break;
4910
4911 switch (VT) {
4912 default: break;
4913 // Scalar SSE types.
4914 case MVT::f32:
4915 case MVT::i32:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004916 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00004917 case MVT::f64:
4918 case MVT::i64:
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004919 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner7451e4d2007-04-09 05:49:22 +00004920 // Vector types.
4921 case MVT::Vector:
4922 case MVT::v16i8:
4923 case MVT::v8i16:
4924 case MVT::v4i32:
4925 case MVT::v2i64:
4926 case MVT::v4f32:
4927 case MVT::v2f64:
4928 return std::make_pair(0U, X86::VR128RegisterClass);
4929 }
Chris Lattner590ed5e5b2007-04-09 05:11:28 +00004930 break;
4931 }
4932 }
4933
Chris Lattner524129d2006-07-31 23:26:50 +00004934 // Use the default implementation in TargetLowering to convert the register
4935 // constraint into a member of a register class.
4936 std::pair<unsigned, const TargetRegisterClass*> Res;
4937 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerf6a69662006-10-31 19:42:44 +00004938
4939 // Not found as a standard register?
4940 if (Res.second == 0) {
4941 // GCC calls "st(0)" just plain "st".
4942 if (StringsEqualNoCase("{st}", Constraint)) {
4943 Res.first = X86::ST0;
4944 Res.second = X86::RSTRegisterClass;
4945 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004946
Chris Lattnerf6a69662006-10-31 19:42:44 +00004947 return Res;
4948 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004949
Chris Lattner524129d2006-07-31 23:26:50 +00004950 // Otherwise, check to see if this is a register class of the wrong value
4951 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4952 // turn into {ax},{dx}.
4953 if (Res.second->hasType(VT))
4954 return Res; // Correct type already, nothing to do.
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004955
Chris Lattner524129d2006-07-31 23:26:50 +00004956 // All of the single-register GCC register classes map their values onto
4957 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4958 // really want an 8-bit or 32-bit register, map to the appropriate register
4959 // class and return the appropriate register.
4960 if (Res.second != X86::GR16RegisterClass)
4961 return Res;
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00004962
Chris Lattner524129d2006-07-31 23:26:50 +00004963 if (VT == MVT::i8) {
4964 unsigned DestReg = 0;
4965 switch (Res.first) {
4966 default: break;
4967 case X86::AX: DestReg = X86::AL; break;
4968 case X86::DX: DestReg = X86::DL; break;
4969 case X86::CX: DestReg = X86::CL; break;
4970 case X86::BX: DestReg = X86::BL; break;
4971 }
4972 if (DestReg) {
4973 Res.first = DestReg;
4974 Res.second = Res.second = X86::GR8RegisterClass;
4975 }
4976 } else if (VT == MVT::i32) {
4977 unsigned DestReg = 0;
4978 switch (Res.first) {
4979 default: break;
4980 case X86::AX: DestReg = X86::EAX; break;
4981 case X86::DX: DestReg = X86::EDX; break;
4982 case X86::CX: DestReg = X86::ECX; break;
4983 case X86::BX: DestReg = X86::EBX; break;
4984 case X86::SI: DestReg = X86::ESI; break;
4985 case X86::DI: DestReg = X86::EDI; break;
4986 case X86::BP: DestReg = X86::EBP; break;
4987 case X86::SP: DestReg = X86::ESP; break;
4988 }
4989 if (DestReg) {
4990 Res.first = DestReg;
4991 Res.second = Res.second = X86::GR32RegisterClass;
4992 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00004993 } else if (VT == MVT::i64) {
4994 unsigned DestReg = 0;
4995 switch (Res.first) {
4996 default: break;
4997 case X86::AX: DestReg = X86::RAX; break;
4998 case X86::DX: DestReg = X86::RDX; break;
4999 case X86::CX: DestReg = X86::RCX; break;
5000 case X86::BX: DestReg = X86::RBX; break;
5001 case X86::SI: DestReg = X86::RSI; break;
5002 case X86::DI: DestReg = X86::RDI; break;
5003 case X86::BP: DestReg = X86::RBP; break;
5004 case X86::SP: DestReg = X86::RSP; break;
5005 }
5006 if (DestReg) {
5007 Res.first = DestReg;
5008 Res.second = Res.second = X86::GR64RegisterClass;
5009 }
Chris Lattner524129d2006-07-31 23:26:50 +00005010 }
Anton Korobeynikov5b96cde2006-11-21 00:01:06 +00005011
Chris Lattner524129d2006-07-31 23:26:50 +00005012 return Res;
5013}