blob: 855167360fa7e825a838f8e8a4520580d37073ba [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000022#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000025#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000026#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000027#include "llvm/LLVMContext.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000028#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000038#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000041#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000042#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000043#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000044#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000045using namespace llvm;
46
Mon P Wang3c81d352008-11-23 04:37:22 +000047static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000048DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000049
Evan Cheng10e86422008-04-25 19:11:04 +000050// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000051static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000052 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000053
Chris Lattnerf0144122009-07-28 03:13:23 +000054static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
55 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
56 default: llvm_unreachable("unknown subtarget type");
57 case X86Subtarget::isDarwin:
Chris Lattnerf26e03b2009-07-31 17:42:42 +000058 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000059 case X86Subtarget::isELF:
60 return new TargetLoweringObjectFileELF();
61 case X86Subtarget::isMingw:
62 case X86Subtarget::isCygwin:
63 case X86Subtarget::isWindows:
64 return new TargetLoweringObjectFileCOFF();
65 }
66
67}
68
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000069X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000070 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000071 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000072 X86ScalarSSEf64 = Subtarget->hasSSE2();
73 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000074 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000075
Anton Korobeynikov2365f512007-07-14 14:06:15 +000076 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000077 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000078
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079 // Set up the TargetLowering object.
80
81 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Andersone50ed302009-08-10 22:56:29 +000082 setShiftAmountType(EVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000083 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000084 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000085 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000086
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000087 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000088 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000089 setUseUnderscoreSetJmp(false);
90 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000091 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000092 // MS runtime is weird: it exports _setjmp, but longjmp!
93 setUseUnderscoreSetJmp(true);
94 setUseUnderscoreLongJmp(false);
95 } else {
96 setUseUnderscoreSetJmp(true);
97 setUseUnderscoreLongJmp(true);
98 }
Scott Michelfdc40a02009-02-17 22:15:04 +000099
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000100 // Set up the register classes.
Owen Andersone50ed302009-08-10 22:56:29 +0000101 addRegisterClass(EVT::i8, X86::GR8RegisterClass);
102 addRegisterClass(EVT::i16, X86::GR16RegisterClass);
103 addRegisterClass(EVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000104 if (Subtarget->is64Bit())
Owen Andersone50ed302009-08-10 22:56:29 +0000105 addRegisterClass(EVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000106
Owen Andersone50ed302009-08-10 22:56:29 +0000107 setLoadExtAction(ISD::SEXTLOAD, EVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000108
Scott Michelfdc40a02009-02-17 22:15:04 +0000109 // We don't accept any truncstore of integer registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000110 setTruncStoreAction(EVT::i64, EVT::i32, Expand);
111 setTruncStoreAction(EVT::i64, EVT::i16, Expand);
112 setTruncStoreAction(EVT::i64, EVT::i8 , Expand);
113 setTruncStoreAction(EVT::i32, EVT::i16, Expand);
114 setTruncStoreAction(EVT::i32, EVT::i8 , Expand);
115 setTruncStoreAction(EVT::i16, EVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000116
117 // SETOEQ and SETUNE require checking two conditions.
Owen Andersone50ed302009-08-10 22:56:29 +0000118 setCondCodeAction(ISD::SETOEQ, EVT::f32, Expand);
119 setCondCodeAction(ISD::SETOEQ, EVT::f64, Expand);
120 setCondCodeAction(ISD::SETOEQ, EVT::f80, Expand);
121 setCondCodeAction(ISD::SETUNE, EVT::f32, Expand);
122 setCondCodeAction(ISD::SETUNE, EVT::f64, Expand);
123 setCondCodeAction(ISD::SETUNE, EVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000124
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000125 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
126 // operation.
Owen Andersone50ed302009-08-10 22:56:29 +0000127 setOperationAction(ISD::UINT_TO_FP , EVT::i1 , Promote);
128 setOperationAction(ISD::UINT_TO_FP , EVT::i8 , Promote);
129 setOperationAction(ISD::UINT_TO_FP , EVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000130
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000132 setOperationAction(ISD::UINT_TO_FP , EVT::i32 , Promote);
133 setOperationAction(ISD::UINT_TO_FP , EVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000134 } else if (!UseSoftFloat) {
135 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000136 // We have an impenetrably clever algorithm for ui64->double only.
Owen Andersone50ed302009-08-10 22:56:29 +0000137 setOperationAction(ISD::UINT_TO_FP , EVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000138 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000139 // We have an algorithm for SSE2, and we turn this into a 64-bit
140 // FILD for other targets.
Owen Andersone50ed302009-08-10 22:56:29 +0000141 setOperationAction(ISD::UINT_TO_FP , EVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000142 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143
144 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
145 // this operation.
Owen Andersone50ed302009-08-10 22:56:29 +0000146 setOperationAction(ISD::SINT_TO_FP , EVT::i1 , Promote);
147 setOperationAction(ISD::SINT_TO_FP , EVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000148
Devang Patel6a784892009-06-05 18:48:29 +0000149 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000150 // SSE has no i16 to fp conversion, only i32
151 if (X86ScalarSSEf32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000152 setOperationAction(ISD::SINT_TO_FP , EVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000153 // f32 and f64 cases are Legal, f80 case is not
Owen Andersone50ed302009-08-10 22:56:29 +0000154 setOperationAction(ISD::SINT_TO_FP , EVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000155 } else {
Owen Andersone50ed302009-08-10 22:56:29 +0000156 setOperationAction(ISD::SINT_TO_FP , EVT::i16 , Custom);
157 setOperationAction(ISD::SINT_TO_FP , EVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000158 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000159 } else {
Owen Andersone50ed302009-08-10 22:56:29 +0000160 setOperationAction(ISD::SINT_TO_FP , EVT::i16 , Promote);
161 setOperationAction(ISD::SINT_TO_FP , EVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000162 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000163
Dale Johannesen73328d12007-09-19 23:55:34 +0000164 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
165 // are Legal, f80 is custom lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000166 setOperationAction(ISD::FP_TO_SINT , EVT::i64 , Custom);
167 setOperationAction(ISD::SINT_TO_FP , EVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000168
Evan Cheng02568ff2006-01-30 22:13:22 +0000169 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
170 // this operation.
Owen Andersone50ed302009-08-10 22:56:29 +0000171 setOperationAction(ISD::FP_TO_SINT , EVT::i1 , Promote);
172 setOperationAction(ISD::FP_TO_SINT , EVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000173
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000174 if (X86ScalarSSEf32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000175 setOperationAction(ISD::FP_TO_SINT , EVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000176 // f32 and f64 cases are Legal, f80 case is not
Owen Andersone50ed302009-08-10 22:56:29 +0000177 setOperationAction(ISD::FP_TO_SINT , EVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000178 } else {
Owen Andersone50ed302009-08-10 22:56:29 +0000179 setOperationAction(ISD::FP_TO_SINT , EVT::i16 , Custom);
180 setOperationAction(ISD::FP_TO_SINT , EVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181 }
182
183 // Handle FP_TO_UINT by promoting the destination to a larger signed
184 // conversion.
Owen Andersone50ed302009-08-10 22:56:29 +0000185 setOperationAction(ISD::FP_TO_UINT , EVT::i1 , Promote);
186 setOperationAction(ISD::FP_TO_UINT , EVT::i8 , Promote);
187 setOperationAction(ISD::FP_TO_UINT , EVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
Evan Cheng25ab6902006-09-08 06:48:29 +0000189 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000190 setOperationAction(ISD::FP_TO_UINT , EVT::i64 , Expand);
191 setOperationAction(ISD::FP_TO_UINT , EVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000192 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000193 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000194 // Expand FP_TO_UINT into a select.
195 // FIXME: We would like to use a Custom expander here eventually to do
196 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Andersone50ed302009-08-10 22:56:29 +0000197 setOperationAction(ISD::FP_TO_UINT , EVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000199 // With SSE3 we can use fisttpll to convert to a signed i64; without
200 // SSE, we're stuck with a fistpll.
Owen Andersone50ed302009-08-10 22:56:29 +0000201 setOperationAction(ISD::FP_TO_UINT , EVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000203
Chris Lattner399610a2006-12-05 18:22:22 +0000204 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000205 if (!X86ScalarSSEf64) {
Owen Andersone50ed302009-08-10 22:56:29 +0000206 setOperationAction(ISD::BIT_CONVERT , EVT::f32 , Expand);
207 setOperationAction(ISD::BIT_CONVERT , EVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000208 }
Chris Lattner21f66852005-12-23 05:15:23 +0000209
Dan Gohmanb00ee212008-02-18 19:34:53 +0000210 // Scalar integer divide and remainder are lowered to use operations that
211 // produce two results, to match the available instructions. This exposes
212 // the two-result form to trivial CSE, which is able to combine x/y and x%y
213 // into a single instruction.
214 //
215 // Scalar integer multiply-high is also lowered to use two-result
216 // operations, to match the available instructions. However, plain multiply
217 // (low) operations are left as Legal, as there are single-result
218 // instructions for this in x86. Using the two-result multiply instructions
219 // when both high and low results are needed must be arranged by dagcombine.
Owen Andersone50ed302009-08-10 22:56:29 +0000220 setOperationAction(ISD::MULHS , EVT::i8 , Expand);
221 setOperationAction(ISD::MULHU , EVT::i8 , Expand);
222 setOperationAction(ISD::SDIV , EVT::i8 , Expand);
223 setOperationAction(ISD::UDIV , EVT::i8 , Expand);
224 setOperationAction(ISD::SREM , EVT::i8 , Expand);
225 setOperationAction(ISD::UREM , EVT::i8 , Expand);
226 setOperationAction(ISD::MULHS , EVT::i16 , Expand);
227 setOperationAction(ISD::MULHU , EVT::i16 , Expand);
228 setOperationAction(ISD::SDIV , EVT::i16 , Expand);
229 setOperationAction(ISD::UDIV , EVT::i16 , Expand);
230 setOperationAction(ISD::SREM , EVT::i16 , Expand);
231 setOperationAction(ISD::UREM , EVT::i16 , Expand);
232 setOperationAction(ISD::MULHS , EVT::i32 , Expand);
233 setOperationAction(ISD::MULHU , EVT::i32 , Expand);
234 setOperationAction(ISD::SDIV , EVT::i32 , Expand);
235 setOperationAction(ISD::UDIV , EVT::i32 , Expand);
236 setOperationAction(ISD::SREM , EVT::i32 , Expand);
237 setOperationAction(ISD::UREM , EVT::i32 , Expand);
238 setOperationAction(ISD::MULHS , EVT::i64 , Expand);
239 setOperationAction(ISD::MULHU , EVT::i64 , Expand);
240 setOperationAction(ISD::SDIV , EVT::i64 , Expand);
241 setOperationAction(ISD::UDIV , EVT::i64 , Expand);
242 setOperationAction(ISD::SREM , EVT::i64 , Expand);
243 setOperationAction(ISD::UREM , EVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000244
Owen Andersone50ed302009-08-10 22:56:29 +0000245 setOperationAction(ISD::BR_JT , EVT::Other, Expand);
246 setOperationAction(ISD::BRCOND , EVT::Other, Custom);
247 setOperationAction(ISD::BR_CC , EVT::Other, Expand);
248 setOperationAction(ISD::SELECT_CC , EVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit())
Owen Andersone50ed302009-08-10 22:56:29 +0000250 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i32, Legal);
251 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i16 , Legal);
252 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i8 , Legal);
253 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i1 , Expand);
254 setOperationAction(ISD::FP_ROUND_INREG , EVT::f32 , Expand);
255 setOperationAction(ISD::FREM , EVT::f32 , Expand);
256 setOperationAction(ISD::FREM , EVT::f64 , Expand);
257 setOperationAction(ISD::FREM , EVT::f80 , Expand);
258 setOperationAction(ISD::FLT_ROUNDS_ , EVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000259
Owen Andersone50ed302009-08-10 22:56:29 +0000260 setOperationAction(ISD::CTPOP , EVT::i8 , Expand);
261 setOperationAction(ISD::CTTZ , EVT::i8 , Custom);
262 setOperationAction(ISD::CTLZ , EVT::i8 , Custom);
263 setOperationAction(ISD::CTPOP , EVT::i16 , Expand);
264 setOperationAction(ISD::CTTZ , EVT::i16 , Custom);
265 setOperationAction(ISD::CTLZ , EVT::i16 , Custom);
266 setOperationAction(ISD::CTPOP , EVT::i32 , Expand);
267 setOperationAction(ISD::CTTZ , EVT::i32 , Custom);
268 setOperationAction(ISD::CTLZ , EVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000270 setOperationAction(ISD::CTPOP , EVT::i64 , Expand);
271 setOperationAction(ISD::CTTZ , EVT::i64 , Custom);
272 setOperationAction(ISD::CTLZ , EVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 }
274
Owen Andersone50ed302009-08-10 22:56:29 +0000275 setOperationAction(ISD::READCYCLECOUNTER , EVT::i64 , Custom);
276 setOperationAction(ISD::BSWAP , EVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000277
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000278 // These should be promoted to a larger select which is supported.
Owen Andersone50ed302009-08-10 22:56:29 +0000279 setOperationAction(ISD::SELECT , EVT::i1 , Promote);
280 setOperationAction(ISD::SELECT , EVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000281 // X86 wants to expand cmov itself.
Owen Andersone50ed302009-08-10 22:56:29 +0000282 setOperationAction(ISD::SELECT , EVT::i16 , Custom);
283 setOperationAction(ISD::SELECT , EVT::i32 , Custom);
284 setOperationAction(ISD::SELECT , EVT::f32 , Custom);
285 setOperationAction(ISD::SELECT , EVT::f64 , Custom);
286 setOperationAction(ISD::SELECT , EVT::f80 , Custom);
287 setOperationAction(ISD::SETCC , EVT::i8 , Custom);
288 setOperationAction(ISD::SETCC , EVT::i16 , Custom);
289 setOperationAction(ISD::SETCC , EVT::i32 , Custom);
290 setOperationAction(ISD::SETCC , EVT::f32 , Custom);
291 setOperationAction(ISD::SETCC , EVT::f64 , Custom);
292 setOperationAction(ISD::SETCC , EVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000294 setOperationAction(ISD::SELECT , EVT::i64 , Custom);
295 setOperationAction(ISD::SETCC , EVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000296 }
Owen Andersone50ed302009-08-10 22:56:29 +0000297 setOperationAction(ISD::EH_RETURN , EVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000298
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000299 // Darwin ABI issue.
Owen Andersone50ed302009-08-10 22:56:29 +0000300 setOperationAction(ISD::ConstantPool , EVT::i32 , Custom);
301 setOperationAction(ISD::JumpTable , EVT::i32 , Custom);
302 setOperationAction(ISD::GlobalAddress , EVT::i32 , Custom);
303 setOperationAction(ISD::GlobalTLSAddress, EVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000304 if (Subtarget->is64Bit())
Owen Andersone50ed302009-08-10 22:56:29 +0000305 setOperationAction(ISD::GlobalTLSAddress, EVT::i64, Custom);
306 setOperationAction(ISD::ExternalSymbol , EVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000307 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000308 setOperationAction(ISD::ConstantPool , EVT::i64 , Custom);
309 setOperationAction(ISD::JumpTable , EVT::i64 , Custom);
310 setOperationAction(ISD::GlobalAddress , EVT::i64 , Custom);
311 setOperationAction(ISD::ExternalSymbol, EVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000313 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Andersone50ed302009-08-10 22:56:29 +0000314 setOperationAction(ISD::SHL_PARTS , EVT::i32 , Custom);
315 setOperationAction(ISD::SRA_PARTS , EVT::i32 , Custom);
316 setOperationAction(ISD::SRL_PARTS , EVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000317 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000318 setOperationAction(ISD::SHL_PARTS , EVT::i64 , Custom);
319 setOperationAction(ISD::SRA_PARTS , EVT::i64 , Custom);
320 setOperationAction(ISD::SRL_PARTS , EVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000321 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000322
Evan Chengd2cde682008-03-10 19:38:10 +0000323 if (Subtarget->hasSSE1())
Owen Andersone50ed302009-08-10 22:56:29 +0000324 setOperationAction(ISD::PREFETCH , EVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000325
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000326 if (!Subtarget->hasSSE2())
Owen Andersone50ed302009-08-10 22:56:29 +0000327 setOperationAction(ISD::MEMBARRIER , EVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000328
Mon P Wang63307c32008-05-05 19:05:59 +0000329 // Expand certain atomics
Owen Andersone50ed302009-08-10 22:56:29 +0000330 setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i8, Custom);
331 setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i16, Custom);
332 setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i32, Custom);
333 setOperationAction(ISD::ATOMIC_CMP_SWAP, EVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000334
Owen Andersone50ed302009-08-10 22:56:29 +0000335 setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i8, Custom);
336 setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i16, Custom);
337 setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i32, Custom);
338 setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000339
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000340 if (!Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000341 setOperationAction(ISD::ATOMIC_LOAD_ADD, EVT::i64, Custom);
342 setOperationAction(ISD::ATOMIC_LOAD_SUB, EVT::i64, Custom);
343 setOperationAction(ISD::ATOMIC_LOAD_AND, EVT::i64, Custom);
344 setOperationAction(ISD::ATOMIC_LOAD_OR, EVT::i64, Custom);
345 setOperationAction(ISD::ATOMIC_LOAD_XOR, EVT::i64, Custom);
346 setOperationAction(ISD::ATOMIC_LOAD_NAND, EVT::i64, Custom);
347 setOperationAction(ISD::ATOMIC_SWAP, EVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000348 }
349
Dan Gohman7f460202008-06-30 20:59:49 +0000350 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
Owen Andersone50ed302009-08-10 22:56:29 +0000351 setOperationAction(ISD::DBG_STOPPOINT, EVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000352 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000353 if (!Subtarget->isTargetDarwin() &&
354 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000355 !Subtarget->isTargetCygMing()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000356 setOperationAction(ISD::DBG_LABEL, EVT::Other, Expand);
357 setOperationAction(ISD::EH_LABEL, EVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000358 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000359
Owen Andersone50ed302009-08-10 22:56:29 +0000360 setOperationAction(ISD::EXCEPTIONADDR, EVT::i64, Expand);
361 setOperationAction(ISD::EHSELECTION, EVT::i64, Expand);
362 setOperationAction(ISD::EXCEPTIONADDR, EVT::i32, Expand);
363 setOperationAction(ISD::EHSELECTION, EVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000364 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000365 setExceptionPointerRegister(X86::RAX);
366 setExceptionSelectorRegister(X86::RDX);
367 } else {
368 setExceptionPointerRegister(X86::EAX);
369 setExceptionSelectorRegister(X86::EDX);
370 }
Owen Andersone50ed302009-08-10 22:56:29 +0000371 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, EVT::i32, Custom);
372 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, EVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000373
Owen Andersone50ed302009-08-10 22:56:29 +0000374 setOperationAction(ISD::TRAMPOLINE, EVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000375
Owen Andersone50ed302009-08-10 22:56:29 +0000376 setOperationAction(ISD::TRAP, EVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000377
Nate Begemanacc398c2006-01-25 18:21:52 +0000378 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Andersone50ed302009-08-10 22:56:29 +0000379 setOperationAction(ISD::VASTART , EVT::Other, Custom);
380 setOperationAction(ISD::VAEND , EVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000381 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000382 setOperationAction(ISD::VAARG , EVT::Other, Custom);
383 setOperationAction(ISD::VACOPY , EVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000384 } else {
Owen Andersone50ed302009-08-10 22:56:29 +0000385 setOperationAction(ISD::VAARG , EVT::Other, Expand);
386 setOperationAction(ISD::VACOPY , EVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000387 }
Evan Chengae642192007-03-02 23:16:35 +0000388
Owen Andersone50ed302009-08-10 22:56:29 +0000389 setOperationAction(ISD::STACKSAVE, EVT::Other, Expand);
390 setOperationAction(ISD::STACKRESTORE, EVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000391 if (Subtarget->is64Bit())
Owen Andersone50ed302009-08-10 22:56:29 +0000392 setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000393 if (Subtarget->isTargetCygMing())
Owen Andersone50ed302009-08-10 22:56:29 +0000394 setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000395 else
Owen Andersone50ed302009-08-10 22:56:29 +0000396 setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000397
Evan Chengc7ce29b2009-02-13 22:36:38 +0000398 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000399 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000400 // Set up the FP register classes.
Owen Andersone50ed302009-08-10 22:56:29 +0000401 addRegisterClass(EVT::f32, X86::FR32RegisterClass);
402 addRegisterClass(EVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403
Evan Cheng223547a2006-01-31 22:28:30 +0000404 // Use ANDPD to simulate FABS.
Owen Andersone50ed302009-08-10 22:56:29 +0000405 setOperationAction(ISD::FABS , EVT::f64, Custom);
406 setOperationAction(ISD::FABS , EVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000407
408 // Use XORP to simulate FNEG.
Owen Andersone50ed302009-08-10 22:56:29 +0000409 setOperationAction(ISD::FNEG , EVT::f64, Custom);
410 setOperationAction(ISD::FNEG , EVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000411
Evan Cheng68c47cb2007-01-05 07:55:56 +0000412 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Andersone50ed302009-08-10 22:56:29 +0000413 setOperationAction(ISD::FCOPYSIGN, EVT::f64, Custom);
414 setOperationAction(ISD::FCOPYSIGN, EVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000415
Evan Chengd25e9e82006-02-02 00:28:23 +0000416 // We don't support sin/cos/fmod
Owen Andersone50ed302009-08-10 22:56:29 +0000417 setOperationAction(ISD::FSIN , EVT::f64, Expand);
418 setOperationAction(ISD::FCOS , EVT::f64, Expand);
419 setOperationAction(ISD::FSIN , EVT::f32, Expand);
420 setOperationAction(ISD::FCOS , EVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000421
Chris Lattnera54aa942006-01-29 06:26:08 +0000422 // Expand FP immediates into loads from the stack, except for the special
423 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000424 addLegalFPImmediate(APFloat(+0.0)); // xorpd
425 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000426 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000427 // Use SSE for f32, x87 for f64.
428 // Set up the FP register classes.
Owen Andersone50ed302009-08-10 22:56:29 +0000429 addRegisterClass(EVT::f32, X86::FR32RegisterClass);
430 addRegisterClass(EVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000431
432 // Use ANDPS to simulate FABS.
Owen Andersone50ed302009-08-10 22:56:29 +0000433 setOperationAction(ISD::FABS , EVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000434
435 // Use XORP to simulate FNEG.
Owen Andersone50ed302009-08-10 22:56:29 +0000436 setOperationAction(ISD::FNEG , EVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000437
Owen Andersone50ed302009-08-10 22:56:29 +0000438 setOperationAction(ISD::UNDEF, EVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000439
440 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Andersone50ed302009-08-10 22:56:29 +0000441 setOperationAction(ISD::FCOPYSIGN, EVT::f64, Expand);
442 setOperationAction(ISD::FCOPYSIGN, EVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000443
444 // We don't support sin/cos/fmod
Owen Andersone50ed302009-08-10 22:56:29 +0000445 setOperationAction(ISD::FSIN , EVT::f32, Expand);
446 setOperationAction(ISD::FCOS , EVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000447
Nate Begemane1795842008-02-14 08:57:00 +0000448 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 addLegalFPImmediate(APFloat(+0.0f)); // xorps
450 addLegalFPImmediate(APFloat(+0.0)); // FLD0
451 addLegalFPImmediate(APFloat(+1.0)); // FLD1
452 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
453 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
454
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455 if (!UnsafeFPMath) {
Owen Andersone50ed302009-08-10 22:56:29 +0000456 setOperationAction(ISD::FSIN , EVT::f64 , Expand);
457 setOperationAction(ISD::FCOS , EVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000459 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461 // Set up the FP register classes.
Owen Andersone50ed302009-08-10 22:56:29 +0000462 addRegisterClass(EVT::f64, X86::RFP64RegisterClass);
463 addRegisterClass(EVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000464
Owen Andersone50ed302009-08-10 22:56:29 +0000465 setOperationAction(ISD::UNDEF, EVT::f64, Expand);
466 setOperationAction(ISD::UNDEF, EVT::f32, Expand);
467 setOperationAction(ISD::FCOPYSIGN, EVT::f64, Expand);
468 setOperationAction(ISD::FCOPYSIGN, EVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000469
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000470 if (!UnsafeFPMath) {
Owen Andersone50ed302009-08-10 22:56:29 +0000471 setOperationAction(ISD::FSIN , EVT::f64 , Expand);
472 setOperationAction(ISD::FCOS , EVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000473 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000474 addLegalFPImmediate(APFloat(+0.0)); // FLD0
475 addLegalFPImmediate(APFloat(+1.0)); // FLD1
476 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
477 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000478 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
479 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
480 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
481 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000482 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000483
Dale Johannesen59a58732007-08-05 18:49:15 +0000484 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000485 if (!UseSoftFloat) {
Owen Andersone50ed302009-08-10 22:56:29 +0000486 addRegisterClass(EVT::f80, X86::RFP80RegisterClass);
487 setOperationAction(ISD::UNDEF, EVT::f80, Expand);
488 setOperationAction(ISD::FCOPYSIGN, EVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000489 {
490 bool ignored;
491 APFloat TmpFlt(+0.0);
492 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
493 &ignored);
494 addLegalFPImmediate(TmpFlt); // FLD0
495 TmpFlt.changeSign();
496 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
497 APFloat TmpFlt2(+1.0);
498 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
499 &ignored);
500 addLegalFPImmediate(TmpFlt2); // FLD1
501 TmpFlt2.changeSign();
502 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
503 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000504
Evan Chengc7ce29b2009-02-13 22:36:38 +0000505 if (!UnsafeFPMath) {
Owen Andersone50ed302009-08-10 22:56:29 +0000506 setOperationAction(ISD::FSIN , EVT::f80 , Expand);
507 setOperationAction(ISD::FCOS , EVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000508 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000509 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000510
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000511 // Always use a library call for pow.
Owen Andersone50ed302009-08-10 22:56:29 +0000512 setOperationAction(ISD::FPOW , EVT::f32 , Expand);
513 setOperationAction(ISD::FPOW , EVT::f64 , Expand);
514 setOperationAction(ISD::FPOW , EVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000515
Owen Andersone50ed302009-08-10 22:56:29 +0000516 setOperationAction(ISD::FLOG, EVT::f80, Expand);
517 setOperationAction(ISD::FLOG2, EVT::f80, Expand);
518 setOperationAction(ISD::FLOG10, EVT::f80, Expand);
519 setOperationAction(ISD::FEXP, EVT::f80, Expand);
520 setOperationAction(ISD::FEXP2, EVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000521
Mon P Wangf007a8b2008-11-06 05:31:54 +0000522 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000523 // (for widening) or expand (for scalarization). Then we will selectively
524 // turn on ones that can be effectively codegen'd.
Owen Andersone50ed302009-08-10 22:56:29 +0000525 for (unsigned VT = (unsigned)EVT::FIRST_VECTOR_VALUETYPE;
526 VT <= (unsigned)EVT::LAST_VECTOR_VALUETYPE; ++VT) {
527 setOperationAction(ISD::ADD , (EVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::SUB , (EVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FADD, (EVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FNEG, (EVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FSUB, (EVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::MUL , (EVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FMUL, (EVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::SDIV, (EVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::UDIV, (EVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::FDIV, (EVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::SREM, (EVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::UREM, (EVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::LOAD, (EVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::VECTOR_SHUFFLE, (EVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(EVT::SimpleValueType)VT,Expand);
542 setOperationAction(ISD::EXTRACT_SUBVECTOR,(EVT::SimpleValueType)VT,Expand);
543 setOperationAction(ISD::INSERT_VECTOR_ELT,(EVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FABS, (EVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FSIN, (EVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FCOS, (EVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FREM, (EVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FPOWI, (EVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FSQRT, (EVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FCOPYSIGN, (EVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::SMUL_LOHI, (EVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::UMUL_LOHI, (EVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SDIVREM, (EVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UDIVREM, (EVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FPOW, (EVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::CTPOP, (EVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::CTTZ, (EVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::CTLZ, (EVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SHL, (EVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::SRA, (EVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SRL, (EVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::ROTL, (EVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::ROTR, (EVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::BSWAP, (EVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::VSETCC, (EVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FLOG, (EVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FLOG2, (EVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FLOG10, (EVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FEXP, (EVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FEXP2, (EVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FP_TO_UINT, (EVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FP_TO_SINT, (EVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::UINT_TO_FP, (EVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::SINT_TO_FP, (EVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000575 }
576
Evan Chengc7ce29b2009-02-13 22:36:38 +0000577 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
578 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000579 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000580 addRegisterClass(EVT::v8i8, X86::VR64RegisterClass);
581 addRegisterClass(EVT::v4i16, X86::VR64RegisterClass);
582 addRegisterClass(EVT::v2i32, X86::VR64RegisterClass);
583 addRegisterClass(EVT::v2f32, X86::VR64RegisterClass);
584 addRegisterClass(EVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000585
Owen Andersone50ed302009-08-10 22:56:29 +0000586 setOperationAction(ISD::ADD, EVT::v8i8, Legal);
587 setOperationAction(ISD::ADD, EVT::v4i16, Legal);
588 setOperationAction(ISD::ADD, EVT::v2i32, Legal);
589 setOperationAction(ISD::ADD, EVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000590
Owen Andersone50ed302009-08-10 22:56:29 +0000591 setOperationAction(ISD::SUB, EVT::v8i8, Legal);
592 setOperationAction(ISD::SUB, EVT::v4i16, Legal);
593 setOperationAction(ISD::SUB, EVT::v2i32, Legal);
594 setOperationAction(ISD::SUB, EVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000595
Owen Andersone50ed302009-08-10 22:56:29 +0000596 setOperationAction(ISD::MULHS, EVT::v4i16, Legal);
597 setOperationAction(ISD::MUL, EVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000598
Owen Andersone50ed302009-08-10 22:56:29 +0000599 setOperationAction(ISD::AND, EVT::v8i8, Promote);
600 AddPromotedToType (ISD::AND, EVT::v8i8, EVT::v1i64);
601 setOperationAction(ISD::AND, EVT::v4i16, Promote);
602 AddPromotedToType (ISD::AND, EVT::v4i16, EVT::v1i64);
603 setOperationAction(ISD::AND, EVT::v2i32, Promote);
604 AddPromotedToType (ISD::AND, EVT::v2i32, EVT::v1i64);
605 setOperationAction(ISD::AND, EVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000606
Owen Andersone50ed302009-08-10 22:56:29 +0000607 setOperationAction(ISD::OR, EVT::v8i8, Promote);
608 AddPromotedToType (ISD::OR, EVT::v8i8, EVT::v1i64);
609 setOperationAction(ISD::OR, EVT::v4i16, Promote);
610 AddPromotedToType (ISD::OR, EVT::v4i16, EVT::v1i64);
611 setOperationAction(ISD::OR, EVT::v2i32, Promote);
612 AddPromotedToType (ISD::OR, EVT::v2i32, EVT::v1i64);
613 setOperationAction(ISD::OR, EVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000614
Owen Andersone50ed302009-08-10 22:56:29 +0000615 setOperationAction(ISD::XOR, EVT::v8i8, Promote);
616 AddPromotedToType (ISD::XOR, EVT::v8i8, EVT::v1i64);
617 setOperationAction(ISD::XOR, EVT::v4i16, Promote);
618 AddPromotedToType (ISD::XOR, EVT::v4i16, EVT::v1i64);
619 setOperationAction(ISD::XOR, EVT::v2i32, Promote);
620 AddPromotedToType (ISD::XOR, EVT::v2i32, EVT::v1i64);
621 setOperationAction(ISD::XOR, EVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000622
Owen Andersone50ed302009-08-10 22:56:29 +0000623 setOperationAction(ISD::LOAD, EVT::v8i8, Promote);
624 AddPromotedToType (ISD::LOAD, EVT::v8i8, EVT::v1i64);
625 setOperationAction(ISD::LOAD, EVT::v4i16, Promote);
626 AddPromotedToType (ISD::LOAD, EVT::v4i16, EVT::v1i64);
627 setOperationAction(ISD::LOAD, EVT::v2i32, Promote);
628 AddPromotedToType (ISD::LOAD, EVT::v2i32, EVT::v1i64);
629 setOperationAction(ISD::LOAD, EVT::v2f32, Promote);
630 AddPromotedToType (ISD::LOAD, EVT::v2f32, EVT::v1i64);
631 setOperationAction(ISD::LOAD, EVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000632
Owen Andersone50ed302009-08-10 22:56:29 +0000633 setOperationAction(ISD::BUILD_VECTOR, EVT::v8i8, Custom);
634 setOperationAction(ISD::BUILD_VECTOR, EVT::v4i16, Custom);
635 setOperationAction(ISD::BUILD_VECTOR, EVT::v2i32, Custom);
636 setOperationAction(ISD::BUILD_VECTOR, EVT::v2f32, Custom);
637 setOperationAction(ISD::BUILD_VECTOR, EVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000638
Owen Andersone50ed302009-08-10 22:56:29 +0000639 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v8i8, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v4i16, Custom);
641 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v2i32, Custom);
642 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000643
Owen Andersone50ed302009-08-10 22:56:29 +0000644 setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v2f32, Custom);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v8i8, Custom);
646 setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v4i16, Custom);
647 setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000648
Owen Andersone50ed302009-08-10 22:56:29 +0000649 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000650
Owen Andersone50ed302009-08-10 22:56:29 +0000651 setTruncStoreAction(EVT::v8i16, EVT::v8i8, Expand);
652 setOperationAction(ISD::TRUNCATE, EVT::v8i8, Expand);
653 setOperationAction(ISD::SELECT, EVT::v8i8, Promote);
654 setOperationAction(ISD::SELECT, EVT::v4i16, Promote);
655 setOperationAction(ISD::SELECT, EVT::v2i32, Promote);
656 setOperationAction(ISD::SELECT, EVT::v1i64, Custom);
657 setOperationAction(ISD::VSETCC, EVT::v8i8, Custom);
658 setOperationAction(ISD::VSETCC, EVT::v4i16, Custom);
659 setOperationAction(ISD::VSETCC, EVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000660 }
661
Evan Cheng92722532009-03-26 23:06:32 +0000662 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000663 addRegisterClass(EVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000664
Owen Andersone50ed302009-08-10 22:56:29 +0000665 setOperationAction(ISD::FADD, EVT::v4f32, Legal);
666 setOperationAction(ISD::FSUB, EVT::v4f32, Legal);
667 setOperationAction(ISD::FMUL, EVT::v4f32, Legal);
668 setOperationAction(ISD::FDIV, EVT::v4f32, Legal);
669 setOperationAction(ISD::FSQRT, EVT::v4f32, Legal);
670 setOperationAction(ISD::FNEG, EVT::v4f32, Custom);
671 setOperationAction(ISD::LOAD, EVT::v4f32, Legal);
672 setOperationAction(ISD::BUILD_VECTOR, EVT::v4f32, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v4f32, Custom);
674 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4f32, Custom);
675 setOperationAction(ISD::SELECT, EVT::v4f32, Custom);
676 setOperationAction(ISD::VSETCC, EVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000677 }
678
Evan Cheng92722532009-03-26 23:06:32 +0000679 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000680 addRegisterClass(EVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000681
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000682 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
683 // registers cannot be used even for integer operations.
Owen Andersone50ed302009-08-10 22:56:29 +0000684 addRegisterClass(EVT::v16i8, X86::VR128RegisterClass);
685 addRegisterClass(EVT::v8i16, X86::VR128RegisterClass);
686 addRegisterClass(EVT::v4i32, X86::VR128RegisterClass);
687 addRegisterClass(EVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000688
Owen Andersone50ed302009-08-10 22:56:29 +0000689 setOperationAction(ISD::ADD, EVT::v16i8, Legal);
690 setOperationAction(ISD::ADD, EVT::v8i16, Legal);
691 setOperationAction(ISD::ADD, EVT::v4i32, Legal);
692 setOperationAction(ISD::ADD, EVT::v2i64, Legal);
693 setOperationAction(ISD::MUL, EVT::v2i64, Custom);
694 setOperationAction(ISD::SUB, EVT::v16i8, Legal);
695 setOperationAction(ISD::SUB, EVT::v8i16, Legal);
696 setOperationAction(ISD::SUB, EVT::v4i32, Legal);
697 setOperationAction(ISD::SUB, EVT::v2i64, Legal);
698 setOperationAction(ISD::MUL, EVT::v8i16, Legal);
699 setOperationAction(ISD::FADD, EVT::v2f64, Legal);
700 setOperationAction(ISD::FSUB, EVT::v2f64, Legal);
701 setOperationAction(ISD::FMUL, EVT::v2f64, Legal);
702 setOperationAction(ISD::FDIV, EVT::v2f64, Legal);
703 setOperationAction(ISD::FSQRT, EVT::v2f64, Legal);
704 setOperationAction(ISD::FNEG, EVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000705
Owen Andersone50ed302009-08-10 22:56:29 +0000706 setOperationAction(ISD::VSETCC, EVT::v2f64, Custom);
707 setOperationAction(ISD::VSETCC, EVT::v16i8, Custom);
708 setOperationAction(ISD::VSETCC, EVT::v8i16, Custom);
709 setOperationAction(ISD::VSETCC, EVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000710
Owen Andersone50ed302009-08-10 22:56:29 +0000711 setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v16i8, Custom);
712 setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v8i16, Custom);
713 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v8i16, Custom);
714 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4i32, Custom);
715 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000716
Evan Cheng2c3ae372006-04-12 21:21:57 +0000717 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Andersone50ed302009-08-10 22:56:29 +0000718 for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v2i64; ++i) {
719 EVT VT = (EVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000720 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000723 // Do not attempt to custom lower non-128-bit vectors
724 if (!VT.is128BitVector())
725 continue;
Owen Anderson70671842009-08-10 20:18:46 +0000726 setOperationAction(ISD::BUILD_VECTOR, VT.getSimpleVT(), Custom);
727 setOperationAction(ISD::VECTOR_SHUFFLE, VT.getSimpleVT(), Custom);
728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT.getSimpleVT(), Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000729 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000730
Owen Andersone50ed302009-08-10 22:56:29 +0000731 setOperationAction(ISD::BUILD_VECTOR, EVT::v2f64, Custom);
732 setOperationAction(ISD::BUILD_VECTOR, EVT::v2i64, Custom);
733 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v2f64, Custom);
734 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v2i64, Custom);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v2f64, Custom);
736 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000737
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000739 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v2i64, Custom);
740 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000741 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000742
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000743 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Andersone50ed302009-08-10 22:56:29 +0000744 for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v2i64; i++) {
745 EVT::SimpleValueType SVT = (EVT::SimpleValueType)i;
746 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000747
748 // Do not attempt to promote non-128-bit vectors
749 if (!VT.is128BitVector()) {
750 continue;
751 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000752 setOperationAction(ISD::AND, SVT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000753 AddPromotedToType (ISD::AND, SVT, EVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000754 setOperationAction(ISD::OR, SVT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000755 AddPromotedToType (ISD::OR, SVT, EVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000756 setOperationAction(ISD::XOR, SVT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000757 AddPromotedToType (ISD::XOR, SVT, EVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000758 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000759 AddPromotedToType (ISD::LOAD, SVT, EVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000760 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000761 AddPromotedToType (ISD::SELECT, SVT, EVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000762 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763
Owen Andersone50ed302009-08-10 22:56:29 +0000764 setTruncStoreAction(EVT::f64, EVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000765
Evan Cheng2c3ae372006-04-12 21:21:57 +0000766 // Custom lower v2i64 and v2f64 selects.
Owen Andersone50ed302009-08-10 22:56:29 +0000767 setOperationAction(ISD::LOAD, EVT::v2f64, Legal);
768 setOperationAction(ISD::LOAD, EVT::v2i64, Legal);
769 setOperationAction(ISD::SELECT, EVT::v2f64, Custom);
770 setOperationAction(ISD::SELECT, EVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000771
Owen Andersone50ed302009-08-10 22:56:29 +0000772 setOperationAction(ISD::FP_TO_SINT, EVT::v4i32, Legal);
773 setOperationAction(ISD::SINT_TO_FP, EVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000774 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000775 setOperationAction(ISD::FP_TO_SINT, EVT::v2i32, Custom);
776 setOperationAction(ISD::SINT_TO_FP, EVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000777 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000778 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000779
Nate Begeman14d12ca2008-02-11 04:19:36 +0000780 if (Subtarget->hasSSE41()) {
781 // FIXME: Do we need to handle scalar-to-vector here?
Owen Andersone50ed302009-08-10 22:56:29 +0000782 setOperationAction(ISD::MUL, EVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000783
784 // i8 and i16 vectors are custom , because the source register and source
785 // source memory operand types are not the same width. f32 vectors are
786 // custom since the immediate controlling the insert encodes additional
787 // information.
Owen Andersone50ed302009-08-10 22:56:29 +0000788 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v16i8, Custom);
789 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v8i16, Custom);
790 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4i32, Custom);
791 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792
Owen Andersone50ed302009-08-10 22:56:29 +0000793 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v16i8, Custom);
794 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v8i16, Custom);
795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4i32, Custom);
796 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000797
798 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000799 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v2i64, Legal);
800 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000801 }
802 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000803
Nate Begeman30a0de92008-07-17 16:51:19 +0000804 if (Subtarget->hasSSE42()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000805 setOperationAction(ISD::VSETCC, EVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000806 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000807
David Greene9b9838d2009-06-29 16:47:10 +0000808 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000809 addRegisterClass(EVT::v8f32, X86::VR256RegisterClass);
810 addRegisterClass(EVT::v4f64, X86::VR256RegisterClass);
811 addRegisterClass(EVT::v8i32, X86::VR256RegisterClass);
812 addRegisterClass(EVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000813
Owen Andersone50ed302009-08-10 22:56:29 +0000814 setOperationAction(ISD::LOAD, EVT::v8f32, Legal);
815 setOperationAction(ISD::LOAD, EVT::v8i32, Legal);
816 setOperationAction(ISD::LOAD, EVT::v4f64, Legal);
817 setOperationAction(ISD::LOAD, EVT::v4i64, Legal);
818 setOperationAction(ISD::FADD, EVT::v8f32, Legal);
819 setOperationAction(ISD::FSUB, EVT::v8f32, Legal);
820 setOperationAction(ISD::FMUL, EVT::v8f32, Legal);
821 setOperationAction(ISD::FDIV, EVT::v8f32, Legal);
822 setOperationAction(ISD::FSQRT, EVT::v8f32, Legal);
823 setOperationAction(ISD::FNEG, EVT::v8f32, Custom);
824 //setOperationAction(ISD::BUILD_VECTOR, EVT::v8f32, Custom);
825 //setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v8f32, Custom);
826 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v8f32, Custom);
827 //setOperationAction(ISD::SELECT, EVT::v8f32, Custom);
828 //setOperationAction(ISD::VSETCC, EVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000829
830 // Operations to consider commented out -v16i16 v32i8
Owen Andersone50ed302009-08-10 22:56:29 +0000831 //setOperationAction(ISD::ADD, EVT::v16i16, Legal);
832 setOperationAction(ISD::ADD, EVT::v8i32, Custom);
833 setOperationAction(ISD::ADD, EVT::v4i64, Custom);
834 //setOperationAction(ISD::SUB, EVT::v32i8, Legal);
835 //setOperationAction(ISD::SUB, EVT::v16i16, Legal);
836 setOperationAction(ISD::SUB, EVT::v8i32, Custom);
837 setOperationAction(ISD::SUB, EVT::v4i64, Custom);
838 //setOperationAction(ISD::MUL, EVT::v16i16, Legal);
839 setOperationAction(ISD::FADD, EVT::v4f64, Legal);
840 setOperationAction(ISD::FSUB, EVT::v4f64, Legal);
841 setOperationAction(ISD::FMUL, EVT::v4f64, Legal);
842 setOperationAction(ISD::FDIV, EVT::v4f64, Legal);
843 setOperationAction(ISD::FSQRT, EVT::v4f64, Legal);
844 setOperationAction(ISD::FNEG, EVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000845
Owen Andersone50ed302009-08-10 22:56:29 +0000846 setOperationAction(ISD::VSETCC, EVT::v4f64, Custom);
847 // setOperationAction(ISD::VSETCC, EVT::v32i8, Custom);
848 // setOperationAction(ISD::VSETCC, EVT::v16i16, Custom);
849 setOperationAction(ISD::VSETCC, EVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000850
Owen Andersone50ed302009-08-10 22:56:29 +0000851 // setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v32i8, Custom);
852 // setOperationAction(ISD::SCALAR_TO_VECTOR, EVT::v16i16, Custom);
853 // setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v16i16, Custom);
854 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v8i32, Custom);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000856
Owen Andersone50ed302009-08-10 22:56:29 +0000857 setOperationAction(ISD::BUILD_VECTOR, EVT::v4f64, Custom);
858 setOperationAction(ISD::BUILD_VECTOR, EVT::v4i64, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v4f64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, EVT::v4i64, Custom);
861 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4f64, Custom);
862 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000863
864#if 0
865 // Not sure we want to do this since there are no 256-bit integer
866 // operations in AVX
867
868 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
869 // This includes 256-bit vectors
Owen Andersone50ed302009-08-10 22:56:29 +0000870 for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v4i64; ++i) {
871 EVT VT = (EVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000872
873 // Do not attempt to custom lower non-power-of-2 vectors
874 if (!isPowerOf2_32(VT.getVectorNumElements()))
875 continue;
876
877 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
878 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
879 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
880 }
881
882 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000883 setOperationAction(ISD::INSERT_VECTOR_ELT, EVT::v4i64, Custom);
884 setOperationAction(ISD::EXTRACT_VECTOR_ELT, EVT::v4i64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000885 }
886#endif
887
888#if 0
889 // Not sure we want to do this since there are no 256-bit integer
890 // operations in AVX
891
892 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
893 // Including 256-bit vectors
Owen Andersone50ed302009-08-10 22:56:29 +0000894 for (unsigned i = (unsigned)EVT::v16i8; i != (unsigned)EVT::v4i64; i++) {
895 EVT VT = (EVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000896
897 if (!VT.is256BitVector()) {
898 continue;
899 }
900 setOperationAction(ISD::AND, VT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000901 AddPromotedToType (ISD::AND, VT, EVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000902 setOperationAction(ISD::OR, VT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000903 AddPromotedToType (ISD::OR, VT, EVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000904 setOperationAction(ISD::XOR, VT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000905 AddPromotedToType (ISD::XOR, VT, EVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000906 setOperationAction(ISD::LOAD, VT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000907 AddPromotedToType (ISD::LOAD, VT, EVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000908 setOperationAction(ISD::SELECT, VT, Promote);
Owen Andersone50ed302009-08-10 22:56:29 +0000909 AddPromotedToType (ISD::SELECT, VT, EVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000910 }
911
Owen Andersone50ed302009-08-10 22:56:29 +0000912 setTruncStoreAction(EVT::f64, EVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000913#endif
914 }
915
Evan Cheng6be2c582006-04-05 23:38:46 +0000916 // We want to custom lower some of our intrinsics.
Owen Andersone50ed302009-08-10 22:56:29 +0000917 setOperationAction(ISD::INTRINSIC_WO_CHAIN, EVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000918
Bill Wendling74c37652008-12-09 22:08:41 +0000919 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000920 setOperationAction(ISD::SADDO, EVT::i32, Custom);
921 setOperationAction(ISD::SADDO, EVT::i64, Custom);
922 setOperationAction(ISD::UADDO, EVT::i32, Custom);
923 setOperationAction(ISD::UADDO, EVT::i64, Custom);
924 setOperationAction(ISD::SSUBO, EVT::i32, Custom);
925 setOperationAction(ISD::SSUBO, EVT::i64, Custom);
926 setOperationAction(ISD::USUBO, EVT::i32, Custom);
927 setOperationAction(ISD::USUBO, EVT::i64, Custom);
928 setOperationAction(ISD::SMULO, EVT::i32, Custom);
929 setOperationAction(ISD::SMULO, EVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000930
Evan Chengd54f2d52009-03-31 19:38:51 +0000931 if (!Subtarget->is64Bit()) {
932 // These libcalls are not available in 32-bit.
933 setLibcallName(RTLIB::SHL_I128, 0);
934 setLibcallName(RTLIB::SRL_I128, 0);
935 setLibcallName(RTLIB::SRA_I128, 0);
936 }
937
Evan Cheng206ee9d2006-07-07 08:33:52 +0000938 // We have target-specific dag combine patterns for the following nodes:
939 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000940 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000941 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000942 setTargetDAGCombine(ISD::SHL);
943 setTargetDAGCombine(ISD::SRA);
944 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000945 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000946 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000947 if (Subtarget->is64Bit())
948 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000949
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000950 computeRegisterProperties();
951
Evan Cheng87ed7162006-02-14 08:25:08 +0000952 // FIXME: These should be based on subtarget info. Plus, the values should
953 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000954 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
955 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
956 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000957 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000958 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000959 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000960}
961
Scott Michel5b8f82e2008-03-10 15:42:14 +0000962
Owen Andersone50ed302009-08-10 22:56:29 +0000963EVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
964 return EVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000965}
966
967
Evan Cheng29286502008-01-23 23:17:41 +0000968/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
969/// the desired ByVal argument alignment.
970static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
971 if (MaxAlign == 16)
972 return;
973 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
974 if (VTy->getBitWidth() == 128)
975 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000976 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
977 unsigned EltAlign = 0;
978 getMaxByValAlign(ATy->getElementType(), EltAlign);
979 if (EltAlign > MaxAlign)
980 MaxAlign = EltAlign;
981 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
982 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
983 unsigned EltAlign = 0;
984 getMaxByValAlign(STy->getElementType(i), EltAlign);
985 if (EltAlign > MaxAlign)
986 MaxAlign = EltAlign;
987 if (MaxAlign == 16)
988 break;
989 }
990 }
991 return;
992}
993
994/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
995/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000996/// that contain SSE vectors are placed at 16-byte boundaries while the rest
997/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000998unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000999 if (Subtarget->is64Bit()) {
1000 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001001 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001002 if (TyAlign > 8)
1003 return TyAlign;
1004 return 8;
1005 }
1006
Evan Cheng29286502008-01-23 23:17:41 +00001007 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001008 if (Subtarget->hasSSE1())
1009 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001010 return Align;
1011}
Chris Lattner2b02a442007-02-25 08:29:00 +00001012
Evan Chengf0df0312008-05-15 08:39:06 +00001013/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001014/// and store operations as a result of memset, memcpy, and memmove
Owen Andersone50ed302009-08-10 22:56:29 +00001015/// lowering. It returns EVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001016/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001017EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001018X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001019 bool isSrcConst, bool isSrcStr,
1020 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001021 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1022 // linux. This is because the stack realignment code can't handle certain
1023 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001024 const Function *F = DAG.getMachineFunction().getFunction();
1025 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1026 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001027 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Andersone50ed302009-08-10 22:56:29 +00001028 return EVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001029 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Andersone50ed302009-08-10 22:56:29 +00001030 return EVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001031 }
Evan Chengf0df0312008-05-15 08:39:06 +00001032 if (Subtarget->is64Bit() && Size >= 8)
Owen Andersone50ed302009-08-10 22:56:29 +00001033 return EVT::i64;
1034 return EVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001035}
1036
Evan Chengcc415862007-11-09 01:32:10 +00001037/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1038/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001039SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001040 SelectionDAG &DAG) const {
1041 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001042 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Chris Lattnere4df7562009-07-09 03:15:51 +00001043 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001044 // This doesn't have DebugLoc associated with it, but is not really the
1045 // same as a Register.
1046 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1047 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001048 return Table;
1049}
1050
Bill Wendlingb4202b82009-07-01 18:50:55 +00001051/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001052unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
1053 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
1054}
1055
Chris Lattner2b02a442007-02-25 08:29:00 +00001056//===----------------------------------------------------------------------===//
1057// Return Value Calling Convention Implementation
1058//===----------------------------------------------------------------------===//
1059
Chris Lattner59ed56b2007-02-28 04:55:35 +00001060#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001061
Dan Gohman98ca4f22009-08-05 01:29:28 +00001062SDValue
1063X86TargetLowering::LowerReturn(SDValue Chain,
1064 unsigned CallConv, bool isVarArg,
1065 const SmallVectorImpl<ISD::OutputArg> &Outs,
1066 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001067
Chris Lattner9774c912007-02-27 05:28:59 +00001068 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001069 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1070 RVLocs, *DAG.getContext());
1071 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001072
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001073 // If this is the first return lowered for this function, add the regs to the
1074 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001075 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001076 for (unsigned i = 0; i != RVLocs.size(); ++i)
1077 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001078 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001080
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001082
Dan Gohman475871a2008-07-27 21:46:04 +00001083 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001084 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1085 // Operand #1 = Bytes To Pop
Owen Andersone50ed302009-08-10 22:56:29 +00001086 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), EVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001087
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001088 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001089 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1090 CCValAssign &VA = RVLocs[i];
1091 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001092 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001093
Chris Lattner447ff682008-03-11 03:23:40 +00001094 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1095 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001096 if (VA.getLocReg() == X86::ST0 ||
1097 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001098 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1099 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001100 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Andersone50ed302009-08-10 22:56:29 +00001101 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, EVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001102 RetOps.push_back(ValToCopy);
1103 // Don't emit a copytoreg.
1104 continue;
1105 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001106
Evan Cheng242b38b2009-02-23 09:03:22 +00001107 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1108 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001109 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001110 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001111 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Andersone50ed302009-08-10 22:56:29 +00001112 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001113 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Andersone50ed302009-08-10 22:56:29 +00001114 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001115 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001116 }
1117
Dale Johannesendd64c412009-02-04 00:33:20 +00001118 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001119 Flag = Chain.getValue(1);
1120 }
Dan Gohman61a92132008-04-21 23:59:07 +00001121
1122 // The x86-64 ABI for returning structs by value requires that we copy
1123 // the sret argument into %rax for the return. We saved the argument into
1124 // a virtual register in the entry block, so now we copy the value out
1125 // and into %rax.
1126 if (Subtarget->is64Bit() &&
1127 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1128 MachineFunction &MF = DAG.getMachineFunction();
1129 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1130 unsigned Reg = FuncInfo->getSRetReturnReg();
1131 if (!Reg) {
Owen Andersone50ed302009-08-10 22:56:29 +00001132 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(EVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001133 FuncInfo->setSRetReturnReg(Reg);
1134 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001135 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001136
Dale Johannesendd64c412009-02-04 00:33:20 +00001137 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001138 Flag = Chain.getValue(1);
1139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001140
Chris Lattner447ff682008-03-11 03:23:40 +00001141 RetOps[0] = Chain; // Update chain.
1142
1143 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001144 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001145 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001146
1147 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00001148 EVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001149}
1150
Dan Gohman98ca4f22009-08-05 01:29:28 +00001151/// LowerCallResult - Lower the result values of a call into the
1152/// appropriate copies out of appropriate physical registers.
1153///
1154SDValue
1155X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1156 unsigned CallConv, bool isVarArg,
1157 const SmallVectorImpl<ISD::InputArg> &Ins,
1158 DebugLoc dl, SelectionDAG &DAG,
1159 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001160
Chris Lattnere32bbf62007-02-28 07:09:55 +00001161 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001162 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001163 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001164 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001165 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001167
Chris Lattner3085e152007-02-25 08:59:22 +00001168 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001169 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001170 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001171 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001172
Torok Edwin3f142c32009-02-01 18:15:56 +00001173 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Andersone50ed302009-08-10 22:56:29 +00001174 if ((CopyVT == EVT::f32 || CopyVT == EVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001176 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001177 }
1178
Chris Lattner8e6da152008-03-10 21:08:41 +00001179 // If this is a call to a function that returns an fp value on the floating
1180 // point stack, but where we prefer to use the value in xmm registers, copy
1181 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001182 if ((VA.getLocReg() == X86::ST0 ||
1183 VA.getLocReg() == X86::ST1) &&
1184 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Andersone50ed302009-08-10 22:56:29 +00001185 CopyVT = EVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001187
Evan Cheng79fb3b42009-02-20 20:43:02 +00001188 SDValue Val;
1189 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001190 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1191 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1192 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Andersone50ed302009-08-10 22:56:29 +00001193 EVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001194 Val = Chain.getValue(0);
Owen Andersone50ed302009-08-10 22:56:29 +00001195 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i64,
1196 Val, DAG.getConstant(0, EVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001197 } else {
1198 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Andersone50ed302009-08-10 22:56:29 +00001199 EVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001200 Val = Chain.getValue(0);
1201 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001202 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1203 } else {
1204 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1205 CopyVT, InFlag).getValue(1);
1206 Val = Chain.getValue(0);
1207 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001208 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001209
Dan Gohman37eed792009-02-04 17:28:58 +00001210 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001211 // Round the F80 the right size, which also moves to the appropriate xmm
1212 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001213 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001214 // This truncation won't change the value.
1215 DAG.getIntPtrConstant(1));
1216 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001217
Dan Gohman98ca4f22009-08-05 01:29:28 +00001218 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001219 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001220
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001222}
1223
1224
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001225//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001226// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001227//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001228// StdCall calling convention seems to be standard for many Windows' API
1229// routines and around. It differs from C calling convention just a little:
1230// callee should clean up the stack, not caller. Symbols should be also
1231// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001232// For info on fast calling convention see Fast Calling Convention (tail call)
1233// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001234
Dan Gohman98ca4f22009-08-05 01:29:28 +00001235/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001236/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001237static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1238 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001239 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001240
Dan Gohman98ca4f22009-08-05 01:29:28 +00001241 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001242}
1243
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001244/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001245/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001246static bool
1247ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1248 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001249 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001250
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001252}
1253
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001254/// IsCalleePop - Determines whether the callee is required to pop its
1255/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001256bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001257 if (IsVarArg)
1258 return false;
1259
Dan Gohman095cc292008-09-13 01:54:27 +00001260 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001261 default:
1262 return false;
1263 case CallingConv::X86_StdCall:
1264 return !Subtarget->is64Bit();
1265 case CallingConv::X86_FastCall:
1266 return !Subtarget->is64Bit();
1267 case CallingConv::Fast:
1268 return PerformTailCallOpt;
1269 }
1270}
1271
Dan Gohman095cc292008-09-13 01:54:27 +00001272/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1273/// given CallingConvention value.
1274CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001275 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001276 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001277 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001278 else
1279 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001280 }
1281
Gordon Henriksen86737662008-01-05 16:56:59 +00001282 if (CC == CallingConv::X86_FastCall)
1283 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001284 else if (CC == CallingConv::Fast)
1285 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001286 else
1287 return CC_X86_32_C;
1288}
1289
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290/// NameDecorationForCallConv - Selects the appropriate decoration to
1291/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001292NameDecorationStyle
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293X86TargetLowering::NameDecorationForCallConv(unsigned CallConv) {
1294 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001295 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001296 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001297 return StdCall;
1298 return None;
1299}
1300
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001301
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001302/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1303/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001304/// the specific parameter attribute. The copy will be passed as a byval
1305/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001306static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001307CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001308 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1309 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00001310 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), EVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001311 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001312 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001313}
1314
Dan Gohman98ca4f22009-08-05 01:29:28 +00001315SDValue
1316X86TargetLowering::LowerMemArgument(SDValue Chain,
1317 unsigned CallConv,
1318 const SmallVectorImpl<ISD::InputArg> &Ins,
1319 DebugLoc dl, SelectionDAG &DAG,
1320 const CCValAssign &VA,
1321 MachineFrameInfo *MFI,
1322 unsigned i) {
1323
Rafael Espindola7effac52007-09-14 15:48:13 +00001324 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001325 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1326 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001327 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001328
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001329 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001330 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001331 // In case of tail call optimization mark all arguments mutable. Since they
1332 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001333 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001334 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001335 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001336 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001337 return FIN;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001338 return DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001339 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001340}
1341
Dan Gohman475871a2008-07-27 21:46:04 +00001342SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343X86TargetLowering::LowerFormalArguments(SDValue Chain,
1344 unsigned CallConv,
1345 bool isVarArg,
1346 const SmallVectorImpl<ISD::InputArg> &Ins,
1347 DebugLoc dl,
1348 SelectionDAG &DAG,
1349 SmallVectorImpl<SDValue> &InVals) {
1350
Evan Cheng1bc78042006-04-26 01:20:17 +00001351 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001352 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Gordon Henriksen86737662008-01-05 16:56:59 +00001354 const Function* Fn = MF.getFunction();
1355 if (Fn->hasExternalLinkage() &&
1356 Subtarget->isTargetCygMing() &&
1357 Fn->getName() == "main")
1358 FuncInfo->setForceFramePointer(true);
1359
1360 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Evan Cheng1bc78042006-04-26 01:20:17 +00001363 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001364 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001365 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001366
Dan Gohman98ca4f22009-08-05 01:29:28 +00001367 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001368 "Var args not supported with calling convention fastcc");
1369
Chris Lattner638402b2007-02-28 07:00:42 +00001370 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001371 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001372 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1373 ArgLocs, *DAG.getContext());
1374 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001375
Chris Lattnerf39f7712007-02-28 05:46:49 +00001376 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001377 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001378 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1379 CCValAssign &VA = ArgLocs[i];
1380 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1381 // places.
1382 assert(VA.getValNo() != LastVal &&
1383 "Don't support value assigned to multiple locs yet");
1384 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001385
Chris Lattnerf39f7712007-02-28 05:46:49 +00001386 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001387 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001388 TargetRegisterClass *RC = NULL;
Owen Andersone50ed302009-08-10 22:56:29 +00001389 if (RegVT == EVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001390 RC = X86::GR32RegisterClass;
Owen Andersone50ed302009-08-10 22:56:29 +00001391 else if (Is64Bit && RegVT == EVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001392 RC = X86::GR64RegisterClass;
Owen Andersone50ed302009-08-10 22:56:29 +00001393 else if (RegVT == EVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001394 RC = X86::FR32RegisterClass;
Owen Andersone50ed302009-08-10 22:56:29 +00001395 else if (RegVT == EVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001396 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001397 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001398 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001399 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1400 RC = X86::VR64RegisterClass;
1401 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001402 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001403
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001404 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001405 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001406
Chris Lattnerf39f7712007-02-28 05:46:49 +00001407 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1408 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1409 // right size.
1410 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001411 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001412 DAG.getValueType(VA.getValVT()));
1413 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001414 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001415 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001416 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001417 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001419 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001420 // Handle MMX values passed in XMM regs.
1421 if (RegVT.isVector()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001422 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i64,
1423 ArgValue, DAG.getConstant(0, EVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001424 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1425 } else
1426 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001427 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001428 } else {
1429 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001430 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001431 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001432
1433 // If value is passed via pointer - do a load.
1434 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001436
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001438 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001439
Dan Gohman61a92132008-04-21 23:59:07 +00001440 // The x86-64 ABI for returning structs by value requires that we copy
1441 // the sret argument into %rax for the return. Save the argument into
1442 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001443 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001444 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1445 unsigned Reg = FuncInfo->getSRetReturnReg();
1446 if (!Reg) {
Owen Andersone50ed302009-08-10 22:56:29 +00001447 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(EVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001448 FuncInfo->setSRetReturnReg(Reg);
1449 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Andersone50ed302009-08-10 22:56:29 +00001451 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001452 }
1453
Chris Lattnerf39f7712007-02-28 05:46:49 +00001454 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001455 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001456 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001457 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001458
Evan Cheng1bc78042006-04-26 01:20:17 +00001459 // If the function takes variable number of arguments, make a frame index for
1460 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001461 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001462 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001463 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1464 }
1465 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001466 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1467
1468 // FIXME: We should really autogenerate these arrays
1469 static const unsigned GPR64ArgRegsWin64[] = {
1470 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001471 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001472 static const unsigned XMMArgRegsWin64[] = {
1473 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1474 };
1475 static const unsigned GPR64ArgRegs64Bit[] = {
1476 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1477 };
1478 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001479 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1480 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1481 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001482 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1483
1484 if (IsWin64) {
1485 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1486 GPR64ArgRegs = GPR64ArgRegsWin64;
1487 XMMArgRegs = XMMArgRegsWin64;
1488 } else {
1489 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1490 GPR64ArgRegs = GPR64ArgRegs64Bit;
1491 XMMArgRegs = XMMArgRegs64Bit;
1492 }
1493 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1494 TotalNumIntRegs);
1495 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1496 TotalNumXMMRegs);
1497
Devang Patel578efa92009-06-05 21:57:13 +00001498 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001499 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001500 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001501 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001502 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001503 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001504 // Kernel mode asks for SSE to be disabled, so don't push them
1505 // on the stack.
1506 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001507
Gordon Henriksen86737662008-01-05 16:56:59 +00001508 // For X86-64, if there are vararg parameters that are passed via
1509 // registers, then we must store them to their spots on the stack so they
1510 // may be loaded by deferencing the result of va_next.
1511 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001512 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1513 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1514 TotalNumXMMRegs * 16, 16);
1515
Gordon Henriksen86737662008-01-05 16:56:59 +00001516 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001517 SmallVector<SDValue, 8> MemOps;
1518 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001519 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001520 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001521 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001522 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1523 X86::GR64RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00001524 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, EVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001525 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001526 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001527 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001529 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001530 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001531 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001532
Gordon Henriksen86737662008-01-05 16:56:59 +00001533 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001534 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001535 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001536 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001537 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1538 X86::VR128RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00001539 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, EVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001541 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001542 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001543 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001544 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001545 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001546 }
1547 if (!MemOps.empty())
Owen Andersone50ed302009-08-10 22:56:29 +00001548 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001549 &MemOps[0], MemOps.size());
1550 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001551 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Gordon Henriksen86737662008-01-05 16:56:59 +00001553 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001554 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001555 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001556 BytesCallerReserves = 0;
1557 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001558 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001560 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001561 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001562 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001563 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001564
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 if (!Is64Bit) {
1566 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001568 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1569 }
Evan Cheng25caf632006-05-23 21:06:34 +00001570
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001571 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001572
Dan Gohman98ca4f22009-08-05 01:29:28 +00001573 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001574}
1575
Dan Gohman475871a2008-07-27 21:46:04 +00001576SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001577X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1578 SDValue StackPtr, SDValue Arg,
1579 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001580 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001582 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001583 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001584 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001585 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001586 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001587 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001588 }
Dale Johannesenace16102009-02-03 19:33:06 +00001589 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001590 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001591}
1592
Bill Wendling64e87322009-01-16 19:25:27 +00001593/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001594/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001595SDValue
1596X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001597 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001598 SDValue Chain,
1599 bool IsTailCall,
1600 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001601 int FPDiff,
1602 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001603 if (!IsTailCall || FPDiff==0) return Chain;
1604
1605 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001606 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001607 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001608
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001609 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001610 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001611 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001612}
1613
1614/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1615/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001616static SDValue
1617EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001619 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001620 // Store the return address to the appropriate stack slot.
1621 if (!FPDiff) return Chain;
1622 // Calculate the new stack slot for the return address.
1623 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001624 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001625 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT VT = Is64Bit ? EVT::i64 : EVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001627 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001628 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001629 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001630 return Chain;
1631}
1632
Dan Gohman98ca4f22009-08-05 01:29:28 +00001633SDValue
1634X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1635 unsigned CallConv, bool isVarArg, bool isTailCall,
1636 const SmallVectorImpl<ISD::OutputArg> &Outs,
1637 const SmallVectorImpl<ISD::InputArg> &Ins,
1638 DebugLoc dl, SelectionDAG &DAG,
1639 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001640
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 MachineFunction &MF = DAG.getMachineFunction();
1642 bool Is64Bit = Subtarget->is64Bit();
1643 bool IsStructRet = CallIsStructReturn(Outs);
1644
1645 assert((!isTailCall ||
1646 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1647 "IsEligibleForTailCallOptimization missed a case!");
1648 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001649 "Var args not supported with calling convention fastcc");
1650
Chris Lattner638402b2007-02-28 07:00:42 +00001651 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001652 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001653 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1654 ArgLocs, *DAG.getContext());
1655 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001656
Chris Lattner423c5f42007-02-28 05:31:48 +00001657 // Get a count of how many bytes are to be pushed on the stack.
1658 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001660 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001661
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001663 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001665 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1667 FPDiff = NumBytesCallerPushed - NumBytes;
1668
1669 // Set the delta of movement of the returnaddr stackslot.
1670 // But only set if delta is greater than previous delta.
1671 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1672 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1673 }
1674
Chris Lattnere563bbc2008-10-11 22:08:30 +00001675 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001676
Dan Gohman475871a2008-07-27 21:46:04 +00001677 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001678 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001680 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001681
Dan Gohman475871a2008-07-27 21:46:04 +00001682 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1683 SmallVector<SDValue, 8> MemOpChains;
1684 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001685
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001686 // Walk the register/memloc assignments, inserting copies/loads. In the case
1687 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001688 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1689 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001690 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 SDValue Arg = Outs[i].Val;
1692 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001693 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001694
Chris Lattner423c5f42007-02-28 05:31:48 +00001695 // Promote the value if needed.
1696 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001697 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001698 case CCValAssign::Full: break;
1699 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001700 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001701 break;
1702 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001703 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001704 break;
1705 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001706 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1707 // Special case: passing MMX values in XMM registers.
Owen Andersone50ed302009-08-10 22:56:29 +00001708 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i64, Arg);
1709 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v2i64, Arg);
1710 Arg = getMOVL(DAG, dl, EVT::v2i64, DAG.getUNDEF(EVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001711 } else
1712 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1713 break;
1714 case CCValAssign::BCvt:
1715 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001716 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001717 case CCValAssign::Indirect: {
1718 // Store the argument.
1719 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
1720 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1721 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
1722 PseudoSourceValue::getFixedStack(FI), 0);
1723 Arg = SpillSlot;
1724 break;
1725 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001726 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001727
Chris Lattner423c5f42007-02-28 05:31:48 +00001728 if (VA.isRegLoc()) {
1729 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1730 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001731 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001732 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001733 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001734 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001735
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1737 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001738 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001739 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001740 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001741
Evan Cheng32fe1032006-05-25 00:59:30 +00001742 if (!MemOpChains.empty())
Owen Andersone50ed302009-08-10 22:56:29 +00001743 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001744 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001745
Evan Cheng347d5f72006-04-28 21:29:37 +00001746 // Build a sequence of copy-to-reg nodes chained together with token chain
1747 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001748 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001749 // Tail call byval lowering might overwrite argument registers so in case of
1750 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001752 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001753 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001754 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001755 InFlag = Chain.getValue(1);
1756 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001757
Chris Lattner951bf7d2009-07-09 02:44:11 +00001758
Chris Lattner88e1fd52009-07-09 04:24:46 +00001759 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001760 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1761 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001762 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001763 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1764 DAG.getNode(X86ISD::GlobalBaseReg,
1765 DebugLoc::getUnknownLoc(),
1766 getPointerTy()),
1767 InFlag);
1768 InFlag = Chain.getValue(1);
1769 } else {
1770 // If we are tail calling and generating PIC/GOT style code load the
1771 // address of the callee into ECX. The value in ecx is used as target of
1772 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1773 // for tail calls on PIC/GOT architectures. Normally we would just put the
1774 // address of GOT into ebx and then call target@PLT. But for tail calls
1775 // ebx would be restored (since ebx is callee saved) before jumping to the
1776 // target@PLT.
1777
1778 // Note: The actual moving to ECX is done further down.
1779 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1780 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1781 !G->getGlobal()->hasProtectedVisibility())
1782 Callee = LowerGlobalAddress(Callee, DAG);
1783 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001784 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001785 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001786 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001787
Gordon Henriksen86737662008-01-05 16:56:59 +00001788 if (Is64Bit && isVarArg) {
1789 // From AMD64 ABI document:
1790 // For calls that may call functions that use varargs or stdargs
1791 // (prototype-less calls or calls to functions containing ellipsis (...) in
1792 // the declaration) %al is used as hidden argument to specify the number
1793 // of SSE registers used. The contents of %al do not need to match exactly
1794 // the number of registers, but must be an ubound on the number of SSE
1795 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001796
1797 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001798 // Count the number of XMM registers allocated.
1799 static const unsigned XMMArgRegs[] = {
1800 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1801 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1802 };
1803 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001804 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001805 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001806
Dale Johannesendd64c412009-02-04 00:33:20 +00001807 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Andersone50ed302009-08-10 22:56:29 +00001808 DAG.getConstant(NumXMMRegs, EVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 InFlag = Chain.getValue(1);
1810 }
1811
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001812
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001813 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814 if (isTailCall) {
1815 // Force all the incoming stack arguments to be loaded from the stack
1816 // before any new outgoing arguments are stored to the stack, because the
1817 // outgoing stack slots may alias the incoming argument stack slots, and
1818 // the alias isn't otherwise explicit. This is slightly more conservative
1819 // than necessary, because it means that each store effectively depends
1820 // on every argument instead of just those arguments it would clobber.
1821 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1822
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SmallVector<SDValue, 8> MemOpChains2;
1824 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001825 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001826 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001827 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1829 CCValAssign &VA = ArgLocs[i];
1830 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001831 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832 SDValue Arg = Outs[i].Val;
1833 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 // Create frame index.
1835 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001836 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001837 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001838 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001839
Duncan Sands276dcbd2008-03-21 09:14:45 +00001840 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001841 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001844 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001845 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001846 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001847
Dan Gohman98ca4f22009-08-05 01:29:28 +00001848 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1849 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001850 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001852 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001853 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001854 DAG.getStore(ArgChain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001855 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001856 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001857 }
1858 }
1859
1860 if (!MemOpChains2.empty())
Owen Andersone50ed302009-08-10 22:56:29 +00001861 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001862 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001863
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001864 // Copy arguments to their registers.
1865 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001866 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001867 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001868 InFlag = Chain.getValue(1);
1869 }
Dan Gohman475871a2008-07-27 21:46:04 +00001870 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001871
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001873 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001874 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001875 }
1876
Evan Cheng32fe1032006-05-25 00:59:30 +00001877 // If the callee is a GlobalAddress node (quite common, every direct call is)
1878 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001879 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001880 // We should use extra load for direct calls to dllimported functions in
1881 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00001882 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00001883 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001884 unsigned char OpFlags = 0;
1885
1886 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1887 // external symbols most go through the PLT in PIC mode. If the symbol
1888 // has hidden or protected visibility, or if it is static or local, then
1889 // we don't need to use the PLT - we can directly call it.
1890 if (Subtarget->isTargetELF() &&
1891 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001892 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001893 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001894 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001895 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1896 Subtarget->getDarwinVers() < 9) {
1897 // PC-relative references to external symbols should go through $stub,
1898 // unless we're building with the leopard linker or later, which
1899 // automatically synthesizes these stubs.
1900 OpFlags = X86II::MO_DARWIN_STUB;
1901 }
Chris Lattner48a7d022009-07-09 05:02:21 +00001902
Chris Lattner74e726e2009-07-09 05:27:35 +00001903 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00001904 G->getOffset(), OpFlags);
1905 }
Bill Wendling056292f2008-09-16 21:48:12 +00001906 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001907 unsigned char OpFlags = 0;
1908
1909 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
1910 // symbols should go through the PLT.
1911 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001912 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001913 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001914 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001915 Subtarget->getDarwinVers() < 9) {
1916 // PC-relative references to external symbols should go through $stub,
1917 // unless we're building with the leopard linker or later, which
1918 // automatically synthesizes these stubs.
1919 OpFlags = X86II::MO_DARWIN_STUB;
1920 }
1921
Chris Lattner48a7d022009-07-09 05:02:21 +00001922 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
1923 OpFlags);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001924 } else if (isTailCall) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001925 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001926
Dale Johannesendd64c412009-02-04 00:33:20 +00001927 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001928 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001929 Callee,InFlag);
1930 Callee = DAG.getRegister(Opc, getPointerTy());
1931 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001932 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001934
Chris Lattnerd96d0722007-02-25 06:40:16 +00001935 // Returns a chain & a flag for retval copy to use.
Owen Andersone50ed302009-08-10 22:56:29 +00001936 SDVTList NodeTys = DAG.getVTList(EVT::Other, EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001937 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001938
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001940 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1941 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00001943 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001944
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001945 Ops.push_back(Chain);
1946 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001947
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 if (isTailCall)
Owen Andersone50ed302009-08-10 22:56:29 +00001949 Ops.push_back(DAG.getConstant(FPDiff, EVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001950
Gordon Henriksen86737662008-01-05 16:56:59 +00001951 // Add argument registers to the end of the list so that they are known live
1952 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001953 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1954 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1955 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Evan Cheng586ccac2008-03-18 23:36:35 +00001957 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00001959 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1960
1961 // Add an implicit use of AL for x86 vararg functions.
1962 if (Is64Bit && isVarArg)
Owen Andersone50ed302009-08-10 22:56:29 +00001963 Ops.push_back(DAG.getRegister(X86::AL, EVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00001964
Gabor Greifba36cb52008-08-28 21:40:38 +00001965 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001966 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968 if (isTailCall) {
1969 // If this is the first return lowered for this function, add the regs
1970 // to the liveout set for the function.
1971 if (MF.getRegInfo().liveout_empty()) {
1972 SmallVector<CCValAssign, 16> RVLocs;
1973 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1974 *DAG.getContext());
1975 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
1976 for (unsigned i = 0; i != RVLocs.size(); ++i)
1977 if (RVLocs[i].isRegLoc())
1978 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001980
Dan Gohman98ca4f22009-08-05 01:29:28 +00001981 assert(((Callee.getOpcode() == ISD::Register &&
1982 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
1983 cast<RegisterSDNode>(Callee)->getReg() == X86::R9)) ||
1984 Callee.getOpcode() == ISD::TargetExternalSymbol ||
1985 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
1986 "Expecting an global address, external symbol, or register");
1987
1988 return DAG.getNode(X86ISD::TC_RETURN, dl,
1989 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001990 }
1991
Dale Johannesenace16102009-02-03 19:33:06 +00001992 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001993 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001994
Chris Lattner2d297092006-05-23 18:50:38 +00001995 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001996 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001997 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00001998 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002000 // If this is is a call to a struct-return function, the callee
2001 // pops the hidden struct pointer, so we have to push it back.
2002 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002004 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002005 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002006
Gordon Henriksenae636f82008-01-03 16:47:34 +00002007 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002008 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002009 DAG.getIntPtrConstant(NumBytes, true),
2010 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2011 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002012 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002013 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002014
Chris Lattner3085e152007-02-25 08:59:22 +00002015 // Handle result values, copying them out of physregs into vregs that we
2016 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002017 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2018 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002019}
2020
Evan Cheng25ab6902006-09-08 06:48:29 +00002021
2022//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002023// Fast Calling Convention (tail call) implementation
2024//===----------------------------------------------------------------------===//
2025
2026// Like std call, callee cleans arguments, convention except that ECX is
2027// reserved for storing the tail called function address. Only 2 registers are
2028// free for argument passing (inreg). Tail call optimization is performed
2029// provided:
2030// * tailcallopt is enabled
2031// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002032// On X86_64 architecture with GOT-style position independent code only local
2033// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002034// To keep the stack aligned according to platform abi the function
2035// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2036// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002037// If a tail called function callee has more arguments than the caller the
2038// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002039// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002040// original REtADDR, but before the saved framepointer or the spilled registers
2041// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2042// stack layout:
2043// arg1
2044// arg2
2045// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002046// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002047// move area ]
2048// (possible EBP)
2049// ESI
2050// EDI
2051// local1 ..
2052
2053/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2054/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002055unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002056 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002057 MachineFunction &MF = DAG.getMachineFunction();
2058 const TargetMachine &TM = MF.getTarget();
2059 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2060 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002061 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002062 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002063 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002064 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2065 // Number smaller than 12 so just add the difference.
2066 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2067 } else {
2068 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002069 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002070 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002071 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002072 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002073}
2074
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2076/// for tail call optimization. Targets which want to do tail call
2077/// optimization should implement this function.
2078bool
2079X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2080 unsigned CalleeCC,
2081 bool isVarArg,
2082 const SmallVectorImpl<ISD::InputArg> &Ins,
2083 SelectionDAG& DAG) const {
2084 MachineFunction &MF = DAG.getMachineFunction();
2085 unsigned CallerCC = MF.getFunction()->getCallingConv();
2086 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002087}
2088
Dan Gohman3df24e62008-09-03 23:12:08 +00002089FastISel *
2090X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002091 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002092 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002093 DenseMap<const Value *, unsigned> &vm,
2094 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002095 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002096 DenseMap<const AllocaInst *, int> &am
2097#ifndef NDEBUG
2098 , SmallSet<Instruction*, 8> &cil
2099#endif
2100 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002101 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002102#ifndef NDEBUG
2103 , cil
2104#endif
2105 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002106}
2107
2108
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002109//===----------------------------------------------------------------------===//
2110// Other Lowering Hooks
2111//===----------------------------------------------------------------------===//
2112
2113
Dan Gohman475871a2008-07-27 21:46:04 +00002114SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002115 MachineFunction &MF = DAG.getMachineFunction();
2116 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2117 int ReturnAddrIndex = FuncInfo->getRAIndex();
2118
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002119 if (ReturnAddrIndex == 0) {
2120 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002121 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002122 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002123 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002124 }
2125
Evan Cheng25ab6902006-09-08 06:48:29 +00002126 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002127}
2128
2129
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002130bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2131 bool hasSymbolicDisplacement) {
2132 // Offset should fit into 32 bit immediate field.
2133 if (!isInt32(Offset))
2134 return false;
2135
2136 // If we don't have a symbolic displacement - we don't have any extra
2137 // restrictions.
2138 if (!hasSymbolicDisplacement)
2139 return true;
2140
2141 // FIXME: Some tweaks might be needed for medium code model.
2142 if (M != CodeModel::Small && M != CodeModel::Kernel)
2143 return false;
2144
2145 // For small code model we assume that latest object is 16MB before end of 31
2146 // bits boundary. We may also accept pretty large negative constants knowing
2147 // that all objects are in the positive half of address space.
2148 if (M == CodeModel::Small && Offset < 16*1024*1024)
2149 return true;
2150
2151 // For kernel code model we know that all object resist in the negative half
2152 // of 32bits address space. We may not accept negative offsets, since they may
2153 // be just off and we may accept pretty large positive ones.
2154 if (M == CodeModel::Kernel && Offset > 0)
2155 return true;
2156
2157 return false;
2158}
2159
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002160/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2161/// specific condition code, returning the condition code and the LHS/RHS of the
2162/// comparison to make.
2163static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2164 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002165 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002166 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2167 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2168 // X > -1 -> X == 0, jump !sign.
2169 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002170 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002171 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2172 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002173 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002174 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002175 // X < 1 -> X <= 0
2176 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002177 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002178 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002179 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002180
Evan Chengd9558e02006-01-06 00:43:03 +00002181 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002182 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002183 case ISD::SETEQ: return X86::COND_E;
2184 case ISD::SETGT: return X86::COND_G;
2185 case ISD::SETGE: return X86::COND_GE;
2186 case ISD::SETLT: return X86::COND_L;
2187 case ISD::SETLE: return X86::COND_LE;
2188 case ISD::SETNE: return X86::COND_NE;
2189 case ISD::SETULT: return X86::COND_B;
2190 case ISD::SETUGT: return X86::COND_A;
2191 case ISD::SETULE: return X86::COND_BE;
2192 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002193 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002194 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002195
Chris Lattner4c78e022008-12-23 23:42:27 +00002196 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002197
Chris Lattner4c78e022008-12-23 23:42:27 +00002198 // If LHS is a foldable load, but RHS is not, flip the condition.
2199 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2200 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2201 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2202 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002203 }
2204
Chris Lattner4c78e022008-12-23 23:42:27 +00002205 switch (SetCCOpcode) {
2206 default: break;
2207 case ISD::SETOLT:
2208 case ISD::SETOLE:
2209 case ISD::SETUGT:
2210 case ISD::SETUGE:
2211 std::swap(LHS, RHS);
2212 break;
2213 }
2214
2215 // On a floating point condition, the flags are set as follows:
2216 // ZF PF CF op
2217 // 0 | 0 | 0 | X > Y
2218 // 0 | 0 | 1 | X < Y
2219 // 1 | 0 | 0 | X == Y
2220 // 1 | 1 | 1 | unordered
2221 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002222 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002223 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002224 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002225 case ISD::SETOLT: // flipped
2226 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002227 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002228 case ISD::SETOLE: // flipped
2229 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002230 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002231 case ISD::SETUGT: // flipped
2232 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002233 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002234 case ISD::SETUGE: // flipped
2235 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002236 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002237 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002238 case ISD::SETNE: return X86::COND_NE;
2239 case ISD::SETUO: return X86::COND_P;
2240 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002241 }
Evan Chengd9558e02006-01-06 00:43:03 +00002242}
2243
Evan Cheng4a460802006-01-11 00:33:36 +00002244/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2245/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002246/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002247static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002248 switch (X86CC) {
2249 default:
2250 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002251 case X86::COND_B:
2252 case X86::COND_BE:
2253 case X86::COND_E:
2254 case X86::COND_P:
2255 case X86::COND_A:
2256 case X86::COND_AE:
2257 case X86::COND_NE:
2258 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002259 return true;
2260 }
2261}
2262
Nate Begeman9008ca62009-04-27 18:41:29 +00002263/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2264/// the specified range (L, H].
2265static bool isUndefOrInRange(int Val, int Low, int Hi) {
2266 return (Val < 0) || (Val >= Low && Val < Hi);
2267}
2268
2269/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2270/// specified value.
2271static bool isUndefOrEqual(int Val, int CmpVal) {
2272 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002273 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002274 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002275}
2276
Nate Begeman9008ca62009-04-27 18:41:29 +00002277/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2278/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2279/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002280static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
2281 if (VT == EVT::v4f32 || VT == EVT::v4i32 || VT == EVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002282 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Andersone50ed302009-08-10 22:56:29 +00002283 if (VT == EVT::v2f64 || VT == EVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002284 return (Mask[0] < 2 && Mask[1] < 2);
2285 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002286}
2287
Nate Begeman9008ca62009-04-27 18:41:29 +00002288bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2289 SmallVector<int, 8> M;
2290 N->getMask(M);
2291 return ::isPSHUFDMask(M, N->getValueType(0));
2292}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002293
Nate Begeman9008ca62009-04-27 18:41:29 +00002294/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2295/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002296static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
2297 if (VT != EVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002298 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002299
2300 // Lower quadword copied in order or undef.
2301 for (int i = 0; i != 4; ++i)
2302 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002303 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002304
Evan Cheng506d3df2006-03-29 23:07:14 +00002305 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002306 for (int i = 4; i != 8; ++i)
2307 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002308 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002309
Evan Cheng506d3df2006-03-29 23:07:14 +00002310 return true;
2311}
2312
Nate Begeman9008ca62009-04-27 18:41:29 +00002313bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2314 SmallVector<int, 8> M;
2315 N->getMask(M);
2316 return ::isPSHUFHWMask(M, N->getValueType(0));
2317}
Evan Cheng506d3df2006-03-29 23:07:14 +00002318
Nate Begeman9008ca62009-04-27 18:41:29 +00002319/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2320/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002321static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
2322 if (VT != EVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002323 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002324
Rafael Espindola15684b22009-04-24 12:40:33 +00002325 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002326 for (int i = 4; i != 8; ++i)
2327 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002328 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002329
Rafael Espindola15684b22009-04-24 12:40:33 +00002330 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002331 for (int i = 0; i != 4; ++i)
2332 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002333 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002334
Rafael Espindola15684b22009-04-24 12:40:33 +00002335 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002336}
2337
Nate Begeman9008ca62009-04-27 18:41:29 +00002338bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2339 SmallVector<int, 8> M;
2340 N->getMask(M);
2341 return ::isPSHUFLWMask(M, N->getValueType(0));
2342}
2343
Evan Cheng14aed5e2006-03-24 01:18:28 +00002344/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2345/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002346static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002347 int NumElems = VT.getVectorNumElements();
2348 if (NumElems != 2 && NumElems != 4)
2349 return false;
2350
2351 int Half = NumElems / 2;
2352 for (int i = 0; i < Half; ++i)
2353 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002354 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002355 for (int i = Half; i < NumElems; ++i)
2356 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002357 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002358
Evan Cheng14aed5e2006-03-24 01:18:28 +00002359 return true;
2360}
2361
Nate Begeman9008ca62009-04-27 18:41:29 +00002362bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2363 SmallVector<int, 8> M;
2364 N->getMask(M);
2365 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002366}
2367
Evan Cheng213d2cf2007-05-17 18:45:50 +00002368/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002369/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2370/// half elements to come from vector 1 (which would equal the dest.) and
2371/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002372static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002373 int NumElems = VT.getVectorNumElements();
2374
2375 if (NumElems != 2 && NumElems != 4)
2376 return false;
2377
2378 int Half = NumElems / 2;
2379 for (int i = 0; i < Half; ++i)
2380 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002381 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002382 for (int i = Half; i < NumElems; ++i)
2383 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002384 return false;
2385 return true;
2386}
2387
Nate Begeman9008ca62009-04-27 18:41:29 +00002388static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2389 SmallVector<int, 8> M;
2390 N->getMask(M);
2391 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002392}
2393
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002394/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2395/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002396bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2397 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002398 return false;
2399
Evan Cheng2064a2b2006-03-28 06:50:32 +00002400 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002401 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2402 isUndefOrEqual(N->getMaskElt(1), 7) &&
2403 isUndefOrEqual(N->getMaskElt(2), 2) &&
2404 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002405}
2406
Evan Cheng5ced1d82006-04-06 23:23:56 +00002407/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2408/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002409bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2410 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002411
Evan Cheng5ced1d82006-04-06 23:23:56 +00002412 if (NumElems != 2 && NumElems != 4)
2413 return false;
2414
Evan Chengc5cdff22006-04-07 21:53:05 +00002415 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002416 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002417 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002418
Evan Chengc5cdff22006-04-07 21:53:05 +00002419 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002420 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002421 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002422
2423 return true;
2424}
2425
2426/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002427/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2428/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002429bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2430 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002431
Evan Cheng5ced1d82006-04-06 23:23:56 +00002432 if (NumElems != 2 && NumElems != 4)
2433 return false;
2434
Evan Chengc5cdff22006-04-07 21:53:05 +00002435 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002436 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002437 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002438
Nate Begeman9008ca62009-04-27 18:41:29 +00002439 for (unsigned i = 0; i < NumElems/2; ++i)
2440 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002441 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002442
2443 return true;
2444}
2445
Nate Begeman9008ca62009-04-27 18:41:29 +00002446/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2447/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2448/// <2, 3, 2, 3>
2449bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2450 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2451
2452 if (NumElems != 4)
2453 return false;
2454
2455 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2456 isUndefOrEqual(N->getMaskElt(1), 3) &&
2457 isUndefOrEqual(N->getMaskElt(2), 2) &&
2458 isUndefOrEqual(N->getMaskElt(3), 3);
2459}
2460
Evan Cheng0038e592006-03-28 00:39:58 +00002461/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2462/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002463static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002464 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002465 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002466 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002467 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002468
2469 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2470 int BitI = Mask[i];
2471 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002472 if (!isUndefOrEqual(BitI, j))
2473 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002474 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002475 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002476 return false;
2477 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002478 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002479 return false;
2480 }
Evan Cheng0038e592006-03-28 00:39:58 +00002481 }
Evan Cheng0038e592006-03-28 00:39:58 +00002482 return true;
2483}
2484
Nate Begeman9008ca62009-04-27 18:41:29 +00002485bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2486 SmallVector<int, 8> M;
2487 N->getMask(M);
2488 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002489}
2490
Evan Cheng4fcb9222006-03-28 02:43:26 +00002491/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2492/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Owen Andersone50ed302009-08-10 22:56:29 +00002493static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002494 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002495 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002496 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002497 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002498
2499 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2500 int BitI = Mask[i];
2501 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002502 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002503 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002504 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002505 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002506 return false;
2507 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002508 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002509 return false;
2510 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002511 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002512 return true;
2513}
2514
Nate Begeman9008ca62009-04-27 18:41:29 +00002515bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2516 SmallVector<int, 8> M;
2517 N->getMask(M);
2518 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002519}
2520
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002521/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2522/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2523/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002524static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002525 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002526 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002527 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002528
2529 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2530 int BitI = Mask[i];
2531 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002532 if (!isUndefOrEqual(BitI, j))
2533 return false;
2534 if (!isUndefOrEqual(BitI1, j))
2535 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002536 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002537 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002538}
2539
Nate Begeman9008ca62009-04-27 18:41:29 +00002540bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2541 SmallVector<int, 8> M;
2542 N->getMask(M);
2543 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2544}
2545
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002546/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2547/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2548/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002549static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002550 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002551 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2552 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002553
2554 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2555 int BitI = Mask[i];
2556 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002557 if (!isUndefOrEqual(BitI, j))
2558 return false;
2559 if (!isUndefOrEqual(BitI1, j))
2560 return false;
2561 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002562 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002563}
2564
Nate Begeman9008ca62009-04-27 18:41:29 +00002565bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2566 SmallVector<int, 8> M;
2567 N->getMask(M);
2568 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2569}
2570
Evan Cheng017dcc62006-04-21 01:05:10 +00002571/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2572/// specifies a shuffle of elements that is suitable for input to MOVSS,
2573/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002574static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002575 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002576 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002577
2578 int NumElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002579
2580 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002581 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002582
2583 for (int i = 1; i < NumElts; ++i)
2584 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002585 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002586
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002587 return true;
2588}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002589
Nate Begeman9008ca62009-04-27 18:41:29 +00002590bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2591 SmallVector<int, 8> M;
2592 N->getMask(M);
2593 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002594}
2595
Evan Cheng017dcc62006-04-21 01:05:10 +00002596/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2597/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002598/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002599static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002600 bool V2IsSplat = false, bool V2IsUndef = false) {
2601 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002602 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002603 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002604
2605 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002606 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002607
2608 for (int i = 1; i < NumOps; ++i)
2609 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2610 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2611 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002612 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002613
Evan Cheng39623da2006-04-20 08:58:49 +00002614 return true;
2615}
2616
Nate Begeman9008ca62009-04-27 18:41:29 +00002617static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002618 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 SmallVector<int, 8> M;
2620 N->getMask(M);
2621 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002622}
2623
Evan Chengd9539472006-04-14 21:59:03 +00002624/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2625/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002626bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2627 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002628 return false;
2629
2630 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002631 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 int Elt = N->getMaskElt(i);
2633 if (Elt >= 0 && Elt != 1)
2634 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002635 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002636
2637 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002638 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 int Elt = N->getMaskElt(i);
2640 if (Elt >= 0 && Elt != 3)
2641 return false;
2642 if (Elt == 3)
2643 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002644 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002645 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002647 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002648}
2649
2650/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2651/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002652bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2653 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002654 return false;
2655
2656 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002657 for (unsigned i = 0; i < 2; ++i)
2658 if (N->getMaskElt(i) > 0)
2659 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002660
2661 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002662 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002663 int Elt = N->getMaskElt(i);
2664 if (Elt >= 0 && Elt != 2)
2665 return false;
2666 if (Elt == 2)
2667 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002668 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002669 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002670 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002671}
2672
Evan Cheng0b457f02008-09-25 20:50:48 +00002673/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2674/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002675bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2676 int e = N->getValueType(0).getVectorNumElements() / 2;
2677
2678 for (int i = 0; i < e; ++i)
2679 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002680 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002681 for (int i = 0; i < e; ++i)
2682 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002683 return false;
2684 return true;
2685}
2686
Evan Cheng63d33002006-03-22 08:01:21 +00002687/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2688/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2689/// instructions.
2690unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002691 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2692 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2693
Evan Chengb9df0ca2006-03-22 02:53:00 +00002694 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2695 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002696 for (int i = 0; i < NumOperands; ++i) {
2697 int Val = SVOp->getMaskElt(NumOperands-i-1);
2698 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002699 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002700 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002701 if (i != NumOperands - 1)
2702 Mask <<= Shift;
2703 }
Evan Cheng63d33002006-03-22 08:01:21 +00002704 return Mask;
2705}
2706
Evan Cheng506d3df2006-03-29 23:07:14 +00002707/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2708/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2709/// instructions.
2710unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002711 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002712 unsigned Mask = 0;
2713 // 8 nodes, but we only care about the last 4.
2714 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002715 int Val = SVOp->getMaskElt(i);
2716 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002717 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002718 if (i != 4)
2719 Mask <<= 2;
2720 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002721 return Mask;
2722}
2723
2724/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2725/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2726/// instructions.
2727unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002728 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002729 unsigned Mask = 0;
2730 // 8 nodes, but we only care about the first 4.
2731 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002732 int Val = SVOp->getMaskElt(i);
2733 if (Val >= 0)
2734 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002735 if (i != 0)
2736 Mask <<= 2;
2737 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002738 return Mask;
2739}
2740
Evan Cheng37b73872009-07-30 08:33:02 +00002741/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2742/// constant +0.0.
2743bool X86::isZeroNode(SDValue Elt) {
2744 return ((isa<ConstantSDNode>(Elt) &&
2745 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2746 (isa<ConstantFPSDNode>(Elt) &&
2747 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2748}
2749
Nate Begeman9008ca62009-04-27 18:41:29 +00002750/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2751/// their permute mask.
2752static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2753 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002754 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002755 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002756 SmallVector<int, 8> MaskVec;
2757
Nate Begeman5a5ca152009-04-29 05:20:52 +00002758 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002759 int idx = SVOp->getMaskElt(i);
2760 if (idx < 0)
2761 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002762 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002764 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002765 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002766 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002767 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2768 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002769}
2770
Evan Cheng779ccea2007-12-07 21:30:01 +00002771/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2772/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00002773static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002774 unsigned NumElems = VT.getVectorNumElements();
2775 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002776 int idx = Mask[i];
2777 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002778 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002779 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002780 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002781 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002782 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002783 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002784}
2785
Evan Cheng533a0aa2006-04-19 20:35:22 +00002786/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2787/// match movhlps. The lower half elements should come from upper half of
2788/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002789/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002790static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2791 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002792 return false;
2793 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002794 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002795 return false;
2796 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002798 return false;
2799 return true;
2800}
2801
Evan Cheng5ced1d82006-04-06 23:23:56 +00002802/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002803/// is promoted to a vector. It also returns the LoadSDNode by reference if
2804/// required.
2805static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002806 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2807 return false;
2808 N = N->getOperand(0).getNode();
2809 if (!ISD::isNON_EXTLoad(N))
2810 return false;
2811 if (LD)
2812 *LD = cast<LoadSDNode>(N);
2813 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002814}
2815
Evan Cheng533a0aa2006-04-19 20:35:22 +00002816/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2817/// match movlp{s|d}. The lower half elements should come from lower half of
2818/// V1 (and in order), and the upper half elements should come from the upper
2819/// half of V2 (and in order). And since V1 will become the source of the
2820/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002821static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2822 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002823 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002824 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002825 // Is V2 is a vector load, don't do this transformation. We will try to use
2826 // load folding shufps op.
2827 if (ISD::isNON_EXTLoad(V2))
2828 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002829
Nate Begeman5a5ca152009-04-29 05:20:52 +00002830 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002831
Evan Cheng533a0aa2006-04-19 20:35:22 +00002832 if (NumElems != 2 && NumElems != 4)
2833 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002834 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002836 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002837 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002838 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002839 return false;
2840 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002841}
2842
Evan Cheng39623da2006-04-20 08:58:49 +00002843/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2844/// all the same.
2845static bool isSplatVector(SDNode *N) {
2846 if (N->getOpcode() != ISD::BUILD_VECTOR)
2847 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002848
Dan Gohman475871a2008-07-27 21:46:04 +00002849 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002850 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2851 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002852 return false;
2853 return true;
2854}
2855
Evan Cheng213d2cf2007-05-17 18:45:50 +00002856/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002857/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002858/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002859static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002860 SDValue V1 = N->getOperand(0);
2861 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002862 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2863 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002865 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002866 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002867 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2868 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00002869 if (Opc != ISD::BUILD_VECTOR ||
2870 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002871 return false;
2872 } else if (Idx >= 0) {
2873 unsigned Opc = V1.getOpcode();
2874 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2875 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00002876 if (Opc != ISD::BUILD_VECTOR ||
2877 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002878 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002879 }
2880 }
2881 return true;
2882}
2883
2884/// getZeroVector - Returns a vector of specified type with all zero elements.
2885///
Owen Andersone50ed302009-08-10 22:56:29 +00002886static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00002887 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002888 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002889
Chris Lattner8a594482007-11-25 00:24:49 +00002890 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2891 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002892 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002893 if (VT.getSizeInBits() == 64) { // MMX
Owen Andersone50ed302009-08-10 22:56:29 +00002894 SDValue Cst = DAG.getTargetConstant(0, EVT::i32);
2895 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, EVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002896 } else if (HasSSE2) { // SSE2
Owen Andersone50ed302009-08-10 22:56:29 +00002897 SDValue Cst = DAG.getTargetConstant(0, EVT::i32);
2898 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, EVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002899 } else { // SSE1
Owen Andersone50ed302009-08-10 22:56:29 +00002900 SDValue Cst = DAG.getTargetConstantFP(+0.0, EVT::f32);
2901 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, EVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002902 }
Dale Johannesenace16102009-02-03 19:33:06 +00002903 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002904}
2905
Chris Lattner8a594482007-11-25 00:24:49 +00002906/// getOnesVector - Returns a vector of specified type with all bits set.
2907///
Owen Andersone50ed302009-08-10 22:56:29 +00002908static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002909 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002910
Chris Lattner8a594482007-11-25 00:24:49 +00002911 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2912 // type. This ensures they get CSE'd.
Owen Andersone50ed302009-08-10 22:56:29 +00002913 SDValue Cst = DAG.getTargetConstant(~0U, EVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00002914 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002915 if (VT.getSizeInBits() == 64) // MMX
Owen Andersone50ed302009-08-10 22:56:29 +00002916 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, EVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002917 else // SSE
Owen Andersone50ed302009-08-10 22:56:29 +00002918 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, EVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002919 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002920}
2921
2922
Evan Cheng39623da2006-04-20 08:58:49 +00002923/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2924/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002925static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002926 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002927 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002928
Evan Cheng39623da2006-04-20 08:58:49 +00002929 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 SmallVector<int, 8> MaskVec;
2931 SVOp->getMask(MaskVec);
2932
Nate Begeman5a5ca152009-04-29 05:20:52 +00002933 for (unsigned i = 0; i != NumElems; ++i) {
2934 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 MaskVec[i] = NumElems;
2936 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002937 }
Evan Cheng39623da2006-04-20 08:58:49 +00002938 }
Evan Cheng39623da2006-04-20 08:58:49 +00002939 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2941 SVOp->getOperand(1), &MaskVec[0]);
2942 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002943}
2944
Evan Cheng017dcc62006-04-21 01:05:10 +00002945/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2946/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00002947static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 SDValue V2) {
2949 unsigned NumElems = VT.getVectorNumElements();
2950 SmallVector<int, 8> Mask;
2951 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002952 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 Mask.push_back(i);
2954 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002955}
2956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00002958static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 SDValue V2) {
2960 unsigned NumElems = VT.getVectorNumElements();
2961 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002962 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002963 Mask.push_back(i);
2964 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002965 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002966 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002967}
2968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00002970static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00002971 SDValue V2) {
2972 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002973 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002974 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002975 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 Mask.push_back(i + Half);
2977 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002978 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002980}
2981
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002982/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002983static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2984 bool HasSSE2) {
2985 if (SV->getValueType(0).getVectorNumElements() <= 4)
2986 return SDValue(SV, 0);
2987
Owen Andersone50ed302009-08-10 22:56:29 +00002988 EVT PVT = EVT::v4f32;
2989 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 DebugLoc dl = SV->getDebugLoc();
2991 SDValue V1 = SV->getOperand(0);
2992 int NumElems = VT.getVectorNumElements();
2993 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002994
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 // unpack elements to the correct location
2996 while (NumElems > 4) {
2997 if (EltNo < NumElems/2) {
2998 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2999 } else {
3000 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3001 EltNo -= NumElems/2;
3002 }
3003 NumElems >>= 1;
3004 }
3005
3006 // Perform the splat.
3007 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003008 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003009 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3010 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003011}
3012
Evan Chengba05f722006-04-21 23:03:30 +00003013/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003014/// vector of zero or undef vector. This produces a shuffle where the low
3015/// element of V2 is swizzled into the zero/undef vector, landing at element
3016/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00003017static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003018 bool isZero, bool HasSSE2,
3019 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003020 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003021 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003022 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3023 unsigned NumElems = VT.getVectorNumElements();
3024 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003025 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003026 // If this is the insertion idx, put the low elt of V2 here.
3027 MaskVec.push_back(i == Idx ? NumElems : i);
3028 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003029}
3030
Evan Chengf26ffe92008-05-29 08:22:04 +00003031/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3032/// a shuffle that is zero.
3033static
Nate Begeman9008ca62009-04-27 18:41:29 +00003034unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3035 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003036 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003038 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 int Idx = SVOp->getMaskElt(Index);
3040 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003041 ++NumZeros;
3042 continue;
3043 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003045 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003046 ++NumZeros;
3047 else
3048 break;
3049 }
3050 return NumZeros;
3051}
3052
3053/// isVectorShift - Returns true if the shuffle can be implemented as a
3054/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003055/// FIXME: split into pslldqi, psrldqi, palignr variants.
3056static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003057 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003059
3060 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003062 if (!NumZeros) {
3063 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003065 if (!NumZeros)
3066 return false;
3067 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003068 bool SeenV1 = false;
3069 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003070 for (int i = NumZeros; i < NumElems; ++i) {
3071 int Val = isLeft ? (i - NumZeros) : i;
3072 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3073 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003074 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003075 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003076 SeenV1 = true;
3077 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003078 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003079 SeenV2 = true;
3080 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003081 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003082 return false;
3083 }
3084 if (SeenV1 && SeenV2)
3085 return false;
3086
Nate Begeman9008ca62009-04-27 18:41:29 +00003087 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003088 ShAmt = NumZeros;
3089 return true;
3090}
3091
3092
Evan Chengc78d3b42006-04-24 18:01:45 +00003093/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3094///
Dan Gohman475871a2008-07-27 21:46:04 +00003095static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003096 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003097 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003098 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003099 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003100
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003101 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003102 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003103 bool First = true;
3104 for (unsigned i = 0; i < 16; ++i) {
3105 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3106 if (ThisIsNonZero && First) {
3107 if (NumZero)
Owen Andersone50ed302009-08-10 22:56:29 +00003108 V = getZeroVector(EVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003109 else
Owen Andersone50ed302009-08-10 22:56:29 +00003110 V = DAG.getUNDEF(EVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003111 First = false;
3112 }
3113
3114 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003115 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003116 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3117 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003118 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003119 EVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003120 }
3121 if (ThisIsNonZero) {
Owen Andersone50ed302009-08-10 22:56:29 +00003122 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, EVT::i16, Op.getOperand(i));
3123 ThisElt = DAG.getNode(ISD::SHL, dl, EVT::i16,
3124 ThisElt, DAG.getConstant(8, EVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003125 if (LastIsNonZero)
Owen Andersone50ed302009-08-10 22:56:29 +00003126 ThisElt = DAG.getNode(ISD::OR, dl, EVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003127 } else
3128 ThisElt = LastElt;
3129
Gabor Greifba36cb52008-08-28 21:40:38 +00003130 if (ThisElt.getNode())
Owen Andersone50ed302009-08-10 22:56:29 +00003131 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, EVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003132 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003133 }
3134 }
3135
Owen Andersone50ed302009-08-10 22:56:29 +00003136 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003137}
3138
Bill Wendlinga348c562007-03-22 18:42:45 +00003139/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003140///
Dan Gohman475871a2008-07-27 21:46:04 +00003141static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003142 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003143 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003144 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003145 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003146
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003147 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003148 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003149 bool First = true;
3150 for (unsigned i = 0; i < 8; ++i) {
3151 bool isNonZero = (NonZeros & (1 << i)) != 0;
3152 if (isNonZero) {
3153 if (First) {
3154 if (NumZero)
Owen Andersone50ed302009-08-10 22:56:29 +00003155 V = getZeroVector(EVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003156 else
Owen Andersone50ed302009-08-10 22:56:29 +00003157 V = DAG.getUNDEF(EVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003158 First = false;
3159 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003160 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003161 EVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003162 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003163 }
3164 }
3165
3166 return V;
3167}
3168
Evan Chengf26ffe92008-05-29 08:22:04 +00003169/// getVShift - Return a vector logical shift node.
3170///
Owen Andersone50ed302009-08-10 22:56:29 +00003171static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 unsigned NumBits, SelectionDAG &DAG,
3173 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003174 bool isMMX = VT.getSizeInBits() == 64;
Owen Andersone50ed302009-08-10 22:56:29 +00003175 EVT ShVT = isMMX ? EVT::v1i64 : EVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003176 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003177 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3178 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3179 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003180 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003181}
3182
Dan Gohman475871a2008-07-27 21:46:04 +00003183SDValue
3184X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003185 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003186 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003187 if (ISD::isBuildVectorAllZeros(Op.getNode())
3188 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003189 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3190 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3191 // eliminated on x86-32 hosts.
Owen Andersone50ed302009-08-10 22:56:29 +00003192 if (Op.getValueType() == EVT::v4i32 || Op.getValueType() == EVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003193 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003194
Gabor Greifba36cb52008-08-28 21:40:38 +00003195 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003196 return getOnesVector(Op.getValueType(), DAG, dl);
3197 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003198 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003199
Owen Andersone50ed302009-08-10 22:56:29 +00003200 EVT VT = Op.getValueType();
3201 EVT ExtVT = VT.getVectorElementType();
3202 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203
3204 unsigned NumElems = Op.getNumOperands();
3205 unsigned NumZero = 0;
3206 unsigned NumNonZero = 0;
3207 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003208 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003210 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003211 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003212 if (Elt.getOpcode() == ISD::UNDEF)
3213 continue;
3214 Values.insert(Elt);
3215 if (Elt.getOpcode() != ISD::Constant &&
3216 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003217 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003218 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003219 NumZero++;
3220 else {
3221 NonZeros |= (1 << i);
3222 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223 }
3224 }
3225
Dan Gohman7f321562007-06-25 16:23:39 +00003226 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003227 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003228 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003229 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003230
Chris Lattner67f453a2008-03-09 05:42:06 +00003231 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003232 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003233 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003234 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003235
Chris Lattner62098042008-03-09 01:05:04 +00003236 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3237 // the value are obviously zero, truncate the value to i32 and do the
3238 // insertion that way. Only do this if the value is non-constant or if the
3239 // value is a constant being inserted into element 0. It is cheaper to do
3240 // a constant pool load than it is to do a movd + shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00003241 if (ExtVT == EVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003242 (!IsAllConstants || Idx == 0)) {
3243 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3244 // Handle MMX and SSE both.
Owen Andersone50ed302009-08-10 22:56:29 +00003245 EVT VecVT = VT == EVT::v2i64 ? EVT::v4i32 : EVT::v2i32;
3246 unsigned VecElts = VT == EVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003247
Chris Lattner62098042008-03-09 01:05:04 +00003248 // Truncate the value (which may itself be a constant) to i32, and
3249 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Andersone50ed302009-08-10 22:56:29 +00003250 Item = DAG.getNode(ISD::TRUNCATE, dl, EVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003251 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003252 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3253 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003254
Chris Lattner62098042008-03-09 01:05:04 +00003255 // Now we have our 32-bit value zero extended in the low element of
3256 // a vector. If Idx != 0, swizzle it into place.
3257 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 SmallVector<int, 4> Mask;
3259 Mask.push_back(Idx);
3260 for (unsigned i = 1; i != VecElts; ++i)
3261 Mask.push_back(i);
3262 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3263 DAG.getUNDEF(Item.getValueType()),
3264 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003265 }
Dale Johannesenace16102009-02-03 19:33:06 +00003266 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003267 }
3268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003269
Chris Lattner19f79692008-03-08 22:59:52 +00003270 // If we have a constant or non-constant insertion into the low element of
3271 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3272 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003273 // depending on what the source datatype is.
3274 if (Idx == 0) {
3275 if (NumZero == 0) {
3276 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Andersone50ed302009-08-10 22:56:29 +00003277 } else if (ExtVT == EVT::i32 || ExtVT == EVT::f32 || ExtVT == EVT::f64 ||
3278 (ExtVT == EVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003279 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3280 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3281 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3282 DAG);
Owen Andersone50ed302009-08-10 22:56:29 +00003283 } else if (ExtVT == EVT::i16 || ExtVT == EVT::i8) {
3284 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, EVT::i32, Item);
3285 EVT MiddleVT = VT.getSizeInBits() == 64 ? EVT::v2i32 : EVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003286 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3287 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3288 Subtarget->hasSSE2(), DAG);
3289 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3290 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003291 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003292
3293 // Is it a vector logical left shift?
3294 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003295 X86::isZeroNode(Op.getOperand(0)) &&
3296 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003297 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003298 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003299 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003300 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003301 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003302 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003303
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003304 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003305 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003306
Chris Lattner19f79692008-03-08 22:59:52 +00003307 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3308 // is a non-constant being inserted into an element other than the low one,
3309 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3310 // movd/movss) to move this into the low element, then shuffle it into
3311 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003312 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003313 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003314
Evan Cheng0db9fe62006-04-25 20:13:52 +00003315 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003316 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3317 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003319 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003320 MaskVec.push_back(i == Idx ? 0 : 1);
3321 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003322 }
3323 }
3324
Chris Lattner67f453a2008-03-09 05:42:06 +00003325 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3326 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003327 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003328
Dan Gohmana3941172007-07-24 22:55:08 +00003329 // A vector full of immediates; various special cases are already
3330 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003331 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003332 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003333
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003334 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003335 if (EVTBits == 64) {
3336 if (NumNonZero == 1) {
3337 // One half is zero or undef.
3338 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003339 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003340 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003341 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3342 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003343 }
Dan Gohman475871a2008-07-27 21:46:04 +00003344 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003345 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003346
3347 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003348 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003349 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003350 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003351 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003352 }
3353
Bill Wendling826f36f2007-03-28 00:57:11 +00003354 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003355 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003356 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003358 }
3359
3360 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003361 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003362 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003363 if (NumElems == 4 && NumZero > 0) {
3364 for (unsigned i = 0; i < 4; ++i) {
3365 bool isZero = !(NonZeros & (1 << i));
3366 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003367 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003368 else
Dale Johannesenace16102009-02-03 19:33:06 +00003369 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003370 }
3371
3372 for (unsigned i = 0; i < 2; ++i) {
3373 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3374 default: break;
3375 case 0:
3376 V[i] = V[i*2]; // Must be a zero vector.
3377 break;
3378 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003380 break;
3381 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003383 break;
3384 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003386 break;
3387 }
3388 }
3389
Nate Begeman9008ca62009-04-27 18:41:29 +00003390 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003391 bool Reverse = (NonZeros & 0x3) == 2;
3392 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003394 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3395 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3397 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003398 }
3399
3400 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003401 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3402 // values to be inserted is equal to the number of elements, in which case
3403 // use the unpack code below in the hopes of matching the consecutive elts
3404 // load merge pattern for shuffles.
3405 // FIXME: We could probably just check that here directly.
3406 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3407 getSubtarget()->hasSSE41()) {
3408 V[0] = DAG.getUNDEF(VT);
3409 for (unsigned i = 0; i < NumElems; ++i)
3410 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3411 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3412 Op.getOperand(i), DAG.getIntPtrConstant(i));
3413 return V[0];
3414 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003415 // Expand into a number of unpckl*.
3416 // e.g. for v4f32
3417 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3418 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3419 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003420 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003421 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003422 NumElems >>= 1;
3423 while (NumElems != 0) {
3424 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003426 NumElems >>= 1;
3427 }
3428 return V[0];
3429 }
3430
Dan Gohman475871a2008-07-27 21:46:04 +00003431 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003432}
3433
Nate Begemanb9a47b82009-02-23 08:49:38 +00003434// v8i16 shuffles - Prefer shuffles in the following order:
3435// 1. [all] pshuflw, pshufhw, optional move
3436// 2. [ssse3] 1 x pshufb
3437// 3. [ssse3] 2 x pshufb + 1 x por
3438// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003439static
Nate Begeman9008ca62009-04-27 18:41:29 +00003440SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3441 SelectionDAG &DAG, X86TargetLowering &TLI) {
3442 SDValue V1 = SVOp->getOperand(0);
3443 SDValue V2 = SVOp->getOperand(1);
3444 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003445 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003446
Nate Begemanb9a47b82009-02-23 08:49:38 +00003447 // Determine if more than 1 of the words in each of the low and high quadwords
3448 // of the result come from the same quadword of one of the two inputs. Undef
3449 // mask values count as coming from any quadword, for better codegen.
3450 SmallVector<unsigned, 4> LoQuad(4);
3451 SmallVector<unsigned, 4> HiQuad(4);
3452 BitVector InputQuads(4);
3453 for (unsigned i = 0; i < 8; ++i) {
3454 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003456 MaskVals.push_back(EltIdx);
3457 if (EltIdx < 0) {
3458 ++Quad[0];
3459 ++Quad[1];
3460 ++Quad[2];
3461 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003462 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003463 }
3464 ++Quad[EltIdx / 4];
3465 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003466 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003467
Nate Begemanb9a47b82009-02-23 08:49:38 +00003468 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003469 unsigned MaxQuad = 1;
3470 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003471 if (LoQuad[i] > MaxQuad) {
3472 BestLoQuad = i;
3473 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003474 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003475 }
3476
Nate Begemanb9a47b82009-02-23 08:49:38 +00003477 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003478 MaxQuad = 1;
3479 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003480 if (HiQuad[i] > MaxQuad) {
3481 BestHiQuad = i;
3482 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003483 }
3484 }
3485
Nate Begemanb9a47b82009-02-23 08:49:38 +00003486 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3487 // of the two input vectors, shuffle them into one input vector so only a
3488 // single pshufb instruction is necessary. If There are more than 2 input
3489 // quads, disable the next transformation since it does not help SSSE3.
3490 bool V1Used = InputQuads[0] || InputQuads[1];
3491 bool V2Used = InputQuads[2] || InputQuads[3];
3492 if (TLI.getSubtarget()->hasSSSE3()) {
3493 if (InputQuads.count() == 2 && V1Used && V2Used) {
3494 BestLoQuad = InputQuads.find_first();
3495 BestHiQuad = InputQuads.find_next(BestLoQuad);
3496 }
3497 if (InputQuads.count() > 2) {
3498 BestLoQuad = -1;
3499 BestHiQuad = -1;
3500 }
3501 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003502
Nate Begemanb9a47b82009-02-23 08:49:38 +00003503 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3504 // the shuffle mask. If a quad is scored as -1, that means that it contains
3505 // words from all 4 input quadwords.
3506 SDValue NewV;
3507 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 SmallVector<int, 8> MaskV;
3509 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3510 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Owen Andersone50ed302009-08-10 22:56:29 +00003511 NewV = DAG.getVectorShuffle(EVT::v2i64, dl,
3512 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2i64, V1),
3513 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2i64, V2), &MaskV[0]);
3514 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003515
Nate Begemanb9a47b82009-02-23 08:49:38 +00003516 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3517 // source words for the shuffle, to aid later transformations.
3518 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003519 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003520 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003521 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003522 if (idx != (int)i)
3523 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003524 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003525 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003526 AllWordsInNewV = false;
3527 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003528 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003529
Nate Begemanb9a47b82009-02-23 08:49:38 +00003530 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3531 if (AllWordsInNewV) {
3532 for (int i = 0; i != 8; ++i) {
3533 int idx = MaskVals[i];
3534 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003535 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003536 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3537 if ((idx != i) && idx < 4)
3538 pshufhw = false;
3539 if ((idx != i) && idx > 3)
3540 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003541 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003542 V1 = NewV;
3543 V2Used = false;
3544 BestLoQuad = 0;
3545 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003546 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003547
Nate Begemanb9a47b82009-02-23 08:49:38 +00003548 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3549 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003550 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Owen Andersone50ed302009-08-10 22:56:29 +00003551 return DAG.getVectorShuffle(EVT::v8i16, dl, NewV,
3552 DAG.getUNDEF(EVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003553 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003554 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003555
3556 // If we have SSSE3, and all words of the result are from 1 input vector,
3557 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3558 // is present, fall back to case 4.
3559 if (TLI.getSubtarget()->hasSSSE3()) {
3560 SmallVector<SDValue,16> pshufbMask;
3561
3562 // If we have elements from both input vectors, set the high bit of the
3563 // shuffle mask element to zero out elements that come from V2 in the V1
3564 // mask, and elements that come from V1 in the V2 mask, so that the two
3565 // results can be OR'd together.
3566 bool TwoInputs = V1Used && V2Used;
3567 for (unsigned i = 0; i != 8; ++i) {
3568 int EltIdx = MaskVals[i] * 2;
3569 if (TwoInputs && (EltIdx >= 16)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003570 pshufbMask.push_back(DAG.getConstant(0x80, EVT::i8));
3571 pshufbMask.push_back(DAG.getConstant(0x80, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003572 continue;
3573 }
Owen Andersone50ed302009-08-10 22:56:29 +00003574 pshufbMask.push_back(DAG.getConstant(EltIdx, EVT::i8));
3575 pshufbMask.push_back(DAG.getConstant(EltIdx+1, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003576 }
Owen Andersone50ed302009-08-10 22:56:29 +00003577 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v16i8, V1);
3578 V1 = DAG.getNode(X86ISD::PSHUFB, dl, EVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003579 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003580 EVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003581 if (!TwoInputs)
Owen Andersone50ed302009-08-10 22:56:29 +00003582 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003583
3584 // Calculate the shuffle mask for the second input, shuffle it, and
3585 // OR it with the first shuffled input.
3586 pshufbMask.clear();
3587 for (unsigned i = 0; i != 8; ++i) {
3588 int EltIdx = MaskVals[i] * 2;
3589 if (EltIdx < 16) {
Owen Andersone50ed302009-08-10 22:56:29 +00003590 pshufbMask.push_back(DAG.getConstant(0x80, EVT::i8));
3591 pshufbMask.push_back(DAG.getConstant(0x80, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003592 continue;
3593 }
Owen Andersone50ed302009-08-10 22:56:29 +00003594 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, EVT::i8));
3595 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003596 }
Owen Andersone50ed302009-08-10 22:56:29 +00003597 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v16i8, V2);
3598 V2 = DAG.getNode(X86ISD::PSHUFB, dl, EVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003599 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003600 EVT::v16i8, &pshufbMask[0], 16));
3601 V1 = DAG.getNode(ISD::OR, dl, EVT::v16i8, V1, V2);
3602 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003603 }
3604
3605 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3606 // and update MaskVals with new element order.
3607 BitVector InOrder(8);
3608 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003610 for (int i = 0; i != 4; ++i) {
3611 int idx = MaskVals[i];
3612 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003614 InOrder.set(i);
3615 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003616 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003617 InOrder.set(i);
3618 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003619 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003620 }
3621 }
3622 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 MaskV.push_back(i);
Owen Andersone50ed302009-08-10 22:56:29 +00003624 NewV = DAG.getVectorShuffle(EVT::v8i16, dl, NewV, DAG.getUNDEF(EVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003626 }
3627
3628 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3629 // and update MaskVals with the new element order.
3630 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003631 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003632 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003634 for (unsigned i = 4; i != 8; ++i) {
3635 int idx = MaskVals[i];
3636 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003637 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003638 InOrder.set(i);
3639 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003640 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003641 InOrder.set(i);
3642 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003644 }
3645 }
Owen Andersone50ed302009-08-10 22:56:29 +00003646 NewV = DAG.getVectorShuffle(EVT::v8i16, dl, NewV, DAG.getUNDEF(EVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003647 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003648 }
3649
3650 // In case BestHi & BestLo were both -1, which means each quadword has a word
3651 // from each of the four input quadwords, calculate the InOrder bitvector now
3652 // before falling through to the insert/extract cleanup.
3653 if (BestLoQuad == -1 && BestHiQuad == -1) {
3654 NewV = V1;
3655 for (int i = 0; i != 8; ++i)
3656 if (MaskVals[i] < 0 || MaskVals[i] == i)
3657 InOrder.set(i);
3658 }
3659
3660 // The other elements are put in the right place using pextrw and pinsrw.
3661 for (unsigned i = 0; i != 8; ++i) {
3662 if (InOrder[i])
3663 continue;
3664 int EltIdx = MaskVals[i];
3665 if (EltIdx < 0)
3666 continue;
3667 SDValue ExtOp = (EltIdx < 8)
Owen Andersone50ed302009-08-10 22:56:29 +00003668 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003669 DAG.getIntPtrConstant(EltIdx))
Owen Andersone50ed302009-08-10 22:56:29 +00003670 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003671 DAG.getIntPtrConstant(EltIdx - 8));
Owen Andersone50ed302009-08-10 22:56:29 +00003672 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, EVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003673 DAG.getIntPtrConstant(i));
3674 }
3675 return NewV;
3676}
3677
3678// v16i8 shuffles - Prefer shuffles in the following order:
3679// 1. [ssse3] 1 x pshufb
3680// 2. [ssse3] 2 x pshufb + 1 x por
3681// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3682static
Nate Begeman9008ca62009-04-27 18:41:29 +00003683SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3684 SelectionDAG &DAG, X86TargetLowering &TLI) {
3685 SDValue V1 = SVOp->getOperand(0);
3686 SDValue V2 = SVOp->getOperand(1);
3687 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003688 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003690
3691 // If we have SSSE3, case 1 is generated when all result bytes come from
3692 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3693 // present, fall back to case 3.
3694 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3695 bool V1Only = true;
3696 bool V2Only = true;
3697 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003699 if (EltIdx < 0)
3700 continue;
3701 if (EltIdx < 16)
3702 V2Only = false;
3703 else
3704 V1Only = false;
3705 }
3706
3707 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3708 if (TLI.getSubtarget()->hasSSSE3()) {
3709 SmallVector<SDValue,16> pshufbMask;
3710
3711 // If all result elements are from one input vector, then only translate
3712 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3713 //
3714 // Otherwise, we have elements from both input vectors, and must zero out
3715 // elements that come from V2 in the first mask, and V1 in the second mask
3716 // so that we can OR them together.
3717 bool TwoInputs = !(V1Only || V2Only);
3718 for (unsigned i = 0; i != 16; ++i) {
3719 int EltIdx = MaskVals[i];
3720 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003721 pshufbMask.push_back(DAG.getConstant(0x80, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003722 continue;
3723 }
Owen Andersone50ed302009-08-10 22:56:29 +00003724 pshufbMask.push_back(DAG.getConstant(EltIdx, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003725 }
3726 // If all the elements are from V2, assign it to V1 and return after
3727 // building the first pshufb.
3728 if (V2Only)
3729 V1 = V2;
Owen Andersone50ed302009-08-10 22:56:29 +00003730 V1 = DAG.getNode(X86ISD::PSHUFB, dl, EVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003731 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003732 EVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003733 if (!TwoInputs)
3734 return V1;
3735
3736 // Calculate the shuffle mask for the second input, shuffle it, and
3737 // OR it with the first shuffled input.
3738 pshufbMask.clear();
3739 for (unsigned i = 0; i != 16; ++i) {
3740 int EltIdx = MaskVals[i];
3741 if (EltIdx < 16) {
Owen Andersone50ed302009-08-10 22:56:29 +00003742 pshufbMask.push_back(DAG.getConstant(0x80, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003743 continue;
3744 }
Owen Andersone50ed302009-08-10 22:56:29 +00003745 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, EVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003746 }
Owen Andersone50ed302009-08-10 22:56:29 +00003747 V2 = DAG.getNode(X86ISD::PSHUFB, dl, EVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003748 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003749 EVT::v16i8, &pshufbMask[0], 16));
3750 return DAG.getNode(ISD::OR, dl, EVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003751 }
3752
3753 // No SSSE3 - Calculate in place words and then fix all out of place words
3754 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3755 // the 16 different words that comprise the two doublequadword input vectors.
Owen Andersone50ed302009-08-10 22:56:29 +00003756 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v8i16, V1);
3757 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003758 SDValue NewV = V2Only ? V2 : V1;
3759 for (int i = 0; i != 8; ++i) {
3760 int Elt0 = MaskVals[i*2];
3761 int Elt1 = MaskVals[i*2+1];
3762
3763 // This word of the result is all undef, skip it.
3764 if (Elt0 < 0 && Elt1 < 0)
3765 continue;
3766
3767 // This word of the result is already in the correct place, skip it.
3768 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3769 continue;
3770 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3771 continue;
3772
3773 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3774 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3775 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003776
3777 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3778 // using a single extract together, load it and store it.
3779 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003780 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00003781 DAG.getIntPtrConstant(Elt1 / 2));
Owen Andersone50ed302009-08-10 22:56:29 +00003782 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, EVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00003783 DAG.getIntPtrConstant(i));
3784 continue;
3785 }
3786
Nate Begemanb9a47b82009-02-23 08:49:38 +00003787 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003788 // source byte is not also odd, shift the extracted word left 8 bits
3789 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003790 if (Elt1 >= 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00003791 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003792 DAG.getIntPtrConstant(Elt1 / 2));
3793 if ((Elt1 & 1) == 0)
Owen Andersone50ed302009-08-10 22:56:29 +00003794 InsElt = DAG.getNode(ISD::SHL, dl, EVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003795 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003796 else if (Elt0 >= 0)
Owen Andersone50ed302009-08-10 22:56:29 +00003797 InsElt = DAG.getNode(ISD::AND, dl, EVT::i16, InsElt,
3798 DAG.getConstant(0xFF00, EVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003799 }
3800 // If Elt0 is defined, extract it from the appropriate source. If the
3801 // source byte is not also even, shift the extracted word right 8 bits. If
3802 // Elt1 was also defined, OR the extracted values together before
3803 // inserting them in the result.
3804 if (Elt0 >= 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00003805 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003806 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3807 if ((Elt0 & 1) != 0)
Owen Andersone50ed302009-08-10 22:56:29 +00003808 InsElt0 = DAG.getNode(ISD::SRL, dl, EVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003809 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003810 else if (Elt1 >= 0)
Owen Andersone50ed302009-08-10 22:56:29 +00003811 InsElt0 = DAG.getNode(ISD::AND, dl, EVT::i16, InsElt0,
3812 DAG.getConstant(0x00FF, EVT::i16));
3813 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, EVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00003814 : InsElt0;
3815 }
Owen Andersone50ed302009-08-10 22:56:29 +00003816 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, EVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003817 DAG.getIntPtrConstant(i));
3818 }
Owen Andersone50ed302009-08-10 22:56:29 +00003819 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003820}
3821
Evan Cheng7a831ce2007-12-15 03:00:47 +00003822/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3823/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3824/// done when every pair / quad of shuffle mask elements point to elements in
3825/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003826/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3827static
Nate Begeman9008ca62009-04-27 18:41:29 +00003828SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3829 SelectionDAG &DAG,
3830 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00003831 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 SDValue V1 = SVOp->getOperand(0);
3833 SDValue V2 = SVOp->getOperand(1);
3834 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003835 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Andersone50ed302009-08-10 22:56:29 +00003836 EVT MaskVT = EVT::getIntVectorWithNumElements(NewWidth);
3837 EVT MaskEltVT = MaskVT.getVectorElementType();
3838 EVT NewVT = MaskVT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003839 switch (VT.getSimpleVT()) {
3840 default: assert(false && "Unexpected!");
Owen Andersone50ed302009-08-10 22:56:29 +00003841 case EVT::v4f32: NewVT = EVT::v2f64; break;
3842 case EVT::v4i32: NewVT = EVT::v2i64; break;
3843 case EVT::v8i16: NewVT = EVT::v4i32; break;
3844 case EVT::v16i8: NewVT = EVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003845 }
3846
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003847 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003848 if (VT.isInteger())
Owen Andersone50ed302009-08-10 22:56:29 +00003849 NewVT = EVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003850 else
Owen Andersone50ed302009-08-10 22:56:29 +00003851 NewVT = EVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003852 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003853 int Scale = NumElems / NewWidth;
3854 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003855 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 int StartIdx = -1;
3857 for (int j = 0; j < Scale; ++j) {
3858 int EltIdx = SVOp->getMaskElt(i+j);
3859 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003860 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003861 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003862 StartIdx = EltIdx - (EltIdx % Scale);
3863 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003864 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003865 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 if (StartIdx == -1)
3867 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003868 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003870 }
3871
Dale Johannesenace16102009-02-03 19:33:06 +00003872 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3873 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003875}
3876
Evan Chengd880b972008-05-09 21:53:03 +00003877/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003878///
Owen Andersone50ed302009-08-10 22:56:29 +00003879static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 SDValue SrcOp, SelectionDAG &DAG,
3881 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00003882 if (VT == EVT::v2f64 || VT == EVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003883 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003884 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003885 LD = dyn_cast<LoadSDNode>(SrcOp);
3886 if (!LD) {
3887 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3888 // instead.
Owen Andersone50ed302009-08-10 22:56:29 +00003889 EVT EVT = (OpVT == EVT::v2f64) ? EVT::i64 : EVT::i32;
3890 if ((EVT != EVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00003891 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3892 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3893 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3894 // PR2108
Owen Andersone50ed302009-08-10 22:56:29 +00003895 OpVT = (OpVT == EVT::v2f64) ? EVT::v2i64 : EVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003896 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3897 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3898 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3899 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003900 SrcOp.getOperand(0)
3901 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003902 }
3903 }
3904 }
3905
Dale Johannesenace16102009-02-03 19:33:06 +00003906 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3907 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003908 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003909 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003910}
3911
Evan Chengace3c172008-07-22 21:13:36 +00003912/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3913/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003914static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003915LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3916 SDValue V1 = SVOp->getOperand(0);
3917 SDValue V2 = SVOp->getOperand(1);
3918 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003919 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003920
Evan Chengace3c172008-07-22 21:13:36 +00003921 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003922 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 SmallVector<int, 8> Mask1(4U, -1);
3924 SmallVector<int, 8> PermMask;
3925 SVOp->getMask(PermMask);
3926
Evan Chengace3c172008-07-22 21:13:36 +00003927 unsigned NumHi = 0;
3928 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003929 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 int Idx = PermMask[i];
3931 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003932 Locs[i] = std::make_pair(-1, -1);
3933 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003934 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3935 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003936 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003938 NumLo++;
3939 } else {
3940 Locs[i] = std::make_pair(1, NumHi);
3941 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003943 NumHi++;
3944 }
3945 }
3946 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003947
Evan Chengace3c172008-07-22 21:13:36 +00003948 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003949 // If no more than two elements come from either vector. This can be
3950 // implemented with two shuffles. First shuffle gather the elements.
3951 // The second shuffle, which takes the first shuffle as both of its
3952 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003954
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 SmallVector<int, 8> Mask2(4U, -1);
3956
Evan Chengace3c172008-07-22 21:13:36 +00003957 for (unsigned i = 0; i != 4; ++i) {
3958 if (Locs[i].first == -1)
3959 continue;
3960 else {
3961 unsigned Idx = (i < 2) ? 0 : 4;
3962 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003964 }
3965 }
3966
Nate Begeman9008ca62009-04-27 18:41:29 +00003967 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003968 } else if (NumLo == 3 || NumHi == 3) {
3969 // Otherwise, we must have three elements from one vector, call it X, and
3970 // one element from the other, call it Y. First, use a shufps to build an
3971 // intermediate vector with the one element from Y and the element from X
3972 // that will be in the same half in the final destination (the indexes don't
3973 // matter). Then, use a shufps to build the final vector, taking the half
3974 // containing the element from Y from the intermediate, and the other half
3975 // from X.
3976 if (NumHi == 3) {
3977 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003979 std::swap(V1, V2);
3980 }
3981
3982 // Find the element from V2.
3983 unsigned HiIndex;
3984 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003985 int Val = PermMask[HiIndex];
3986 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003987 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003988 if (Val >= 4)
3989 break;
3990 }
3991
Nate Begeman9008ca62009-04-27 18:41:29 +00003992 Mask1[0] = PermMask[HiIndex];
3993 Mask1[1] = -1;
3994 Mask1[2] = PermMask[HiIndex^1];
3995 Mask1[3] = -1;
3996 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003997
3998 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003999 Mask1[0] = PermMask[0];
4000 Mask1[1] = PermMask[1];
4001 Mask1[2] = HiIndex & 1 ? 6 : 4;
4002 Mask1[3] = HiIndex & 1 ? 4 : 6;
4003 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004004 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004005 Mask1[0] = HiIndex & 1 ? 2 : 0;
4006 Mask1[1] = HiIndex & 1 ? 0 : 2;
4007 Mask1[2] = PermMask[2];
4008 Mask1[3] = PermMask[3];
4009 if (Mask1[2] >= 0)
4010 Mask1[2] += 4;
4011 if (Mask1[3] >= 0)
4012 Mask1[3] += 4;
4013 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004014 }
Evan Chengace3c172008-07-22 21:13:36 +00004015 }
4016
4017 // Break it into (shuffle shuffle_hi, shuffle_lo).
4018 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004019 SmallVector<int,8> LoMask(4U, -1);
4020 SmallVector<int,8> HiMask(4U, -1);
4021
4022 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004023 unsigned MaskIdx = 0;
4024 unsigned LoIdx = 0;
4025 unsigned HiIdx = 2;
4026 for (unsigned i = 0; i != 4; ++i) {
4027 if (i == 2) {
4028 MaskPtr = &HiMask;
4029 MaskIdx = 1;
4030 LoIdx = 0;
4031 HiIdx = 2;
4032 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004033 int Idx = PermMask[i];
4034 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004035 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004036 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004037 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004038 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004039 LoIdx++;
4040 } else {
4041 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004042 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004043 HiIdx++;
4044 }
4045 }
4046
Nate Begeman9008ca62009-04-27 18:41:29 +00004047 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4048 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4049 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004050 for (unsigned i = 0; i != 4; ++i) {
4051 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004053 } else {
4054 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004055 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004056 }
4057 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004058 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004059}
4060
Dan Gohman475871a2008-07-27 21:46:04 +00004061SDValue
4062X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004063 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004064 SDValue V1 = Op.getOperand(0);
4065 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004066 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004067 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004069 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004070 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4071 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004072 bool V1IsSplat = false;
4073 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004074
Nate Begeman9008ca62009-04-27 18:41:29 +00004075 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004076 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004077
Nate Begeman9008ca62009-04-27 18:41:29 +00004078 // Promote splats to v4f32.
4079 if (SVOp->isSplat()) {
4080 if (isMMX || NumElems < 4)
4081 return Op;
4082 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004083 }
4084
Evan Cheng7a831ce2007-12-15 03:00:47 +00004085 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4086 // do it!
Owen Andersone50ed302009-08-10 22:56:29 +00004087 if (VT == EVT::v8i16 || VT == EVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004088 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004089 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004090 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004091 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Andersone50ed302009-08-10 22:56:29 +00004092 } else if ((VT == EVT::v4i32 || (VT == EVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004093 // FIXME: Figure out a cleaner way to do this.
4094 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004095 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004096 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004097 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004098 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4099 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4100 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004101 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004102 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004103 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4104 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004105 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004107 }
4108 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004109
4110 if (X86::isPSHUFDMask(SVOp))
4111 return Op;
4112
Evan Chengf26ffe92008-05-29 08:22:04 +00004113 // Check if this can be converted into a logical shift.
4114 bool isLeft = false;
4115 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004116 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004117 bool isShift = getSubtarget()->hasSSE2() &&
4118 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004119 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004120 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004121 // v_set0 + movlhps or movhlps, etc.
Owen Andersone50ed302009-08-10 22:56:29 +00004122 EVT EVT = VT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004124 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004125 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004126
4127 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004128 if (V1IsUndef)
4129 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004130 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004131 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004132 if (!isMMX)
4133 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004134 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004135
4136 // FIXME: fold these into legal mask.
4137 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4138 X86::isMOVSLDUPMask(SVOp) ||
4139 X86::isMOVHLPSMask(SVOp) ||
4140 X86::isMOVHPMask(SVOp) ||
4141 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004142 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004143
Nate Begeman9008ca62009-04-27 18:41:29 +00004144 if (ShouldXformToMOVHLPS(SVOp) ||
4145 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4146 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147
Evan Chengf26ffe92008-05-29 08:22:04 +00004148 if (isShift) {
4149 // No better options. Use a vshl / vsrl.
Owen Andersone50ed302009-08-10 22:56:29 +00004150 EVT EVT = VT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004151 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004152 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004153 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004154
Evan Cheng9eca5e82006-10-25 21:49:50 +00004155 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004156 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4157 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004158 V1IsSplat = isSplatVector(V1.getNode());
4159 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004160
Chris Lattner8a594482007-11-25 00:24:49 +00004161 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004162 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 Op = CommuteVectorShuffle(SVOp, DAG);
4164 SVOp = cast<ShuffleVectorSDNode>(Op);
4165 V1 = SVOp->getOperand(0);
4166 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004167 std::swap(V1IsSplat, V2IsSplat);
4168 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004169 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004170 }
4171
Nate Begeman9008ca62009-04-27 18:41:29 +00004172 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4173 // Shuffling low element of v1 into undef, just return v1.
4174 if (V2IsUndef)
4175 return V1;
4176 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4177 // the instruction selector will not match, so get a canonical MOVL with
4178 // swapped operands to undo the commute.
4179 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004180 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004181
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4183 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4184 X86::isUNPCKLMask(SVOp) ||
4185 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004186 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004187
Evan Cheng9bbbb982006-10-25 20:48:19 +00004188 if (V2IsSplat) {
4189 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004190 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004191 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 SDValue NewMask = NormalizeMask(SVOp, DAG);
4193 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4194 if (NSVOp != SVOp) {
4195 if (X86::isUNPCKLMask(NSVOp, true)) {
4196 return NewMask;
4197 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4198 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004199 }
4200 }
4201 }
4202
Evan Cheng9eca5e82006-10-25 21:49:50 +00004203 if (Commuted) {
4204 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004205 // FIXME: this seems wrong.
4206 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4207 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4208 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4209 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4210 X86::isUNPCKLMask(NewSVOp) ||
4211 X86::isUNPCKHMask(NewSVOp))
4212 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004213 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004214
Nate Begemanb9a47b82009-02-23 08:49:38 +00004215 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004216
4217 // Normalize the node to match x86 shuffle ops if needed
4218 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4219 return CommuteVectorShuffle(SVOp, DAG);
4220
4221 // Check for legal shuffle and return?
4222 SmallVector<int, 16> PermMask;
4223 SVOp->getMask(PermMask);
4224 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004225 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004226
Evan Cheng14b32e12007-12-11 01:46:18 +00004227 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Andersone50ed302009-08-10 22:56:29 +00004228 if (VT == EVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004229 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004230 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004231 return NewOp;
4232 }
4233
Owen Andersone50ed302009-08-10 22:56:29 +00004234 if (VT == EVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004235 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004236 if (NewOp.getNode())
4237 return NewOp;
4238 }
4239
Evan Chengace3c172008-07-22 21:13:36 +00004240 // Handle all 4 wide cases with a number of shuffles except for MMX.
4241 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004242 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004243
Dan Gohman475871a2008-07-27 21:46:04 +00004244 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004245}
4246
Dan Gohman475871a2008-07-27 21:46:04 +00004247SDValue
4248X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004249 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004250 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004251 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004252 if (VT.getSizeInBits() == 8) {
Owen Andersone50ed302009-08-10 22:56:29 +00004253 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, EVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004254 Op.getOperand(0), Op.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004255 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004256 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004257 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004258 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004259 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4260 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4261 if (Idx == 0)
Owen Andersone50ed302009-08-10 22:56:29 +00004262 return DAG.getNode(ISD::TRUNCATE, dl, EVT::i16,
4263 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004264 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004265 EVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004266 Op.getOperand(0)),
4267 Op.getOperand(1)));
Owen Andersone50ed302009-08-10 22:56:29 +00004268 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004269 Op.getOperand(0), Op.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004270 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004271 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004272 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Andersone50ed302009-08-10 22:56:29 +00004273 } else if (VT == EVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004274 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4275 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004276 // result has a single use which is a store or a bitcast to i32. And in
4277 // the case of a store, it's not worth it if the index is a constant 0,
4278 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004279 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004281 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004282 if ((User->getOpcode() != ISD::STORE ||
4283 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4284 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004285 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Andersone50ed302009-08-10 22:56:29 +00004286 User->getValueType(0) != EVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004287 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00004288 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i32,
4289 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004290 Op.getOperand(0)),
4291 Op.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004292 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f32, Extract);
4293 } else if (VT == EVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004294 // ExtractPS works with constant index.
4295 if (isa<ConstantSDNode>(Op.getOperand(1)))
4296 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004297 }
Dan Gohman475871a2008-07-27 21:46:04 +00004298 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004299}
4300
4301
Dan Gohman475871a2008-07-27 21:46:04 +00004302SDValue
4303X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004304 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004305 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004306
Evan Cheng62a3f152008-03-24 21:52:23 +00004307 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004308 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004309 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004310 return Res;
4311 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004312
Owen Andersone50ed302009-08-10 22:56:29 +00004313 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004314 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004316 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004317 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004318 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004319 if (Idx == 0)
Owen Andersone50ed302009-08-10 22:56:29 +00004320 return DAG.getNode(ISD::TRUNCATE, dl, EVT::i16,
4321 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004322 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004323 EVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004324 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004325 // Transform it so it match pextrw which produces a 32-bit result.
Owen Andersone50ed302009-08-10 22:56:29 +00004326 EVT EVT = (EVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004327 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004328 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004329 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004331 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004332 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004333 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004334 if (Idx == 0)
4335 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004336
Evan Cheng0db9fe62006-04-25 20:13:52 +00004337 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004339 EVT VVT = Op.getOperand(0).getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4341 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004342 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004343 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004344 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004345 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4346 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4347 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004348 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349 if (Idx == 0)
4350 return Op;
4351
4352 // UNPCKHPD the element to the lowest double word, then movsd.
4353 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4354 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004355 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004356 EVT VVT = Op.getOperand(0).getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4358 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004359 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004360 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004361 }
4362
Dan Gohman475871a2008-07-27 21:46:04 +00004363 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004364}
4365
Dan Gohman475871a2008-07-27 21:46:04 +00004366SDValue
4367X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004368 EVT VT = Op.getValueType();
4369 EVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004370 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004371
Dan Gohman475871a2008-07-27 21:46:04 +00004372 SDValue N0 = Op.getOperand(0);
4373 SDValue N1 = Op.getOperand(1);
4374 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004375
Dan Gohmanef521f12008-08-14 22:53:18 +00004376 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4377 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004378 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004379 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004380 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4381 // argument.
Owen Andersone50ed302009-08-10 22:56:29 +00004382 if (N1.getValueType() != EVT::i32)
4383 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, EVT::i32, N1);
4384 if (N2.getValueType() != EVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004385 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004386 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004387 } else if (EVT == EVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004388 // Bits [7:6] of the constant are the source select. This will always be
4389 // zero here. The DAG Combiner may combine an extract_elt index into these
4390 // bits. For example (insert (extract, 3), 2) could be matched by putting
4391 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004392 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004393 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004394 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004395 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004396 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004397 // Create this as a scalar to vector..
Owen Andersone50ed302009-08-10 22:56:29 +00004398 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004399 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004400 } else if (EVT == EVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004401 // PINSR* works with constant index.
4402 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004403 }
Dan Gohman475871a2008-07-27 21:46:04 +00004404 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004405}
4406
Dan Gohman475871a2008-07-27 21:46:04 +00004407SDValue
4408X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004409 EVT VT = Op.getValueType();
4410 EVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004411
4412 if (Subtarget->hasSSE41())
4413 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4414
Owen Andersone50ed302009-08-10 22:56:29 +00004415 if (EVT == EVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004416 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004417
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004418 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004419 SDValue N0 = Op.getOperand(0);
4420 SDValue N1 = Op.getOperand(1);
4421 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004422
Eli Friedman30e71eb2009-06-06 06:32:50 +00004423 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004424 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4425 // as its second argument.
Owen Andersone50ed302009-08-10 22:56:29 +00004426 if (N1.getValueType() != EVT::i32)
4427 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, EVT::i32, N1);
4428 if (N2.getValueType() != EVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004429 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004430 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004431 }
Dan Gohman475871a2008-07-27 21:46:04 +00004432 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004433}
4434
Dan Gohman475871a2008-07-27 21:46:04 +00004435SDValue
4436X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004437 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004438 if (Op.getValueType() == EVT::v2f32)
4439 return DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2f32,
4440 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v2i32,
4441 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004442 Op.getOperand(0))));
4443
Owen Andersone50ed302009-08-10 22:56:29 +00004444 if (Op.getValueType() == EVT::v1i64 && Op.getOperand(0).getValueType() == EVT::i64)
4445 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004446
Owen Andersone50ed302009-08-10 22:56:29 +00004447 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, EVT::i32, Op.getOperand(0));
4448 EVT VT = EVT::v2i32;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004449 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004450 default: break;
Owen Andersone50ed302009-08-10 22:56:29 +00004451 case EVT::v16i8:
4452 case EVT::v8i16:
4453 VT = EVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004454 break;
4455 }
Dale Johannesenace16102009-02-03 19:33:06 +00004456 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4457 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004458}
4459
Bill Wendling056292f2008-09-16 21:48:12 +00004460// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4461// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4462// one of the above mentioned nodes. It has to be wrapped because otherwise
4463// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4464// be used to form addressing mode. These wrapped nodes will be selected
4465// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004466SDValue
4467X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004468 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Chris Lattner41621a22009-06-26 19:22:52 +00004469
4470 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4471 // global base reg.
4472 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004473 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004474 CodeModel::Model M = getTargetMachine().getCodeModel();
4475
Chris Lattner4f066492009-07-11 20:29:19 +00004476 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004477 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004478 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004479 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004480 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004481 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004482 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner41621a22009-06-26 19:22:52 +00004483
Evan Cheng1606e8e2009-03-13 07:51:59 +00004484 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004485 CP->getAlignment(),
4486 CP->getOffset(), OpFlag);
4487 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004488 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004489 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004490 if (OpFlag) {
4491 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004492 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004493 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004494 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004495 }
4496
4497 return Result;
4498}
4499
Chris Lattner18c59872009-06-27 04:16:01 +00004500SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4501 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4502
4503 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4504 // global base reg.
4505 unsigned char OpFlag = 0;
4506 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004507 CodeModel::Model M = getTargetMachine().getCodeModel();
4508
Chris Lattner4f066492009-07-11 20:29:19 +00004509 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004510 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004511 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004512 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004513 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004514 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004515 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner18c59872009-06-27 04:16:01 +00004516
4517 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4518 OpFlag);
4519 DebugLoc DL = JT->getDebugLoc();
4520 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4521
4522 // With PIC, the address is actually $g + Offset.
4523 if (OpFlag) {
4524 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4525 DAG.getNode(X86ISD::GlobalBaseReg,
4526 DebugLoc::getUnknownLoc(), getPointerTy()),
4527 Result);
4528 }
4529
4530 return Result;
4531}
4532
4533SDValue
4534X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4535 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4536
4537 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4538 // global base reg.
4539 unsigned char OpFlag = 0;
4540 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004541 CodeModel::Model M = getTargetMachine().getCodeModel();
4542
Chris Lattner4f066492009-07-11 20:29:19 +00004543 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004544 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004545 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004546 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004547 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004548 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004549 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner18c59872009-06-27 04:16:01 +00004550
4551 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
4552
4553 DebugLoc DL = Op.getDebugLoc();
4554 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4555
4556
4557 // With PIC, the address is actually $g + Offset.
4558 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004559 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004560 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4561 DAG.getNode(X86ISD::GlobalBaseReg,
4562 DebugLoc::getUnknownLoc(),
4563 getPointerTy()),
4564 Result);
4565 }
4566
4567 return Result;
4568}
4569
Dan Gohman475871a2008-07-27 21:46:04 +00004570SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004571X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004572 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004573 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004574 // Create the TargetGlobalAddress node, folding in the constant
4575 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004576 unsigned char OpFlags =
4577 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004578 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004579 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004580 if (OpFlags == X86II::MO_NO_FLAG &&
4581 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004582 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004583 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004584 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004585 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004586 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004587 }
4588
Chris Lattner4f066492009-07-11 20:29:19 +00004589 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004590 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004591 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4592 else
4593 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004594
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004595 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004596 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004597 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4598 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004599 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004600 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004601
Chris Lattner36c25012009-07-10 07:34:39 +00004602 // For globals that require a load from a stub to get the address, emit the
4603 // load.
4604 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004605 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004606 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004607
Dan Gohman6520e202008-10-18 02:06:02 +00004608 // If there was a non-zero offset that we didn't fold, create an explicit
4609 // addition for it.
4610 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004611 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004612 DAG.getConstant(Offset, getPointerTy()));
4613
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 return Result;
4615}
4616
Evan Chengda43bcf2008-09-24 00:05:32 +00004617SDValue
4618X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4619 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004620 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004621 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004622}
4623
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004624static SDValue
4625GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004626 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004627 unsigned char OperandFlags) {
Owen Andersone50ed302009-08-10 22:56:29 +00004628 SDVTList NodeTys = DAG.getVTList(EVT::Other, EVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004629 DebugLoc dl = GA->getDebugLoc();
4630 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4631 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004632 GA->getOffset(),
4633 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004634 if (InFlag) {
4635 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004636 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004637 } else {
4638 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004639 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004640 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004641 SDValue Flag = Chain.getValue(1);
4642 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004643}
4644
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004645// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004646static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004647LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004648 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004649 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004650 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4651 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004652 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004653 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004654 PtrVT), InFlag);
4655 InFlag = Chain.getValue(1);
4656
Chris Lattnerb903bed2009-06-26 21:20:29 +00004657 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004658}
4659
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004660// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004661static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004662LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004663 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004664 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4665 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004666}
4667
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004668// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4669// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004670static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004671 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004672 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004673 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004674 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004675 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4676 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004677 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Andersone50ed302009-08-10 22:56:29 +00004678 EVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004679
4680 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4681 NULL, 0);
4682
Chris Lattnerb903bed2009-06-26 21:20:29 +00004683 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004684 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
4685 // initialexec.
4686 unsigned WrapperKind = X86ISD::Wrapper;
4687 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004688 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00004689 } else if (is64Bit) {
4690 assert(model == TLSModel::InitialExec);
4691 OperandFlags = X86II::MO_GOTTPOFF;
4692 WrapperKind = X86ISD::WrapperRIP;
4693 } else {
4694 assert(model == TLSModel::InitialExec);
4695 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00004696 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004697
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004698 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4699 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00004700 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004701 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004702 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004703
Rafael Espindola9a580232009-02-27 13:37:18 +00004704 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004705 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004706 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004707
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004708 // The address of the thread local variable is the add of the thread
4709 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004710 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004711}
4712
Dan Gohman475871a2008-07-27 21:46:04 +00004713SDValue
4714X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004715 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004716 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004717 assert(Subtarget->isTargetELF() &&
4718 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004719 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00004720 const GlobalValue *GV = GA->getGlobal();
4721
4722 // If GV is an alias then use the aliasee for determining
4723 // thread-localness.
4724 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
4725 GV = GA->resolveAliasedGlobal(false);
4726
4727 TLSModel::Model model = getTLSModel(GV,
4728 getTargetMachine().getRelocationModel());
4729
4730 switch (model) {
4731 case TLSModel::GeneralDynamic:
4732 case TLSModel::LocalDynamic: // not implemented
4733 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00004734 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00004735 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4736
4737 case TLSModel::InitialExec:
4738 case TLSModel::LocalExec:
4739 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
4740 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004741 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004742
Torok Edwinc23197a2009-07-14 16:55:14 +00004743 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00004744 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004745}
4746
Evan Cheng0db9fe62006-04-25 20:13:52 +00004747
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004748/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004749/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004750SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004751 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00004752 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004753 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004754 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004755 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004756 SDValue ShOpLo = Op.getOperand(0);
4757 SDValue ShOpHi = Op.getOperand(1);
4758 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00004759 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Andersone50ed302009-08-10 22:56:29 +00004760 DAG.getConstant(VTBits - 1, EVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00004761 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004762
Dan Gohman475871a2008-07-27 21:46:04 +00004763 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004764 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004765 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4766 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004767 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004768 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4769 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004770 }
Evan Chenge3413162006-01-09 18:33:28 +00004771
Owen Andersone50ed302009-08-10 22:56:29 +00004772 SDValue AndNode = DAG.getNode(ISD::AND, dl, EVT::i8, ShAmt,
4773 DAG.getConstant(VTBits, EVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004774 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00004775 AndNode, DAG.getConstant(0, EVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004776
Dan Gohman475871a2008-07-27 21:46:04 +00004777 SDValue Hi, Lo;
Owen Andersone50ed302009-08-10 22:56:29 +00004778 SDValue CC = DAG.getConstant(X86::COND_NE, EVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00004779 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4780 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004781
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004782 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004783 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4784 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004785 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004786 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4787 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004788 }
4789
Dan Gohman475871a2008-07-27 21:46:04 +00004790 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004791 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792}
Evan Chenga3195e82006-01-12 22:54:21 +00004793
Dan Gohman475871a2008-07-27 21:46:04 +00004794SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004795 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004796
4797 if (SrcVT.isVector()) {
Owen Andersone50ed302009-08-10 22:56:29 +00004798 if (SrcVT == EVT::v2i32 && Op.getValueType() == EVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00004799 return Op;
4800 }
4801 return SDValue();
4802 }
4803
Owen Andersone50ed302009-08-10 22:56:29 +00004804 assert(SrcVT.getSimpleVT() <= EVT::i64 && SrcVT.getSimpleVT() >= EVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004805 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004806
Eli Friedman36df4992009-05-27 00:47:34 +00004807 // These are really Legal; return the operand so the caller accepts it as
4808 // Legal.
Owen Andersone50ed302009-08-10 22:56:29 +00004809 if (SrcVT == EVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004810 return Op;
Owen Andersone50ed302009-08-10 22:56:29 +00004811 if (SrcVT == EVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00004812 Subtarget->is64Bit()) {
4813 return Op;
4814 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004815
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004816 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004817 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004818 MachineFunction &MF = DAG.getMachineFunction();
4819 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004820 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004821 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004822 StackSlot,
4823 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004824 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4825}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826
Owen Andersone50ed302009-08-10 22:56:29 +00004827SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00004828 SDValue StackSlot,
4829 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004831 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004832 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004833 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004834 if (useSSE)
Owen Andersone50ed302009-08-10 22:56:29 +00004835 Tys = DAG.getVTList(EVT::f64, EVT::Other, EVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004836 else
Owen Andersone50ed302009-08-10 22:56:29 +00004837 Tys = DAG.getVTList(Op.getValueType(), EVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004838 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004839 Ops.push_back(Chain);
4840 Ops.push_back(StackSlot);
4841 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004842 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004843 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004845 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004846 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004847 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004848
4849 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4850 // shouldn't be necessary except that RFP cannot be live across
4851 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004852 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004853 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004854 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Andersone50ed302009-08-10 22:56:29 +00004855 Tys = DAG.getVTList(EVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004856 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004857 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004858 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004859 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004860 Ops.push_back(DAG.getValueType(Op.getValueType()));
4861 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004862 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4863 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004864 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004865 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004866
Evan Cheng0db9fe62006-04-25 20:13:52 +00004867 return Result;
4868}
4869
Bill Wendling8b8a6362009-01-17 03:56:04 +00004870// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4871SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4872 // This algorithm is not obvious. Here it is in C code, more or less:
4873 /*
4874 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4875 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4876 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004877
Bill Wendling8b8a6362009-01-17 03:56:04 +00004878 // Copy ints to xmm registers.
4879 __m128i xh = _mm_cvtsi32_si128( hi );
4880 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004881
Bill Wendling8b8a6362009-01-17 03:56:04 +00004882 // Combine into low half of a single xmm register.
4883 __m128i x = _mm_unpacklo_epi32( xh, xl );
4884 __m128d d;
4885 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004886
Bill Wendling8b8a6362009-01-17 03:56:04 +00004887 // Merge in appropriate exponents to give the integer bits the right
4888 // magnitude.
4889 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004890
Bill Wendling8b8a6362009-01-17 03:56:04 +00004891 // Subtract away the biases to deal with the IEEE-754 double precision
4892 // implicit 1.
4893 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004894
Bill Wendling8b8a6362009-01-17 03:56:04 +00004895 // All conversions up to here are exact. The correctly rounded result is
4896 // calculated using the current rounding mode using the following
4897 // horizontal add.
4898 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4899 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4900 // store doesn't really need to be here (except
4901 // maybe to zero the other double)
4902 return sd;
4903 }
4904 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004905
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004906 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00004907 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00004908
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004909 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004910 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00004911 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
4912 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
4913 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
4914 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00004915 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004916 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004917
Bill Wendling8b8a6362009-01-17 03:56:04 +00004918 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00004919 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00004920 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00004921 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00004922 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00004923 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004924 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004925
Owen Andersone50ed302009-08-10 22:56:29 +00004926 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v4i32,
4927 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004928 Op.getOperand(0),
4929 DAG.getIntPtrConstant(1)));
Owen Andersone50ed302009-08-10 22:56:29 +00004930 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v4i32,
4931 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004932 Op.getOperand(0),
4933 DAG.getIntPtrConstant(0)));
Owen Andersone50ed302009-08-10 22:56:29 +00004934 SDValue Unpck1 = getUnpackl(DAG, dl, EVT::v4i32, XR1, XR2);
4935 SDValue CLod0 = DAG.getLoad(EVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004936 PseudoSourceValue::getConstantPool(), 0,
4937 false, 16);
Owen Andersone50ed302009-08-10 22:56:29 +00004938 SDValue Unpck2 = getUnpackl(DAG, dl, EVT::v4i32, Unpck1, CLod0);
4939 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2f64, Unpck2);
4940 SDValue CLod1 = DAG.getLoad(EVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004941 PseudoSourceValue::getConstantPool(), 0,
4942 false, 16);
Owen Andersone50ed302009-08-10 22:56:29 +00004943 SDValue Sub = DAG.getNode(ISD::FSUB, dl, EVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004944
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004945 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004946 int ShufMask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004947 SDValue Shuf = DAG.getVectorShuffle(EVT::v2f64, dl, Sub,
4948 DAG.getUNDEF(EVT::v2f64), ShufMask);
4949 SDValue Add = DAG.getNode(ISD::FADD, dl, EVT::v2f64, Shuf, Sub);
4950 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004951 DAG.getIntPtrConstant(0));
4952}
4953
Bill Wendling8b8a6362009-01-17 03:56:04 +00004954// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4955SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004956 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004957 // FP constant to bias correct the final result.
4958 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Andersone50ed302009-08-10 22:56:29 +00004959 EVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004960
4961 // Load the 32-bit value into an XMM register.
Owen Andersone50ed302009-08-10 22:56:29 +00004962 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v4i32,
4963 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004964 Op.getOperand(0),
4965 DAG.getIntPtrConstant(0)));
4966
Owen Andersone50ed302009-08-10 22:56:29 +00004967 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::f64,
4968 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004969 DAG.getIntPtrConstant(0));
4970
4971 // Or the load with the bias.
Owen Andersone50ed302009-08-10 22:56:29 +00004972 SDValue Or = DAG.getNode(ISD::OR, dl, EVT::v2i64,
4973 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004974 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004975 EVT::v2f64, Load)),
4976 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004977 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004978 EVT::v2f64, Bias)));
4979 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::f64,
4980 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004981 DAG.getIntPtrConstant(0));
4982
4983 // Subtract the bias.
Owen Andersone50ed302009-08-10 22:56:29 +00004984 SDValue Sub = DAG.getNode(ISD::FSUB, dl, EVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004985
4986 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00004987 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00004988
Owen Andersone50ed302009-08-10 22:56:29 +00004989 if (DestVT.bitsLT(EVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004990 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004991 DAG.getIntPtrConstant(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004992 } else if (DestVT.bitsGT(EVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004993 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004994 }
4995
4996 // Handle final rounding.
4997 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004998}
4999
5000SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005001 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005002 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005003
Evan Chenga06ec9e2009-01-19 08:08:22 +00005004 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5005 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5006 // the optimization here.
5007 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005008 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005009
Owen Andersone50ed302009-08-10 22:56:29 +00005010 EVT SrcVT = N0.getValueType();
5011 if (SrcVT == EVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005012 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Andersone50ed302009-08-10 22:56:29 +00005013 if (Op.getValueType() != EVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005014 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005015
Bill Wendling8b8a6362009-01-17 03:56:04 +00005016 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Andersone50ed302009-08-10 22:56:29 +00005017 } else if (SrcVT == EVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005018 return LowerUINT_TO_FP_i32(Op, DAG);
5019 }
5020
Owen Andersone50ed302009-08-10 22:56:29 +00005021 assert(SrcVT == EVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005022
5023 // Make a 64-bit buffer, and use it to build an FILD.
Owen Andersone50ed302009-08-10 22:56:29 +00005024 SDValue StackSlot = DAG.CreateStackTemporary(EVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005025 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5026 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5027 getPointerTy(), StackSlot, WordOff);
5028 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5029 StackSlot, NULL, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00005030 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, EVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005031 OffsetSlot, NULL, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00005032 return BuildFILD(Op, EVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005033}
5034
Dan Gohman475871a2008-07-27 21:46:04 +00005035std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005036FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005037 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005038
Owen Andersone50ed302009-08-10 22:56:29 +00005039 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005040
5041 if (!IsSigned) {
Owen Andersone50ed302009-08-10 22:56:29 +00005042 assert(DstTy == EVT::i32 && "Unexpected FP_TO_UINT");
5043 DstTy = EVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005044 }
5045
Owen Andersone50ed302009-08-10 22:56:29 +00005046 assert(DstTy.getSimpleVT() <= EVT::i64 &&
5047 DstTy.getSimpleVT() >= EVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005048 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005050 // These are really Legal.
Owen Andersone50ed302009-08-10 22:56:29 +00005051 if (DstTy == EVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005052 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005053 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005054 if (Subtarget->is64Bit() &&
Owen Andersone50ed302009-08-10 22:56:29 +00005055 DstTy == EVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005056 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005057 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005058
Evan Cheng87c89352007-10-15 20:11:21 +00005059 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5060 // stack slot.
5061 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005062 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00005063 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00005064 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00005065
Evan Cheng0db9fe62006-04-25 20:13:52 +00005066 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00005067 switch (DstTy.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005068 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Andersone50ed302009-08-10 22:56:29 +00005069 case EVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5070 case EVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5071 case EVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005073
Dan Gohman475871a2008-07-27 21:46:04 +00005074 SDValue Chain = DAG.getEntryNode();
5075 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005076 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00005077 assert(DstTy == EVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005078 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00005079 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Andersone50ed302009-08-10 22:56:29 +00005080 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), EVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005081 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005082 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5083 };
Dale Johannesenace16102009-02-03 19:33:06 +00005084 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005085 Chain = Value.getValue(1);
5086 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5087 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5088 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005089
Evan Cheng0db9fe62006-04-25 20:13:52 +00005090 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005091 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Andersone50ed302009-08-10 22:56:29 +00005092 SDValue FIST = DAG.getNode(Opc, dl, EVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005093
Chris Lattner27a6c732007-11-24 07:07:01 +00005094 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005095}
5096
Dan Gohman475871a2008-07-27 21:46:04 +00005097SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005098 if (Op.getValueType().isVector()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005099 if (Op.getValueType() == EVT::v2i32 &&
5100 Op.getOperand(0).getValueType() == EVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005101 return Op;
5102 }
5103 return SDValue();
5104 }
5105
Eli Friedman948e95a2009-05-23 09:59:16 +00005106 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005107 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005108 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5109 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005110
Chris Lattner27a6c732007-11-24 07:07:01 +00005111 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005112 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005113 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005114}
5115
Eli Friedman948e95a2009-05-23 09:59:16 +00005116SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5117 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5118 SDValue FIST = Vals.first, StackSlot = Vals.second;
5119 assert(FIST.getNode() && "Unexpected failure");
5120
5121 // Load the result.
5122 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5123 FIST, StackSlot, NULL, 0);
5124}
5125
Dan Gohman475871a2008-07-27 21:46:04 +00005126SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005127 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005128 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005129 EVT VT = Op.getValueType();
5130 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005131 if (VT.isVector())
5132 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005133 std::vector<Constant*> CV;
Owen Andersone50ed302009-08-10 22:56:29 +00005134 if (EltVT == EVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005135 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005136 CV.push_back(C);
5137 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005138 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005139 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005140 CV.push_back(C);
5141 CV.push_back(C);
5142 CV.push_back(C);
5143 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005144 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005145 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005146 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005147 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005148 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005149 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005150 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005151}
5152
Dan Gohman475871a2008-07-27 21:46:04 +00005153SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005154 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005155 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005156 EVT VT = Op.getValueType();
5157 EVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00005158 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005159 if (VT.isVector()) {
5160 EltVT = VT.getVectorElementType();
5161 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00005162 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163 std::vector<Constant*> CV;
Owen Andersone50ed302009-08-10 22:56:29 +00005164 if (EltVT == EVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005165 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005166 CV.push_back(C);
5167 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005168 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005169 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005170 CV.push_back(C);
5171 CV.push_back(C);
5172 CV.push_back(C);
5173 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005174 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005175 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005176 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005177 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005178 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005179 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005180 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005181 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00005182 DAG.getNode(ISD::XOR, dl, EVT::v2i64,
5183 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005184 Op.getOperand(0)),
Owen Andersone50ed302009-08-10 22:56:29 +00005185 DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005186 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005187 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005188 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005189}
5190
Dan Gohman475871a2008-07-27 21:46:04 +00005191SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005192 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005193 SDValue Op0 = Op.getOperand(0);
5194 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005195 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005196 EVT VT = Op.getValueType();
5197 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005198
5199 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005200 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005201 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005202 SrcVT = VT;
5203 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005204 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005205 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005206 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005207 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005208 }
5209
5210 // At this point the operands and the result should have the same
5211 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005212
Evan Cheng68c47cb2007-01-05 07:55:56 +00005213 // First get the sign bit of second operand.
5214 std::vector<Constant*> CV;
Owen Andersone50ed302009-08-10 22:56:29 +00005215 if (SrcVT == EVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005216 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5217 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005218 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005219 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5220 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5221 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5222 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005223 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005224 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005225 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005226 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005227 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005228 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005229 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005230
5231 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005232 if (SrcVT.bitsGT(VT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005233 // Op0 is EVT::f32, Op1 is EVT::f64.
5234 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, EVT::v2f64, SignBit);
5235 SignBit = DAG.getNode(X86ISD::FSRL, dl, EVT::v2f64, SignBit,
5236 DAG.getConstant(32, EVT::i32));
5237 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::v4f32, SignBit);
5238 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005239 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005240 }
5241
Evan Cheng73d6cf12007-01-05 21:37:56 +00005242 // Clear first operand sign bit.
5243 CV.clear();
Owen Andersone50ed302009-08-10 22:56:29 +00005244 if (VT == EVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005245 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5246 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005247 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005248 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5249 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5250 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5251 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005252 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005253 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005254 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005255 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005256 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005257 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005258 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005259
5260 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005261 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005262}
5263
Dan Gohman076aee32009-03-04 19:44:21 +00005264/// Emit nodes that will be selected as "test Op0,Op0", or something
5265/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005266SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5267 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005268 DebugLoc dl = Op.getDebugLoc();
5269
Dan Gohman31125812009-03-07 01:58:32 +00005270 // CF and OF aren't always set the way we want. Determine which
5271 // of these we need.
5272 bool NeedCF = false;
5273 bool NeedOF = false;
5274 switch (X86CC) {
5275 case X86::COND_A: case X86::COND_AE:
5276 case X86::COND_B: case X86::COND_BE:
5277 NeedCF = true;
5278 break;
5279 case X86::COND_G: case X86::COND_GE:
5280 case X86::COND_L: case X86::COND_LE:
5281 case X86::COND_O: case X86::COND_NO:
5282 NeedOF = true;
5283 break;
5284 default: break;
5285 }
5286
Dan Gohman076aee32009-03-04 19:44:21 +00005287 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005288 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5289 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5290 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005291 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005292 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005293 switch (Op.getNode()->getOpcode()) {
5294 case ISD::ADD:
5295 // Due to an isel shortcoming, be conservative if this add is likely to
5296 // be selected as part of a load-modify-store instruction. When the root
5297 // node in a match is a store, isel doesn't know how to remap non-chain
5298 // non-flag uses of other nodes in the match, such as the ADD in this
5299 // case. This leads to the ADD being left around and reselected, with
5300 // the result being two adds in the output.
5301 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5302 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5303 if (UI->getOpcode() == ISD::STORE)
5304 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005305 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005306 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5307 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005308 if (C->getAPIntValue() == 1) {
5309 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005310 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005311 break;
5312 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005313 // An add of negative one (subtract of one) will be selected as a DEC.
5314 if (C->getAPIntValue().isAllOnesValue()) {
5315 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005316 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005317 break;
5318 }
5319 }
Dan Gohman076aee32009-03-04 19:44:21 +00005320 // Otherwise use a regular EFLAGS-setting add.
5321 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005322 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005323 break;
5324 case ISD::SUB:
5325 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5326 // likely to be selected as part of a load-modify-store instruction.
5327 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5328 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5329 if (UI->getOpcode() == ISD::STORE)
5330 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005331 // Otherwise use a regular EFLAGS-setting sub.
5332 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005333 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005334 break;
5335 case X86ISD::ADD:
5336 case X86ISD::SUB:
5337 case X86ISD::INC:
5338 case X86ISD::DEC:
5339 return SDValue(Op.getNode(), 1);
5340 default:
5341 default_case:
5342 break;
5343 }
5344 if (Opcode != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00005345 SDVTList VTs = DAG.getVTList(Op.getValueType(), EVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005346 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005347 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005348 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005349 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005350 DAG.ReplaceAllUsesWith(Op, New);
5351 return SDValue(New.getNode(), 1);
5352 }
5353 }
5354
5355 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Andersone50ed302009-08-10 22:56:29 +00005356 return DAG.getNode(X86ISD::CMP, dl, EVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005357 DAG.getConstant(0, Op.getValueType()));
5358}
5359
5360/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5361/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005362SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5363 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005364 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5365 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005366 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005367
5368 DebugLoc dl = Op0.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005369 return DAG.getNode(X86ISD::CMP, dl, EVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005370}
5371
Dan Gohman475871a2008-07-27 21:46:04 +00005372SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005373 assert(Op.getValueType() == EVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005374 SDValue Op0 = Op.getOperand(0);
5375 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005376 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005377 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005378
Dan Gohmane5af2d32009-01-29 01:59:02 +00005379 // Lower (X & (1 << N)) == 0 to BT(X, N).
5380 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5381 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005382 if (Op0.getOpcode() == ISD::AND &&
5383 Op0.hasOneUse() &&
5384 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005385 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005386 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005387 SDValue LHS, RHS;
5388 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5389 if (ConstantSDNode *Op010C =
5390 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5391 if (Op010C->getZExtValue() == 1) {
5392 LHS = Op0.getOperand(0);
5393 RHS = Op0.getOperand(1).getOperand(1);
5394 }
5395 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5396 if (ConstantSDNode *Op000C =
5397 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5398 if (Op000C->getZExtValue() == 1) {
5399 LHS = Op0.getOperand(1);
5400 RHS = Op0.getOperand(0).getOperand(1);
5401 }
5402 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5403 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5404 SDValue AndLHS = Op0.getOperand(0);
5405 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5406 LHS = AndLHS.getOperand(0);
5407 RHS = AndLHS.getOperand(1);
5408 }
5409 }
Evan Cheng0488db92007-09-25 01:57:46 +00005410
Dan Gohmane5af2d32009-01-29 01:59:02 +00005411 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005412 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5413 // instruction. Since the shift amount is in-range-or-undefined, we know
5414 // that doing a bittest on the i16 value is ok. We extend to i32 because
5415 // the encoding for the i16 version is larger than the i32 version.
Owen Andersone50ed302009-08-10 22:56:29 +00005416 if (LHS.getValueType() == EVT::i8)
5417 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, EVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005418
5419 // If the operand types disagree, extend the shift amount to match. Since
5420 // BT ignores high bits (like shifts) we can use anyextend.
5421 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005422 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005423
Owen Andersone50ed302009-08-10 22:56:29 +00005424 SDValue BT = DAG.getNode(X86ISD::BT, dl, EVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005425 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Owen Andersone50ed302009-08-10 22:56:29 +00005426 return DAG.getNode(X86ISD::SETCC, dl, EVT::i8,
5427 DAG.getConstant(Cond, EVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005428 }
5429 }
5430
5431 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5432 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005433
Dan Gohman31125812009-03-07 01:58:32 +00005434 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Andersone50ed302009-08-10 22:56:29 +00005435 return DAG.getNode(X86ISD::SETCC, dl, EVT::i8,
5436 DAG.getConstant(X86CC, EVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005437}
5438
Dan Gohman475871a2008-07-27 21:46:04 +00005439SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5440 SDValue Cond;
5441 SDValue Op0 = Op.getOperand(0);
5442 SDValue Op1 = Op.getOperand(1);
5443 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005444 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005445 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5446 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005447 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005448
5449 if (isFP) {
5450 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005451 EVT VT0 = Op0.getValueType();
5452 assert(VT0 == EVT::v4f32 || VT0 == EVT::v2f64);
5453 unsigned Opc = VT0 == EVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005454 bool Swap = false;
5455
5456 switch (SetCCOpcode) {
5457 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005458 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005459 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005460 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005461 case ISD::SETGT: Swap = true; // Fallthrough
5462 case ISD::SETLT:
5463 case ISD::SETOLT: SSECC = 1; break;
5464 case ISD::SETOGE:
5465 case ISD::SETGE: Swap = true; // Fallthrough
5466 case ISD::SETLE:
5467 case ISD::SETOLE: SSECC = 2; break;
5468 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005469 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005470 case ISD::SETNE: SSECC = 4; break;
5471 case ISD::SETULE: Swap = true;
5472 case ISD::SETUGE: SSECC = 5; break;
5473 case ISD::SETULT: Swap = true;
5474 case ISD::SETUGT: SSECC = 6; break;
5475 case ISD::SETO: SSECC = 7; break;
5476 }
5477 if (Swap)
5478 std::swap(Op0, Op1);
5479
Nate Begemanfb8ead02008-07-25 19:05:58 +00005480 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005481 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005482 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005483 SDValue UNORD, EQ;
Owen Andersone50ed302009-08-10 22:56:29 +00005484 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, EVT::i8));
5485 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, EVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005486 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005487 }
5488 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005489 SDValue ORD, NEQ;
Owen Andersone50ed302009-08-10 22:56:29 +00005490 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, EVT::i8));
5491 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, EVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005492 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005493 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005494 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005495 }
5496 // Handle all other FP comparisons here.
Owen Andersone50ed302009-08-10 22:56:29 +00005497 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, EVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005498 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005499
Nate Begeman30a0de92008-07-17 16:51:19 +00005500 // We are handling one of the integer comparisons here. Since SSE only has
5501 // GT and EQ comparisons for integer, swapping operands and multiple
5502 // operations may be required for some comparisons.
5503 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5504 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005505
Nate Begeman30a0de92008-07-17 16:51:19 +00005506 switch (VT.getSimpleVT()) {
5507 default: break;
Owen Andersone50ed302009-08-10 22:56:29 +00005508 case EVT::v8i8:
5509 case EVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5510 case EVT::v4i16:
5511 case EVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5512 case EVT::v2i32:
5513 case EVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5514 case EVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005515 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005516
Nate Begeman30a0de92008-07-17 16:51:19 +00005517 switch (SetCCOpcode) {
5518 default: break;
5519 case ISD::SETNE: Invert = true;
5520 case ISD::SETEQ: Opc = EQOpc; break;
5521 case ISD::SETLT: Swap = true;
5522 case ISD::SETGT: Opc = GTOpc; break;
5523 case ISD::SETGE: Swap = true;
5524 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5525 case ISD::SETULT: Swap = true;
5526 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5527 case ISD::SETUGE: Swap = true;
5528 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5529 }
5530 if (Swap)
5531 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005532
Nate Begeman30a0de92008-07-17 16:51:19 +00005533 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5534 // bits of the inputs before performing those operations.
5535 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005536 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005537 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5538 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005539 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005540 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5541 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005542 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5543 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005544 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005545
Dale Johannesenace16102009-02-03 19:33:06 +00005546 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005547
5548 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005549 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005550 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005551
Nate Begeman30a0de92008-07-17 16:51:19 +00005552 return Result;
5553}
Evan Cheng0488db92007-09-25 01:57:46 +00005554
Evan Cheng370e5342008-12-03 08:38:43 +00005555// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005556static bool isX86LogicalCmp(SDValue Op) {
5557 unsigned Opc = Op.getNode()->getOpcode();
5558 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5559 return true;
5560 if (Op.getResNo() == 1 &&
5561 (Opc == X86ISD::ADD ||
5562 Opc == X86ISD::SUB ||
5563 Opc == X86ISD::SMUL ||
5564 Opc == X86ISD::UMUL ||
5565 Opc == X86ISD::INC ||
5566 Opc == X86ISD::DEC))
5567 return true;
5568
5569 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005570}
5571
Dan Gohman475871a2008-07-27 21:46:04 +00005572SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005573 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005574 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005575 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005576 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005577
Evan Cheng734503b2006-09-11 02:19:56 +00005578 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005579 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005580
Evan Cheng3f41d662007-10-08 22:16:29 +00005581 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5582 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005583 if (Cond.getOpcode() == X86ISD::SETCC) {
5584 CC = Cond.getOperand(0);
5585
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005587 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00005588 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005589
Evan Cheng3f41d662007-10-08 22:16:29 +00005590 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005591 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005592 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005593 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005594
Chris Lattnerd1980a52009-03-12 06:52:53 +00005595 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5596 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005597 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005598 addTest = false;
5599 }
5600 }
5601
5602 if (addTest) {
Owen Andersone50ed302009-08-10 22:56:29 +00005603 CC = DAG.getConstant(X86::COND_NE, EVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005604 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005605 }
5606
Owen Andersone50ed302009-08-10 22:56:29 +00005607 SDVTList VTs = DAG.getVTList(Op.getValueType(), EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005608 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005609 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5610 // condition is true.
5611 Ops.push_back(Op.getOperand(2));
5612 Ops.push_back(Op.getOperand(1));
5613 Ops.push_back(CC);
5614 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005615 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005616}
5617
Evan Cheng370e5342008-12-03 08:38:43 +00005618// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5619// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5620// from the AND / OR.
5621static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5622 Opc = Op.getOpcode();
5623 if (Opc != ISD::OR && Opc != ISD::AND)
5624 return false;
5625 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5626 Op.getOperand(0).hasOneUse() &&
5627 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5628 Op.getOperand(1).hasOneUse());
5629}
5630
Evan Cheng961d6d42009-02-02 08:19:07 +00005631// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5632// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005633static bool isXor1OfSetCC(SDValue Op) {
5634 if (Op.getOpcode() != ISD::XOR)
5635 return false;
5636 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5637 if (N1C && N1C->getAPIntValue() == 1) {
5638 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5639 Op.getOperand(0).hasOneUse();
5640 }
5641 return false;
5642}
5643
Dan Gohman475871a2008-07-27 21:46:04 +00005644SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005645 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SDValue Chain = Op.getOperand(0);
5647 SDValue Cond = Op.getOperand(1);
5648 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005649 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005650 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005651
Evan Cheng0db9fe62006-04-25 20:13:52 +00005652 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005653 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005654#if 0
5655 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005656 else if (Cond.getOpcode() == X86ISD::ADD ||
5657 Cond.getOpcode() == X86ISD::SUB ||
5658 Cond.getOpcode() == X86ISD::SMUL ||
5659 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005660 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005661#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005662
Evan Cheng3f41d662007-10-08 22:16:29 +00005663 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5664 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005665 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005666 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005667
Dan Gohman475871a2008-07-27 21:46:04 +00005668 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005669 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005670 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005671 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005672 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005673 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005674 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005675 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005676 default: break;
5677 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005678 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005679 // These can only come from an arithmetic instruction with overflow,
5680 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005681 Cond = Cond.getNode()->getOperand(1);
5682 addTest = false;
5683 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005684 }
Evan Cheng0488db92007-09-25 01:57:46 +00005685 }
Evan Cheng370e5342008-12-03 08:38:43 +00005686 } else {
5687 unsigned CondOpc;
5688 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5689 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005690 if (CondOpc == ISD::OR) {
5691 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5692 // two branches instead of an explicit OR instruction with a
5693 // separate test.
5694 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005695 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005696 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005697 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005698 Chain, Dest, CC, Cmp);
5699 CC = Cond.getOperand(1).getOperand(0);
5700 Cond = Cmp;
5701 addTest = false;
5702 }
5703 } else { // ISD::AND
5704 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5705 // two branches instead of an explicit AND instruction with a
5706 // separate test. However, we only do this if this block doesn't
5707 // have a fall-through edge, because this requires an explicit
5708 // jmp when the condition is false.
5709 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005710 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005711 Op.getNode()->hasOneUse()) {
5712 X86::CondCode CCode =
5713 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5714 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Andersone50ed302009-08-10 22:56:29 +00005715 CC = DAG.getConstant(CCode, EVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00005716 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5717 // Look for an unconditional branch following this conditional branch.
5718 // We need this because we need to reverse the successors in order
5719 // to implement FCMP_OEQ.
5720 if (User.getOpcode() == ISD::BR) {
5721 SDValue FalseBB = User.getOperand(1);
5722 SDValue NewBR =
5723 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5724 assert(NewBR == User);
5725 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005726
Dale Johannesene4d209d2009-02-03 20:21:25 +00005727 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005728 Chain, Dest, CC, Cmp);
5729 X86::CondCode CCode =
5730 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5731 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Andersone50ed302009-08-10 22:56:29 +00005732 CC = DAG.getConstant(CCode, EVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00005733 Cond = Cmp;
5734 addTest = false;
5735 }
5736 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005737 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005738 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5739 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5740 // It should be transformed during dag combiner except when the condition
5741 // is set by a arithmetics with overflow node.
5742 X86::CondCode CCode =
5743 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5744 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Andersone50ed302009-08-10 22:56:29 +00005745 CC = DAG.getConstant(CCode, EVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00005746 Cond = Cond.getOperand(0).getOperand(1);
5747 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005748 }
Evan Cheng0488db92007-09-25 01:57:46 +00005749 }
5750
5751 if (addTest) {
Owen Andersone50ed302009-08-10 22:56:29 +00005752 CC = DAG.getConstant(X86::COND_NE, EVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005753 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005754 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005755 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005756 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005757}
5758
Anton Korobeynikove060b532007-04-17 19:34:00 +00005759
5760// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5761// Calls to _alloca is needed to probe the stack when allocating more than 4k
5762// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5763// that the guard pages used by the OS virtual memory manager are allocated in
5764// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005765SDValue
5766X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005767 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005768 assert(Subtarget->isTargetCygMing() &&
5769 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005770 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005771
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005772 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005773 SDValue Chain = Op.getOperand(0);
5774 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005775 // FIXME: Ensure alignment here
5776
Dan Gohman475871a2008-07-27 21:46:04 +00005777 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005778
Owen Andersone50ed302009-08-10 22:56:29 +00005779 EVT IntPtr = getPointerTy();
5780 EVT SPTy = Subtarget->is64Bit() ? EVT::i64 : EVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005781
Chris Lattnere563bbc2008-10-11 22:08:30 +00005782 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005783
Dale Johannesendd64c412009-02-04 00:33:20 +00005784 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005785 Flag = Chain.getValue(1);
5786
Owen Andersone50ed302009-08-10 22:56:29 +00005787 SDVTList NodeTys = DAG.getVTList(EVT::Other, EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005788 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005789 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005790 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005791 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005792 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005793 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005794 Flag = Chain.getValue(1);
5795
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005796 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005797 DAG.getIntPtrConstant(0, true),
5798 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005799 Flag);
5800
Dale Johannesendd64c412009-02-04 00:33:20 +00005801 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005802
Dan Gohman475871a2008-07-27 21:46:04 +00005803 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005804 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005805}
5806
Dan Gohman475871a2008-07-27 21:46:04 +00005807SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005808X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005809 SDValue Chain,
5810 SDValue Dst, SDValue Src,
5811 SDValue Size, unsigned Align,
5812 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005813 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005814 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005815
Bill Wendling6f287b22008-09-30 21:22:07 +00005816 // If not DWORD aligned or size is more than the threshold, call the library.
5817 // The libc version is likely to be faster for these cases. It can use the
5818 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005819 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005820 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005821 ConstantSize->getZExtValue() >
5822 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005823 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005824
5825 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005826 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005827
Bill Wendling6158d842008-10-01 00:59:58 +00005828 if (const char *bzeroEntry = V &&
5829 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00005830 EVT IntPtr = getPointerTy();
Bill Wendling6158d842008-10-01 00:59:58 +00005831 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005832 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005833 TargetLowering::ArgListEntry Entry;
5834 Entry.Node = Dst;
5835 Entry.Ty = IntPtrTy;
5836 Args.push_back(Entry);
5837 Entry.Node = Size;
5838 Args.push_back(Entry);
5839 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005840 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005841 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005842 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005843 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005844 }
5845
Dan Gohman707e0182008-04-12 04:36:06 +00005846 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005847 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005848 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005849
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005850 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005851 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00005852 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005853 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005854 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005855 unsigned BytesLeft = 0;
5856 bool TwoRepStos = false;
5857 if (ValC) {
5858 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005859 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005860
Evan Cheng0db9fe62006-04-25 20:13:52 +00005861 // If the value is a constant, then we can potentially use larger sets.
5862 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005863 case 2: // WORD aligned
Owen Andersone50ed302009-08-10 22:56:29 +00005864 AVT = EVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00005865 ValReg = X86::AX;
5866 Val = (Val << 8) | Val;
5867 break;
5868 case 0: // DWORD aligned
Owen Andersone50ed302009-08-10 22:56:29 +00005869 AVT = EVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00005870 ValReg = X86::EAX;
5871 Val = (Val << 8) | Val;
5872 Val = (Val << 16) | Val;
5873 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Andersone50ed302009-08-10 22:56:29 +00005874 AVT = EVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00005875 ValReg = X86::RAX;
5876 Val = (Val << 32) | Val;
5877 }
5878 break;
5879 default: // Byte aligned
Owen Andersone50ed302009-08-10 22:56:29 +00005880 AVT = EVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00005881 ValReg = X86::AL;
5882 Count = DAG.getIntPtrConstant(SizeVal);
5883 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005884 }
5885
Owen Andersone50ed302009-08-10 22:56:29 +00005886 if (AVT.bitsGT(EVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005887 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005888 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5889 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005890 }
5891
Dale Johannesen0f502f62009-02-03 22:26:09 +00005892 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005893 InFlag);
5894 InFlag = Chain.getValue(1);
5895 } else {
Owen Andersone50ed302009-08-10 22:56:29 +00005896 AVT = EVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005897 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005898 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005899 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005900 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005901
Scott Michelfdc40a02009-02-17 22:15:04 +00005902 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005903 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005904 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005905 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005906 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005907 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005908 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005909 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005910
Owen Andersone50ed302009-08-10 22:56:29 +00005911 SDVTList Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005912 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913 Ops.push_back(Chain);
5914 Ops.push_back(DAG.getValueType(AVT));
5915 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005916 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005917
Evan Cheng0db9fe62006-04-25 20:13:52 +00005918 if (TwoRepStos) {
5919 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005920 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00005921 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005922 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Andersone50ed302009-08-10 22:56:29 +00005923 DAG.getConstant((AVT == EVT::i64) ? 7 : 3, CVT));
5924 Chain = DAG.getCopyToReg(Chain, dl, (CVT == EVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005925 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005926 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005927 InFlag = Chain.getValue(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005928 Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005929 Ops.clear();
5930 Ops.push_back(Chain);
Owen Andersone50ed302009-08-10 22:56:29 +00005931 Ops.push_back(DAG.getValueType(EVT::i8));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005932 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005933 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005934 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005935 // Handle the last 1 - 7 bytes.
5936 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00005937 EVT AddrVT = Dst.getValueType();
5938 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005939
Dale Johannesen0f502f62009-02-03 22:26:09 +00005940 Chain = DAG.getMemset(Chain, dl,
5941 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005942 DAG.getConstant(Offset, AddrVT)),
5943 Src,
5944 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005945 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005946 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005947
Dan Gohman707e0182008-04-12 04:36:06 +00005948 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005949 return Chain;
5950}
Evan Cheng11e15b32006-04-03 20:53:28 +00005951
Dan Gohman475871a2008-07-27 21:46:04 +00005952SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005953X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005954 SDValue Chain, SDValue Dst, SDValue Src,
5955 SDValue Size, unsigned Align,
5956 bool AlwaysInline,
5957 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005958 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005959 // This requires the copy size to be a constant, preferrably
5960 // within a subtarget-specific limit.
5961 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5962 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005963 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005964 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005965 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005966 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005967
Evan Cheng1887c1c2008-08-21 21:00:15 +00005968 /// If not DWORD aligned, call the library.
5969 if ((Align & 3) != 0)
5970 return SDValue();
5971
5972 // DWORD aligned
Owen Andersone50ed302009-08-10 22:56:29 +00005973 EVT AVT = EVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00005974 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Andersone50ed302009-08-10 22:56:29 +00005975 AVT = EVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005976
Duncan Sands83ec4b62008-06-06 12:08:01 +00005977 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005978 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005979 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005980 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005981
Dan Gohman475871a2008-07-27 21:46:04 +00005982 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005983 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005984 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005985 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005987 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005988 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005989 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005990 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005991 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005992 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005993 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005994 InFlag = Chain.getValue(1);
5995
Owen Andersone50ed302009-08-10 22:56:29 +00005996 SDVTList Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005997 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005998 Ops.push_back(Chain);
5999 Ops.push_back(DAG.getValueType(AVT));
6000 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006001 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006002
Dan Gohman475871a2008-07-27 21:46:04 +00006003 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006004 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006005 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006006 // Handle the last 1 - 7 bytes.
6007 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006008 EVT DstVT = Dst.getValueType();
6009 EVT SrcVT = Src.getValueType();
6010 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006011 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006012 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006013 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006014 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006015 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006016 DAG.getConstant(BytesLeft, SizeVT),
6017 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006018 DstSV, DstSVOff + Offset,
6019 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006020 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006021
Owen Andersone50ed302009-08-10 22:56:29 +00006022 return DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006023 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006024}
6025
Dan Gohman475871a2008-07-27 21:46:04 +00006026SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006027 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006028 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006029
Evan Cheng25ab6902006-09-08 06:48:29 +00006030 if (!Subtarget->is64Bit()) {
6031 // vastart just stores the address of the VarArgsFrameIndex slot into the
6032 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006033 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006034 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006035 }
6036
6037 // __va_list_tag:
6038 // gp_offset (0 - 6 * 8)
6039 // fp_offset (48 - 48 + 8 * 16)
6040 // overflow_arg_area (point to parameters coming in memory).
6041 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006042 SmallVector<SDValue, 8> MemOps;
6043 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006044 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006045 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Andersone50ed302009-08-10 22:56:29 +00006046 DAG.getConstant(VarArgsGPOffset, EVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006047 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006048 MemOps.push_back(Store);
6049
6050 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006051 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006052 FIN, DAG.getIntPtrConstant(4));
6053 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Andersone50ed302009-08-10 22:56:29 +00006054 DAG.getConstant(VarArgsFPOffset, EVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006055 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006056 MemOps.push_back(Store);
6057
6058 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006059 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006060 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006061 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006062 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006063 MemOps.push_back(Store);
6064
6065 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006066 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006067 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006068 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006069 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006070 MemOps.push_back(Store);
Owen Andersone50ed302009-08-10 22:56:29 +00006071 return DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006072 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006073}
6074
Dan Gohman475871a2008-07-27 21:46:04 +00006075SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006076 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6077 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006078 SDValue Chain = Op.getOperand(0);
6079 SDValue SrcPtr = Op.getOperand(1);
6080 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006081
Torok Edwindac237e2009-07-08 20:53:28 +00006082 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006083 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006084}
6085
Dan Gohman475871a2008-07-27 21:46:04 +00006086SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006087 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006088 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006089 SDValue Chain = Op.getOperand(0);
6090 SDValue DstPtr = Op.getOperand(1);
6091 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006092 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6093 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006094 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006095
Dale Johannesendd64c412009-02-04 00:33:20 +00006096 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006097 DAG.getIntPtrConstant(24), 8, false,
6098 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006099}
6100
Dan Gohman475871a2008-07-27 21:46:04 +00006101SDValue
6102X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006103 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006104 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006105 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006106 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006107 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006108 case Intrinsic::x86_sse_comieq_ss:
6109 case Intrinsic::x86_sse_comilt_ss:
6110 case Intrinsic::x86_sse_comile_ss:
6111 case Intrinsic::x86_sse_comigt_ss:
6112 case Intrinsic::x86_sse_comige_ss:
6113 case Intrinsic::x86_sse_comineq_ss:
6114 case Intrinsic::x86_sse_ucomieq_ss:
6115 case Intrinsic::x86_sse_ucomilt_ss:
6116 case Intrinsic::x86_sse_ucomile_ss:
6117 case Intrinsic::x86_sse_ucomigt_ss:
6118 case Intrinsic::x86_sse_ucomige_ss:
6119 case Intrinsic::x86_sse_ucomineq_ss:
6120 case Intrinsic::x86_sse2_comieq_sd:
6121 case Intrinsic::x86_sse2_comilt_sd:
6122 case Intrinsic::x86_sse2_comile_sd:
6123 case Intrinsic::x86_sse2_comigt_sd:
6124 case Intrinsic::x86_sse2_comige_sd:
6125 case Intrinsic::x86_sse2_comineq_sd:
6126 case Intrinsic::x86_sse2_ucomieq_sd:
6127 case Intrinsic::x86_sse2_ucomilt_sd:
6128 case Intrinsic::x86_sse2_ucomile_sd:
6129 case Intrinsic::x86_sse2_ucomigt_sd:
6130 case Intrinsic::x86_sse2_ucomige_sd:
6131 case Intrinsic::x86_sse2_ucomineq_sd: {
6132 unsigned Opc = 0;
6133 ISD::CondCode CC = ISD::SETCC_INVALID;
6134 switch (IntNo) {
6135 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006136 case Intrinsic::x86_sse_comieq_ss:
6137 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006138 Opc = X86ISD::COMI;
6139 CC = ISD::SETEQ;
6140 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006141 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006142 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006143 Opc = X86ISD::COMI;
6144 CC = ISD::SETLT;
6145 break;
6146 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006147 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006148 Opc = X86ISD::COMI;
6149 CC = ISD::SETLE;
6150 break;
6151 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006152 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006153 Opc = X86ISD::COMI;
6154 CC = ISD::SETGT;
6155 break;
6156 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006157 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006158 Opc = X86ISD::COMI;
6159 CC = ISD::SETGE;
6160 break;
6161 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006162 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006163 Opc = X86ISD::COMI;
6164 CC = ISD::SETNE;
6165 break;
6166 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006167 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006168 Opc = X86ISD::UCOMI;
6169 CC = ISD::SETEQ;
6170 break;
6171 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006172 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006173 Opc = X86ISD::UCOMI;
6174 CC = ISD::SETLT;
6175 break;
6176 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006177 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006178 Opc = X86ISD::UCOMI;
6179 CC = ISD::SETLE;
6180 break;
6181 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006182 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006183 Opc = X86ISD::UCOMI;
6184 CC = ISD::SETGT;
6185 break;
6186 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006187 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006188 Opc = X86ISD::UCOMI;
6189 CC = ISD::SETGE;
6190 break;
6191 case Intrinsic::x86_sse_ucomineq_ss:
6192 case Intrinsic::x86_sse2_ucomineq_sd:
6193 Opc = X86ISD::UCOMI;
6194 CC = ISD::SETNE;
6195 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006196 }
Evan Cheng734503b2006-09-11 02:19:56 +00006197
Dan Gohman475871a2008-07-27 21:46:04 +00006198 SDValue LHS = Op.getOperand(1);
6199 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006200 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Owen Andersone50ed302009-08-10 22:56:29 +00006201 SDValue Cond = DAG.getNode(Opc, dl, EVT::i32, LHS, RHS);
6202 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, EVT::i8,
6203 DAG.getConstant(X86CC, EVT::i8), Cond);
6204 return DAG.getNode(ISD::ZERO_EXTEND, dl, EVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006205 }
Eric Christopher71c67532009-07-29 00:28:05 +00006206 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006207 // an integer value, not just an instruction so lower it to the ptest
6208 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006209 case Intrinsic::x86_sse41_ptestz:
6210 case Intrinsic::x86_sse41_ptestc:
6211 case Intrinsic::x86_sse41_ptestnzc:{
6212 unsigned X86CC = 0;
6213 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006214 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006215 case Intrinsic::x86_sse41_ptestz:
6216 // ZF = 1
6217 X86CC = X86::COND_E;
6218 break;
6219 case Intrinsic::x86_sse41_ptestc:
6220 // CF = 1
6221 X86CC = X86::COND_B;
6222 break;
6223 case Intrinsic::x86_sse41_ptestnzc:
6224 // ZF and CF = 0
6225 X86CC = X86::COND_A;
6226 break;
6227 }
6228
6229 SDValue LHS = Op.getOperand(1);
6230 SDValue RHS = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006231 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, EVT::i32, LHS, RHS);
6232 SDValue CC = DAG.getConstant(X86CC, EVT::i8);
6233 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, EVT::i8, CC, Test);
6234 return DAG.getNode(ISD::ZERO_EXTEND, dl, EVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006235 }
Evan Cheng5759f972008-05-04 09:15:50 +00006236
6237 // Fix vector shift instructions where the last operand is a non-immediate
6238 // i32 value.
6239 case Intrinsic::x86_sse2_pslli_w:
6240 case Intrinsic::x86_sse2_pslli_d:
6241 case Intrinsic::x86_sse2_pslli_q:
6242 case Intrinsic::x86_sse2_psrli_w:
6243 case Intrinsic::x86_sse2_psrli_d:
6244 case Intrinsic::x86_sse2_psrli_q:
6245 case Intrinsic::x86_sse2_psrai_w:
6246 case Intrinsic::x86_sse2_psrai_d:
6247 case Intrinsic::x86_mmx_pslli_w:
6248 case Intrinsic::x86_mmx_pslli_d:
6249 case Intrinsic::x86_mmx_pslli_q:
6250 case Intrinsic::x86_mmx_psrli_w:
6251 case Intrinsic::x86_mmx_psrli_d:
6252 case Intrinsic::x86_mmx_psrli_q:
6253 case Intrinsic::x86_mmx_psrai_w:
6254 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006255 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006256 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006257 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006258
6259 unsigned NewIntNo = 0;
Owen Andersone50ed302009-08-10 22:56:29 +00006260 EVT ShAmtVT = EVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006261 switch (IntNo) {
6262 case Intrinsic::x86_sse2_pslli_w:
6263 NewIntNo = Intrinsic::x86_sse2_psll_w;
6264 break;
6265 case Intrinsic::x86_sse2_pslli_d:
6266 NewIntNo = Intrinsic::x86_sse2_psll_d;
6267 break;
6268 case Intrinsic::x86_sse2_pslli_q:
6269 NewIntNo = Intrinsic::x86_sse2_psll_q;
6270 break;
6271 case Intrinsic::x86_sse2_psrli_w:
6272 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6273 break;
6274 case Intrinsic::x86_sse2_psrli_d:
6275 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6276 break;
6277 case Intrinsic::x86_sse2_psrli_q:
6278 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6279 break;
6280 case Intrinsic::x86_sse2_psrai_w:
6281 NewIntNo = Intrinsic::x86_sse2_psra_w;
6282 break;
6283 case Intrinsic::x86_sse2_psrai_d:
6284 NewIntNo = Intrinsic::x86_sse2_psra_d;
6285 break;
6286 default: {
Owen Andersone50ed302009-08-10 22:56:29 +00006287 ShAmtVT = EVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006288 switch (IntNo) {
6289 case Intrinsic::x86_mmx_pslli_w:
6290 NewIntNo = Intrinsic::x86_mmx_psll_w;
6291 break;
6292 case Intrinsic::x86_mmx_pslli_d:
6293 NewIntNo = Intrinsic::x86_mmx_psll_d;
6294 break;
6295 case Intrinsic::x86_mmx_pslli_q:
6296 NewIntNo = Intrinsic::x86_mmx_psll_q;
6297 break;
6298 case Intrinsic::x86_mmx_psrli_w:
6299 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6300 break;
6301 case Intrinsic::x86_mmx_psrli_d:
6302 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6303 break;
6304 case Intrinsic::x86_mmx_psrli_q:
6305 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6306 break;
6307 case Intrinsic::x86_mmx_psrai_w:
6308 NewIntNo = Intrinsic::x86_mmx_psra_w;
6309 break;
6310 case Intrinsic::x86_mmx_psrai_d:
6311 NewIntNo = Intrinsic::x86_mmx_psra_d;
6312 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006313 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006314 }
6315 break;
6316 }
6317 }
Owen Andersone50ed302009-08-10 22:56:29 +00006318 EVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006319 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6320 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6321 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006322 DAG.getConstant(NewIntNo, EVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006323 Op.getOperand(1), ShAmt);
6324 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006326}
Evan Cheng72261582005-12-20 06:22:03 +00006327
Dan Gohman475871a2008-07-27 21:46:04 +00006328SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006329 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006330 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006331
6332 if (Depth > 0) {
6333 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6334 SDValue Offset =
6335 DAG.getConstant(TD->getPointerSize(),
Owen Andersone50ed302009-08-10 22:56:29 +00006336 Subtarget->is64Bit() ? EVT::i64 : EVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006337 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006338 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006339 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006340 NULL, 0);
6341 }
6342
6343 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006344 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006345 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006346 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006347}
6348
Dan Gohman475871a2008-07-27 21:46:04 +00006349SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006350 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6351 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006352 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006353 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006354 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6355 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006356 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006357 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006358 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006359 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006360}
6361
Dan Gohman475871a2008-07-27 21:46:04 +00006362SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006363 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006364 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006365}
6366
Dan Gohman475871a2008-07-27 21:46:04 +00006367SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006368{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006369 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006370 SDValue Chain = Op.getOperand(0);
6371 SDValue Offset = Op.getOperand(1);
6372 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006373 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006374
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006375 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6376 getPointerTy());
6377 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006378
Dale Johannesene4d209d2009-02-03 20:21:25 +00006379 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006380 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006381 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6382 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006383 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006384 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006385
Dale Johannesene4d209d2009-02-03 20:21:25 +00006386 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00006387 EVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006388 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006389}
6390
Dan Gohman475871a2008-07-27 21:46:04 +00006391SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006392 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006393 SDValue Root = Op.getOperand(0);
6394 SDValue Trmp = Op.getOperand(1); // trampoline
6395 SDValue FPtr = Op.getOperand(2); // nested function
6396 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006397 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006398
Dan Gohman69de1932008-02-06 22:27:42 +00006399 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006400
Duncan Sands339e14f2008-01-16 22:55:25 +00006401 const X86InstrInfo *TII =
6402 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6403
Duncan Sandsb116fac2007-07-27 20:02:49 +00006404 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006405 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006406
6407 // Large code-model.
6408
6409 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6410 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6411
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006412 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6413 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006414
6415 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6416
6417 // Load the pointer to the nested function into R11.
6418 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006419 SDValue Addr = Trmp;
Owen Andersone50ed302009-08-10 22:56:29 +00006420 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, EVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006421 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006422
Owen Andersone50ed302009-08-10 22:56:29 +00006423 Addr = DAG.getNode(ISD::ADD, dl, EVT::i64, Trmp,
6424 DAG.getConstant(2, EVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006425 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006426
6427 // Load the 'nest' parameter value into R10.
6428 // R10 is specified in X86CallingConv.td
6429 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Andersone50ed302009-08-10 22:56:29 +00006430 Addr = DAG.getNode(ISD::ADD, dl, EVT::i64, Trmp,
6431 DAG.getConstant(10, EVT::i64));
6432 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, EVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006433 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006434
Owen Andersone50ed302009-08-10 22:56:29 +00006435 Addr = DAG.getNode(ISD::ADD, dl, EVT::i64, Trmp,
6436 DAG.getConstant(12, EVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006437 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006438
6439 // Jump to the nested function.
6440 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Andersone50ed302009-08-10 22:56:29 +00006441 Addr = DAG.getNode(ISD::ADD, dl, EVT::i64, Trmp,
6442 DAG.getConstant(20, EVT::i64));
6443 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, EVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006444 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006445
6446 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Andersone50ed302009-08-10 22:56:29 +00006447 Addr = DAG.getNode(ISD::ADD, dl, EVT::i64, Trmp,
6448 DAG.getConstant(22, EVT::i64));
6449 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, EVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006450 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006451
Dan Gohman475871a2008-07-27 21:46:04 +00006452 SDValue Ops[] =
Owen Andersone50ed302009-08-10 22:56:29 +00006453 { Trmp, DAG.getNode(ISD::TokenFactor, dl, EVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006454 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006455 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006456 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006457 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6458 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006459 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006460
6461 switch (CC) {
6462 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006463 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006464 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006465 case CallingConv::X86_StdCall: {
6466 // Pass 'nest' parameter in ECX.
6467 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006468 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006469
6470 // Check that ECX wasn't needed by an 'inreg' parameter.
6471 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006472 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006473
Chris Lattner58d74912008-03-12 17:45:29 +00006474 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006475 unsigned InRegCount = 0;
6476 unsigned Idx = 1;
6477
6478 for (FunctionType::param_iterator I = FTy->param_begin(),
6479 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006480 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006481 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006482 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006483
6484 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006485 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006486 }
6487 }
6488 break;
6489 }
6490 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006491 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006492 // Pass 'nest' parameter in EAX.
6493 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006494 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006495 break;
6496 }
6497
Dan Gohman475871a2008-07-27 21:46:04 +00006498 SDValue OutChains[4];
6499 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006500
Owen Andersone50ed302009-08-10 22:56:29 +00006501 Addr = DAG.getNode(ISD::ADD, dl, EVT::i32, Trmp,
6502 DAG.getConstant(10, EVT::i32));
6503 Disp = DAG.getNode(ISD::SUB, dl, EVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006504
Duncan Sands339e14f2008-01-16 22:55:25 +00006505 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006506 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006507 OutChains[0] = DAG.getStore(Root, dl,
Owen Andersone50ed302009-08-10 22:56:29 +00006508 DAG.getConstant(MOV32ri|N86Reg, EVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006509 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006510
Owen Andersone50ed302009-08-10 22:56:29 +00006511 Addr = DAG.getNode(ISD::ADD, dl, EVT::i32, Trmp,
6512 DAG.getConstant(1, EVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006513 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006514
Duncan Sands339e14f2008-01-16 22:55:25 +00006515 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Andersone50ed302009-08-10 22:56:29 +00006516 Addr = DAG.getNode(ISD::ADD, dl, EVT::i32, Trmp,
6517 DAG.getConstant(5, EVT::i32));
6518 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, EVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006519 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006520
Owen Andersone50ed302009-08-10 22:56:29 +00006521 Addr = DAG.getNode(ISD::ADD, dl, EVT::i32, Trmp,
6522 DAG.getConstant(6, EVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006523 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006524
Dan Gohman475871a2008-07-27 21:46:04 +00006525 SDValue Ops[] =
Owen Andersone50ed302009-08-10 22:56:29 +00006526 { Trmp, DAG.getNode(ISD::TokenFactor, dl, EVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006527 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006528 }
6529}
6530
Dan Gohman475871a2008-07-27 21:46:04 +00006531SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006532 /*
6533 The rounding mode is in bits 11:10 of FPSR, and has the following
6534 settings:
6535 00 Round to nearest
6536 01 Round to -inf
6537 10 Round to +inf
6538 11 Round to 0
6539
6540 FLT_ROUNDS, on the other hand, expects the following:
6541 -1 Undefined
6542 0 Round to 0
6543 1 Round to nearest
6544 2 Round to +inf
6545 3 Round to -inf
6546
6547 To perform the conversion, we do:
6548 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6549 */
6550
6551 MachineFunction &MF = DAG.getMachineFunction();
6552 const TargetMachine &TM = MF.getTarget();
6553 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6554 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00006555 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006556 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006557
6558 // Save FP Control Word to stack slot
6559 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006560 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006561
Owen Andersone50ed302009-08-10 22:56:29 +00006562 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, EVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006563 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006564
6565 // Load FP Control Word from stack slot
Owen Andersone50ed302009-08-10 22:56:29 +00006566 SDValue CWD = DAG.getLoad(EVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006567
6568 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006569 SDValue CWD1 =
Owen Andersone50ed302009-08-10 22:56:29 +00006570 DAG.getNode(ISD::SRL, dl, EVT::i16,
6571 DAG.getNode(ISD::AND, dl, EVT::i16,
6572 CWD, DAG.getConstant(0x800, EVT::i16)),
6573 DAG.getConstant(11, EVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006574 SDValue CWD2 =
Owen Andersone50ed302009-08-10 22:56:29 +00006575 DAG.getNode(ISD::SRL, dl, EVT::i16,
6576 DAG.getNode(ISD::AND, dl, EVT::i16,
6577 CWD, DAG.getConstant(0x400, EVT::i16)),
6578 DAG.getConstant(9, EVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006579
Dan Gohman475871a2008-07-27 21:46:04 +00006580 SDValue RetVal =
Owen Andersone50ed302009-08-10 22:56:29 +00006581 DAG.getNode(ISD::AND, dl, EVT::i16,
6582 DAG.getNode(ISD::ADD, dl, EVT::i16,
6583 DAG.getNode(ISD::OR, dl, EVT::i16, CWD1, CWD2),
6584 DAG.getConstant(1, EVT::i16)),
6585 DAG.getConstant(3, EVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006586
6587
Duncan Sands83ec4b62008-06-06 12:08:01 +00006588 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006589 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006590}
6591
Dan Gohman475871a2008-07-27 21:46:04 +00006592SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00006593 EVT VT = Op.getValueType();
6594 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006595 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006596 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006597
6598 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006599 if (VT == EVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006600 // Zero extend to i32 since there is not an i8 bsr.
Owen Andersone50ed302009-08-10 22:56:29 +00006601 OpVT = EVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006602 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006603 }
Evan Cheng18efe262007-12-14 02:13:44 +00006604
Evan Cheng152804e2007-12-14 08:30:15 +00006605 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Andersone50ed302009-08-10 22:56:29 +00006606 SDVTList VTs = DAG.getVTList(OpVT, EVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006607 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006608
6609 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006610 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006611 Ops.push_back(Op);
6612 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
Owen Andersone50ed302009-08-10 22:56:29 +00006613 Ops.push_back(DAG.getConstant(X86::COND_E, EVT::i8));
Evan Cheng152804e2007-12-14 08:30:15 +00006614 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006615 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006616
6617 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006618 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006619
Owen Andersone50ed302009-08-10 22:56:29 +00006620 if (VT == EVT::i8)
6621 Op = DAG.getNode(ISD::TRUNCATE, dl, EVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006622 return Op;
6623}
6624
Dan Gohman475871a2008-07-27 21:46:04 +00006625SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00006626 EVT VT = Op.getValueType();
6627 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006628 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006629 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006630
6631 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006632 if (VT == EVT::i8) {
6633 OpVT = EVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006634 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006635 }
Evan Cheng152804e2007-12-14 08:30:15 +00006636
6637 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Andersone50ed302009-08-10 22:56:29 +00006638 SDVTList VTs = DAG.getVTList(OpVT, EVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006639 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006640
6641 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006642 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006643 Ops.push_back(Op);
6644 Ops.push_back(DAG.getConstant(NumBits, OpVT));
Owen Andersone50ed302009-08-10 22:56:29 +00006645 Ops.push_back(DAG.getConstant(X86::COND_E, EVT::i8));
Evan Cheng152804e2007-12-14 08:30:15 +00006646 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006647 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006648
Owen Andersone50ed302009-08-10 22:56:29 +00006649 if (VT == EVT::i8)
6650 Op = DAG.getNode(ISD::TRUNCATE, dl, EVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006651 return Op;
6652}
6653
Mon P Wangaf9b9522008-12-18 21:42:19 +00006654SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00006655 EVT VT = Op.getValueType();
6656 assert(VT == EVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006657 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006658
Mon P Wangaf9b9522008-12-18 21:42:19 +00006659 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6660 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6661 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6662 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6663 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6664 //
6665 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6666 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6667 // return AloBlo + AloBhi + AhiBlo;
6668
6669 SDValue A = Op.getOperand(0);
6670 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006671
Dale Johannesene4d209d2009-02-03 20:21:25 +00006672 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006673 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, EVT::i32),
6674 A, DAG.getConstant(32, EVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006675 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006676 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, EVT::i32),
6677 B, DAG.getConstant(32, EVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006678 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006679 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, EVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00006680 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006681 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006682 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, EVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00006683 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006684 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006685 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, EVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00006686 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006687 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006688 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, EVT::i32),
6689 AloBhi, DAG.getConstant(32, EVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006690 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00006691 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, EVT::i32),
6692 AhiBlo, DAG.getConstant(32, EVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006693 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6694 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006695 return Res;
6696}
6697
6698
Bill Wendling74c37652008-12-09 22:08:41 +00006699SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6700 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6701 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006702 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6703 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006704 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006705 SDValue LHS = N->getOperand(0);
6706 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006707 unsigned BaseOp = 0;
6708 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006709 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006710
6711 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006712 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00006713 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006714 // A subtract of one will be selected as a INC. Note that INC doesn't
6715 // set CF, so we can't do this for UADDO.
6716 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6717 if (C->getAPIntValue() == 1) {
6718 BaseOp = X86ISD::INC;
6719 Cond = X86::COND_O;
6720 break;
6721 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006722 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006723 Cond = X86::COND_O;
6724 break;
6725 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006726 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006727 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006728 break;
6729 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006730 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6731 // set CF, so we can't do this for USUBO.
6732 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6733 if (C->getAPIntValue() == 1) {
6734 BaseOp = X86ISD::DEC;
6735 Cond = X86::COND_O;
6736 break;
6737 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006738 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006739 Cond = X86::COND_O;
6740 break;
6741 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006742 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006743 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006744 break;
6745 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006746 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006747 Cond = X86::COND_O;
6748 break;
6749 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006750 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006751 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006752 break;
6753 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006754
Bill Wendling61edeb52008-12-02 01:06:39 +00006755 // Also sets EFLAGS.
Owen Andersone50ed302009-08-10 22:56:29 +00006756 SDVTList VTs = DAG.getVTList(N->getValueType(0), EVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006757 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006758
Bill Wendling61edeb52008-12-02 01:06:39 +00006759 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006760 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Andersone50ed302009-08-10 22:56:29 +00006761 DAG.getConstant(Cond, EVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006762
Bill Wendling61edeb52008-12-02 01:06:39 +00006763 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6764 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006765}
6766
Dan Gohman475871a2008-07-27 21:46:04 +00006767SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00006768 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006769 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006770 unsigned Reg = 0;
6771 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006772 switch(T.getSimpleVT()) {
6773 default:
6774 assert(false && "Invalid value type!");
Owen Andersone50ed302009-08-10 22:56:29 +00006775 case EVT::i8: Reg = X86::AL; size = 1; break;
6776 case EVT::i16: Reg = X86::AX; size = 2; break;
6777 case EVT::i32: Reg = X86::EAX; size = 4; break;
6778 case EVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006779 assert(Subtarget->is64Bit() && "Node not type legal!");
6780 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006781 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006782 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006783 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006784 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006785 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006786 Op.getOperand(1),
6787 Op.getOperand(3),
Owen Andersone50ed302009-08-10 22:56:29 +00006788 DAG.getTargetConstant(size, EVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006789 cpIn.getValue(1) };
Owen Andersone50ed302009-08-10 22:56:29 +00006790 SDVTList Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006791 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006792 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006793 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006794 return cpOut;
6795}
6796
Duncan Sands1607f052008-12-01 11:39:25 +00006797SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006798 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006799 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Andersone50ed302009-08-10 22:56:29 +00006800 SDVTList Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006801 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006802 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006803 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Andersone50ed302009-08-10 22:56:29 +00006804 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, EVT::i64, rd.getValue(1));
6805 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, EVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006806 rax.getValue(2));
Owen Andersone50ed302009-08-10 22:56:29 +00006807 SDValue Tmp = DAG.getNode(ISD::SHL, dl, EVT::i64, rdx,
6808 DAG.getConstant(32, EVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00006809 SDValue Ops[] = {
Owen Andersone50ed302009-08-10 22:56:29 +00006810 DAG.getNode(ISD::OR, dl, EVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006811 rdx.getValue(1)
6812 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006813 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006814}
6815
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006816SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6817 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006818 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006819 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006820 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006821 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006822 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006823 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006824 Node->getOperand(0),
6825 Node->getOperand(1), negOp,
6826 cast<AtomicSDNode>(Node)->getSrcValue(),
6827 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006828}
6829
Evan Cheng0db9fe62006-04-25 20:13:52 +00006830/// LowerOperation - Provide custom lowering hooks for some operations.
6831///
Dan Gohman475871a2008-07-27 21:46:04 +00006832SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006833 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006834 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006835 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6836 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006837 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6838 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6839 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6840 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6841 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6842 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6843 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006844 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006845 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846 case ISD::SHL_PARTS:
6847 case ISD::SRA_PARTS:
6848 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6849 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006850 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006851 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006852 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006853 case ISD::FABS: return LowerFABS(Op, DAG);
6854 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006855 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006856 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006857 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006858 case ISD::SELECT: return LowerSELECT(Op, DAG);
6859 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006860 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006861 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006862 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006863 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006864 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006865 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6866 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006867 case ISD::FRAME_TO_ARGS_OFFSET:
6868 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006869 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006870 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006871 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006872 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006873 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6874 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006875 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006876 case ISD::SADDO:
6877 case ISD::UADDO:
6878 case ISD::SSUBO:
6879 case ISD::USUBO:
6880 case ISD::SMULO:
6881 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006882 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006883 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006884}
6885
Duncan Sands1607f052008-12-01 11:39:25 +00006886void X86TargetLowering::
6887ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6888 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00006889 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006890 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006891 assert (T == EVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00006892
6893 SDValue Chain = Node->getOperand(0);
6894 SDValue In1 = Node->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006895 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006896 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Andersone50ed302009-08-10 22:56:29 +00006897 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006898 Node->getOperand(2), DAG.getIntPtrConstant(1));
6899 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6900 // have a MemOperand. Pass the info through as a normal operand.
6901 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6902 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Owen Andersone50ed302009-08-10 22:56:29 +00006903 SDVTList Tys = DAG.getVTList(EVT::i32, EVT::i32, EVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006904 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006905 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Andersone50ed302009-08-10 22:56:29 +00006906 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, EVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006907 Results.push_back(Result.getValue(2));
6908}
6909
Duncan Sands126d9072008-07-04 11:47:58 +00006910/// ReplaceNodeResults - Replace a node with an illegal result type
6911/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006912void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6913 SmallVectorImpl<SDValue>&Results,
6914 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006915 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006916 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006917 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006918 assert(false && "Do not know how to custom type legalize this operation!");
6919 return;
6920 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006921 std::pair<SDValue,SDValue> Vals =
6922 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006923 SDValue FIST = Vals.first, StackSlot = Vals.second;
6924 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006925 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00006926 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006927 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006928 }
6929 return;
6930 }
6931 case ISD::READCYCLECOUNTER: {
Owen Andersone50ed302009-08-10 22:56:29 +00006932 SDVTList Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006933 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006934 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Andersone50ed302009-08-10 22:56:29 +00006935 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, EVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006936 rd.getValue(1));
Owen Andersone50ed302009-08-10 22:56:29 +00006937 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, EVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006938 eax.getValue(2));
6939 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6940 SDValue Ops[] = { eax, edx };
Owen Andersone50ed302009-08-10 22:56:29 +00006941 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, EVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006942 Results.push_back(edx.getValue(1));
6943 return;
6944 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006945 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00006946 EVT T = N->getValueType(0);
6947 assert (T == EVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00006948 SDValue cpInL, cpInH;
Owen Andersone50ed302009-08-10 22:56:29 +00006949 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, N->getOperand(2),
6950 DAG.getConstant(0, EVT::i32));
6951 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, N->getOperand(2),
6952 DAG.getConstant(1, EVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006953 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6954 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006955 cpInL.getValue(1));
6956 SDValue swapInL, swapInH;
Owen Andersone50ed302009-08-10 22:56:29 +00006957 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, N->getOperand(3),
6958 DAG.getConstant(0, EVT::i32));
6959 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, N->getOperand(3),
6960 DAG.getConstant(1, EVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006961 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006962 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006963 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006964 swapInL.getValue(1));
6965 SDValue Ops[] = { swapInH.getValue(0),
6966 N->getOperand(1),
6967 swapInH.getValue(1) };
Owen Andersone50ed302009-08-10 22:56:29 +00006968 SDVTList Tys = DAG.getVTList(EVT::Other, EVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006969 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006970 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Andersone50ed302009-08-10 22:56:29 +00006971 EVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006972 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Andersone50ed302009-08-10 22:56:29 +00006973 EVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006974 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Andersone50ed302009-08-10 22:56:29 +00006975 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, EVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006976 Results.push_back(cpOutH.getValue(1));
6977 return;
6978 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006979 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006980 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6981 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006982 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006983 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6984 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006985 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006986 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6987 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006988 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006989 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6990 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006991 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006992 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6993 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006994 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006995 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6996 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006997 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006998 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6999 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007000 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007001}
7002
Evan Cheng72261582005-12-20 06:22:03 +00007003const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7004 switch (Opcode) {
7005 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007006 case X86ISD::BSF: return "X86ISD::BSF";
7007 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007008 case X86ISD::SHLD: return "X86ISD::SHLD";
7009 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007010 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007011 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007012 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007013 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007014 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007015 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007016 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7017 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7018 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007019 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007020 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007021 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007022 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007023 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007024 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007025 case X86ISD::COMI: return "X86ISD::COMI";
7026 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007027 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00007028 case X86ISD::CMOV: return "X86ISD::CMOV";
7029 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007030 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007031 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7032 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007033 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007034 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007035 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007036 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007037 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007038 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7039 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007040 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007041 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007042 case X86ISD::FMAX: return "X86ISD::FMAX";
7043 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007044 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7045 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007046 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007047 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007048 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007049 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007050 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007051 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7052 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007053 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7054 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7055 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7056 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7057 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7058 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007059 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7060 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007061 case X86ISD::VSHL: return "X86ISD::VSHL";
7062 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007063 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7064 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7065 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7066 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7067 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7068 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7069 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7070 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7071 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7072 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007073 case X86ISD::ADD: return "X86ISD::ADD";
7074 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007075 case X86ISD::SMUL: return "X86ISD::SMUL";
7076 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007077 case X86ISD::INC: return "X86ISD::INC";
7078 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00007079 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007080 case X86ISD::PTEST: return "X86ISD::PTEST";
Evan Cheng72261582005-12-20 06:22:03 +00007081 }
7082}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007083
Chris Lattnerc9addb72007-03-30 23:15:24 +00007084// isLegalAddressingMode - Return true if the addressing mode represented
7085// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007086bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007087 const Type *Ty) const {
7088 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007089 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007090
Chris Lattnerc9addb72007-03-30 23:15:24 +00007091 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007092 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007093 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007094
Chris Lattnerc9addb72007-03-30 23:15:24 +00007095 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007096 unsigned GVFlags =
7097 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007098
Chris Lattnerdfed4132009-07-10 07:38:24 +00007099 // If a reference to this global requires an extra load, we can't fold it.
7100 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007101 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007102
Chris Lattnerdfed4132009-07-10 07:38:24 +00007103 // If BaseGV requires a register for the PIC base, we cannot also have a
7104 // BaseReg specified.
7105 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007106 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007107
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007108 // If lower 4G is not available, then we must use rip-relative addressing.
7109 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7110 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007112
Chris Lattnerc9addb72007-03-30 23:15:24 +00007113 switch (AM.Scale) {
7114 case 0:
7115 case 1:
7116 case 2:
7117 case 4:
7118 case 8:
7119 // These scales always work.
7120 break;
7121 case 3:
7122 case 5:
7123 case 9:
7124 // These scales are formed with basereg+scalereg. Only accept if there is
7125 // no basereg yet.
7126 if (AM.HasBaseReg)
7127 return false;
7128 break;
7129 default: // Other stuff never works.
7130 return false;
7131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007132
Chris Lattnerc9addb72007-03-30 23:15:24 +00007133 return true;
7134}
7135
7136
Evan Cheng2bd122c2007-10-26 01:56:11 +00007137bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7138 if (!Ty1->isInteger() || !Ty2->isInteger())
7139 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007140 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7141 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007142 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007143 return false;
7144 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007145}
7146
Owen Andersone50ed302009-08-10 22:56:29 +00007147bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007148 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007149 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007150 unsigned NumBits1 = VT1.getSizeInBits();
7151 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007152 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007153 return false;
7154 return Subtarget->is64Bit() || NumBits1 < 64;
7155}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007156
Dan Gohman97121ba2009-04-08 00:15:30 +00007157bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007158 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00007159 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
7160}
7161
Owen Andersone50ed302009-08-10 22:56:29 +00007162bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007163 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Andersone50ed302009-08-10 22:56:29 +00007164 return VT1 == EVT::i32 && VT2 == EVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007165}
7166
Owen Andersone50ed302009-08-10 22:56:29 +00007167bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007168 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Andersone50ed302009-08-10 22:56:29 +00007169 return !(VT1 == EVT::i32 && VT2 == EVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007170}
7171
Evan Cheng60c07e12006-07-05 22:17:51 +00007172/// isShuffleMaskLegal - Targets can use this to indicate that they only
7173/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7174/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7175/// are assumed to be legal.
7176bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007177X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007178 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007179 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007180 if (VT.getSizeInBits() == 64)
7181 return false;
7182
7183 // FIXME: pshufb, blends, palignr, shifts.
7184 return (VT.getVectorNumElements() == 2 ||
7185 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7186 isMOVLMask(M, VT) ||
7187 isSHUFPMask(M, VT) ||
7188 isPSHUFDMask(M, VT) ||
7189 isPSHUFHWMask(M, VT) ||
7190 isPSHUFLWMask(M, VT) ||
7191 isUNPCKLMask(M, VT) ||
7192 isUNPCKHMask(M, VT) ||
7193 isUNPCKL_v_undef_Mask(M, VT) ||
7194 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007195}
7196
Dan Gohman7d8143f2008-04-09 20:09:42 +00007197bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007198X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007199 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007200 unsigned NumElts = VT.getVectorNumElements();
7201 // FIXME: This collection of masks seems suspect.
7202 if (NumElts == 2)
7203 return true;
7204 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7205 return (isMOVLMask(Mask, VT) ||
7206 isCommutedMOVLMask(Mask, VT, true) ||
7207 isSHUFPMask(Mask, VT) ||
7208 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007209 }
7210 return false;
7211}
7212
7213//===----------------------------------------------------------------------===//
7214// X86 Scheduler Hooks
7215//===----------------------------------------------------------------------===//
7216
Mon P Wang63307c32008-05-05 19:05:59 +00007217// private utility function
7218MachineBasicBlock *
7219X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7220 MachineBasicBlock *MBB,
7221 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007222 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007223 unsigned LoadOpc,
7224 unsigned CXchgOpc,
7225 unsigned copyOpc,
7226 unsigned notOpc,
7227 unsigned EAXreg,
7228 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007229 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007230 // For the atomic bitwise operator, we generate
7231 // thisMBB:
7232 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007233 // ld t1 = [bitinstr.addr]
7234 // op t2 = t1, [bitinstr.val]
7235 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007236 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7237 // bz newMBB
7238 // fallthrough -->nextMBB
7239 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7240 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007241 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007242 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007243
Mon P Wang63307c32008-05-05 19:05:59 +00007244 /// First build the CFG
7245 MachineFunction *F = MBB->getParent();
7246 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007247 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7248 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7249 F->insert(MBBIter, newMBB);
7250 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007251
Mon P Wang63307c32008-05-05 19:05:59 +00007252 // Move all successors to thisMBB to nextMBB
7253 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007254
Mon P Wang63307c32008-05-05 19:05:59 +00007255 // Update thisMBB to fall through to newMBB
7256 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007257
Mon P Wang63307c32008-05-05 19:05:59 +00007258 // newMBB jumps to itself and fall through to nextMBB
7259 newMBB->addSuccessor(nextMBB);
7260 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007261
Mon P Wang63307c32008-05-05 19:05:59 +00007262 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007263 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007264 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007265 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007266 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007267 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007268 int numArgs = bInstr->getNumOperands() - 1;
7269 for (int i=0; i < numArgs; ++i)
7270 argOpers[i] = &bInstr->getOperand(i+1);
7271
7272 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007273 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7274 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007275
Dale Johannesen140be2d2008-08-19 18:47:28 +00007276 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007277 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007278 for (int i=0; i <= lastAddrIndx; ++i)
7279 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007280
Dale Johannesen140be2d2008-08-19 18:47:28 +00007281 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007282 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007283 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007284 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007285 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007286 tt = t1;
7287
Dale Johannesen140be2d2008-08-19 18:47:28 +00007288 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007289 assert((argOpers[valArgIndx]->isReg() ||
7290 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007291 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007292 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007293 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007294 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007295 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007296 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007297 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007298
Dale Johannesene4d209d2009-02-03 20:21:25 +00007299 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007300 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007301
Dale Johannesene4d209d2009-02-03 20:21:25 +00007302 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007303 for (int i=0; i <= lastAddrIndx; ++i)
7304 (*MIB).addOperand(*argOpers[i]);
7305 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007306 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7307 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7308
Dale Johannesene4d209d2009-02-03 20:21:25 +00007309 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007310 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007311
Mon P Wang63307c32008-05-05 19:05:59 +00007312 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007313 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007314
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007315 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007316 return nextMBB;
7317}
7318
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007319// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007320MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007321X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7322 MachineBasicBlock *MBB,
7323 unsigned regOpcL,
7324 unsigned regOpcH,
7325 unsigned immOpcL,
7326 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007327 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007328 // For the atomic bitwise operator, we generate
7329 // thisMBB (instructions are in pairs, except cmpxchg8b)
7330 // ld t1,t2 = [bitinstr.addr]
7331 // newMBB:
7332 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7333 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007334 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007335 // mov ECX, EBX <- t5, t6
7336 // mov EAX, EDX <- t1, t2
7337 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7338 // mov t3, t4 <- EAX, EDX
7339 // bz newMBB
7340 // result in out1, out2
7341 // fallthrough -->nextMBB
7342
7343 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7344 const unsigned LoadOpc = X86::MOV32rm;
7345 const unsigned copyOpc = X86::MOV32rr;
7346 const unsigned NotOpc = X86::NOT32r;
7347 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7348 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7349 MachineFunction::iterator MBBIter = MBB;
7350 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007351
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007352 /// First build the CFG
7353 MachineFunction *F = MBB->getParent();
7354 MachineBasicBlock *thisMBB = MBB;
7355 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7356 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7357 F->insert(MBBIter, newMBB);
7358 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007359
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007360 // Move all successors to thisMBB to nextMBB
7361 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007362
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007363 // Update thisMBB to fall through to newMBB
7364 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007365
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007366 // newMBB jumps to itself and fall through to nextMBB
7367 newMBB->addSuccessor(nextMBB);
7368 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007369
Dale Johannesene4d209d2009-02-03 20:21:25 +00007370 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007371 // Insert instructions into newMBB based on incoming instruction
7372 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007373 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007374 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007375 MachineOperand& dest1Oper = bInstr->getOperand(0);
7376 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007377 MachineOperand* argOpers[2 + X86AddrNumOperands];
7378 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007379 argOpers[i] = &bInstr->getOperand(i+2);
7380
7381 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007382 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007383
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007384 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007385 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007386 for (int i=0; i <= lastAddrIndx; ++i)
7387 (*MIB).addOperand(*argOpers[i]);
7388 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007389 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007390 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007391 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007392 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007393 MachineOperand newOp3 = *(argOpers[3]);
7394 if (newOp3.isImm())
7395 newOp3.setImm(newOp3.getImm()+4);
7396 else
7397 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007398 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007399 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007400
7401 // t3/4 are defined later, at the bottom of the loop
7402 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7403 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007404 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007405 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007406 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007407 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7408
7409 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7410 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007411 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7413 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007414 } else {
7415 tt1 = t1;
7416 tt2 = t2;
7417 }
7418
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007419 int valArgIndx = lastAddrIndx + 1;
7420 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007421 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007422 "invalid operand");
7423 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7424 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007425 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007426 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007427 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007428 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007429 if (regOpcL != X86::MOV32rr)
7430 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007431 (*MIB).addOperand(*argOpers[valArgIndx]);
7432 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007433 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007434 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007435 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007436 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007437 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007438 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007439 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007440 if (regOpcH != X86::MOV32rr)
7441 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007442 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007443
Dale Johannesene4d209d2009-02-03 20:21:25 +00007444 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007445 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007446 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007447 MIB.addReg(t2);
7448
Dale Johannesene4d209d2009-02-03 20:21:25 +00007449 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007450 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007451 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007452 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007453
Dale Johannesene4d209d2009-02-03 20:21:25 +00007454 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007455 for (int i=0; i <= lastAddrIndx; ++i)
7456 (*MIB).addOperand(*argOpers[i]);
7457
7458 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7459 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7460
Dale Johannesene4d209d2009-02-03 20:21:25 +00007461 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007462 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007463 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007464 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007465
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007466 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007467 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007468
7469 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7470 return nextMBB;
7471}
7472
7473// private utility function
7474MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007475X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7476 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007477 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007478 // For the atomic min/max operator, we generate
7479 // thisMBB:
7480 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007481 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007482 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007483 // cmp t1, t2
7484 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007485 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007486 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7487 // bz newMBB
7488 // fallthrough -->nextMBB
7489 //
7490 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7491 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007492 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007493 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007494
Mon P Wang63307c32008-05-05 19:05:59 +00007495 /// First build the CFG
7496 MachineFunction *F = MBB->getParent();
7497 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007498 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7499 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7500 F->insert(MBBIter, newMBB);
7501 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007502
Mon P Wang63307c32008-05-05 19:05:59 +00007503 // Move all successors to thisMBB to nextMBB
7504 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007505
Mon P Wang63307c32008-05-05 19:05:59 +00007506 // Update thisMBB to fall through to newMBB
7507 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007508
Mon P Wang63307c32008-05-05 19:05:59 +00007509 // newMBB jumps to newMBB and fall through to nextMBB
7510 newMBB->addSuccessor(nextMBB);
7511 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007512
Dale Johannesene4d209d2009-02-03 20:21:25 +00007513 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007514 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007515 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007516 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007517 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007518 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007519 int numArgs = mInstr->getNumOperands() - 1;
7520 for (int i=0; i < numArgs; ++i)
7521 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007522
Mon P Wang63307c32008-05-05 19:05:59 +00007523 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007524 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7525 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007526
Mon P Wangab3e7472008-05-05 22:56:23 +00007527 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007528 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007529 for (int i=0; i <= lastAddrIndx; ++i)
7530 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007531
Mon P Wang63307c32008-05-05 19:05:59 +00007532 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007533 assert((argOpers[valArgIndx]->isReg() ||
7534 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007535 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007536
7537 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007538 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007539 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007540 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007541 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007542 (*MIB).addOperand(*argOpers[valArgIndx]);
7543
Dale Johannesene4d209d2009-02-03 20:21:25 +00007544 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007545 MIB.addReg(t1);
7546
Dale Johannesene4d209d2009-02-03 20:21:25 +00007547 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007548 MIB.addReg(t1);
7549 MIB.addReg(t2);
7550
7551 // Generate movc
7552 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007553 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007554 MIB.addReg(t2);
7555 MIB.addReg(t1);
7556
7557 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007558 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007559 for (int i=0; i <= lastAddrIndx; ++i)
7560 (*MIB).addOperand(*argOpers[i]);
7561 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007562 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7563 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007564
Dale Johannesene4d209d2009-02-03 20:21:25 +00007565 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007566 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007567
Mon P Wang63307c32008-05-05 19:05:59 +00007568 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007569 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007570
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007571 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007572 return nextMBB;
7573}
7574
7575
Evan Cheng60c07e12006-07-05 22:17:51 +00007576MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007577X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007578 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007579 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007580 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007581 switch (MI->getOpcode()) {
7582 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007583 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007584 case X86::CMOV_FR32:
7585 case X86::CMOV_FR64:
7586 case X86::CMOV_V4F32:
7587 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007588 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007589 // To "insert" a SELECT_CC instruction, we actually have to insert the
7590 // diamond control-flow pattern. The incoming instruction knows the
7591 // destination vreg to set, the condition code register to branch on, the
7592 // true/false values to select between, and a branch opcode to use.
7593 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007594 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007595 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007596
Evan Cheng60c07e12006-07-05 22:17:51 +00007597 // thisMBB:
7598 // ...
7599 // TrueVal = ...
7600 // cmpTY ccX, r1, r2
7601 // bCC copy1MBB
7602 // fallthrough --> copy0MBB
7603 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007604 MachineFunction *F = BB->getParent();
7605 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7606 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007607 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007608 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007609 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007610 F->insert(It, copy0MBB);
7611 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007612 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007613 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007614 sinkMBB->transferSuccessors(BB);
7615
7616 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007617 BB->addSuccessor(copy0MBB);
7618 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007619
Evan Cheng60c07e12006-07-05 22:17:51 +00007620 // copy0MBB:
7621 // %FalseValue = ...
7622 // # fallthrough to sinkMBB
7623 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007624
Evan Cheng60c07e12006-07-05 22:17:51 +00007625 // Update machine-CFG edges
7626 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007627
Evan Cheng60c07e12006-07-05 22:17:51 +00007628 // sinkMBB:
7629 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7630 // ...
7631 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007632 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007633 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7634 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7635
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007636 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007637 return BB;
7638 }
7639
Dale Johannesen849f2142007-07-03 00:53:03 +00007640 case X86::FP32_TO_INT16_IN_MEM:
7641 case X86::FP32_TO_INT32_IN_MEM:
7642 case X86::FP32_TO_INT64_IN_MEM:
7643 case X86::FP64_TO_INT16_IN_MEM:
7644 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007645 case X86::FP64_TO_INT64_IN_MEM:
7646 case X86::FP80_TO_INT16_IN_MEM:
7647 case X86::FP80_TO_INT32_IN_MEM:
7648 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007649 // Change the floating point control register to use "round towards zero"
7650 // mode when truncating to an integer value.
7651 MachineFunction *F = BB->getParent();
7652 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007653 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007654
7655 // Load the old value of the high byte of the control word...
7656 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007657 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007658 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007659 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007660
7661 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007662 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007663 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007664
7665 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007666 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007667
7668 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007669 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007670 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007671
7672 // Get the X86 opcode to use.
7673 unsigned Opc;
7674 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007675 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007676 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7677 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7678 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7679 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7680 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7681 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007682 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7683 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7684 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007685 }
7686
7687 X86AddressMode AM;
7688 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007689 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007690 AM.BaseType = X86AddressMode::RegBase;
7691 AM.Base.Reg = Op.getReg();
7692 } else {
7693 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007694 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007695 }
7696 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007697 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007698 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007699 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007700 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007701 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007702 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007703 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007704 AM.GV = Op.getGlobal();
7705 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007706 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007707 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007708 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007709 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007710
7711 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007712 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007713
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007714 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007715 return BB;
7716 }
Mon P Wang63307c32008-05-05 19:05:59 +00007717 case X86::ATOMAND32:
7718 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007719 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007720 X86::LCMPXCHG32, X86::MOV32rr,
7721 X86::NOT32r, X86::EAX,
7722 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007723 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007724 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7725 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007726 X86::LCMPXCHG32, X86::MOV32rr,
7727 X86::NOT32r, X86::EAX,
7728 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007729 case X86::ATOMXOR32:
7730 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007731 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007732 X86::LCMPXCHG32, X86::MOV32rr,
7733 X86::NOT32r, X86::EAX,
7734 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007735 case X86::ATOMNAND32:
7736 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007737 X86::AND32ri, X86::MOV32rm,
7738 X86::LCMPXCHG32, X86::MOV32rr,
7739 X86::NOT32r, X86::EAX,
7740 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007741 case X86::ATOMMIN32:
7742 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7743 case X86::ATOMMAX32:
7744 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7745 case X86::ATOMUMIN32:
7746 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7747 case X86::ATOMUMAX32:
7748 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007749
7750 case X86::ATOMAND16:
7751 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7752 X86::AND16ri, X86::MOV16rm,
7753 X86::LCMPXCHG16, X86::MOV16rr,
7754 X86::NOT16r, X86::AX,
7755 X86::GR16RegisterClass);
7756 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007757 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007758 X86::OR16ri, X86::MOV16rm,
7759 X86::LCMPXCHG16, X86::MOV16rr,
7760 X86::NOT16r, X86::AX,
7761 X86::GR16RegisterClass);
7762 case X86::ATOMXOR16:
7763 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7764 X86::XOR16ri, X86::MOV16rm,
7765 X86::LCMPXCHG16, X86::MOV16rr,
7766 X86::NOT16r, X86::AX,
7767 X86::GR16RegisterClass);
7768 case X86::ATOMNAND16:
7769 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7770 X86::AND16ri, X86::MOV16rm,
7771 X86::LCMPXCHG16, X86::MOV16rr,
7772 X86::NOT16r, X86::AX,
7773 X86::GR16RegisterClass, true);
7774 case X86::ATOMMIN16:
7775 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7776 case X86::ATOMMAX16:
7777 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7778 case X86::ATOMUMIN16:
7779 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7780 case X86::ATOMUMAX16:
7781 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7782
7783 case X86::ATOMAND8:
7784 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7785 X86::AND8ri, X86::MOV8rm,
7786 X86::LCMPXCHG8, X86::MOV8rr,
7787 X86::NOT8r, X86::AL,
7788 X86::GR8RegisterClass);
7789 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007790 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007791 X86::OR8ri, X86::MOV8rm,
7792 X86::LCMPXCHG8, X86::MOV8rr,
7793 X86::NOT8r, X86::AL,
7794 X86::GR8RegisterClass);
7795 case X86::ATOMXOR8:
7796 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7797 X86::XOR8ri, X86::MOV8rm,
7798 X86::LCMPXCHG8, X86::MOV8rr,
7799 X86::NOT8r, X86::AL,
7800 X86::GR8RegisterClass);
7801 case X86::ATOMNAND8:
7802 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7803 X86::AND8ri, X86::MOV8rm,
7804 X86::LCMPXCHG8, X86::MOV8rr,
7805 X86::NOT8r, X86::AL,
7806 X86::GR8RegisterClass, true);
7807 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007808 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007809 case X86::ATOMAND64:
7810 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007811 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007812 X86::LCMPXCHG64, X86::MOV64rr,
7813 X86::NOT64r, X86::RAX,
7814 X86::GR64RegisterClass);
7815 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007816 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7817 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007818 X86::LCMPXCHG64, X86::MOV64rr,
7819 X86::NOT64r, X86::RAX,
7820 X86::GR64RegisterClass);
7821 case X86::ATOMXOR64:
7822 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007823 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007824 X86::LCMPXCHG64, X86::MOV64rr,
7825 X86::NOT64r, X86::RAX,
7826 X86::GR64RegisterClass);
7827 case X86::ATOMNAND64:
7828 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7829 X86::AND64ri32, X86::MOV64rm,
7830 X86::LCMPXCHG64, X86::MOV64rr,
7831 X86::NOT64r, X86::RAX,
7832 X86::GR64RegisterClass, true);
7833 case X86::ATOMMIN64:
7834 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7835 case X86::ATOMMAX64:
7836 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7837 case X86::ATOMUMIN64:
7838 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7839 case X86::ATOMUMAX64:
7840 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007841
7842 // This group does 64-bit operations on a 32-bit host.
7843 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007844 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007845 X86::AND32rr, X86::AND32rr,
7846 X86::AND32ri, X86::AND32ri,
7847 false);
7848 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007849 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007850 X86::OR32rr, X86::OR32rr,
7851 X86::OR32ri, X86::OR32ri,
7852 false);
7853 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007854 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007855 X86::XOR32rr, X86::XOR32rr,
7856 X86::XOR32ri, X86::XOR32ri,
7857 false);
7858 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007859 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007860 X86::AND32rr, X86::AND32rr,
7861 X86::AND32ri, X86::AND32ri,
7862 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007863 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007864 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007865 X86::ADD32rr, X86::ADC32rr,
7866 X86::ADD32ri, X86::ADC32ri,
7867 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007868 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007869 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007870 X86::SUB32rr, X86::SBB32rr,
7871 X86::SUB32ri, X86::SBB32ri,
7872 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007873 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007874 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007875 X86::MOV32rr, X86::MOV32rr,
7876 X86::MOV32ri, X86::MOV32ri,
7877 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007878 }
7879}
7880
7881//===----------------------------------------------------------------------===//
7882// X86 Optimization Hooks
7883//===----------------------------------------------------------------------===//
7884
Dan Gohman475871a2008-07-27 21:46:04 +00007885void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007886 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007887 APInt &KnownZero,
7888 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007889 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007890 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007891 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007892 assert((Opc >= ISD::BUILTIN_OP_END ||
7893 Opc == ISD::INTRINSIC_WO_CHAIN ||
7894 Opc == ISD::INTRINSIC_W_CHAIN ||
7895 Opc == ISD::INTRINSIC_VOID) &&
7896 "Should use MaskedValueIsZero if you don't know whether Op"
7897 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007898
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007899 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007900 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007901 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007902 case X86ISD::ADD:
7903 case X86ISD::SUB:
7904 case X86ISD::SMUL:
7905 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007906 case X86ISD::INC:
7907 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007908 // These nodes' second result is a boolean.
7909 if (Op.getResNo() == 0)
7910 break;
7911 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007912 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007913 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7914 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007915 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007916 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007917}
Chris Lattner259e97c2006-01-31 19:43:35 +00007918
Evan Cheng206ee9d2006-07-07 08:33:52 +00007919/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007920/// node is a GlobalAddress + offset.
7921bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7922 GlobalValue* &GA, int64_t &Offset) const{
7923 if (N->getOpcode() == X86ISD::Wrapper) {
7924 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007925 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007926 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007927 return true;
7928 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007929 }
Evan Chengad4196b2008-05-12 19:56:52 +00007930 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007931}
7932
Evan Chengad4196b2008-05-12 19:56:52 +00007933static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7934 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007935 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007936 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007937 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007938 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007939 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007940 return false;
7941}
7942
Nate Begeman9008ca62009-04-27 18:41:29 +00007943static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Owen Andersone50ed302009-08-10 22:56:29 +00007944 EVT EVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00007945 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00007946 SelectionDAG &DAG, MachineFrameInfo *MFI,
7947 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007948 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00007949 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007950 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007951 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007952 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00007953 return false;
7954 continue;
7955 }
7956
Dan Gohman475871a2008-07-27 21:46:04 +00007957 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007958 if (!Elt.getNode() ||
7959 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007960 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007961 if (!LDBase) {
7962 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00007963 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007964 LDBase = cast<LoadSDNode>(Elt.getNode());
7965 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007966 continue;
7967 }
7968 if (Elt.getOpcode() == ISD::UNDEF)
7969 continue;
7970
Nate Begemanabc01992009-06-05 21:37:30 +00007971 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Nate Begemanabc01992009-06-05 21:37:30 +00007972 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007973 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007974 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007975 }
7976 return true;
7977}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007978
7979/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7980/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7981/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007982/// order. In the case of v2i64, it will see if it can rewrite the
7983/// shuffle to be an appropriate build vector so it can take advantage of
7984// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007985static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007986 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007987 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007988 EVT VT = N->getValueType(0);
7989 EVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007990 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7991 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007992
Eli Friedman7a5e5552009-06-07 06:52:44 +00007993 if (VT.getSizeInBits() != 128)
7994 return SDValue();
7995
Mon P Wang1e955802009-04-03 02:43:30 +00007996 // Try to combine a vector_shuffle into a 128-bit load.
7997 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00007998 LoadSDNode *LD = NULL;
7999 unsigned LastLoadedElt;
8000 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG,
8001 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008002 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008003
Eli Friedman7a5e5552009-06-07 06:52:44 +00008004 if (LastLoadedElt == NumElems - 1) {
8005 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI))
8006 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8007 LD->getSrcValue(), LD->getSrcValueOffset(),
8008 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008009 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008010 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008011 LD->isVolatile(), LD->getAlignment());
8012 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Andersone50ed302009-08-10 22:56:29 +00008013 SDVTList Tys = DAG.getVTList(EVT::v2i64, EVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008014 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8015 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008016 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8017 }
8018 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008019}
Evan Chengd880b972008-05-09 21:53:03 +00008020
Chris Lattner83e6c992006-10-04 06:57:07 +00008021/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008022static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008023 const X86Subtarget *Subtarget) {
8024 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008025 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008026 // Get the LHS/RHS of the select.
8027 SDValue LHS = N->getOperand(1);
8028 SDValue RHS = N->getOperand(2);
8029
Chris Lattner83e6c992006-10-04 06:57:07 +00008030 // If we have SSE[12] support, try to form min/max nodes.
8031 if (Subtarget->hasSSE2() &&
Owen Andersone50ed302009-08-10 22:56:29 +00008032 (LHS.getValueType() == EVT::f32 || LHS.getValueType() == EVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008033 Cond.getOpcode() == ISD::SETCC) {
8034 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008035
Chris Lattner47b4ce82009-03-11 05:48:52 +00008036 unsigned Opcode = 0;
8037 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8038 switch (CC) {
8039 default: break;
8040 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
8041 case ISD::SETULE:
8042 case ISD::SETLE:
8043 if (!UnsafeFPMath) break;
8044 // FALL THROUGH.
8045 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
8046 case ISD::SETLT:
8047 Opcode = X86ISD::FMIN;
8048 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008049
Chris Lattner47b4ce82009-03-11 05:48:52 +00008050 case ISD::SETOGT: // (X > Y) ? X : Y -> max
8051 case ISD::SETUGT:
8052 case ISD::SETGT:
8053 if (!UnsafeFPMath) break;
8054 // FALL THROUGH.
8055 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
8056 case ISD::SETGE:
8057 Opcode = X86ISD::FMAX;
8058 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008059 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008060 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8061 switch (CC) {
8062 default: break;
8063 case ISD::SETOGT: // (X > Y) ? Y : X -> min
8064 case ISD::SETUGT:
8065 case ISD::SETGT:
8066 if (!UnsafeFPMath) break;
8067 // FALL THROUGH.
8068 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
8069 case ISD::SETGE:
8070 Opcode = X86ISD::FMIN;
8071 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008072
Chris Lattner47b4ce82009-03-11 05:48:52 +00008073 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
8074 case ISD::SETULE:
8075 case ISD::SETLE:
8076 if (!UnsafeFPMath) break;
8077 // FALL THROUGH.
8078 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
8079 case ISD::SETLT:
8080 Opcode = X86ISD::FMAX;
8081 break;
8082 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008083 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008084
Chris Lattner47b4ce82009-03-11 05:48:52 +00008085 if (Opcode)
8086 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008087 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008088
Chris Lattnerd1980a52009-03-12 06:52:53 +00008089 // If this is a select between two integer constants, try to do some
8090 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008091 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8092 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008093 // Don't do this for crazy integer types.
8094 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8095 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008096 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008097 bool NeedsCondInvert = false;
8098
Chris Lattnercee56e72009-03-13 05:53:31 +00008099 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008100 // Efficiently invertible.
8101 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8102 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8103 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8104 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008105 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008106 }
8107
8108 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008109 if (FalseC->getAPIntValue() == 0 &&
8110 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008111 if (NeedsCondInvert) // Invert the condition if needed.
8112 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8113 DAG.getConstant(1, Cond.getValueType()));
8114
8115 // Zero extend the condition if needed.
8116 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
8117
Chris Lattnercee56e72009-03-13 05:53:31 +00008118 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008119 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Andersone50ed302009-08-10 22:56:29 +00008120 DAG.getConstant(ShAmt, EVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008121 }
Chris Lattner97a29a52009-03-13 05:22:11 +00008122
8123 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008124 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008125 if (NeedsCondInvert) // Invert the condition if needed.
8126 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8127 DAG.getConstant(1, Cond.getValueType()));
8128
8129 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008130 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8131 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008132 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008133 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008134 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008135
8136 // Optimize cases that will turn into an LEA instruction. This requires
8137 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Andersone50ed302009-08-10 22:56:29 +00008138 if (N->getValueType(0) == EVT::i32 || N->getValueType(0) == EVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008139 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Andersone50ed302009-08-10 22:56:29 +00008140 if (N->getValueType(0) == EVT::i32) Diff = (unsigned)Diff;
Chris Lattnercee56e72009-03-13 05:53:31 +00008141
8142 bool isFastMultiplier = false;
8143 if (Diff < 10) {
8144 switch ((unsigned char)Diff) {
8145 default: break;
8146 case 1: // result = add base, cond
8147 case 2: // result = lea base( , cond*2)
8148 case 3: // result = lea base(cond, cond*2)
8149 case 4: // result = lea base( , cond*4)
8150 case 5: // result = lea base(cond, cond*4)
8151 case 8: // result = lea base( , cond*8)
8152 case 9: // result = lea base(cond, cond*8)
8153 isFastMultiplier = true;
8154 break;
8155 }
8156 }
8157
8158 if (isFastMultiplier) {
8159 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8160 if (NeedsCondInvert) // Invert the condition if needed.
8161 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8162 DAG.getConstant(1, Cond.getValueType()));
8163
8164 // Zero extend the condition if needed.
8165 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8166 Cond);
8167 // Scale the condition by the difference.
8168 if (Diff != 1)
8169 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8170 DAG.getConstant(Diff, Cond.getValueType()));
8171
8172 // Add the base if non-zero.
8173 if (FalseC->getAPIntValue() != 0)
8174 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8175 SDValue(FalseC, 0));
8176 return Cond;
8177 }
8178 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008179 }
8180 }
8181
Dan Gohman475871a2008-07-27 21:46:04 +00008182 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008183}
8184
Chris Lattnerd1980a52009-03-12 06:52:53 +00008185/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8186static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8187 TargetLowering::DAGCombinerInfo &DCI) {
8188 DebugLoc DL = N->getDebugLoc();
8189
8190 // If the flag operand isn't dead, don't touch this CMOV.
8191 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8192 return SDValue();
8193
8194 // If this is a select between two integer constants, try to do some
8195 // optimizations. Note that the operands are ordered the opposite of SELECT
8196 // operands.
8197 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8198 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8199 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8200 // larger than FalseC (the false value).
8201 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8202
8203 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8204 CC = X86::GetOppositeBranchCondition(CC);
8205 std::swap(TrueC, FalseC);
8206 }
8207
8208 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008209 // This is efficient for any integer data type (including i8/i16) and
8210 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008211 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8212 SDValue Cond = N->getOperand(3);
Owen Andersone50ed302009-08-10 22:56:29 +00008213 Cond = DAG.getNode(X86ISD::SETCC, DL, EVT::i8,
8214 DAG.getConstant(CC, EVT::i8), Cond);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008215
8216 // Zero extend the condition if needed.
8217 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8218
8219 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8220 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Andersone50ed302009-08-10 22:56:29 +00008221 DAG.getConstant(ShAmt, EVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008222 if (N->getNumValues() == 2) // Dead flag value?
8223 return DCI.CombineTo(N, Cond, SDValue());
8224 return Cond;
8225 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008226
8227 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8228 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008229 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8230 SDValue Cond = N->getOperand(3);
Owen Andersone50ed302009-08-10 22:56:29 +00008231 Cond = DAG.getNode(X86ISD::SETCC, DL, EVT::i8,
8232 DAG.getConstant(CC, EVT::i8), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008233
8234 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008235 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8236 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008237 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8238 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008239
Chris Lattner97a29a52009-03-13 05:22:11 +00008240 if (N->getNumValues() == 2) // Dead flag value?
8241 return DCI.CombineTo(N, Cond, SDValue());
8242 return Cond;
8243 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008244
8245 // Optimize cases that will turn into an LEA instruction. This requires
8246 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Andersone50ed302009-08-10 22:56:29 +00008247 if (N->getValueType(0) == EVT::i32 || N->getValueType(0) == EVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008248 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Andersone50ed302009-08-10 22:56:29 +00008249 if (N->getValueType(0) == EVT::i32) Diff = (unsigned)Diff;
Chris Lattnercee56e72009-03-13 05:53:31 +00008250
8251 bool isFastMultiplier = false;
8252 if (Diff < 10) {
8253 switch ((unsigned char)Diff) {
8254 default: break;
8255 case 1: // result = add base, cond
8256 case 2: // result = lea base( , cond*2)
8257 case 3: // result = lea base(cond, cond*2)
8258 case 4: // result = lea base( , cond*4)
8259 case 5: // result = lea base(cond, cond*4)
8260 case 8: // result = lea base( , cond*8)
8261 case 9: // result = lea base(cond, cond*8)
8262 isFastMultiplier = true;
8263 break;
8264 }
8265 }
8266
8267 if (isFastMultiplier) {
8268 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8269 SDValue Cond = N->getOperand(3);
Owen Andersone50ed302009-08-10 22:56:29 +00008270 Cond = DAG.getNode(X86ISD::SETCC, DL, EVT::i8,
8271 DAG.getConstant(CC, EVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00008272 // Zero extend the condition if needed.
8273 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8274 Cond);
8275 // Scale the condition by the difference.
8276 if (Diff != 1)
8277 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8278 DAG.getConstant(Diff, Cond.getValueType()));
8279
8280 // Add the base if non-zero.
8281 if (FalseC->getAPIntValue() != 0)
8282 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8283 SDValue(FalseC, 0));
8284 if (N->getNumValues() == 2) // Dead flag value?
8285 return DCI.CombineTo(N, Cond, SDValue());
8286 return Cond;
8287 }
8288 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008289 }
8290 }
8291 return SDValue();
8292}
8293
8294
Evan Cheng0b0cd912009-03-28 05:57:29 +00008295/// PerformMulCombine - Optimize a single multiply with constant into two
8296/// in order to implement it with two cheaper instructions, e.g.
8297/// LEA + SHL, LEA + LEA.
8298static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8299 TargetLowering::DAGCombinerInfo &DCI) {
8300 if (DAG.getMachineFunction().
8301 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8302 return SDValue();
8303
8304 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8305 return SDValue();
8306
Owen Andersone50ed302009-08-10 22:56:29 +00008307 EVT VT = N->getValueType(0);
8308 if (VT != EVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00008309 return SDValue();
8310
8311 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8312 if (!C)
8313 return SDValue();
8314 uint64_t MulAmt = C->getZExtValue();
8315 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8316 return SDValue();
8317
8318 uint64_t MulAmt1 = 0;
8319 uint64_t MulAmt2 = 0;
8320 if ((MulAmt % 9) == 0) {
8321 MulAmt1 = 9;
8322 MulAmt2 = MulAmt / 9;
8323 } else if ((MulAmt % 5) == 0) {
8324 MulAmt1 = 5;
8325 MulAmt2 = MulAmt / 5;
8326 } else if ((MulAmt % 3) == 0) {
8327 MulAmt1 = 3;
8328 MulAmt2 = MulAmt / 3;
8329 }
8330 if (MulAmt2 &&
8331 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8332 DebugLoc DL = N->getDebugLoc();
8333
8334 if (isPowerOf2_64(MulAmt2) &&
8335 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8336 // If second multiplifer is pow2, issue it first. We want the multiply by
8337 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8338 // is an add.
8339 std::swap(MulAmt1, MulAmt2);
8340
8341 SDValue NewMul;
8342 if (isPowerOf2_64(MulAmt1))
8343 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Andersone50ed302009-08-10 22:56:29 +00008344 DAG.getConstant(Log2_64(MulAmt1), EVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00008345 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008346 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008347 DAG.getConstant(MulAmt1, VT));
8348
8349 if (isPowerOf2_64(MulAmt2))
8350 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Andersone50ed302009-08-10 22:56:29 +00008351 DAG.getConstant(Log2_64(MulAmt2), EVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00008352 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008353 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008354 DAG.getConstant(MulAmt2, VT));
8355
8356 // Do not add new nodes to DAG combiner worklist.
8357 DCI.CombineTo(N, NewMul, false);
8358 }
8359 return SDValue();
8360}
8361
8362
Nate Begeman740ab032009-01-26 00:52:55 +00008363/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8364/// when possible.
8365static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8366 const X86Subtarget *Subtarget) {
8367 // On X86 with SSE2 support, we can transform this to a vector shift if
8368 // all elements are shifted by the same amount. We can't do this in legalize
8369 // because the a constant vector is typically transformed to a constant pool
8370 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008371 if (!Subtarget->hasSSE2())
8372 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008373
Owen Andersone50ed302009-08-10 22:56:29 +00008374 EVT VT = N->getValueType(0);
8375 if (VT != EVT::v2i64 && VT != EVT::v4i32 && VT != EVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008376 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008377
Mon P Wang3becd092009-01-28 08:12:05 +00008378 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00008379 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008380 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008381 SDValue BaseShAmt;
8382 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8383 unsigned NumElts = VT.getVectorNumElements();
8384 unsigned i = 0;
8385 for (; i != NumElts; ++i) {
8386 SDValue Arg = ShAmtOp.getOperand(i);
8387 if (Arg.getOpcode() == ISD::UNDEF) continue;
8388 BaseShAmt = Arg;
8389 break;
8390 }
8391 for (; i != NumElts; ++i) {
8392 SDValue Arg = ShAmtOp.getOperand(i);
8393 if (Arg.getOpcode() == ISD::UNDEF) continue;
8394 if (Arg != BaseShAmt) {
8395 return SDValue();
8396 }
8397 }
8398 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008399 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8400 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8401 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008402 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008403 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008404
Owen Andersone50ed302009-08-10 22:56:29 +00008405 if (EltVT.bitsGT(EVT::i32))
8406 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, EVT::i32, BaseShAmt);
8407 else if (EltVT.bitsLT(EVT::i32))
8408 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, EVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008409
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008410 // The shift amount is identical so we can do a vector shift.
8411 SDValue ValOp = N->getOperand(0);
8412 switch (N->getOpcode()) {
8413 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008414 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008415 break;
8416 case ISD::SHL:
Owen Andersone50ed302009-08-10 22:56:29 +00008417 if (VT == EVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008418 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008419 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008420 ValOp, BaseShAmt);
Owen Andersone50ed302009-08-10 22:56:29 +00008421 if (VT == EVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008422 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008423 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008424 ValOp, BaseShAmt);
Owen Andersone50ed302009-08-10 22:56:29 +00008425 if (VT == EVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008426 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008427 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008428 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008429 break;
8430 case ISD::SRA:
Owen Andersone50ed302009-08-10 22:56:29 +00008431 if (VT == EVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008432 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008433 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008434 ValOp, BaseShAmt);
Owen Andersone50ed302009-08-10 22:56:29 +00008435 if (VT == EVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008436 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008437 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008438 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008439 break;
8440 case ISD::SRL:
Owen Andersone50ed302009-08-10 22:56:29 +00008441 if (VT == EVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008442 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008443 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008444 ValOp, BaseShAmt);
Owen Andersone50ed302009-08-10 22:56:29 +00008445 if (VT == EVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008446 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008447 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008448 ValOp, BaseShAmt);
Owen Andersone50ed302009-08-10 22:56:29 +00008449 if (VT == EVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008450 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Andersone50ed302009-08-10 22:56:29 +00008451 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, EVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00008452 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008453 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008454 }
8455 return SDValue();
8456}
8457
Chris Lattner149a4e52008-02-22 02:09:43 +00008458/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008459static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008460 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008461 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8462 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008463 // A preferable solution to the general problem is to figure out the right
8464 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008465
8466 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008467 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008468 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00008469 if (VT.getSizeInBits() != 64)
8470 return SDValue();
8471
Devang Patel578efa92009-06-05 21:57:13 +00008472 const Function *F = DAG.getMachineFunction().getFunction();
8473 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8474 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8475 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008476 if ((VT.isVector() ||
Owen Andersone50ed302009-08-10 22:56:29 +00008477 (VT == EVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008478 isa<LoadSDNode>(St->getValue()) &&
8479 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8480 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008481 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008482 LoadSDNode *Ld = 0;
8483 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008484 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008485 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008486 // Must be a store of a load. We currently handle two cases: the load
8487 // is a direct child, and it's under an intervening TokenFactor. It is
8488 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008489 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008490 Ld = cast<LoadSDNode>(St->getChain());
8491 else if (St->getValue().hasOneUse() &&
8492 ChainVal->getOpcode() == ISD::TokenFactor) {
8493 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008494 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008495 TokenFactorIndex = i;
8496 Ld = cast<LoadSDNode>(St->getValue());
8497 } else
8498 Ops.push_back(ChainVal->getOperand(i));
8499 }
8500 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008501
Evan Cheng536e6672009-03-12 05:59:15 +00008502 if (!Ld || !ISD::isNormalLoad(Ld))
8503 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008504
Evan Cheng536e6672009-03-12 05:59:15 +00008505 // If this is not the MMX case, i.e. we are just turning i64 load/store
8506 // into f64 load/store, avoid the transformation if there are multiple
8507 // uses of the loaded value.
8508 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8509 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008510
Evan Cheng536e6672009-03-12 05:59:15 +00008511 DebugLoc LdDL = Ld->getDebugLoc();
8512 DebugLoc StDL = N->getDebugLoc();
8513 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8514 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8515 // pair instead.
8516 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Andersone50ed302009-08-10 22:56:29 +00008517 EVT LdVT = Subtarget->is64Bit() ? EVT::i64 : EVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00008518 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8519 Ld->getBasePtr(), Ld->getSrcValue(),
8520 Ld->getSrcValueOffset(), Ld->isVolatile(),
8521 Ld->getAlignment());
8522 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008523 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008524 Ops.push_back(NewChain);
Owen Andersone50ed302009-08-10 22:56:29 +00008525 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, EVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008526 Ops.size());
8527 }
Evan Cheng536e6672009-03-12 05:59:15 +00008528 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008529 St->getSrcValue(), St->getSrcValueOffset(),
8530 St->isVolatile(), St->getAlignment());
8531 }
Evan Cheng536e6672009-03-12 05:59:15 +00008532
8533 // Otherwise, lower to two pairs of 32-bit loads / stores.
8534 SDValue LoAddr = Ld->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00008535 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, EVT::i32, LoAddr,
8536 DAG.getConstant(4, EVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00008537
Owen Andersone50ed302009-08-10 22:56:29 +00008538 SDValue LoLd = DAG.getLoad(EVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00008539 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8540 Ld->isVolatile(), Ld->getAlignment());
Owen Andersone50ed302009-08-10 22:56:29 +00008541 SDValue HiLd = DAG.getLoad(EVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00008542 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8543 Ld->isVolatile(),
8544 MinAlign(Ld->getAlignment(), 4));
8545
8546 SDValue NewChain = LoLd.getValue(1);
8547 if (TokenFactorIndex != -1) {
8548 Ops.push_back(LoLd);
8549 Ops.push_back(HiLd);
Owen Andersone50ed302009-08-10 22:56:29 +00008550 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, EVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00008551 Ops.size());
8552 }
8553
8554 LoAddr = St->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00008555 HiAddr = DAG.getNode(ISD::ADD, StDL, EVT::i32, LoAddr,
8556 DAG.getConstant(4, EVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00008557
8558 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8559 St->getSrcValue(), St->getSrcValueOffset(),
8560 St->isVolatile(), St->getAlignment());
8561 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8562 St->getSrcValue(),
8563 St->getSrcValueOffset() + 4,
8564 St->isVolatile(),
8565 MinAlign(St->getAlignment(), 4));
Owen Andersone50ed302009-08-10 22:56:29 +00008566 return DAG.getNode(ISD::TokenFactor, StDL, EVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008567 }
Dan Gohman475871a2008-07-27 21:46:04 +00008568 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008569}
8570
Chris Lattner6cf73262008-01-25 06:14:17 +00008571/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8572/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008573static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008574 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8575 // F[X]OR(0.0, x) -> x
8576 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008577 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8578 if (C->getValueAPF().isPosZero())
8579 return N->getOperand(1);
8580 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8581 if (C->getValueAPF().isPosZero())
8582 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008583 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008584}
8585
8586/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008587static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008588 // FAND(0.0, x) -> 0.0
8589 // FAND(x, 0.0) -> 0.0
8590 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8591 if (C->getValueAPF().isPosZero())
8592 return N->getOperand(0);
8593 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8594 if (C->getValueAPF().isPosZero())
8595 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008596 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008597}
8598
Dan Gohmane5af2d32009-01-29 01:59:02 +00008599static SDValue PerformBTCombine(SDNode *N,
8600 SelectionDAG &DAG,
8601 TargetLowering::DAGCombinerInfo &DCI) {
8602 // BT ignores high bits in the bit index operand.
8603 SDValue Op1 = N->getOperand(1);
8604 if (Op1.hasOneUse()) {
8605 unsigned BitWidth = Op1.getValueSizeInBits();
8606 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8607 APInt KnownZero, KnownOne;
8608 TargetLowering::TargetLoweringOpt TLO(DAG);
8609 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8610 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8611 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8612 DCI.CommitTargetLoweringOpt(TLO);
8613 }
8614 return SDValue();
8615}
Chris Lattner83e6c992006-10-04 06:57:07 +00008616
Eli Friedman7a5e5552009-06-07 06:52:44 +00008617static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
8618 SDValue Op = N->getOperand(0);
8619 if (Op.getOpcode() == ISD::BIT_CONVERT)
8620 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008621 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008622 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
8623 VT.getVectorElementType().getSizeInBits() ==
8624 OpVT.getVectorElementType().getSizeInBits()) {
8625 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
8626 }
8627 return SDValue();
8628}
8629
Owen Anderson99177002009-06-29 18:04:45 +00008630// On X86 and X86-64, atomic operations are lowered to locked instructions.
8631// Locked instructions, in turn, have implicit fence semantics (all memory
8632// operations are flushed before issuing the locked instruction, and the
8633// are not buffered), so we can fold away the common pattern of
8634// fence-atomic-fence.
8635static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
8636 SDValue atomic = N->getOperand(0);
8637 switch (atomic.getOpcode()) {
8638 case ISD::ATOMIC_CMP_SWAP:
8639 case ISD::ATOMIC_SWAP:
8640 case ISD::ATOMIC_LOAD_ADD:
8641 case ISD::ATOMIC_LOAD_SUB:
8642 case ISD::ATOMIC_LOAD_AND:
8643 case ISD::ATOMIC_LOAD_OR:
8644 case ISD::ATOMIC_LOAD_XOR:
8645 case ISD::ATOMIC_LOAD_NAND:
8646 case ISD::ATOMIC_LOAD_MIN:
8647 case ISD::ATOMIC_LOAD_MAX:
8648 case ISD::ATOMIC_LOAD_UMIN:
8649 case ISD::ATOMIC_LOAD_UMAX:
8650 break;
8651 default:
8652 return SDValue();
8653 }
8654
8655 SDValue fence = atomic.getOperand(0);
8656 if (fence.getOpcode() != ISD::MEMBARRIER)
8657 return SDValue();
8658
8659 switch (atomic.getOpcode()) {
8660 case ISD::ATOMIC_CMP_SWAP:
8661 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8662 atomic.getOperand(1), atomic.getOperand(2),
8663 atomic.getOperand(3));
8664 case ISD::ATOMIC_SWAP:
8665 case ISD::ATOMIC_LOAD_ADD:
8666 case ISD::ATOMIC_LOAD_SUB:
8667 case ISD::ATOMIC_LOAD_AND:
8668 case ISD::ATOMIC_LOAD_OR:
8669 case ISD::ATOMIC_LOAD_XOR:
8670 case ISD::ATOMIC_LOAD_NAND:
8671 case ISD::ATOMIC_LOAD_MIN:
8672 case ISD::ATOMIC_LOAD_MAX:
8673 case ISD::ATOMIC_LOAD_UMIN:
8674 case ISD::ATOMIC_LOAD_UMAX:
8675 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8676 atomic.getOperand(1), atomic.getOperand(2));
8677 default:
8678 return SDValue();
8679 }
8680}
8681
Dan Gohman475871a2008-07-27 21:46:04 +00008682SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008683 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008684 SelectionDAG &DAG = DCI.DAG;
8685 switch (N->getOpcode()) {
8686 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008687 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008688 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008689 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008690 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008691 case ISD::SHL:
8692 case ISD::SRA:
8693 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008694 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008695 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008696 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8697 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008698 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00008699 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00008700 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008701 }
8702
Dan Gohman475871a2008-07-27 21:46:04 +00008703 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008704}
8705
Evan Cheng60c07e12006-07-05 22:17:51 +00008706//===----------------------------------------------------------------------===//
8707// X86 Inline Assembly Support
8708//===----------------------------------------------------------------------===//
8709
Chris Lattnerb8105652009-07-20 17:51:36 +00008710static bool LowerToBSwap(CallInst *CI) {
8711 // FIXME: this should verify that we are targetting a 486 or better. If not,
8712 // we will turn this bswap into something that will be lowered to logical ops
8713 // instead of emitting the bswap asm. For now, we don't support 486 or lower
8714 // so don't worry about this.
8715
8716 // Verify this is a simple bswap.
8717 if (CI->getNumOperands() != 2 ||
8718 CI->getType() != CI->getOperand(1)->getType() ||
8719 !CI->getType()->isInteger())
8720 return false;
8721
8722 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
8723 if (!Ty || Ty->getBitWidth() % 16 != 0)
8724 return false;
8725
8726 // Okay, we can do this xform, do so now.
8727 const Type *Tys[] = { Ty };
8728 Module *M = CI->getParent()->getParent()->getParent();
8729 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
8730
8731 Value *Op = CI->getOperand(1);
8732 Op = CallInst::Create(Int, Op, CI->getName(), CI);
8733
8734 CI->replaceAllUsesWith(Op);
8735 CI->eraseFromParent();
8736 return true;
8737}
8738
8739bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
8740 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8741 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
8742
8743 std::string AsmStr = IA->getAsmString();
8744
8745 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
8746 std::vector<std::string> AsmPieces;
8747 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
8748
8749 switch (AsmPieces.size()) {
8750 default: return false;
8751 case 1:
8752 AsmStr = AsmPieces[0];
8753 AsmPieces.clear();
8754 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
8755
8756 // bswap $0
8757 if (AsmPieces.size() == 2 &&
8758 (AsmPieces[0] == "bswap" ||
8759 AsmPieces[0] == "bswapq" ||
8760 AsmPieces[0] == "bswapl") &&
8761 (AsmPieces[1] == "$0" ||
8762 AsmPieces[1] == "${0:q}")) {
8763 // No need to check constraints, nothing other than the equivalent of
8764 // "=r,0" would be valid here.
8765 return LowerToBSwap(CI);
8766 }
8767 // rorw $$8, ${0:w} --> llvm.bswap.i16
8768 if (CI->getType() == Type::Int16Ty &&
8769 AsmPieces.size() == 3 &&
8770 AsmPieces[0] == "rorw" &&
8771 AsmPieces[1] == "$$8," &&
8772 AsmPieces[2] == "${0:w}" &&
8773 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
8774 return LowerToBSwap(CI);
8775 }
8776 break;
8777 case 3:
8778 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
8779 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
8780 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
8781 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
8782 std::vector<std::string> Words;
8783 SplitString(AsmPieces[0], Words, " \t");
8784 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
8785 Words.clear();
8786 SplitString(AsmPieces[1], Words, " \t");
8787 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
8788 Words.clear();
8789 SplitString(AsmPieces[2], Words, " \t,");
8790 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
8791 Words[2] == "%edx") {
8792 return LowerToBSwap(CI);
8793 }
8794 }
8795 }
8796 }
8797 break;
8798 }
8799 return false;
8800}
8801
8802
8803
Chris Lattnerf4dff842006-07-11 02:54:03 +00008804/// getConstraintType - Given a constraint letter, return the type of
8805/// constraint it is for this target.
8806X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008807X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8808 if (Constraint.size() == 1) {
8809 switch (Constraint[0]) {
8810 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008811 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008812 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008813 case 'r':
8814 case 'R':
8815 case 'l':
8816 case 'q':
8817 case 'Q':
8818 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008819 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008820 case 'Y':
8821 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008822 case 'e':
8823 case 'Z':
8824 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008825 default:
8826 break;
8827 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008828 }
Chris Lattner4234f572007-03-25 02:14:49 +00008829 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008830}
8831
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008832/// LowerXConstraint - try to replace an X constraint, which matches anything,
8833/// with another that has more specific requirements based on the type of the
8834/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008835const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00008836LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008837 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8838 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008839 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008840 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008841 return "Y";
8842 if (Subtarget->hasSSE1())
8843 return "x";
8844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008845
Chris Lattner5e764232008-04-26 23:02:14 +00008846 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008847}
8848
Chris Lattner48884cd2007-08-25 00:47:38 +00008849/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8850/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008851void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008852 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008853 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008854 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008855 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008856 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008857
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008858 switch (Constraint) {
8859 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008860 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008862 if (C->getZExtValue() <= 31) {
8863 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008864 break;
8865 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008866 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008867 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008868 case 'J':
8869 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008870 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00008871 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8872 break;
8873 }
8874 }
8875 return;
8876 case 'K':
8877 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008878 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00008879 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8880 break;
8881 }
8882 }
8883 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008884 case 'N':
8885 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008886 if (C->getZExtValue() <= 255) {
8887 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008888 break;
8889 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008890 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008891 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008892 case 'e': {
8893 // 32-bit signed value
8894 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8895 const ConstantInt *CI = C->getConstantIntValue();
8896 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8897 // Widen to 64 bits here to get it sign extended.
Owen Andersone50ed302009-08-10 22:56:29 +00008898 Result = DAG.getTargetConstant(C->getSExtValue(), EVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00008899 break;
8900 }
8901 // FIXME gcc accepts some relocatable values here too, but only in certain
8902 // memory models; it's complicated.
8903 }
8904 return;
8905 }
8906 case 'Z': {
8907 // 32-bit unsigned value
8908 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8909 const ConstantInt *CI = C->getConstantIntValue();
8910 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8911 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8912 break;
8913 }
8914 }
8915 // FIXME gcc accepts some relocatable values here too, but only in certain
8916 // memory models; it's complicated.
8917 return;
8918 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008919 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008920 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008921 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008922 // Widen to 64 bits here to get it sign extended.
Owen Andersone50ed302009-08-10 22:56:29 +00008923 Result = DAG.getTargetConstant(CST->getSExtValue(), EVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008924 break;
8925 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008926
Chris Lattnerdc43a882007-05-03 16:52:29 +00008927 // If we are in non-pic codegen mode, we allow the address of a global (with
8928 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008929 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008930 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008931
Chris Lattner49921962009-05-08 18:23:14 +00008932 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8933 while (1) {
8934 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8935 Offset += GA->getOffset();
8936 break;
8937 } else if (Op.getOpcode() == ISD::ADD) {
8938 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8939 Offset += C->getZExtValue();
8940 Op = Op.getOperand(0);
8941 continue;
8942 }
8943 } else if (Op.getOpcode() == ISD::SUB) {
8944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8945 Offset += -C->getZExtValue();
8946 Op = Op.getOperand(0);
8947 continue;
8948 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008949 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008950
Chris Lattner49921962009-05-08 18:23:14 +00008951 // Otherwise, this isn't something we can handle, reject it.
8952 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008953 }
Chris Lattner3b6b36d2009-07-10 06:29:59 +00008954
Chris Lattner36c25012009-07-10 07:34:39 +00008955 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008956 // If we require an extra load to get this address, as in PIC mode, we
8957 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00008958 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
8959 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008960 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00008961
Dale Johannesen60b3ba02009-07-21 00:12:29 +00008962 if (hasMemory)
8963 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
8964 else
8965 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00008966 Result = Op;
8967 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008968 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008969 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008970
Gabor Greifba36cb52008-08-28 21:40:38 +00008971 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008972 Ops.push_back(Result);
8973 return;
8974 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008975 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8976 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008977}
8978
Chris Lattner259e97c2006-01-31 19:43:35 +00008979std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008980getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00008981 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008982 if (Constraint.size() == 1) {
8983 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008984 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008985 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00008986 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
8987 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00008988 if (VT == EVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00008989 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
8990 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
8991 X86::R10D,X86::R11D,X86::R12D,
8992 X86::R13D,X86::R14D,X86::R15D,
8993 X86::EBP, X86::ESP, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00008994 else if (VT == EVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00008995 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
8996 X86::SI, X86::DI, X86::R8W,X86::R9W,
8997 X86::R10W,X86::R11W,X86::R12W,
8998 X86::R13W,X86::R14W,X86::R15W,
8999 X86::BP, X86::SP, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00009000 else if (VT == EVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009001 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9002 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9003 X86::R10B,X86::R11B,X86::R12B,
9004 X86::R13B,X86::R14B,X86::R15B,
9005 X86::BPL, X86::SPL, 0);
9006
Owen Andersone50ed302009-08-10 22:56:29 +00009007 else if (VT == EVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009008 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9009 X86::RSI, X86::RDI, X86::R8, X86::R9,
9010 X86::R10, X86::R11, X86::R12,
9011 X86::R13, X86::R14, X86::R15,
9012 X86::RBP, X86::RSP, 0);
9013
9014 break;
9015 }
9016 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009017 case 'Q': // Q_REGS
Owen Andersone50ed302009-08-10 22:56:29 +00009018 if (VT == EVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009019 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00009020 else if (VT == EVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009021 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00009022 else if (VT == EVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009023 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00009024 else if (VT == EVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009025 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9026 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009027 }
9028 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009029
Chris Lattner1efa40f2006-02-22 00:56:39 +00009030 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009031}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009032
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009033std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009034X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009035 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009036 // First, see if this is a constraint that directly corresponds to an LLVM
9037 // register class.
9038 if (Constraint.size() == 1) {
9039 // GCC Constraint Letters
9040 switch (Constraint[0]) {
9041 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009042 case 'r': // GENERAL_REGS
9043 case 'R': // LEGACY_REGS
9044 case 'l': // INDEX_REGS
Owen Andersone50ed302009-08-10 22:56:29 +00009045 if (VT == EVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009046 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00009047 if (VT == EVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009048 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00009049 if (VT == EVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009050 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009051 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009052 case 'f': // FP Stack registers.
9053 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9054 // value to the correct fpstack register class.
Owen Andersone50ed302009-08-10 22:56:29 +00009055 if (VT == EVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009056 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00009057 if (VT == EVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009058 return std::make_pair(0U, X86::RFP64RegisterClass);
9059 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009060 case 'y': // MMX_REGS if MMX allowed.
9061 if (!Subtarget->hasMMX()) break;
9062 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009063 case 'Y': // SSE_REGS if SSE2 allowed
9064 if (!Subtarget->hasSSE2()) break;
9065 // FALL THROUGH.
9066 case 'x': // SSE_REGS if SSE1 allowed
9067 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009068
9069 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009070 default: break;
9071 // Scalar SSE types.
Owen Andersone50ed302009-08-10 22:56:29 +00009072 case EVT::f32:
9073 case EVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009074 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00009075 case EVT::f64:
9076 case EVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009077 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009078 // Vector types.
Owen Andersone50ed302009-08-10 22:56:29 +00009079 case EVT::v16i8:
9080 case EVT::v8i16:
9081 case EVT::v4i32:
9082 case EVT::v2i64:
9083 case EVT::v4f32:
9084 case EVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009085 return std::make_pair(0U, X86::VR128RegisterClass);
9086 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009087 break;
9088 }
9089 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009090
Chris Lattnerf76d1802006-07-31 23:26:50 +00009091 // Use the default implementation in TargetLowering to convert the register
9092 // constraint into a member of a register class.
9093 std::pair<unsigned, const TargetRegisterClass*> Res;
9094 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009095
9096 // Not found as a standard register?
9097 if (Res.second == 0) {
9098 // GCC calls "st(0)" just plain "st".
9099 if (StringsEqualNoCase("{st}", Constraint)) {
9100 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00009101 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00009102 }
Dale Johannesen330169f2008-11-13 21:52:36 +00009103 // 'A' means EAX + EDX.
9104 if (Constraint == "A") {
9105 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +00009106 Res.second = X86::GR32_ADRegisterClass;
Dale Johannesen330169f2008-11-13 21:52:36 +00009107 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00009108 return Res;
9109 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009110
Chris Lattnerf76d1802006-07-31 23:26:50 +00009111 // Otherwise, check to see if this is a register class of the wrong value
9112 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
9113 // turn into {ax},{dx}.
9114 if (Res.second->hasType(VT))
9115 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009116
Chris Lattnerf76d1802006-07-31 23:26:50 +00009117 // All of the single-register GCC register classes map their values onto
9118 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
9119 // really want an 8-bit or 32-bit register, map to the appropriate register
9120 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00009121 if (Res.second == X86::GR16RegisterClass) {
Owen Andersone50ed302009-08-10 22:56:29 +00009122 if (VT == EVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009123 unsigned DestReg = 0;
9124 switch (Res.first) {
9125 default: break;
9126 case X86::AX: DestReg = X86::AL; break;
9127 case X86::DX: DestReg = X86::DL; break;
9128 case X86::CX: DestReg = X86::CL; break;
9129 case X86::BX: DestReg = X86::BL; break;
9130 }
9131 if (DestReg) {
9132 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009133 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009134 }
Owen Andersone50ed302009-08-10 22:56:29 +00009135 } else if (VT == EVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009136 unsigned DestReg = 0;
9137 switch (Res.first) {
9138 default: break;
9139 case X86::AX: DestReg = X86::EAX; break;
9140 case X86::DX: DestReg = X86::EDX; break;
9141 case X86::CX: DestReg = X86::ECX; break;
9142 case X86::BX: DestReg = X86::EBX; break;
9143 case X86::SI: DestReg = X86::ESI; break;
9144 case X86::DI: DestReg = X86::EDI; break;
9145 case X86::BP: DestReg = X86::EBP; break;
9146 case X86::SP: DestReg = X86::ESP; break;
9147 }
9148 if (DestReg) {
9149 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009150 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009151 }
Owen Andersone50ed302009-08-10 22:56:29 +00009152 } else if (VT == EVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009153 unsigned DestReg = 0;
9154 switch (Res.first) {
9155 default: break;
9156 case X86::AX: DestReg = X86::RAX; break;
9157 case X86::DX: DestReg = X86::RDX; break;
9158 case X86::CX: DestReg = X86::RCX; break;
9159 case X86::BX: DestReg = X86::RBX; break;
9160 case X86::SI: DestReg = X86::RSI; break;
9161 case X86::DI: DestReg = X86::RDI; break;
9162 case X86::BP: DestReg = X86::RBP; break;
9163 case X86::SP: DestReg = X86::RSP; break;
9164 }
9165 if (DestReg) {
9166 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009167 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009168 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00009169 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00009170 } else if (Res.second == X86::FR32RegisterClass ||
9171 Res.second == X86::FR64RegisterClass ||
9172 Res.second == X86::VR128RegisterClass) {
9173 // Handle references to XMM physical registers that got mapped into the
9174 // wrong class. This can happen with constraints like {xmm0} where the
9175 // target independent register mapper will just pick the first match it can
9176 // find, ignoring the required type.
Owen Andersone50ed302009-08-10 22:56:29 +00009177 if (VT == EVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +00009178 Res.second = X86::FR32RegisterClass;
Owen Andersone50ed302009-08-10 22:56:29 +00009179 else if (VT == EVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +00009180 Res.second = X86::FR64RegisterClass;
9181 else if (X86::VR128RegisterClass->hasType(VT))
9182 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00009183 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009184
Chris Lattnerf76d1802006-07-31 23:26:50 +00009185 return Res;
9186}
Mon P Wang0c397192008-10-30 08:01:45 +00009187
9188//===----------------------------------------------------------------------===//
9189// X86 Widen vector type
9190//===----------------------------------------------------------------------===//
9191
9192/// getWidenVectorType: given a vector type, returns the type to widen
9193/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Andersone50ed302009-08-10 22:56:29 +00009194/// If there is no vector type that we want to widen to, returns EVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00009195/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00009196/// scalarizing vs using the wider vector type.
9197
Owen Andersone50ed302009-08-10 22:56:29 +00009198EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00009199 assert(VT.isVector());
9200 if (isTypeLegal(VT))
9201 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009202
Mon P Wang0c397192008-10-30 08:01:45 +00009203 // TODO: In computeRegisterProperty, we can compute the list of legal vector
9204 // type based on element type. This would speed up our search (though
9205 // it may not be worth it since the size of the list is relatively
9206 // small).
Owen Andersone50ed302009-08-10 22:56:29 +00009207 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +00009208 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00009209
Mon P Wang0c397192008-10-30 08:01:45 +00009210 // On X86, it make sense to widen any vector wider than 1
9211 if (NElts <= 1)
Owen Andersone50ed302009-08-10 22:56:29 +00009212 return EVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00009213
Owen Andersone50ed302009-08-10 22:56:29 +00009214 for (unsigned nVT = EVT::FIRST_VECTOR_VALUETYPE;
9215 nVT <= EVT::LAST_VECTOR_VALUETYPE; ++nVT) {
9216 EVT SVT = (EVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009217
9218 if (isTypeLegal(SVT) &&
9219 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00009220 SVT.getVectorNumElements() > NElts)
9221 return SVT;
9222 }
Owen Andersone50ed302009-08-10 22:56:29 +00009223 return EVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +00009224}