blob: 298a5a837831566bb7f8bdf9571e3bf24cd71b95 [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.
Nate Begeman9008ca62009-04-27 18:41:29 +000051static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
52 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.
82 setShiftAmountType(MVT::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.
Evan Cheng069287d2006-05-16 07:21:53 +0000101 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
102 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
103 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000104 if (Subtarget->is64Bit())
105 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000106
Evan Cheng03294662008-10-14 21:26:46 +0000107 setLoadExtAction(ISD::SEXTLOAD, MVT::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.
Chris Lattnerddf89562008-01-17 19:59:44 +0000110 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
111 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
112 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
113 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
114 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000115 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
116
117 // SETOEQ and SETUNE require checking two conditions.
118 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
119 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
120 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
121 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
122 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
123 setCondCodeAction(ISD::SETUNE, MVT::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.
127 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
128 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
129 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000130
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000132 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000133 setOperationAction(ISD::UINT_TO_FP , MVT::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.
137 setOperationAction(ISD::UINT_TO_FP , MVT::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.
141 setOperationAction(ISD::UINT_TO_FP , MVT::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.
146 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
147 setOperationAction(ISD::SINT_TO_FP , MVT::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) {
152 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
153 // f32 and f64 cases are Legal, f80 case is not
154 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
155 } else {
156 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
157 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
158 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000159 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000160 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
161 setOperationAction(ISD::SINT_TO_FP , MVT::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.
166 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
167 setOperationAction(ISD::SINT_TO_FP , MVT::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.
171 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
172 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
173
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000174 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000175 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000176 // f32 and f64 cases are Legal, f80 case is not
177 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000178 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000180 setOperationAction(ISD::FP_TO_SINT , MVT::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.
185 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
186 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
187 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
188
Evan Cheng25ab6902006-09-08 06:48:29 +0000189 if (Subtarget->is64Bit()) {
190 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000191 setOperationAction(ISD::FP_TO_UINT , MVT::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.
197 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
198 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.
201 setOperationAction(ISD::FP_TO_UINT , MVT::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) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000206 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
207 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
208 }
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.
Dan Gohman525178c2007-10-08 18:33:35 +0000220 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
221 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
222 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
223 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
224 setOperationAction(ISD::SREM , MVT::i8 , Expand);
225 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000226 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
227 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
228 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
229 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
230 setOperationAction(ISD::SREM , MVT::i16 , Expand);
231 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000232 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
233 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
234 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
235 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
236 setOperationAction(ISD::SREM , MVT::i32 , Expand);
237 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000238 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
239 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
240 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
241 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
242 setOperationAction(ISD::SREM , MVT::i64 , Expand);
243 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000244
Evan Chengc35497f2006-10-30 08:02:39 +0000245 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000246 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000247 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
248 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000250 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
251 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
252 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000253 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
254 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000255 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000256 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000257 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000258 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000259
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000261 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
262 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000263 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000264 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
265 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000266 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000267 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
268 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000271 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
272 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 }
274
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000275 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000276 setOperationAction(ISD::BSWAP , MVT::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.
279 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
280 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000281 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000282 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
283 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
284 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
285 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000286 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
288 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
289 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
290 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
291 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000292 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 if (Subtarget->is64Bit()) {
294 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
295 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
296 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000297 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000298 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000299 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000300
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000301 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000302 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000303 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000304 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000305 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000306 if (Subtarget->is64Bit())
307 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000308 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 if (Subtarget->is64Bit()) {
310 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
311 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
312 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000313 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000315 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000316 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
317 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
318 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000319 if (Subtarget->is64Bit()) {
320 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
321 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
322 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
323 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000324
Evan Chengd2cde682008-03-10 19:38:10 +0000325 if (Subtarget->hasSSE1())
326 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000327
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000328 if (!Subtarget->hasSSE2())
329 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
330
Mon P Wang63307c32008-05-05 19:05:59 +0000331 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000332 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
333 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
334 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
335 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000336
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000337 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
338 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
339 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
340 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000341
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000342 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000343 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
344 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
345 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
346 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
347 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
348 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
349 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000350 }
351
Dan Gohman7f460202008-06-30 20:59:49 +0000352 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
353 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000354 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000355 if (!Subtarget->isTargetDarwin() &&
356 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000357 !Subtarget->isTargetCygMing()) {
358 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
359 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
360 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000361
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000362 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
363 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
364 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
365 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
366 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000367 setExceptionPointerRegister(X86::RAX);
368 setExceptionSelectorRegister(X86::RDX);
369 } else {
370 setExceptionPointerRegister(X86::EAX);
371 setExceptionSelectorRegister(X86::EDX);
372 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000373 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000374 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
375
Duncan Sandsf7331b32007-09-11 14:10:23 +0000376 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000377
Chris Lattnerda68d302008-01-15 21:58:22 +0000378 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000379
Nate Begemanacc398c2006-01-25 18:21:52 +0000380 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
381 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000382 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000383 if (Subtarget->is64Bit()) {
384 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000385 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000386 } else {
387 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000388 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000389 }
Evan Chengae642192007-03-02 23:16:35 +0000390
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000391 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000392 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000393 if (Subtarget->is64Bit())
394 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000395 if (Subtarget->isTargetCygMing())
396 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
397 else
398 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000399
Evan Chengc7ce29b2009-02-13 22:36:38 +0000400 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000401 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000402 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000403 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
404 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405
Evan Cheng223547a2006-01-31 22:28:30 +0000406 // Use ANDPD to simulate FABS.
407 setOperationAction(ISD::FABS , MVT::f64, Custom);
408 setOperationAction(ISD::FABS , MVT::f32, Custom);
409
410 // Use XORP to simulate FNEG.
411 setOperationAction(ISD::FNEG , MVT::f64, Custom);
412 setOperationAction(ISD::FNEG , MVT::f32, Custom);
413
Evan Cheng68c47cb2007-01-05 07:55:56 +0000414 // Use ANDPD and ORPD to simulate FCOPYSIGN.
415 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
416 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
417
Evan Chengd25e9e82006-02-02 00:28:23 +0000418 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419 setOperationAction(ISD::FSIN , MVT::f64, Expand);
420 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000421 setOperationAction(ISD::FSIN , MVT::f32, Expand);
422 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000423
Chris Lattnera54aa942006-01-29 06:26:08 +0000424 // Expand FP immediates into loads from the stack, except for the special
425 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000426 addLegalFPImmediate(APFloat(+0.0)); // xorpd
427 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000428 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429 // Use SSE for f32, x87 for f64.
430 // Set up the FP register classes.
431 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
432 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
433
434 // Use ANDPS to simulate FABS.
435 setOperationAction(ISD::FABS , MVT::f32, Custom);
436
437 // Use XORP to simulate FNEG.
438 setOperationAction(ISD::FNEG , MVT::f32, Custom);
439
440 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
441
442 // Use ANDPS and ORPS to simulate FCOPYSIGN.
443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
445
446 // We don't support sin/cos/fmod
447 setOperationAction(ISD::FSIN , MVT::f32, Expand);
448 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449
Nate Begemane1795842008-02-14 08:57:00 +0000450 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000451 addLegalFPImmediate(APFloat(+0.0f)); // xorps
452 addLegalFPImmediate(APFloat(+0.0)); // FLD0
453 addLegalFPImmediate(APFloat(+1.0)); // FLD1
454 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
455 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
456
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457 if (!UnsafeFPMath) {
458 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
459 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
460 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000461 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000463 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
465 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000466
Evan Cheng68c47cb2007-01-05 07:55:56 +0000467 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000468 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000469 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000471
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000472 if (!UnsafeFPMath) {
473 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
474 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
475 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000476 addLegalFPImmediate(APFloat(+0.0)); // FLD0
477 addLegalFPImmediate(APFloat(+1.0)); // FLD1
478 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
479 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000485
Dale Johannesen59a58732007-08-05 18:49:15 +0000486 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000487 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000488 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
489 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
490 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
491 {
492 bool ignored;
493 APFloat TmpFlt(+0.0);
494 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
495 &ignored);
496 addLegalFPImmediate(TmpFlt); // FLD0
497 TmpFlt.changeSign();
498 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
499 APFloat TmpFlt2(+1.0);
500 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
501 &ignored);
502 addLegalFPImmediate(TmpFlt2); // FLD1
503 TmpFlt2.changeSign();
504 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
505 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000506
Evan Chengc7ce29b2009-02-13 22:36:38 +0000507 if (!UnsafeFPMath) {
508 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
509 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
510 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000511 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000512
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000513 // Always use a library call for pow.
514 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
515 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
516 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
517
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000518 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000519 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000520 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000521 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000522 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
523
Mon P Wangf007a8b2008-11-06 05:31:54 +0000524 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000525 // (for widening) or expand (for scalarization). Then we will selectively
526 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000527 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
528 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000529 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000542 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000544 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000545 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000546 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000568 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Eli Friedman23ef1052009-06-06 03:57:58 +0000573 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000577 }
578
Evan Chengc7ce29b2009-02-13 22:36:38 +0000579 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
580 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000581 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000582 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
583 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
584 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000585 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000586 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000587
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000588 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
589 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
590 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000591 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000592
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000593 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
594 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
595 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000596 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000597
Bill Wendling74027e92007-03-15 21:24:36 +0000598 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
599 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
600
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000601 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000602 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000603 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000604 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
605 setOperationAction(ISD::AND, MVT::v2i32, Promote);
606 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
607 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000608
609 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000610 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000611 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000612 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
613 setOperationAction(ISD::OR, MVT::v2i32, Promote);
614 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
615 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000616
617 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000618 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000619 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000620 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
621 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
622 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
623 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000624
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000625 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000626 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000627 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000628 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
629 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
630 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000631 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
632 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000633 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000634
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000635 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
636 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000638 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000639 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000640
641 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000645
Evan Cheng52672b82008-07-22 18:39:19 +0000646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000650
651 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000652
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000653 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000654 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
655 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
656 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
657 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
658 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Eli Friedman3dae2842009-07-22 01:06:52 +0000659 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
660 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
661 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000662 }
663
Evan Cheng92722532009-03-26 23:06:32 +0000664 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000665 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
666
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000667 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
668 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
669 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
670 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000671 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
672 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000673 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
674 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000676 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000677 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000678 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000679 }
680
Evan Cheng92722532009-03-26 23:06:32 +0000681 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000682 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000683
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000684 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
685 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000686 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
687 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
688 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
689 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
690
Evan Chengf7c378e2006-04-10 07:23:14 +0000691 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
692 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
693 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000694 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000695 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000696 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
697 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
698 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000699 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000700 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000701 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
702 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
703 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
704 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000705 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
706 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000707
Nate Begeman30a0de92008-07-17 16:51:19 +0000708 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
709 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
710 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
711 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000712
Evan Chengf7c378e2006-04-10 07:23:14 +0000713 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
714 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000718
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
721 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000723 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000724 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000725 // Do not attempt to custom lower non-128-bit vectors
726 if (!VT.is128BitVector())
727 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000728 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
729 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000731 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000732
Evan Cheng2c3ae372006-04-12 21:21:57 +0000733 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
734 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
735 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
736 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000737 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000738 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000739
Nate Begemancdd1eec2008-02-12 22:51:28 +0000740 if (Subtarget->is64Bit()) {
741 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000742 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000744
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000745 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
David Greene9b9838d2009-06-29 16:47:10 +0000746 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
747 MVT VT = (MVT::SimpleValueType)i;
748
749 // Do not attempt to promote non-128-bit vectors
750 if (!VT.is128BitVector()) {
751 continue;
752 }
753 setOperationAction(ISD::AND, VT, Promote);
754 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
755 setOperationAction(ISD::OR, VT, Promote);
756 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
757 setOperationAction(ISD::XOR, VT, Promote);
758 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
759 setOperationAction(ISD::LOAD, VT, Promote);
760 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
761 setOperationAction(ISD::SELECT, VT, Promote);
762 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000763 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000764
Chris Lattnerddf89562008-01-17 19:59:44 +0000765 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000766
Evan Cheng2c3ae372006-04-12 21:21:57 +0000767 // Custom lower v2i64 and v2f64 selects.
768 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000769 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000770 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000771 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000772
Eli Friedman23ef1052009-06-06 03:57:58 +0000773 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
774 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
775 if (!DisableMMX && Subtarget->hasMMX()) {
776 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
777 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
778 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000779 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000780
Nate Begeman14d12ca2008-02-11 04:19:36 +0000781 if (Subtarget->hasSSE41()) {
782 // FIXME: Do we need to handle scalar-to-vector here?
783 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
784
785 // i8 and i16 vectors are custom , because the source register and source
786 // source memory operand types are not the same width. f32 vectors are
787 // custom since the immediate controlling the insert encodes additional
788 // information.
789 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
790 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000791 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
793
794 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000796 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000797 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000798
799 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000800 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
801 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000802 }
803 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000804
Nate Begeman30a0de92008-07-17 16:51:19 +0000805 if (Subtarget->hasSSE42()) {
806 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
807 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000808
David Greene9b9838d2009-06-29 16:47:10 +0000809 if (!UseSoftFloat && Subtarget->hasAVX()) {
David Greened94c1012009-06-29 22:50:51 +0000810 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
811 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
812 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
813 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
814
David Greene9b9838d2009-06-29 16:47:10 +0000815 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
816 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
817 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
818 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
819 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
820 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
821 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
822 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
823 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
824 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
825 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
826 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
827 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
828 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
829 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
830
831 // Operations to consider commented out -v16i16 v32i8
832 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
833 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
834 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
835 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
836 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
837 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
838 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
839 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
840 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
841 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
842 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
843 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
844 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
845 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
846
847 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
848 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
849 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
850 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
851
852 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
853 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
854 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
855 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
857
858 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
859 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
860 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
862 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
864
865#if 0
866 // Not sure we want to do this since there are no 256-bit integer
867 // operations in AVX
868
869 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
870 // This includes 256-bit vectors
871 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
872 MVT VT = (MVT::SimpleValueType)i;
873
874 // Do not attempt to custom lower non-power-of-2 vectors
875 if (!isPowerOf2_32(VT.getVectorNumElements()))
876 continue;
877
878 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
879 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
880 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
881 }
882
883 if (Subtarget->is64Bit()) {
884 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
885 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
886 }
887#endif
888
889#if 0
890 // Not sure we want to do this since there are no 256-bit integer
891 // operations in AVX
892
893 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
894 // Including 256-bit vectors
895 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
896 MVT VT = (MVT::SimpleValueType)i;
897
898 if (!VT.is256BitVector()) {
899 continue;
900 }
901 setOperationAction(ISD::AND, VT, Promote);
902 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
903 setOperationAction(ISD::OR, VT, Promote);
904 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
905 setOperationAction(ISD::XOR, VT, Promote);
906 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
907 setOperationAction(ISD::LOAD, VT, Promote);
908 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
909 setOperationAction(ISD::SELECT, VT, Promote);
910 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
911 }
912
913 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
914#endif
915 }
916
Evan Cheng6be2c582006-04-05 23:38:46 +0000917 // We want to custom lower some of our intrinsics.
918 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
919
Bill Wendling74c37652008-12-09 22:08:41 +0000920 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000921 setOperationAction(ISD::SADDO, MVT::i32, Custom);
922 setOperationAction(ISD::SADDO, MVT::i64, Custom);
923 setOperationAction(ISD::UADDO, MVT::i32, Custom);
924 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000925 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
926 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
927 setOperationAction(ISD::USUBO, MVT::i32, Custom);
928 setOperationAction(ISD::USUBO, MVT::i64, Custom);
929 setOperationAction(ISD::SMULO, MVT::i32, Custom);
930 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000931
Evan Chengd54f2d52009-03-31 19:38:51 +0000932 if (!Subtarget->is64Bit()) {
933 // These libcalls are not available in 32-bit.
934 setLibcallName(RTLIB::SHL_I128, 0);
935 setLibcallName(RTLIB::SRL_I128, 0);
936 setLibcallName(RTLIB::SRA_I128, 0);
937 }
938
Evan Cheng206ee9d2006-07-07 08:33:52 +0000939 // We have target-specific dag combine patterns for the following nodes:
940 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000941 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000942 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000943 setTargetDAGCombine(ISD::SHL);
944 setTargetDAGCombine(ISD::SRA);
945 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000946 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000947 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000948 if (Subtarget->is64Bit())
949 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000950
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000951 computeRegisterProperties();
952
Evan Cheng87ed7162006-02-14 08:25:08 +0000953 // FIXME: These should be based on subtarget info. Plus, the values should
954 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000955 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
956 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
957 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000958 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000959 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000960 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000961}
962
Scott Michel5b8f82e2008-03-10 15:42:14 +0000963
Duncan Sands5480c042009-01-01 15:52:00 +0000964MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000965 return MVT::i8;
966}
967
968
Evan Cheng29286502008-01-23 23:17:41 +0000969/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
970/// the desired ByVal argument alignment.
971static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
972 if (MaxAlign == 16)
973 return;
974 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
975 if (VTy->getBitWidth() == 128)
976 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000977 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
978 unsigned EltAlign = 0;
979 getMaxByValAlign(ATy->getElementType(), EltAlign);
980 if (EltAlign > MaxAlign)
981 MaxAlign = EltAlign;
982 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
983 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
984 unsigned EltAlign = 0;
985 getMaxByValAlign(STy->getElementType(i), EltAlign);
986 if (EltAlign > MaxAlign)
987 MaxAlign = EltAlign;
988 if (MaxAlign == 16)
989 break;
990 }
991 }
992 return;
993}
994
995/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
996/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000997/// that contain SSE vectors are placed at 16-byte boundaries while the rest
998/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000999unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001000 if (Subtarget->is64Bit()) {
1001 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001002 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001003 if (TyAlign > 8)
1004 return TyAlign;
1005 return 8;
1006 }
1007
Evan Cheng29286502008-01-23 23:17:41 +00001008 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001009 if (Subtarget->hasSSE1())
1010 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001011 return Align;
1012}
Chris Lattner2b02a442007-02-25 08:29:00 +00001013
Evan Chengf0df0312008-05-15 08:39:06 +00001014/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001015/// and store operations as a result of memset, memcpy, and memmove
1016/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001017/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001018MVT
Evan Chengf0df0312008-05-15 08:39:06 +00001019X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001020 bool isSrcConst, bool isSrcStr,
1021 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001022 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1023 // linux. This is because the stack realignment code can't handle certain
1024 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001025 const Function *F = DAG.getMachineFunction().getFunction();
1026 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1027 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001028 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
1029 return MVT::v4i32;
1030 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
1031 return MVT::v4f32;
1032 }
Evan Chengf0df0312008-05-15 08:39:06 +00001033 if (Subtarget->is64Bit() && Size >= 8)
1034 return MVT::i64;
1035 return MVT::i32;
1036}
1037
Evan Chengcc415862007-11-09 01:32:10 +00001038/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1039/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001040SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001041 SelectionDAG &DAG) const {
1042 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001043 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Chris Lattnere4df7562009-07-09 03:15:51 +00001044 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001045 // This doesn't have DebugLoc associated with it, but is not really the
1046 // same as a Register.
1047 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1048 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001049 return Table;
1050}
1051
Bill Wendlingb4202b82009-07-01 18:50:55 +00001052/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001053unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
1054 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
1055}
1056
Chris Lattner2b02a442007-02-25 08:29:00 +00001057//===----------------------------------------------------------------------===//
1058// Return Value Calling Convention Implementation
1059//===----------------------------------------------------------------------===//
1060
Chris Lattner59ed56b2007-02-28 04:55:35 +00001061#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001062
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001063/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +00001064SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001065 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001066 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +00001067
Chris Lattner9774c912007-02-27 05:28:59 +00001068 SmallVector<CCValAssign, 16> RVLocs;
1069 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +00001070 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Owen Andersone922c022009-07-22 00:24:57 +00001071 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
Gabor Greifba36cb52008-08-28 21:40:38 +00001072 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001073
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001074 // If this is the first return lowered for this function, add the regs to the
1075 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001076 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001077 for (unsigned i = 0; i != RVLocs.size(); ++i)
1078 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001079 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001080 }
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001082
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001083 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001084 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001085 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001086 SDValue TailCall = Chain;
1087 SDValue TargetAddress = TailCall.getOperand(1);
1088 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +00001089 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001090 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001091 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R11)) ||
Bill Wendling056292f2008-09-16 21:48:12 +00001092 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +00001093 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001094 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +00001095 assert(StackAdjustment.getOpcode() == ISD::Constant &&
1096 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001097
Dan Gohman475871a2008-07-27 21:46:04 +00001098 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001099 Operands.push_back(Chain.getOperand(0));
1100 Operands.push_back(TargetAddress);
1101 Operands.push_back(StackAdjustment);
1102 // Copy registers used by the call. Last operand is a flag so it is not
1103 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001104 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001105 Operands.push_back(Chain.getOperand(i));
1106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001107 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001108 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001110
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001111 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +00001112 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001113
Dan Gohman475871a2008-07-27 21:46:04 +00001114 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001115 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1116 // Operand #1 = Bytes To Pop
1117 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001118
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001119 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001120 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1121 CCValAssign &VA = RVLocs[i];
1122 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +00001123 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001124
Chris Lattner447ff682008-03-11 03:23:40 +00001125 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1126 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001127 if (VA.getLocReg() == X86::ST0 ||
1128 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001129 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1130 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001131 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +00001132 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001133 RetOps.push_back(ValToCopy);
1134 // Don't emit a copytoreg.
1135 continue;
1136 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001137
Evan Cheng242b38b2009-02-23 09:03:22 +00001138 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1139 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001140 if (Subtarget->is64Bit()) {
1141 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001142 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +00001143 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001144 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1145 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1146 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001147 }
1148
Dale Johannesendd64c412009-02-04 00:33:20 +00001149 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001150 Flag = Chain.getValue(1);
1151 }
Dan Gohman61a92132008-04-21 23:59:07 +00001152
1153 // The x86-64 ABI for returning structs by value requires that we copy
1154 // the sret argument into %rax for the return. We saved the argument into
1155 // a virtual register in the entry block, so now we copy the value out
1156 // and into %rax.
1157 if (Subtarget->is64Bit() &&
1158 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1159 MachineFunction &MF = DAG.getMachineFunction();
1160 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1161 unsigned Reg = FuncInfo->getSRetReturnReg();
1162 if (!Reg) {
1163 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1164 FuncInfo->setSRetReturnReg(Reg);
1165 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001166 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001167
Dale Johannesendd64c412009-02-04 00:33:20 +00001168 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001169 Flag = Chain.getValue(1);
1170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001171
Chris Lattner447ff682008-03-11 03:23:40 +00001172 RetOps[0] = Chain; // Update chain.
1173
1174 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001175 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001176 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001177
1178 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001179 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001180}
1181
1182
Chris Lattner3085e152007-02-25 08:59:22 +00001183/// LowerCallResult - Lower the result values of an ISD::CALL into the
1184/// appropriate copies out of appropriate physical registers. This assumes that
1185/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1186/// being lowered. The returns a SDNode with the same number of values as the
1187/// ISD::CALL.
1188SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001189LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001190 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001191
Scott Michelfdc40a02009-02-17 22:15:04 +00001192 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001193 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001194 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001195 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001196 bool Is64Bit = Subtarget->is64Bit();
Owen Andersond1474d02009-07-09 17:57:24 +00001197 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001198 RVLocs, *DAG.getContext());
Chris Lattnere32bbf62007-02-28 07:09:55 +00001199 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1200
Dan Gohman475871a2008-07-27 21:46:04 +00001201 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001202
Chris Lattner3085e152007-02-25 08:59:22 +00001203 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001204 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001205 CCValAssign &VA = RVLocs[i];
1206 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001207
Torok Edwin3f142c32009-02-01 18:15:56 +00001208 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001209 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001210 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001211 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001212 }
1213
Chris Lattner8e6da152008-03-10 21:08:41 +00001214 // If this is a call to a function that returns an fp value on the floating
1215 // point stack, but where we prefer to use the value in xmm registers, copy
1216 // 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 +00001217 if ((VA.getLocReg() == X86::ST0 ||
1218 VA.getLocReg() == X86::ST1) &&
1219 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001220 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001221 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001222
Evan Cheng79fb3b42009-02-20 20:43:02 +00001223 SDValue Val;
1224 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001225 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1226 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1227 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1228 MVT::v2i64, InFlag).getValue(1);
1229 Val = Chain.getValue(0);
1230 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001231 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001232 } else {
1233 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1234 MVT::i64, InFlag).getValue(1);
1235 Val = Chain.getValue(0);
1236 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001237 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1238 } else {
1239 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1240 CopyVT, InFlag).getValue(1);
1241 Val = Chain.getValue(0);
1242 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001243 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001244
Dan Gohman37eed792009-02-04 17:28:58 +00001245 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001246 // Round the F80 the right size, which also moves to the appropriate xmm
1247 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001248 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001249 // This truncation won't change the value.
1250 DAG.getIntPtrConstant(1));
1251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001252
Chris Lattner8e6da152008-03-10 21:08:41 +00001253 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001254 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001255
Chris Lattner3085e152007-02-25 08:59:22 +00001256 // Merge everything together with a MERGE_VALUES node.
1257 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001258 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1259 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001260}
1261
1262
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001263//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001264// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001265//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001266// StdCall calling convention seems to be standard for many Windows' API
1267// routines and around. It differs from C calling convention just a little:
1268// callee should clean up the stack, not caller. Symbols should be also
1269// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001270// For info on fast calling convention see Fast Calling Convention (tail call)
1271// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001272
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001273/// CallIsStructReturn - Determines whether a CALL node uses struct return
1274/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001275static bool CallIsStructReturn(CallSDNode *TheCall) {
1276 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001277 if (!NumOps)
1278 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001279
Dan Gohman095cc292008-09-13 01:54:27 +00001280 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001281}
1282
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001283/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001284/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001285static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001287 if (!NumArgs)
1288 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001289
1290 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001291}
1292
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001293/// IsCalleePop - Determines whether the callee is required to pop its
1294/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001295bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001296 if (IsVarArg)
1297 return false;
1298
Dan Gohman095cc292008-09-13 01:54:27 +00001299 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001300 default:
1301 return false;
1302 case CallingConv::X86_StdCall:
1303 return !Subtarget->is64Bit();
1304 case CallingConv::X86_FastCall:
1305 return !Subtarget->is64Bit();
1306 case CallingConv::Fast:
1307 return PerformTailCallOpt;
1308 }
1309}
1310
Dan Gohman095cc292008-09-13 01:54:27 +00001311/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1312/// given CallingConvention value.
1313CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001314 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001315 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001316 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001317 else
1318 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001319 }
1320
Gordon Henriksen86737662008-01-05 16:56:59 +00001321 if (CC == CallingConv::X86_FastCall)
1322 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001323 else if (CC == CallingConv::Fast)
1324 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001325 else
1326 return CC_X86_32_C;
1327}
1328
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001329/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1330/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001331NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001332X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001333 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001334 if (CC == CallingConv::X86_FastCall)
1335 return FastCall;
1336 else if (CC == CallingConv::X86_StdCall)
1337 return StdCall;
1338 return None;
1339}
1340
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001341
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001342/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1343/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001344/// the specific parameter attribute. The copy will be passed as a byval
1345/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001346static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001347CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001348 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1349 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001350 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001351 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001352 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001353}
1354
Dan Gohman475871a2008-07-27 21:46:04 +00001355SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001356 const CCValAssign &VA,
1357 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001358 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001359 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001360 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001361 ISD::ArgFlagsTy Flags =
1362 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001363 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001364 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001365
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001366 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001367 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001368 // In case of tail call optimization mark all arguments mutable. Since they
1369 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001370 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001371 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001373 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001374 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001375 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001376 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001377}
1378
Dan Gohman475871a2008-07-27 21:46:04 +00001379SDValue
1380X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001381 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001382 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001383 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001384
Gordon Henriksen86737662008-01-05 16:56:59 +00001385 const Function* Fn = MF.getFunction();
1386 if (Fn->hasExternalLinkage() &&
1387 Subtarget->isTargetCygMing() &&
1388 Fn->getName() == "main")
1389 FuncInfo->setForceFramePointer(true);
1390
1391 // Decorate the function name.
1392 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001393
Evan Cheng1bc78042006-04-26 01:20:17 +00001394 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001395 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001396 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001397 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001398 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001399 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001400
1401 assert(!(isVarArg && CC == CallingConv::Fast) &&
1402 "Var args not supported with calling convention fastcc");
1403
Chris Lattner638402b2007-02-28 07:00:42 +00001404 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001405 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersone922c022009-07-22 00:24:57 +00001406 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman095cc292008-09-13 01:54:27 +00001407 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001408
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001410 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001411 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001412 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1413 CCValAssign &VA = ArgLocs[i];
1414 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1415 // places.
1416 assert(VA.getValNo() != LastVal &&
1417 "Don't support value assigned to multiple locs yet");
1418 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001419
Chris Lattnerf39f7712007-02-28 05:46:49 +00001420 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001421 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001422 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001423 if (RegVT == MVT::i32)
1424 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001425 else if (Is64Bit && RegVT == MVT::i64)
1426 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001427 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001428 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001429 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001430 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001431 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001432 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001433 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1434 RC = X86::VR64RegisterClass;
1435 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001436 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001437
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001438 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001439 ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
Chris Lattnerf39f7712007-02-28 05:46:49 +00001441 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1442 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1443 // right size.
1444 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001445 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001446 DAG.getValueType(VA.getValVT()));
1447 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001448 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001449 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001450 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001451 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001452
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001453 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001454 // Handle MMX values passed in XMM regs.
1455 if (RegVT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00001456 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1457 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001458 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1459 } else
1460 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001461 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001462 } else {
1463 assert(VA.isMemLoc());
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001464 ArgValue = LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001465 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001466
1467 // If value is passed via pointer - do a load.
1468 if (VA.getLocInfo() == CCValAssign::Indirect)
1469 ArgValue = DAG.getLoad(VA.getValVT(), dl, Root, ArgValue, NULL, 0);
1470
1471 ArgValues.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001472 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001473
Dan Gohman61a92132008-04-21 23:59:07 +00001474 // The x86-64 ABI for returning structs by value requires that we copy
1475 // the sret argument into %rax for the return. Save the argument into
1476 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001477 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001478 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1479 unsigned Reg = FuncInfo->getSRetReturnReg();
1480 if (!Reg) {
1481 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1482 FuncInfo->setSRetReturnReg(Reg);
1483 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001484 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001485 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001486 }
1487
Chris Lattnerf39f7712007-02-28 05:46:49 +00001488 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001489 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001490 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001491 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001492
Evan Cheng1bc78042006-04-26 01:20:17 +00001493 // If the function takes variable number of arguments, make a frame index for
1494 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001495 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001496 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1497 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1498 }
1499 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001500 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1501
1502 // FIXME: We should really autogenerate these arrays
1503 static const unsigned GPR64ArgRegsWin64[] = {
1504 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001505 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001506 static const unsigned XMMArgRegsWin64[] = {
1507 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1508 };
1509 static const unsigned GPR64ArgRegs64Bit[] = {
1510 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1511 };
1512 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001513 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1514 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1515 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001516 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1517
1518 if (IsWin64) {
1519 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1520 GPR64ArgRegs = GPR64ArgRegsWin64;
1521 XMMArgRegs = XMMArgRegsWin64;
1522 } else {
1523 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1524 GPR64ArgRegs = GPR64ArgRegs64Bit;
1525 XMMArgRegs = XMMArgRegs64Bit;
1526 }
1527 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1528 TotalNumIntRegs);
1529 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1530 TotalNumXMMRegs);
1531
Devang Patel578efa92009-06-05 21:57:13 +00001532 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001533 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001534 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001535 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001536 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001537 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001538 // Kernel mode asks for SSE to be disabled, so don't push them
1539 // on the stack.
1540 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001541
Gordon Henriksen86737662008-01-05 16:56:59 +00001542 // For X86-64, if there are vararg parameters that are passed via
1543 // registers, then we must store them to their spots on the stack so they
1544 // may be loaded by deferencing the result of va_next.
1545 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001546 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1547 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1548 TotalNumXMMRegs * 16, 16);
1549
Gordon Henriksen86737662008-01-05 16:56:59 +00001550 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001551 SmallVector<SDValue, 8> MemOps;
1552 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001553 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001554 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001555 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001556 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1557 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001558 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001559 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001560 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001561 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001562 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001563 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001564 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001566
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001568 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001569 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001570 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001571 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1572 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001573 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001574 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001575 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001576 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001577 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001578 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001579 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001580 }
1581 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001582 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001583 &MemOps[0], MemOps.size());
1584 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001586
Gordon Henriksenae636f82008-01-03 16:47:34 +00001587 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001588
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001590 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001591 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001592 BytesCallerReserves = 0;
1593 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001594 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001595 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001596 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001597 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001598 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001599 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001600
Gordon Henriksen86737662008-01-05 16:56:59 +00001601 if (!Is64Bit) {
1602 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1603 if (CC == CallingConv::X86_FastCall)
1604 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1605 }
Evan Cheng25caf632006-05-23 21:06:34 +00001606
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001607 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001608
Evan Cheng25caf632006-05-23 21:06:34 +00001609 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001610 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001611 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001612}
1613
Dan Gohman475871a2008-07-27 21:46:04 +00001614SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001615X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001616 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001617 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001619 SDValue Arg, ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001620 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Dale Johannesenace16102009-02-03 19:33:06 +00001621 DebugLoc dl = TheCall->getDebugLoc();
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001622 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001623 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001624 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001625 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001626 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001627 }
Dale Johannesenace16102009-02-03 19:33:06 +00001628 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001629 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001630}
1631
Bill Wendling64e87322009-01-16 19:25:27 +00001632/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001633/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001634SDValue
1635X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001636 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001637 SDValue Chain,
1638 bool IsTailCall,
1639 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001640 int FPDiff,
1641 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001642 if (!IsTailCall || FPDiff==0) return Chain;
1643
1644 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001645 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001646 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001647
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001648 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001649 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001650 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001651}
1652
1653/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1654/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001655static SDValue
1656EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001657 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001658 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001659 // Store the return address to the appropriate stack slot.
1660 if (!FPDiff) return Chain;
1661 // Calculate the new stack slot for the return address.
1662 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001663 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001664 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001665 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001666 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001667 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001668 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001669 return Chain;
1670}
1671
Dan Gohman475871a2008-07-27 21:46:04 +00001672SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001673 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001674 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1675 SDValue Chain = TheCall->getChain();
1676 unsigned CC = TheCall->getCallingConv();
1677 bool isVarArg = TheCall->isVarArg();
1678 bool IsTailCall = TheCall->isTailCall() &&
1679 CC == CallingConv::Fast && PerformTailCallOpt;
1680 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001681 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001682 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001683 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001684
1685 assert(!(isVarArg && CC == CallingConv::Fast) &&
1686 "Var args not supported with calling convention fastcc");
1687
Chris Lattner638402b2007-02-28 07:00:42 +00001688 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001689 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersone922c022009-07-22 00:24:57 +00001690 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman095cc292008-09-13 01:54:27 +00001691 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001692
Chris Lattner423c5f42007-02-28 05:31:48 +00001693 // Get a count of how many bytes are to be pushed on the stack.
1694 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001695 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001696 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001697
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 int FPDiff = 0;
1699 if (IsTailCall) {
1700 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001701 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1703 FPDiff = NumBytesCallerPushed - NumBytes;
1704
1705 // Set the delta of movement of the returnaddr stackslot.
1706 // But only set if delta is greater than previous delta.
1707 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1708 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1709 }
1710
Chris Lattnere563bbc2008-10-11 22:08:30 +00001711 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001712
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001714 // Load return adress for tail calls.
1715 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001716 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001717
Dan Gohman475871a2008-07-27 21:46:04 +00001718 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1719 SmallVector<SDValue, 8> MemOpChains;
1720 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001721
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001722 // Walk the register/memloc assignments, inserting copies/loads. In the case
1723 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001724 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1725 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001726 MVT RegVT = VA.getLocVT();
Dan Gohman095cc292008-09-13 01:54:27 +00001727 SDValue Arg = TheCall->getArg(i);
1728 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1729 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001730
Chris Lattner423c5f42007-02-28 05:31:48 +00001731 // Promote the value if needed.
1732 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001733 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001734 case CCValAssign::Full: break;
1735 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001736 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001737 break;
1738 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001739 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001740 break;
1741 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001742 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1743 // Special case: passing MMX values in XMM registers.
1744 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1745 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1746 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
1747 } else
1748 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1749 break;
1750 case CCValAssign::BCvt:
1751 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001752 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001753 case CCValAssign::Indirect: {
1754 // Store the argument.
1755 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
1756 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1757 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
1758 PseudoSourceValue::getFixedStack(FI), 0);
1759 Arg = SpillSlot;
1760 break;
1761 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Chris Lattner423c5f42007-02-28 05:31:48 +00001764 if (VA.isRegLoc()) {
1765 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1766 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001767 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001768 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001769 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001770 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001771
Dan Gohman095cc292008-09-13 01:54:27 +00001772 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1773 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001774 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001775 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001776 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001777
Evan Cheng32fe1032006-05-25 00:59:30 +00001778 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001779 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001780 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001781
Evan Cheng347d5f72006-04-28 21:29:37 +00001782 // Build a sequence of copy-to-reg nodes chained together with token chain
1783 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001784 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001785 // Tail call byval lowering might overwrite argument registers so in case of
1786 // tail call optimization the copies to registers are lowered later.
1787 if (!IsTailCall)
1788 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001789 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001790 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001791 InFlag = Chain.getValue(1);
1792 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001793
Chris Lattner951bf7d2009-07-09 02:44:11 +00001794
Chris Lattner88e1fd52009-07-09 04:24:46 +00001795 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001796 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1797 // GOT pointer.
1798 if (!IsTailCall) {
1799 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1800 DAG.getNode(X86ISD::GlobalBaseReg,
1801 DebugLoc::getUnknownLoc(),
1802 getPointerTy()),
1803 InFlag);
1804 InFlag = Chain.getValue(1);
1805 } else {
1806 // If we are tail calling and generating PIC/GOT style code load the
1807 // address of the callee into ECX. The value in ecx is used as target of
1808 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1809 // for tail calls on PIC/GOT architectures. Normally we would just put the
1810 // address of GOT into ebx and then call target@PLT. But for tail calls
1811 // ebx would be restored (since ebx is callee saved) before jumping to the
1812 // target@PLT.
1813
1814 // Note: The actual moving to ECX is done further down.
1815 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1816 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1817 !G->getGlobal()->hasProtectedVisibility())
1818 Callee = LowerGlobalAddress(Callee, DAG);
1819 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001820 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001821 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001822 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001823
Gordon Henriksen86737662008-01-05 16:56:59 +00001824 if (Is64Bit && isVarArg) {
1825 // From AMD64 ABI document:
1826 // For calls that may call functions that use varargs or stdargs
1827 // (prototype-less calls or calls to functions containing ellipsis (...) in
1828 // the declaration) %al is used as hidden argument to specify the number
1829 // of SSE registers used. The contents of %al do not need to match exactly
1830 // the number of registers, but must be an ubound on the number of SSE
1831 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001832
1833 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 // Count the number of XMM registers allocated.
1835 static const unsigned XMMArgRegs[] = {
1836 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1837 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1838 };
1839 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001840 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001841 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001842
Dale Johannesendd64c412009-02-04 00:33:20 +00001843 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001844 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1845 InFlag = Chain.getValue(1);
1846 }
1847
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001848
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001849 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001851 SmallVector<SDValue, 8> MemOpChains2;
1852 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001854 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001855 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1857 CCValAssign &VA = ArgLocs[i];
1858 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001859 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001860 SDValue Arg = TheCall->getArg(i);
1861 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 // Create frame index.
1863 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001864 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001865 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001866 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001867
Duncan Sands276dcbd2008-03-21 09:14:45 +00001868 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001869 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001870 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001871 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001872 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001873 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001874 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001875
1876 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001877 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001878 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001879 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001880 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001881 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001882 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001883 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001884 }
1885 }
1886
1887 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001888 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001889 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001890
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001891 // Copy arguments to their registers.
1892 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001893 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001894 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001895 InFlag = Chain.getValue(1);
1896 }
Dan Gohman475871a2008-07-27 21:46:04 +00001897 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001898
Gordon Henriksen86737662008-01-05 16:56:59 +00001899 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001900 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001901 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001902 }
1903
Evan Cheng32fe1032006-05-25 00:59:30 +00001904 // If the callee is a GlobalAddress node (quite common, every direct call is)
1905 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001906 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001907 // We should use extra load for direct calls to dllimported functions in
1908 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00001909 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00001910 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001911 unsigned char OpFlags = 0;
1912
1913 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1914 // external symbols most go through the PLT in PIC mode. If the symbol
1915 // has hidden or protected visibility, or if it is static or local, then
1916 // we don't need to use the PLT - we can directly call it.
1917 if (Subtarget->isTargetELF() &&
1918 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001919 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001920 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001921 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001922 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1923 Subtarget->getDarwinVers() < 9) {
1924 // PC-relative references to external symbols should go through $stub,
1925 // unless we're building with the leopard linker or later, which
1926 // automatically synthesizes these stubs.
1927 OpFlags = X86II::MO_DARWIN_STUB;
1928 }
Chris Lattner48a7d022009-07-09 05:02:21 +00001929
Chris Lattner74e726e2009-07-09 05:27:35 +00001930 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00001931 G->getOffset(), OpFlags);
1932 }
Bill Wendling056292f2008-09-16 21:48:12 +00001933 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001934 unsigned char OpFlags = 0;
1935
1936 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
1937 // symbols should go through the PLT.
1938 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001939 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001940 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001941 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001942 Subtarget->getDarwinVers() < 9) {
1943 // PC-relative references to external symbols should go through $stub,
1944 // unless we're building with the leopard linker or later, which
1945 // automatically synthesizes these stubs.
1946 OpFlags = X86II::MO_DARWIN_STUB;
1947 }
1948
Chris Lattner48a7d022009-07-09 05:02:21 +00001949 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
1950 OpFlags);
Gordon Henriksen86737662008-01-05 16:56:59 +00001951 } else if (IsTailCall) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001952 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001953
Dale Johannesendd64c412009-02-04 00:33:20 +00001954 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001955 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001956 Callee,InFlag);
1957 Callee = DAG.getRegister(Opc, getPointerTy());
1958 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001959 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001960 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001961
Chris Lattnerd96d0722007-02-25 06:40:16 +00001962 // Returns a chain & a flag for retval copy to use.
1963 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001964 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001965
1966 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001967 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1968 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001969 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 // Returns a chain & a flag for retval copy to use.
1972 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1973 Ops.clear();
1974 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001975
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001976 Ops.push_back(Chain);
1977 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001978
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 if (IsTailCall)
1980 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001981
Gordon Henriksen86737662008-01-05 16:56:59 +00001982 // Add argument registers to the end of the list so that they are known live
1983 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001984 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1985 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1986 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001987
Evan Cheng586ccac2008-03-18 23:36:35 +00001988 // Add an implicit use GOT pointer in EBX.
Chris Lattner88e1fd52009-07-09 04:24:46 +00001989 if (!IsTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00001990 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1991
1992 // Add an implicit use of AL for x86 vararg functions.
1993 if (Is64Bit && isVarArg)
1994 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1995
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001997 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001998
Gordon Henriksen86737662008-01-05 16:56:59 +00001999 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002000 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00002001 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00002002 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00002003 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00002004
Gabor Greifba36cb52008-08-28 21:40:38 +00002005 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00002006 }
2007
Dale Johannesenace16102009-02-03 19:33:06 +00002008 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002009 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002010
Chris Lattner2d297092006-05-23 18:50:38 +00002011 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002012 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00002013 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00002014 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00002015 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002016 // If this is is a call to a struct-return function, the callee
2017 // pops the hidden struct pointer, so we have to push it back.
2018 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002019 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002021 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002022
Gordon Henriksenae636f82008-01-03 16:47:34 +00002023 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002024 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002025 DAG.getIntPtrConstant(NumBytes, true),
2026 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2027 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002028 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002029 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002030
Chris Lattner3085e152007-02-25 08:59:22 +00002031 // Handle result values, copying them out of physregs into vregs that we
2032 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00002033 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00002034 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002035}
2036
Evan Cheng25ab6902006-09-08 06:48:29 +00002037
2038//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002039// Fast Calling Convention (tail call) implementation
2040//===----------------------------------------------------------------------===//
2041
2042// Like std call, callee cleans arguments, convention except that ECX is
2043// reserved for storing the tail called function address. Only 2 registers are
2044// free for argument passing (inreg). Tail call optimization is performed
2045// provided:
2046// * tailcallopt is enabled
2047// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002048// On X86_64 architecture with GOT-style position independent code only local
2049// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002050// To keep the stack aligned according to platform abi the function
2051// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2052// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002053// If a tail called function callee has more arguments than the caller the
2054// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002055// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002056// original REtADDR, but before the saved framepointer or the spilled registers
2057// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2058// stack layout:
2059// arg1
2060// arg2
2061// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002062// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002063// move area ]
2064// (possible EBP)
2065// ESI
2066// EDI
2067// local1 ..
2068
2069/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2070/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002071unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002072 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002073 MachineFunction &MF = DAG.getMachineFunction();
2074 const TargetMachine &TM = MF.getTarget();
2075 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2076 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002077 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002078 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002079 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002080 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2081 // Number smaller than 12 so just add the difference.
2082 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2083 } else {
2084 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002085 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002086 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002087 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002088 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002089}
2090
2091/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00002092/// following the call is a return. A function is eligible if caller/callee
2093/// calling conventions match, currently only fastcc supports tail calls, and
2094/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00002095bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002096 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002097 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00002098 if (!PerformTailCallOpt)
2099 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002100
Dan Gohman095cc292008-09-13 01:54:27 +00002101 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Chris Lattner3fff30d2009-07-09 04:27:47 +00002102 unsigned CallerCC =
2103 DAG.getMachineFunction().getFunction()->getCallingConv();
2104 unsigned CalleeCC = TheCall->getCallingConv();
2105 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC)
2106 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002107 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00002108
2109 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002110}
2111
Dan Gohman3df24e62008-09-03 23:12:08 +00002112FastISel *
2113X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002114 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002115 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002116 DenseMap<const Value *, unsigned> &vm,
2117 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002118 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002119 DenseMap<const AllocaInst *, int> &am
2120#ifndef NDEBUG
2121 , SmallSet<Instruction*, 8> &cil
2122#endif
2123 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002124 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002125#ifndef NDEBUG
2126 , cil
2127#endif
2128 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002129}
2130
2131
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002132//===----------------------------------------------------------------------===//
2133// Other Lowering Hooks
2134//===----------------------------------------------------------------------===//
2135
2136
Dan Gohman475871a2008-07-27 21:46:04 +00002137SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002138 MachineFunction &MF = DAG.getMachineFunction();
2139 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2140 int ReturnAddrIndex = FuncInfo->getRAIndex();
2141
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002142 if (ReturnAddrIndex == 0) {
2143 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002144 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002145 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002146 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002147 }
2148
Evan Cheng25ab6902006-09-08 06:48:29 +00002149 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002150}
2151
2152
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002153/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2154/// specific condition code, returning the condition code and the LHS/RHS of the
2155/// comparison to make.
2156static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2157 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002158 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002159 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2160 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2161 // X > -1 -> X == 0, jump !sign.
2162 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002163 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002164 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2165 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002166 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002167 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002168 // X < 1 -> X <= 0
2169 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002170 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002171 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002172 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002173
Evan Chengd9558e02006-01-06 00:43:03 +00002174 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002175 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002176 case ISD::SETEQ: return X86::COND_E;
2177 case ISD::SETGT: return X86::COND_G;
2178 case ISD::SETGE: return X86::COND_GE;
2179 case ISD::SETLT: return X86::COND_L;
2180 case ISD::SETLE: return X86::COND_LE;
2181 case ISD::SETNE: return X86::COND_NE;
2182 case ISD::SETULT: return X86::COND_B;
2183 case ISD::SETUGT: return X86::COND_A;
2184 case ISD::SETULE: return X86::COND_BE;
2185 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002186 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002188
Chris Lattner4c78e022008-12-23 23:42:27 +00002189 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002190
Chris Lattner4c78e022008-12-23 23:42:27 +00002191 // If LHS is a foldable load, but RHS is not, flip the condition.
2192 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2193 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2194 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2195 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002196 }
2197
Chris Lattner4c78e022008-12-23 23:42:27 +00002198 switch (SetCCOpcode) {
2199 default: break;
2200 case ISD::SETOLT:
2201 case ISD::SETOLE:
2202 case ISD::SETUGT:
2203 case ISD::SETUGE:
2204 std::swap(LHS, RHS);
2205 break;
2206 }
2207
2208 // On a floating point condition, the flags are set as follows:
2209 // ZF PF CF op
2210 // 0 | 0 | 0 | X > Y
2211 // 0 | 0 | 1 | X < Y
2212 // 1 | 0 | 0 | X == Y
2213 // 1 | 1 | 1 | unordered
2214 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002215 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002216 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002217 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002218 case ISD::SETOLT: // flipped
2219 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002220 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002221 case ISD::SETOLE: // flipped
2222 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002223 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002224 case ISD::SETUGT: // flipped
2225 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002226 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002227 case ISD::SETUGE: // flipped
2228 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002229 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002230 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002231 case ISD::SETNE: return X86::COND_NE;
2232 case ISD::SETUO: return X86::COND_P;
2233 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002234 }
Evan Chengd9558e02006-01-06 00:43:03 +00002235}
2236
Evan Cheng4a460802006-01-11 00:33:36 +00002237/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2238/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002239/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002240static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002241 switch (X86CC) {
2242 default:
2243 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002244 case X86::COND_B:
2245 case X86::COND_BE:
2246 case X86::COND_E:
2247 case X86::COND_P:
2248 case X86::COND_A:
2249 case X86::COND_AE:
2250 case X86::COND_NE:
2251 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002252 return true;
2253 }
2254}
2255
Nate Begeman9008ca62009-04-27 18:41:29 +00002256/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2257/// the specified range (L, H].
2258static bool isUndefOrInRange(int Val, int Low, int Hi) {
2259 return (Val < 0) || (Val >= Low && Val < Hi);
2260}
2261
2262/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2263/// specified value.
2264static bool isUndefOrEqual(int Val, int CmpVal) {
2265 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002266 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002267 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002268}
2269
Nate Begeman9008ca62009-04-27 18:41:29 +00002270/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2271/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2272/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002273static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002274 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2275 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2276 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2277 return (Mask[0] < 2 && Mask[1] < 2);
2278 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002279}
2280
Nate Begeman9008ca62009-04-27 18:41:29 +00002281bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2282 SmallVector<int, 8> M;
2283 N->getMask(M);
2284 return ::isPSHUFDMask(M, N->getValueType(0));
2285}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002286
Nate Begeman9008ca62009-04-27 18:41:29 +00002287/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2288/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002289static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002290 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002291 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002292
2293 // Lower quadword copied in order or undef.
2294 for (int i = 0; i != 4; ++i)
2295 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002296 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002297
Evan Cheng506d3df2006-03-29 23:07:14 +00002298 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002299 for (int i = 4; i != 8; ++i)
2300 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002301 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002302
Evan Cheng506d3df2006-03-29 23:07:14 +00002303 return true;
2304}
2305
Nate Begeman9008ca62009-04-27 18:41:29 +00002306bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2307 SmallVector<int, 8> M;
2308 N->getMask(M);
2309 return ::isPSHUFHWMask(M, N->getValueType(0));
2310}
Evan Cheng506d3df2006-03-29 23:07:14 +00002311
Nate Begeman9008ca62009-04-27 18:41:29 +00002312/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2313/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002314static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002315 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002316 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002317
Rafael Espindola15684b22009-04-24 12:40:33 +00002318 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002319 for (int i = 4; i != 8; ++i)
2320 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002321 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002322
Rafael Espindola15684b22009-04-24 12:40:33 +00002323 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002324 for (int i = 0; i != 4; ++i)
2325 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002326 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002327
Rafael Espindola15684b22009-04-24 12:40:33 +00002328 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002329}
2330
Nate Begeman9008ca62009-04-27 18:41:29 +00002331bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2332 SmallVector<int, 8> M;
2333 N->getMask(M);
2334 return ::isPSHUFLWMask(M, N->getValueType(0));
2335}
2336
Evan Cheng14aed5e2006-03-24 01:18:28 +00002337/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2338/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002339static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002340 int NumElems = VT.getVectorNumElements();
2341 if (NumElems != 2 && NumElems != 4)
2342 return false;
2343
2344 int Half = NumElems / 2;
2345 for (int i = 0; i < Half; ++i)
2346 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002347 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002348 for (int i = Half; i < NumElems; ++i)
2349 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002350 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002351
Evan Cheng14aed5e2006-03-24 01:18:28 +00002352 return true;
2353}
2354
Nate Begeman9008ca62009-04-27 18:41:29 +00002355bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2356 SmallVector<int, 8> M;
2357 N->getMask(M);
2358 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002359}
2360
Evan Cheng213d2cf2007-05-17 18:45:50 +00002361/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002362/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2363/// half elements to come from vector 1 (which would equal the dest.) and
2364/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002365static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002366 int NumElems = VT.getVectorNumElements();
2367
2368 if (NumElems != 2 && NumElems != 4)
2369 return false;
2370
2371 int Half = NumElems / 2;
2372 for (int i = 0; i < Half; ++i)
2373 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002374 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002375 for (int i = Half; i < NumElems; ++i)
2376 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002377 return false;
2378 return true;
2379}
2380
Nate Begeman9008ca62009-04-27 18:41:29 +00002381static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2382 SmallVector<int, 8> M;
2383 N->getMask(M);
2384 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002385}
2386
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002387/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2388/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002389bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2390 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002391 return false;
2392
Evan Cheng2064a2b2006-03-28 06:50:32 +00002393 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002394 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2395 isUndefOrEqual(N->getMaskElt(1), 7) &&
2396 isUndefOrEqual(N->getMaskElt(2), 2) &&
2397 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002398}
2399
Evan Cheng5ced1d82006-04-06 23:23:56 +00002400/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2401/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002402bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2403 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002404
Evan Cheng5ced1d82006-04-06 23:23:56 +00002405 if (NumElems != 2 && NumElems != 4)
2406 return false;
2407
Evan Chengc5cdff22006-04-07 21:53:05 +00002408 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002409 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002410 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002411
Evan Chengc5cdff22006-04-07 21:53:05 +00002412 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002413 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002414 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002415
2416 return true;
2417}
2418
2419/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002420/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2421/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002422bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2423 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002424
Evan Cheng5ced1d82006-04-06 23:23:56 +00002425 if (NumElems != 2 && NumElems != 4)
2426 return false;
2427
Evan Chengc5cdff22006-04-07 21:53:05 +00002428 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002429 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002430 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002431
Nate Begeman9008ca62009-04-27 18:41:29 +00002432 for (unsigned i = 0; i < NumElems/2; ++i)
2433 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002434 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002435
2436 return true;
2437}
2438
Nate Begeman9008ca62009-04-27 18:41:29 +00002439/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2440/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2441/// <2, 3, 2, 3>
2442bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2443 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2444
2445 if (NumElems != 4)
2446 return false;
2447
2448 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2449 isUndefOrEqual(N->getMaskElt(1), 3) &&
2450 isUndefOrEqual(N->getMaskElt(2), 2) &&
2451 isUndefOrEqual(N->getMaskElt(3), 3);
2452}
2453
Evan Cheng0038e592006-03-28 00:39:58 +00002454/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2455/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002456static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002457 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002458 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002459 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002460 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002461
2462 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2463 int BitI = Mask[i];
2464 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002465 if (!isUndefOrEqual(BitI, j))
2466 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002467 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002468 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002469 return false;
2470 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002471 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002472 return false;
2473 }
Evan Cheng0038e592006-03-28 00:39:58 +00002474 }
Evan Cheng0038e592006-03-28 00:39:58 +00002475 return true;
2476}
2477
Nate Begeman9008ca62009-04-27 18:41:29 +00002478bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2479 SmallVector<int, 8> M;
2480 N->getMask(M);
2481 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002482}
2483
Evan Cheng4fcb9222006-03-28 02:43:26 +00002484/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2485/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002486static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002487 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002488 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002489 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002490 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002491
2492 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2493 int BitI = Mask[i];
2494 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002495 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002496 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002497 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002498 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002499 return false;
2500 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002501 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002502 return false;
2503 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002504 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002505 return true;
2506}
2507
Nate Begeman9008ca62009-04-27 18:41:29 +00002508bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2509 SmallVector<int, 8> M;
2510 N->getMask(M);
2511 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002512}
2513
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002514/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2515/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2516/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002517static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002518 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002519 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002520 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002521
2522 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2523 int BitI = Mask[i];
2524 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002525 if (!isUndefOrEqual(BitI, j))
2526 return false;
2527 if (!isUndefOrEqual(BitI1, j))
2528 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002529 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002530 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002531}
2532
Nate Begeman9008ca62009-04-27 18:41:29 +00002533bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2534 SmallVector<int, 8> M;
2535 N->getMask(M);
2536 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2537}
2538
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002539/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2540/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2541/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002542static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002543 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002544 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2545 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002546
2547 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2548 int BitI = Mask[i];
2549 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002550 if (!isUndefOrEqual(BitI, j))
2551 return false;
2552 if (!isUndefOrEqual(BitI1, j))
2553 return false;
2554 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002555 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002556}
2557
Nate Begeman9008ca62009-04-27 18:41:29 +00002558bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2559 SmallVector<int, 8> M;
2560 N->getMask(M);
2561 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2562}
2563
Evan Cheng017dcc62006-04-21 01:05:10 +00002564/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2565/// specifies a shuffle of elements that is suitable for input to MOVSS,
2566/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002567static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002568 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002569 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002570
2571 int NumElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002572
2573 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002574 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002575
2576 for (int i = 1; i < NumElts; ++i)
2577 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002578 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002579
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002580 return true;
2581}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002582
Nate Begeman9008ca62009-04-27 18:41:29 +00002583bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2584 SmallVector<int, 8> M;
2585 N->getMask(M);
2586 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002587}
2588
Evan Cheng017dcc62006-04-21 01:05:10 +00002589/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2590/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002591/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002592static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002593 bool V2IsSplat = false, bool V2IsUndef = false) {
2594 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002595 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002596 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002597
2598 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002599 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002600
2601 for (int i = 1; i < NumOps; ++i)
2602 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2603 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2604 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002605 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002606
Evan Cheng39623da2006-04-20 08:58:49 +00002607 return true;
2608}
2609
Nate Begeman9008ca62009-04-27 18:41:29 +00002610static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002611 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 SmallVector<int, 8> M;
2613 N->getMask(M);
2614 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002615}
2616
Evan Chengd9539472006-04-14 21:59:03 +00002617/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2618/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002619bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2620 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002621 return false;
2622
2623 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002624 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 int Elt = N->getMaskElt(i);
2626 if (Elt >= 0 && Elt != 1)
2627 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002628 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002629
2630 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002631 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 int Elt = N->getMaskElt(i);
2633 if (Elt >= 0 && Elt != 3)
2634 return false;
2635 if (Elt == 3)
2636 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002637 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002638 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002640 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002641}
2642
2643/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2644/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002645bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2646 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002647 return false;
2648
2649 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002650 for (unsigned i = 0; i < 2; ++i)
2651 if (N->getMaskElt(i) > 0)
2652 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002653
2654 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002655 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 int Elt = N->getMaskElt(i);
2657 if (Elt >= 0 && Elt != 2)
2658 return false;
2659 if (Elt == 2)
2660 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002661 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002663 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002664}
2665
Evan Cheng0b457f02008-09-25 20:50:48 +00002666/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2667/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002668bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2669 int e = N->getValueType(0).getVectorNumElements() / 2;
2670
2671 for (int i = 0; i < e; ++i)
2672 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002673 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002674 for (int i = 0; i < e; ++i)
2675 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002676 return false;
2677 return true;
2678}
2679
Evan Cheng63d33002006-03-22 08:01:21 +00002680/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2681/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2682/// instructions.
2683unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002684 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2685 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2686
Evan Chengb9df0ca2006-03-22 02:53:00 +00002687 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2688 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002689 for (int i = 0; i < NumOperands; ++i) {
2690 int Val = SVOp->getMaskElt(NumOperands-i-1);
2691 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002692 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002693 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002694 if (i != NumOperands - 1)
2695 Mask <<= Shift;
2696 }
Evan Cheng63d33002006-03-22 08:01:21 +00002697 return Mask;
2698}
2699
Evan Cheng506d3df2006-03-29 23:07:14 +00002700/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2701/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2702/// instructions.
2703unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002704 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002705 unsigned Mask = 0;
2706 // 8 nodes, but we only care about the last 4.
2707 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002708 int Val = SVOp->getMaskElt(i);
2709 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002710 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002711 if (i != 4)
2712 Mask <<= 2;
2713 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002714 return Mask;
2715}
2716
2717/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2718/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2719/// instructions.
2720unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002722 unsigned Mask = 0;
2723 // 8 nodes, but we only care about the first 4.
2724 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002725 int Val = SVOp->getMaskElt(i);
2726 if (Val >= 0)
2727 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002728 if (i != 0)
2729 Mask <<= 2;
2730 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002731 return Mask;
2732}
2733
Evan Cheng37b73872009-07-30 08:33:02 +00002734/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2735/// constant +0.0.
2736bool X86::isZeroNode(SDValue Elt) {
2737 return ((isa<ConstantSDNode>(Elt) &&
2738 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2739 (isa<ConstantFPSDNode>(Elt) &&
2740 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2741}
2742
Nate Begeman9008ca62009-04-27 18:41:29 +00002743/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2744/// their permute mask.
2745static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2746 SelectionDAG &DAG) {
2747 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002748 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002749 SmallVector<int, 8> MaskVec;
2750
Nate Begeman5a5ca152009-04-29 05:20:52 +00002751 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002752 int idx = SVOp->getMaskElt(i);
2753 if (idx < 0)
2754 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002755 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002756 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002757 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002758 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002759 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002760 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2761 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002762}
2763
Evan Cheng779ccea2007-12-07 21:30:01 +00002764/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2765/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002766static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002767 unsigned NumElems = VT.getVectorNumElements();
2768 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002769 int idx = Mask[i];
2770 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002771 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002772 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002773 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002774 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002775 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002776 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002777}
2778
Evan Cheng533a0aa2006-04-19 20:35:22 +00002779/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2780/// match movhlps. The lower half elements should come from upper half of
2781/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002782/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002783static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2784 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002785 return false;
2786 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002787 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002788 return false;
2789 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002790 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002791 return false;
2792 return true;
2793}
2794
Evan Cheng5ced1d82006-04-06 23:23:56 +00002795/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002796/// is promoted to a vector. It also returns the LoadSDNode by reference if
2797/// required.
2798static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002799 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2800 return false;
2801 N = N->getOperand(0).getNode();
2802 if (!ISD::isNON_EXTLoad(N))
2803 return false;
2804 if (LD)
2805 *LD = cast<LoadSDNode>(N);
2806 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002807}
2808
Evan Cheng533a0aa2006-04-19 20:35:22 +00002809/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2810/// match movlp{s|d}. The lower half elements should come from lower half of
2811/// V1 (and in order), and the upper half elements should come from the upper
2812/// half of V2 (and in order). And since V1 will become the source of the
2813/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002814static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2815 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002816 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002817 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002818 // Is V2 is a vector load, don't do this transformation. We will try to use
2819 // load folding shufps op.
2820 if (ISD::isNON_EXTLoad(V2))
2821 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002822
Nate Begeman5a5ca152009-04-29 05:20:52 +00002823 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002824
Evan Cheng533a0aa2006-04-19 20:35:22 +00002825 if (NumElems != 2 && NumElems != 4)
2826 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002827 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002829 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002830 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002831 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002832 return false;
2833 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002834}
2835
Evan Cheng39623da2006-04-20 08:58:49 +00002836/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2837/// all the same.
2838static bool isSplatVector(SDNode *N) {
2839 if (N->getOpcode() != ISD::BUILD_VECTOR)
2840 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002841
Dan Gohman475871a2008-07-27 21:46:04 +00002842 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002843 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2844 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002845 return false;
2846 return true;
2847}
2848
Evan Cheng213d2cf2007-05-17 18:45:50 +00002849/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002850/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002851/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002852static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002853 SDValue V1 = N->getOperand(0);
2854 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002855 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2856 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002857 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002858 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002860 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2861 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00002862 if (Opc != ISD::BUILD_VECTOR ||
2863 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 return false;
2865 } else if (Idx >= 0) {
2866 unsigned Opc = V1.getOpcode();
2867 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2868 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00002869 if (Opc != ISD::BUILD_VECTOR ||
2870 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002871 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002872 }
2873 }
2874 return true;
2875}
2876
2877/// getZeroVector - Returns a vector of specified type with all zero elements.
2878///
Dale Johannesenace16102009-02-03 19:33:06 +00002879static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2880 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002881 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002882
Chris Lattner8a594482007-11-25 00:24:49 +00002883 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2884 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002885 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002886 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002887 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002888 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002889 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002890 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002891 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002892 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002893 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002894 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002895 }
Dale Johannesenace16102009-02-03 19:33:06 +00002896 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002897}
2898
Chris Lattner8a594482007-11-25 00:24:49 +00002899/// getOnesVector - Returns a vector of specified type with all bits set.
2900///
Dale Johannesenace16102009-02-03 19:33:06 +00002901static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002902 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002903
Chris Lattner8a594482007-11-25 00:24:49 +00002904 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2905 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2907 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002908 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002909 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002910 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002911 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002912 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002913}
2914
2915
Evan Cheng39623da2006-04-20 08:58:49 +00002916/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2917/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002918static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2919 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002920 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002921
Evan Cheng39623da2006-04-20 08:58:49 +00002922 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002923 SmallVector<int, 8> MaskVec;
2924 SVOp->getMask(MaskVec);
2925
Nate Begeman5a5ca152009-04-29 05:20:52 +00002926 for (unsigned i = 0; i != NumElems; ++i) {
2927 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002928 MaskVec[i] = NumElems;
2929 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002930 }
Evan Cheng39623da2006-04-20 08:58:49 +00002931 }
Evan Cheng39623da2006-04-20 08:58:49 +00002932 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002933 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2934 SVOp->getOperand(1), &MaskVec[0]);
2935 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002936}
2937
Evan Cheng017dcc62006-04-21 01:05:10 +00002938/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2939/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002940static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2941 SDValue V2) {
2942 unsigned NumElems = VT.getVectorNumElements();
2943 SmallVector<int, 8> Mask;
2944 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002945 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 Mask.push_back(i);
2947 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002948}
2949
Nate Begeman9008ca62009-04-27 18:41:29 +00002950/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2951static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2952 SDValue V2) {
2953 unsigned NumElems = VT.getVectorNumElements();
2954 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002955 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002956 Mask.push_back(i);
2957 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002958 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002960}
2961
Nate Begeman9008ca62009-04-27 18:41:29 +00002962/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2963static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2964 SDValue V2) {
2965 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002966 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002968 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 Mask.push_back(i + Half);
2970 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002971 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002973}
2974
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002975/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002976static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2977 bool HasSSE2) {
2978 if (SV->getValueType(0).getVectorNumElements() <= 4)
2979 return SDValue(SV, 0);
2980
2981 MVT PVT = MVT::v4f32;
2982 MVT VT = SV->getValueType(0);
2983 DebugLoc dl = SV->getDebugLoc();
2984 SDValue V1 = SV->getOperand(0);
2985 int NumElems = VT.getVectorNumElements();
2986 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002987
Nate Begeman9008ca62009-04-27 18:41:29 +00002988 // unpack elements to the correct location
2989 while (NumElems > 4) {
2990 if (EltNo < NumElems/2) {
2991 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2992 } else {
2993 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2994 EltNo -= NumElems/2;
2995 }
2996 NumElems >>= 1;
2997 }
2998
2999 // Perform the splat.
3000 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003001 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003002 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3003 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003004}
3005
Evan Chengba05f722006-04-21 23:03:30 +00003006/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003007/// vector of zero or undef vector. This produces a shuffle where the low
3008/// element of V2 is swizzled into the zero/undef vector, landing at element
3009/// 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 +00003010static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003011 bool isZero, bool HasSSE2,
3012 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003013 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003014 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003015 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3016 unsigned NumElems = VT.getVectorNumElements();
3017 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003018 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003019 // If this is the insertion idx, put the low elt of V2 here.
3020 MaskVec.push_back(i == Idx ? NumElems : i);
3021 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003022}
3023
Evan Chengf26ffe92008-05-29 08:22:04 +00003024/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3025/// a shuffle that is zero.
3026static
Nate Begeman9008ca62009-04-27 18:41:29 +00003027unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3028 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003029 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003031 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 int Idx = SVOp->getMaskElt(Index);
3033 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003034 ++NumZeros;
3035 continue;
3036 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003037 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003038 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003039 ++NumZeros;
3040 else
3041 break;
3042 }
3043 return NumZeros;
3044}
3045
3046/// isVectorShift - Returns true if the shuffle can be implemented as a
3047/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003048/// FIXME: split into pslldqi, psrldqi, palignr variants.
3049static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003050 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003051 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003052
3053 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003054 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003055 if (!NumZeros) {
3056 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003058 if (!NumZeros)
3059 return false;
3060 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003061 bool SeenV1 = false;
3062 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003063 for (int i = NumZeros; i < NumElems; ++i) {
3064 int Val = isLeft ? (i - NumZeros) : i;
3065 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3066 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003067 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003068 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003069 SeenV1 = true;
3070 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003071 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003072 SeenV2 = true;
3073 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003075 return false;
3076 }
3077 if (SeenV1 && SeenV2)
3078 return false;
3079
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003081 ShAmt = NumZeros;
3082 return true;
3083}
3084
3085
Evan Chengc78d3b42006-04-24 18:01:45 +00003086/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3087///
Dan Gohman475871a2008-07-27 21:46:04 +00003088static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003089 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003090 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003091 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003092 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003093
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003094 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003095 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003096 bool First = true;
3097 for (unsigned i = 0; i < 16; ++i) {
3098 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3099 if (ThisIsNonZero && First) {
3100 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003101 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003102 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003103 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003104 First = false;
3105 }
3106
3107 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003109 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3110 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003111 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003112 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003113 }
3114 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003115 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3116 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00003117 ThisElt, DAG.getConstant(8, MVT::i8));
3118 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003119 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003120 } else
3121 ThisElt = LastElt;
3122
Gabor Greifba36cb52008-08-28 21:40:38 +00003123 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003124 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003125 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003126 }
3127 }
3128
Dale Johannesenace16102009-02-03 19:33:06 +00003129 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003130}
3131
Bill Wendlinga348c562007-03-22 18:42:45 +00003132/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003133///
Dan Gohman475871a2008-07-27 21:46:04 +00003134static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003135 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003136 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003137 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003138 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003139
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003140 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003141 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003142 bool First = true;
3143 for (unsigned i = 0; i < 8; ++i) {
3144 bool isNonZero = (NonZeros & (1 << i)) != 0;
3145 if (isNonZero) {
3146 if (First) {
3147 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003148 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003149 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003150 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003151 First = false;
3152 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003153 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003154 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003155 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003156 }
3157 }
3158
3159 return V;
3160}
3161
Evan Chengf26ffe92008-05-29 08:22:04 +00003162/// getVShift - Return a vector logical shift node.
3163///
Dan Gohman475871a2008-07-27 21:46:04 +00003164static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 unsigned NumBits, SelectionDAG &DAG,
3166 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003167 bool isMMX = VT.getSizeInBits() == 64;
3168 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003169 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003170 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3171 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3172 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003173 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003174}
3175
Dan Gohman475871a2008-07-27 21:46:04 +00003176SDValue
3177X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003178 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003179 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003180 if (ISD::isBuildVectorAllZeros(Op.getNode())
3181 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003182 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3183 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3184 // eliminated on x86-32 hosts.
3185 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3186 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003187
Gabor Greifba36cb52008-08-28 21:40:38 +00003188 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003189 return getOnesVector(Op.getValueType(), DAG, dl);
3190 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003191 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003192
Duncan Sands83ec4b62008-06-06 12:08:01 +00003193 MVT VT = Op.getValueType();
3194 MVT EVT = VT.getVectorElementType();
3195 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003196
3197 unsigned NumElems = Op.getNumOperands();
3198 unsigned NumZero = 0;
3199 unsigned NumNonZero = 0;
3200 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003201 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003205 if (Elt.getOpcode() == ISD::UNDEF)
3206 continue;
3207 Values.insert(Elt);
3208 if (Elt.getOpcode() != ISD::Constant &&
3209 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003210 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003211 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003212 NumZero++;
3213 else {
3214 NonZeros |= (1 << i);
3215 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003216 }
3217 }
3218
Dan Gohman7f321562007-06-25 16:23:39 +00003219 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003220 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003221 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003222 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223
Chris Lattner67f453a2008-03-09 05:42:06 +00003224 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003225 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003226 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003227 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003228
Chris Lattner62098042008-03-09 01:05:04 +00003229 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3230 // the value are obviously zero, truncate the value to i32 and do the
3231 // insertion that way. Only do this if the value is non-constant or if the
3232 // value is a constant being inserted into element 0. It is cheaper to do
3233 // a constant pool load than it is to do a movd + shuffle.
3234 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3235 (!IsAllConstants || Idx == 0)) {
3236 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3237 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003238 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3239 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003240
Chris Lattner62098042008-03-09 01:05:04 +00003241 // Truncate the value (which may itself be a constant) to i32, and
3242 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003243 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3244 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003245 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3246 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003247
Chris Lattner62098042008-03-09 01:05:04 +00003248 // Now we have our 32-bit value zero extended in the low element of
3249 // a vector. If Idx != 0, swizzle it into place.
3250 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 SmallVector<int, 4> Mask;
3252 Mask.push_back(Idx);
3253 for (unsigned i = 1; i != VecElts; ++i)
3254 Mask.push_back(i);
3255 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3256 DAG.getUNDEF(Item.getValueType()),
3257 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003258 }
Dale Johannesenace16102009-02-03 19:33:06 +00003259 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003260 }
3261 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003262
Chris Lattner19f79692008-03-08 22:59:52 +00003263 // If we have a constant or non-constant insertion into the low element of
3264 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3265 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003266 // depending on what the source datatype is.
3267 if (Idx == 0) {
3268 if (NumZero == 0) {
3269 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3270 } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 ||
3271 (EVT == MVT::i64 && Subtarget->is64Bit())) {
3272 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3273 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3274 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3275 DAG);
3276 } else if (EVT == MVT::i16 || EVT == MVT::i8) {
3277 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3278 MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
3279 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3280 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3281 Subtarget->hasSSE2(), DAG);
3282 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3283 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003284 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003285
3286 // Is it a vector logical left shift?
3287 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003288 X86::isZeroNode(Op.getOperand(0)) &&
3289 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003290 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003291 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003292 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003293 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003294 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003296
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003297 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003298 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003299
Chris Lattner19f79692008-03-08 22:59:52 +00003300 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3301 // is a non-constant being inserted into an element other than the low one,
3302 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3303 // movd/movss) to move this into the low element, then shuffle it into
3304 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003305 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003306 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003307
Evan Cheng0db9fe62006-04-25 20:13:52 +00003308 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003309 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3310 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003312 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003313 MaskVec.push_back(i == Idx ? 0 : 1);
3314 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003315 }
3316 }
3317
Chris Lattner67f453a2008-03-09 05:42:06 +00003318 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3319 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003320 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003321
Dan Gohmana3941172007-07-24 22:55:08 +00003322 // A vector full of immediates; various special cases are already
3323 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003324 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003325 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003326
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003327 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003328 if (EVTBits == 64) {
3329 if (NumNonZero == 1) {
3330 // One half is zero or undef.
3331 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003332 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003333 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003334 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3335 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003336 }
Dan Gohman475871a2008-07-27 21:46:04 +00003337 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003338 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003339
3340 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003341 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003343 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003344 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003345 }
3346
Bill Wendling826f36f2007-03-28 00:57:11 +00003347 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003348 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003349 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003350 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003351 }
3352
3353 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003354 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003355 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003356 if (NumElems == 4 && NumZero > 0) {
3357 for (unsigned i = 0; i < 4; ++i) {
3358 bool isZero = !(NonZeros & (1 << i));
3359 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003360 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003361 else
Dale Johannesenace16102009-02-03 19:33:06 +00003362 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003363 }
3364
3365 for (unsigned i = 0; i < 2; ++i) {
3366 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3367 default: break;
3368 case 0:
3369 V[i] = V[i*2]; // Must be a zero vector.
3370 break;
3371 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003373 break;
3374 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003376 break;
3377 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003378 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003379 break;
3380 }
3381 }
3382
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003384 bool Reverse = (NonZeros & 0x3) == 2;
3385 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003386 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003387 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3388 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3390 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003391 }
3392
3393 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3395 // values to be inserted is equal to the number of elements, in which case
3396 // use the unpack code below in the hopes of matching the consecutive elts
3397 // load merge pattern for shuffles.
3398 // FIXME: We could probably just check that here directly.
3399 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3400 getSubtarget()->hasSSE41()) {
3401 V[0] = DAG.getUNDEF(VT);
3402 for (unsigned i = 0; i < NumElems; ++i)
3403 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3404 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3405 Op.getOperand(i), DAG.getIntPtrConstant(i));
3406 return V[0];
3407 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003408 // Expand into a number of unpckl*.
3409 // e.g. for v4f32
3410 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3411 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3412 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003413 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003414 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003415 NumElems >>= 1;
3416 while (NumElems != 0) {
3417 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003419 NumElems >>= 1;
3420 }
3421 return V[0];
3422 }
3423
Dan Gohman475871a2008-07-27 21:46:04 +00003424 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003425}
3426
Nate Begemanb9a47b82009-02-23 08:49:38 +00003427// v8i16 shuffles - Prefer shuffles in the following order:
3428// 1. [all] pshuflw, pshufhw, optional move
3429// 2. [ssse3] 1 x pshufb
3430// 3. [ssse3] 2 x pshufb + 1 x por
3431// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003432static
Nate Begeman9008ca62009-04-27 18:41:29 +00003433SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3434 SelectionDAG &DAG, X86TargetLowering &TLI) {
3435 SDValue V1 = SVOp->getOperand(0);
3436 SDValue V2 = SVOp->getOperand(1);
3437 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003438 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003439
Nate Begemanb9a47b82009-02-23 08:49:38 +00003440 // Determine if more than 1 of the words in each of the low and high quadwords
3441 // of the result come from the same quadword of one of the two inputs. Undef
3442 // mask values count as coming from any quadword, for better codegen.
3443 SmallVector<unsigned, 4> LoQuad(4);
3444 SmallVector<unsigned, 4> HiQuad(4);
3445 BitVector InputQuads(4);
3446 for (unsigned i = 0; i < 8; ++i) {
3447 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003449 MaskVals.push_back(EltIdx);
3450 if (EltIdx < 0) {
3451 ++Quad[0];
3452 ++Quad[1];
3453 ++Quad[2];
3454 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003455 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003456 }
3457 ++Quad[EltIdx / 4];
3458 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003459 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003460
Nate Begemanb9a47b82009-02-23 08:49:38 +00003461 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003462 unsigned MaxQuad = 1;
3463 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003464 if (LoQuad[i] > MaxQuad) {
3465 BestLoQuad = i;
3466 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003467 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003468 }
3469
Nate Begemanb9a47b82009-02-23 08:49:38 +00003470 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003471 MaxQuad = 1;
3472 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003473 if (HiQuad[i] > MaxQuad) {
3474 BestHiQuad = i;
3475 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003476 }
3477 }
3478
Nate Begemanb9a47b82009-02-23 08:49:38 +00003479 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3480 // of the two input vectors, shuffle them into one input vector so only a
3481 // single pshufb instruction is necessary. If There are more than 2 input
3482 // quads, disable the next transformation since it does not help SSSE3.
3483 bool V1Used = InputQuads[0] || InputQuads[1];
3484 bool V2Used = InputQuads[2] || InputQuads[3];
3485 if (TLI.getSubtarget()->hasSSSE3()) {
3486 if (InputQuads.count() == 2 && V1Used && V2Used) {
3487 BestLoQuad = InputQuads.find_first();
3488 BestHiQuad = InputQuads.find_next(BestLoQuad);
3489 }
3490 if (InputQuads.count() > 2) {
3491 BestLoQuad = -1;
3492 BestHiQuad = -1;
3493 }
3494 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003495
Nate Begemanb9a47b82009-02-23 08:49:38 +00003496 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3497 // the shuffle mask. If a quad is scored as -1, that means that it contains
3498 // words from all 4 input quadwords.
3499 SDValue NewV;
3500 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 SmallVector<int, 8> MaskV;
3502 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3503 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3504 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3505 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3506 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003507 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003508
Nate Begemanb9a47b82009-02-23 08:49:38 +00003509 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3510 // source words for the shuffle, to aid later transformations.
3511 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003512 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003513 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003514 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003515 if (idx != (int)i)
3516 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003517 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003518 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003519 AllWordsInNewV = false;
3520 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003521 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003522
Nate Begemanb9a47b82009-02-23 08:49:38 +00003523 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3524 if (AllWordsInNewV) {
3525 for (int i = 0; i != 8; ++i) {
3526 int idx = MaskVals[i];
3527 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003528 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003529 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3530 if ((idx != i) && idx < 4)
3531 pshufhw = false;
3532 if ((idx != i) && idx > 3)
3533 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003534 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003535 V1 = NewV;
3536 V2Used = false;
3537 BestLoQuad = 0;
3538 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003539 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003540
Nate Begemanb9a47b82009-02-23 08:49:38 +00003541 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3542 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003543 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3545 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003546 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003547 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003548
3549 // If we have SSSE3, and all words of the result are from 1 input vector,
3550 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3551 // is present, fall back to case 4.
3552 if (TLI.getSubtarget()->hasSSSE3()) {
3553 SmallVector<SDValue,16> pshufbMask;
3554
3555 // If we have elements from both input vectors, set the high bit of the
3556 // shuffle mask element to zero out elements that come from V2 in the V1
3557 // mask, and elements that come from V1 in the V2 mask, so that the two
3558 // results can be OR'd together.
3559 bool TwoInputs = V1Used && V2Used;
3560 for (unsigned i = 0; i != 8; ++i) {
3561 int EltIdx = MaskVals[i] * 2;
3562 if (TwoInputs && (EltIdx >= 16)) {
3563 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3564 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3565 continue;
3566 }
3567 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3568 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3569 }
3570 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3571 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003572 DAG.getNode(ISD::BUILD_VECTOR, dl,
3573 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003574 if (!TwoInputs)
3575 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3576
3577 // Calculate the shuffle mask for the second input, shuffle it, and
3578 // OR it with the first shuffled input.
3579 pshufbMask.clear();
3580 for (unsigned i = 0; i != 8; ++i) {
3581 int EltIdx = MaskVals[i] * 2;
3582 if (EltIdx < 16) {
3583 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3584 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3585 continue;
3586 }
3587 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3588 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3589 }
3590 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3591 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003592 DAG.getNode(ISD::BUILD_VECTOR, dl,
3593 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003594 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3595 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3596 }
3597
3598 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3599 // and update MaskVals with new element order.
3600 BitVector InOrder(8);
3601 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003602 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003603 for (int i = 0; i != 4; ++i) {
3604 int idx = MaskVals[i];
3605 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003607 InOrder.set(i);
3608 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003610 InOrder.set(i);
3611 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003613 }
3614 }
3615 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003616 MaskV.push_back(i);
3617 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3618 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003619 }
3620
3621 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3622 // and update MaskVals with the new element order.
3623 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003624 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003625 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003626 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003627 for (unsigned i = 4; i != 8; ++i) {
3628 int idx = MaskVals[i];
3629 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003630 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003631 InOrder.set(i);
3632 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003633 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003634 InOrder.set(i);
3635 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003636 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003637 }
3638 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003639 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3640 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003641 }
3642
3643 // In case BestHi & BestLo were both -1, which means each quadword has a word
3644 // from each of the four input quadwords, calculate the InOrder bitvector now
3645 // before falling through to the insert/extract cleanup.
3646 if (BestLoQuad == -1 && BestHiQuad == -1) {
3647 NewV = V1;
3648 for (int i = 0; i != 8; ++i)
3649 if (MaskVals[i] < 0 || MaskVals[i] == i)
3650 InOrder.set(i);
3651 }
3652
3653 // The other elements are put in the right place using pextrw and pinsrw.
3654 for (unsigned i = 0; i != 8; ++i) {
3655 if (InOrder[i])
3656 continue;
3657 int EltIdx = MaskVals[i];
3658 if (EltIdx < 0)
3659 continue;
3660 SDValue ExtOp = (EltIdx < 8)
3661 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3662 DAG.getIntPtrConstant(EltIdx))
3663 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3664 DAG.getIntPtrConstant(EltIdx - 8));
3665 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3666 DAG.getIntPtrConstant(i));
3667 }
3668 return NewV;
3669}
3670
3671// v16i8 shuffles - Prefer shuffles in the following order:
3672// 1. [ssse3] 1 x pshufb
3673// 2. [ssse3] 2 x pshufb + 1 x por
3674// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3675static
Nate Begeman9008ca62009-04-27 18:41:29 +00003676SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3677 SelectionDAG &DAG, X86TargetLowering &TLI) {
3678 SDValue V1 = SVOp->getOperand(0);
3679 SDValue V2 = SVOp->getOperand(1);
3680 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003681 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003683
3684 // If we have SSSE3, case 1 is generated when all result bytes come from
3685 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3686 // present, fall back to case 3.
3687 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3688 bool V1Only = true;
3689 bool V2Only = true;
3690 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003691 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003692 if (EltIdx < 0)
3693 continue;
3694 if (EltIdx < 16)
3695 V2Only = false;
3696 else
3697 V1Only = false;
3698 }
3699
3700 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3701 if (TLI.getSubtarget()->hasSSSE3()) {
3702 SmallVector<SDValue,16> pshufbMask;
3703
3704 // If all result elements are from one input vector, then only translate
3705 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3706 //
3707 // Otherwise, we have elements from both input vectors, and must zero out
3708 // elements that come from V2 in the first mask, and V1 in the second mask
3709 // so that we can OR them together.
3710 bool TwoInputs = !(V1Only || V2Only);
3711 for (unsigned i = 0; i != 16; ++i) {
3712 int EltIdx = MaskVals[i];
3713 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3714 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3715 continue;
3716 }
3717 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3718 }
3719 // If all the elements are from V2, assign it to V1 and return after
3720 // building the first pshufb.
3721 if (V2Only)
3722 V1 = V2;
3723 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003724 DAG.getNode(ISD::BUILD_VECTOR, dl,
3725 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003726 if (!TwoInputs)
3727 return V1;
3728
3729 // Calculate the shuffle mask for the second input, shuffle it, and
3730 // OR it with the first shuffled input.
3731 pshufbMask.clear();
3732 for (unsigned i = 0; i != 16; ++i) {
3733 int EltIdx = MaskVals[i];
3734 if (EltIdx < 16) {
3735 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3736 continue;
3737 }
3738 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3739 }
3740 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003741 DAG.getNode(ISD::BUILD_VECTOR, dl,
3742 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003743 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3744 }
3745
3746 // No SSSE3 - Calculate in place words and then fix all out of place words
3747 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3748 // the 16 different words that comprise the two doublequadword input vectors.
3749 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3750 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3751 SDValue NewV = V2Only ? V2 : V1;
3752 for (int i = 0; i != 8; ++i) {
3753 int Elt0 = MaskVals[i*2];
3754 int Elt1 = MaskVals[i*2+1];
3755
3756 // This word of the result is all undef, skip it.
3757 if (Elt0 < 0 && Elt1 < 0)
3758 continue;
3759
3760 // This word of the result is already in the correct place, skip it.
3761 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3762 continue;
3763 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3764 continue;
3765
3766 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3767 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3768 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003769
3770 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3771 // using a single extract together, load it and store it.
3772 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3773 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3774 DAG.getIntPtrConstant(Elt1 / 2));
3775 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3776 DAG.getIntPtrConstant(i));
3777 continue;
3778 }
3779
Nate Begemanb9a47b82009-02-23 08:49:38 +00003780 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003781 // source byte is not also odd, shift the extracted word left 8 bits
3782 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003783 if (Elt1 >= 0) {
3784 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3785 DAG.getIntPtrConstant(Elt1 / 2));
3786 if ((Elt1 & 1) == 0)
3787 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3788 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003789 else if (Elt0 >= 0)
3790 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3791 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003792 }
3793 // If Elt0 is defined, extract it from the appropriate source. If the
3794 // source byte is not also even, shift the extracted word right 8 bits. If
3795 // Elt1 was also defined, OR the extracted values together before
3796 // inserting them in the result.
3797 if (Elt0 >= 0) {
3798 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3799 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3800 if ((Elt0 & 1) != 0)
3801 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3802 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003803 else if (Elt1 >= 0)
3804 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3805 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003806 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3807 : InsElt0;
3808 }
3809 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3810 DAG.getIntPtrConstant(i));
3811 }
3812 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003813}
3814
Evan Cheng7a831ce2007-12-15 03:00:47 +00003815/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3816/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3817/// done when every pair / quad of shuffle mask elements point to elements in
3818/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003819/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3820static
Nate Begeman9008ca62009-04-27 18:41:29 +00003821SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3822 SelectionDAG &DAG,
3823 TargetLowering &TLI, DebugLoc dl) {
3824 MVT VT = SVOp->getValueType(0);
3825 SDValue V1 = SVOp->getOperand(0);
3826 SDValue V2 = SVOp->getOperand(1);
3827 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003828 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003830 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003831 MVT NewVT = MaskVT;
3832 switch (VT.getSimpleVT()) {
3833 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003834 case MVT::v4f32: NewVT = MVT::v2f64; break;
3835 case MVT::v4i32: NewVT = MVT::v2i64; break;
3836 case MVT::v8i16: NewVT = MVT::v4i32; break;
3837 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003838 }
3839
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003840 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003841 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003842 NewVT = MVT::v2i64;
3843 else
3844 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003845 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003846 int Scale = NumElems / NewWidth;
3847 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003848 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 int StartIdx = -1;
3850 for (int j = 0; j < Scale; ++j) {
3851 int EltIdx = SVOp->getMaskElt(i+j);
3852 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003853 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003854 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003855 StartIdx = EltIdx - (EltIdx % Scale);
3856 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003857 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003858 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003859 if (StartIdx == -1)
3860 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003861 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003863 }
3864
Dale Johannesenace16102009-02-03 19:33:06 +00003865 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3866 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003868}
3869
Evan Chengd880b972008-05-09 21:53:03 +00003870/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003871///
Dan Gohman475871a2008-07-27 21:46:04 +00003872static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 SDValue SrcOp, SelectionDAG &DAG,
3874 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003875 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3876 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003877 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003878 LD = dyn_cast<LoadSDNode>(SrcOp);
3879 if (!LD) {
3880 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3881 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003883 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3884 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3885 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3886 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3887 // PR2108
3888 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003889 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3890 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3891 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3892 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003893 SrcOp.getOperand(0)
3894 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003895 }
3896 }
3897 }
3898
Dale Johannesenace16102009-02-03 19:33:06 +00003899 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3900 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003901 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003902 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003903}
3904
Evan Chengace3c172008-07-22 21:13:36 +00003905/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3906/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003907static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003908LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3909 SDValue V1 = SVOp->getOperand(0);
3910 SDValue V2 = SVOp->getOperand(1);
3911 DebugLoc dl = SVOp->getDebugLoc();
3912 MVT VT = SVOp->getValueType(0);
3913
Evan Chengace3c172008-07-22 21:13:36 +00003914 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003915 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 SmallVector<int, 8> Mask1(4U, -1);
3917 SmallVector<int, 8> PermMask;
3918 SVOp->getMask(PermMask);
3919
Evan Chengace3c172008-07-22 21:13:36 +00003920 unsigned NumHi = 0;
3921 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003922 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 int Idx = PermMask[i];
3924 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003925 Locs[i] = std::make_pair(-1, -1);
3926 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3928 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003929 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003931 NumLo++;
3932 } else {
3933 Locs[i] = std::make_pair(1, NumHi);
3934 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003936 NumHi++;
3937 }
3938 }
3939 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003940
Evan Chengace3c172008-07-22 21:13:36 +00003941 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003942 // If no more than two elements come from either vector. This can be
3943 // implemented with two shuffles. First shuffle gather the elements.
3944 // The second shuffle, which takes the first shuffle as both of its
3945 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003947
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 SmallVector<int, 8> Mask2(4U, -1);
3949
Evan Chengace3c172008-07-22 21:13:36 +00003950 for (unsigned i = 0; i != 4; ++i) {
3951 if (Locs[i].first == -1)
3952 continue;
3953 else {
3954 unsigned Idx = (i < 2) ? 0 : 4;
3955 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003957 }
3958 }
3959
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003961 } else if (NumLo == 3 || NumHi == 3) {
3962 // Otherwise, we must have three elements from one vector, call it X, and
3963 // one element from the other, call it Y. First, use a shufps to build an
3964 // intermediate vector with the one element from Y and the element from X
3965 // that will be in the same half in the final destination (the indexes don't
3966 // matter). Then, use a shufps to build the final vector, taking the half
3967 // containing the element from Y from the intermediate, and the other half
3968 // from X.
3969 if (NumHi == 3) {
3970 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003972 std::swap(V1, V2);
3973 }
3974
3975 // Find the element from V2.
3976 unsigned HiIndex;
3977 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 int Val = PermMask[HiIndex];
3979 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003980 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003981 if (Val >= 4)
3982 break;
3983 }
3984
Nate Begeman9008ca62009-04-27 18:41:29 +00003985 Mask1[0] = PermMask[HiIndex];
3986 Mask1[1] = -1;
3987 Mask1[2] = PermMask[HiIndex^1];
3988 Mask1[3] = -1;
3989 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003990
3991 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003992 Mask1[0] = PermMask[0];
3993 Mask1[1] = PermMask[1];
3994 Mask1[2] = HiIndex & 1 ? 6 : 4;
3995 Mask1[3] = HiIndex & 1 ? 4 : 6;
3996 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003997 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003998 Mask1[0] = HiIndex & 1 ? 2 : 0;
3999 Mask1[1] = HiIndex & 1 ? 0 : 2;
4000 Mask1[2] = PermMask[2];
4001 Mask1[3] = PermMask[3];
4002 if (Mask1[2] >= 0)
4003 Mask1[2] += 4;
4004 if (Mask1[3] >= 0)
4005 Mask1[3] += 4;
4006 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004007 }
Evan Chengace3c172008-07-22 21:13:36 +00004008 }
4009
4010 // Break it into (shuffle shuffle_hi, shuffle_lo).
4011 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004012 SmallVector<int,8> LoMask(4U, -1);
4013 SmallVector<int,8> HiMask(4U, -1);
4014
4015 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004016 unsigned MaskIdx = 0;
4017 unsigned LoIdx = 0;
4018 unsigned HiIdx = 2;
4019 for (unsigned i = 0; i != 4; ++i) {
4020 if (i == 2) {
4021 MaskPtr = &HiMask;
4022 MaskIdx = 1;
4023 LoIdx = 0;
4024 HiIdx = 2;
4025 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004026 int Idx = PermMask[i];
4027 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004028 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004029 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004030 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004031 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004032 LoIdx++;
4033 } else {
4034 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004035 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004036 HiIdx++;
4037 }
4038 }
4039
Nate Begeman9008ca62009-04-27 18:41:29 +00004040 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4041 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4042 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004043 for (unsigned i = 0; i != 4; ++i) {
4044 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004045 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004046 } else {
4047 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004048 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004049 }
4050 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004051 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004052}
4053
Dan Gohman475871a2008-07-27 21:46:04 +00004054SDValue
4055X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004057 SDValue V1 = Op.getOperand(0);
4058 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004060 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004061 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004062 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4064 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004065 bool V1IsSplat = false;
4066 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004067
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004069 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004070
Nate Begeman9008ca62009-04-27 18:41:29 +00004071 // Promote splats to v4f32.
4072 if (SVOp->isSplat()) {
4073 if (isMMX || NumElems < 4)
4074 return Op;
4075 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004076 }
4077
Evan Cheng7a831ce2007-12-15 03:00:47 +00004078 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4079 // do it!
4080 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004081 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004082 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004083 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004084 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00004085 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4086 // FIXME: Figure out a cleaner way to do this.
4087 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004088 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004089 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004090 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004091 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4092 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4093 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004094 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004095 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004096 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4097 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004098 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004099 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004100 }
4101 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004102
4103 if (X86::isPSHUFDMask(SVOp))
4104 return Op;
4105
Evan Chengf26ffe92008-05-29 08:22:04 +00004106 // Check if this can be converted into a logical shift.
4107 bool isLeft = false;
4108 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004110 bool isShift = getSubtarget()->hasSSE2() &&
4111 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004112 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004113 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004114 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004115 MVT EVT = VT.getVectorElementType();
4116 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004117 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004118 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004119
4120 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004121 if (V1IsUndef)
4122 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004123 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004124 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004125 if (!isMMX)
4126 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004127 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004128
4129 // FIXME: fold these into legal mask.
4130 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4131 X86::isMOVSLDUPMask(SVOp) ||
4132 X86::isMOVHLPSMask(SVOp) ||
4133 X86::isMOVHPMask(SVOp) ||
4134 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004135 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004136
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 if (ShouldXformToMOVHLPS(SVOp) ||
4138 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4139 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004140
Evan Chengf26ffe92008-05-29 08:22:04 +00004141 if (isShift) {
4142 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004143 MVT EVT = VT.getVectorElementType();
4144 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004145 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004146 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004147
Evan Cheng9eca5e82006-10-25 21:49:50 +00004148 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004149 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4150 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004151 V1IsSplat = isSplatVector(V1.getNode());
4152 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004153
Chris Lattner8a594482007-11-25 00:24:49 +00004154 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004155 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 Op = CommuteVectorShuffle(SVOp, DAG);
4157 SVOp = cast<ShuffleVectorSDNode>(Op);
4158 V1 = SVOp->getOperand(0);
4159 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004160 std::swap(V1IsSplat, V2IsSplat);
4161 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004162 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004163 }
4164
Nate Begeman9008ca62009-04-27 18:41:29 +00004165 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4166 // Shuffling low element of v1 into undef, just return v1.
4167 if (V2IsUndef)
4168 return V1;
4169 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4170 // the instruction selector will not match, so get a canonical MOVL with
4171 // swapped operands to undo the commute.
4172 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004173 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004174
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4176 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4177 X86::isUNPCKLMask(SVOp) ||
4178 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004179 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004180
Evan Cheng9bbbb982006-10-25 20:48:19 +00004181 if (V2IsSplat) {
4182 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004183 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004184 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004185 SDValue NewMask = NormalizeMask(SVOp, DAG);
4186 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4187 if (NSVOp != SVOp) {
4188 if (X86::isUNPCKLMask(NSVOp, true)) {
4189 return NewMask;
4190 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4191 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004192 }
4193 }
4194 }
4195
Evan Cheng9eca5e82006-10-25 21:49:50 +00004196 if (Commuted) {
4197 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 // FIXME: this seems wrong.
4199 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4200 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4201 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4202 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4203 X86::isUNPCKLMask(NewSVOp) ||
4204 X86::isUNPCKHMask(NewSVOp))
4205 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004206 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004207
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004209
4210 // Normalize the node to match x86 shuffle ops if needed
4211 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4212 return CommuteVectorShuffle(SVOp, DAG);
4213
4214 // Check for legal shuffle and return?
4215 SmallVector<int, 16> PermMask;
4216 SVOp->getMask(PermMask);
4217 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004218 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004219
Evan Cheng14b32e12007-12-11 01:46:18 +00004220 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4221 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004223 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004224 return NewOp;
4225 }
4226
Nate Begemanb9a47b82009-02-23 08:49:38 +00004227 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004229 if (NewOp.getNode())
4230 return NewOp;
4231 }
4232
Evan Chengace3c172008-07-22 21:13:36 +00004233 // Handle all 4 wide cases with a number of shuffles except for MMX.
4234 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004235 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004236
Dan Gohman475871a2008-07-27 21:46:04 +00004237 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004238}
4239
Dan Gohman475871a2008-07-27 21:46:04 +00004240SDValue
4241X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004242 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004244 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004245 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004246 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004247 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004248 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004249 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004250 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004251 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004252 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4253 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4254 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004255 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4256 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4257 DAG.getNode(ISD::BIT_CONVERT, dl,
4258 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004259 Op.getOperand(0)),
4260 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004261 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004262 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004263 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004264 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004265 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004266 } else if (VT == MVT::f32) {
4267 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4268 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004269 // result has a single use which is a store or a bitcast to i32. And in
4270 // the case of a store, it's not worth it if the index is a constant 0,
4271 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004272 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004273 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004274 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004275 if ((User->getOpcode() != ISD::STORE ||
4276 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4277 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004278 (User->getOpcode() != ISD::BIT_CONVERT ||
4279 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004281 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004282 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004283 Op.getOperand(0)),
4284 Op.getOperand(1));
4285 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004286 } else if (VT == MVT::i32) {
4287 // ExtractPS works with constant index.
4288 if (isa<ConstantSDNode>(Op.getOperand(1)))
4289 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004290 }
Dan Gohman475871a2008-07-27 21:46:04 +00004291 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004292}
4293
4294
Dan Gohman475871a2008-07-27 21:46:04 +00004295SDValue
4296X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004297 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004298 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004299
Evan Cheng62a3f152008-03-24 21:52:23 +00004300 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004302 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004303 return Res;
4304 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004305
Duncan Sands83ec4b62008-06-06 12:08:01 +00004306 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004307 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004310 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004311 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004312 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004313 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4314 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004315 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004316 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004317 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004318 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004319 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004320 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004321 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004322 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004323 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004324 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004325 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004326 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004327 if (Idx == 0)
4328 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004329
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004331 int Mask[4] = { Idx, -1, -1, -1 };
4332 MVT VVT = Op.getOperand(0).getValueType();
4333 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4334 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004335 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004336 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004337 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004338 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4339 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4340 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004341 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 if (Idx == 0)
4343 return Op;
4344
4345 // UNPCKHPD the element to the lowest double word, then movsd.
4346 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4347 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 int Mask[2] = { 1, -1 };
4349 MVT VVT = Op.getOperand(0).getValueType();
4350 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4351 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004352 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004353 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004354 }
4355
Dan Gohman475871a2008-07-27 21:46:04 +00004356 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357}
4358
Dan Gohman475871a2008-07-27 21:46:04 +00004359SDValue
4360X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004361 MVT VT = Op.getValueType();
4362 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004363 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004364
Dan Gohman475871a2008-07-27 21:46:04 +00004365 SDValue N0 = Op.getOperand(0);
4366 SDValue N1 = Op.getOperand(1);
4367 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004368
Dan Gohmanef521f12008-08-14 22:53:18 +00004369 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4370 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004371 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004372 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004373 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4374 // argument.
4375 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004376 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004377 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004378 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004379 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004380 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004381 // Bits [7:6] of the constant are the source select. This will always be
4382 // zero here. The DAG Combiner may combine an extract_elt index into these
4383 // bits. For example (insert (extract, 3), 2) could be matched by putting
4384 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004385 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004386 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004387 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004388 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004389 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004390 // Create this as a scalar to vector..
4391 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004392 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Eric Christopherfbd66872009-07-24 00:33:09 +00004393 } else if (EVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
4394 // PINSR* works with constant index.
4395 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004396 }
Dan Gohman475871a2008-07-27 21:46:04 +00004397 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004398}
4399
Dan Gohman475871a2008-07-27 21:46:04 +00004400SDValue
4401X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004402 MVT VT = Op.getValueType();
4403 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004404
4405 if (Subtarget->hasSSE41())
4406 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4407
Evan Cheng794405e2007-12-12 07:55:34 +00004408 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004409 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004410
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004411 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004412 SDValue N0 = Op.getOperand(0);
4413 SDValue N1 = Op.getOperand(1);
4414 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004415
Eli Friedman30e71eb2009-06-06 06:32:50 +00004416 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004417 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4418 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004419 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004420 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004421 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004422 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004423 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004424 }
Dan Gohman475871a2008-07-27 21:46:04 +00004425 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426}
4427
Dan Gohman475871a2008-07-27 21:46:04 +00004428SDValue
4429X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004430 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004431 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004432 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4433 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4434 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004435 Op.getOperand(0))));
4436
Rafael Espindoladef390a2009-08-03 02:45:34 +00004437 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
Rafael Espindolacc2b67a2009-08-03 03:00:05 +00004438 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004439
Dale Johannesenace16102009-02-03 19:33:06 +00004440 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004441 MVT VT = MVT::v2i32;
4442 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004443 default: break;
4444 case MVT::v16i8:
4445 case MVT::v8i16:
4446 VT = MVT::v4i32;
4447 break;
4448 }
Dale Johannesenace16102009-02-03 19:33:06 +00004449 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4450 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004451}
4452
Bill Wendling056292f2008-09-16 21:48:12 +00004453// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4454// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4455// one of the above mentioned nodes. It has to be wrapped because otherwise
4456// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4457// be used to form addressing mode. These wrapped nodes will be selected
4458// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004459SDValue
4460X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004461 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Chris Lattner41621a22009-06-26 19:22:52 +00004462
4463 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4464 // global base reg.
4465 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004466 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattnere4df7562009-07-09 03:15:51 +00004467
Chris Lattner4f066492009-07-11 20:29:19 +00004468 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004469 getTargetMachine().getCodeModel() == CodeModel::Small)
Chris Lattnere4df7562009-07-09 03:15:51 +00004470 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004471 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004472 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004473 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004474 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner41621a22009-06-26 19:22:52 +00004475
Evan Cheng1606e8e2009-03-13 07:51:59 +00004476 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004477 CP->getAlignment(),
4478 CP->getOffset(), OpFlag);
4479 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004480 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004481 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004482 if (OpFlag) {
4483 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004484 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004485 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004486 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487 }
4488
4489 return Result;
4490}
4491
Chris Lattner18c59872009-06-27 04:16:01 +00004492SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4493 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4494
4495 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4496 // global base reg.
4497 unsigned char OpFlag = 0;
4498 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattnere4df7562009-07-09 03:15:51 +00004499
Chris Lattner4f066492009-07-11 20:29:19 +00004500 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004501 getTargetMachine().getCodeModel() == CodeModel::Small)
Chris Lattnere4df7562009-07-09 03:15:51 +00004502 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004503 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004504 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004505 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004506 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner18c59872009-06-27 04:16:01 +00004507
4508 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4509 OpFlag);
4510 DebugLoc DL = JT->getDebugLoc();
4511 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4512
4513 // With PIC, the address is actually $g + Offset.
4514 if (OpFlag) {
4515 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4516 DAG.getNode(X86ISD::GlobalBaseReg,
4517 DebugLoc::getUnknownLoc(), getPointerTy()),
4518 Result);
4519 }
4520
4521 return Result;
4522}
4523
4524SDValue
4525X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4526 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4527
4528 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4529 // global base reg.
4530 unsigned char OpFlag = 0;
4531 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattner4f066492009-07-11 20:29:19 +00004532 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004533 getTargetMachine().getCodeModel() == CodeModel::Small)
Chris Lattnere4df7562009-07-09 03:15:51 +00004534 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004535 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004536 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004537 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004538 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner18c59872009-06-27 04:16:01 +00004539
4540 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
4541
4542 DebugLoc DL = Op.getDebugLoc();
4543 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4544
4545
4546 // With PIC, the address is actually $g + Offset.
4547 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004548 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004549 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4550 DAG.getNode(X86ISD::GlobalBaseReg,
4551 DebugLoc::getUnknownLoc(),
4552 getPointerTy()),
4553 Result);
4554 }
4555
4556 return Result;
4557}
4558
Dan Gohman475871a2008-07-27 21:46:04 +00004559SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004560X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004561 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004562 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004563 // Create the TargetGlobalAddress node, folding in the constant
4564 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004565 unsigned char OpFlags =
4566 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Dan Gohman6520e202008-10-18 02:06:02 +00004567 SDValue Result;
Chris Lattner36c25012009-07-10 07:34:39 +00004568 if (OpFlags == X86II::MO_NO_FLAG && isInt32(Offset)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004569 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004570 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004571 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004572 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004573 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004574 }
4575
Chris Lattner4f066492009-07-11 20:29:19 +00004576 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner18c59872009-06-27 04:16:01 +00004577 getTargetMachine().getCodeModel() == CodeModel::Small)
4578 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4579 else
4580 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004581
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004582 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004583 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004584 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4585 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004586 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004588
Chris Lattner36c25012009-07-10 07:34:39 +00004589 // For globals that require a load from a stub to get the address, emit the
4590 // load.
4591 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004592 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004593 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004594
Dan Gohman6520e202008-10-18 02:06:02 +00004595 // If there was a non-zero offset that we didn't fold, create an explicit
4596 // addition for it.
4597 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004598 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004599 DAG.getConstant(Offset, getPointerTy()));
4600
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 return Result;
4602}
4603
Evan Chengda43bcf2008-09-24 00:05:32 +00004604SDValue
4605X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4606 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004607 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004608 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004609}
4610
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004611static SDValue
4612GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004613 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg,
4614 unsigned char OperandFlags) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004615 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4616 DebugLoc dl = GA->getDebugLoc();
4617 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4618 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004619 GA->getOffset(),
4620 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004621 if (InFlag) {
4622 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004623 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004624 } else {
4625 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004626 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004627 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004628 SDValue Flag = Chain.getValue(1);
4629 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004630}
4631
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004632// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004633static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004634LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004635 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004636 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004637 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4638 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004639 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004640 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004641 PtrVT), InFlag);
4642 InFlag = Chain.getValue(1);
4643
Chris Lattnerb903bed2009-06-26 21:20:29 +00004644 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004645}
4646
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004647// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004648static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004649LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004650 const MVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004651 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4652 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004653}
4654
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004655// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4656// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004657static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004658 const MVT PtrVT, TLSModel::Model model,
4659 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004660 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004661 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004662 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4663 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004664 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4665 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004666
4667 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4668 NULL, 0);
4669
Chris Lattnerb903bed2009-06-26 21:20:29 +00004670 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004671 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
4672 // initialexec.
4673 unsigned WrapperKind = X86ISD::Wrapper;
4674 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004675 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00004676 } else if (is64Bit) {
4677 assert(model == TLSModel::InitialExec);
4678 OperandFlags = X86II::MO_GOTTPOFF;
4679 WrapperKind = X86ISD::WrapperRIP;
4680 } else {
4681 assert(model == TLSModel::InitialExec);
4682 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00004683 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004684
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004685 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4686 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00004687 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004688 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004689 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004690
Rafael Espindola9a580232009-02-27 13:37:18 +00004691 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004692 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004693 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004694
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004695 // The address of the thread local variable is the add of the thread
4696 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004697 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004698}
4699
Dan Gohman475871a2008-07-27 21:46:04 +00004700SDValue
4701X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004702 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004703 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004704 assert(Subtarget->isTargetELF() &&
4705 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004706 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00004707 const GlobalValue *GV = GA->getGlobal();
4708
4709 // If GV is an alias then use the aliasee for determining
4710 // thread-localness.
4711 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
4712 GV = GA->resolveAliasedGlobal(false);
4713
4714 TLSModel::Model model = getTLSModel(GV,
4715 getTargetMachine().getRelocationModel());
4716
4717 switch (model) {
4718 case TLSModel::GeneralDynamic:
4719 case TLSModel::LocalDynamic: // not implemented
4720 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00004721 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00004722 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4723
4724 case TLSModel::InitialExec:
4725 case TLSModel::LocalExec:
4726 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
4727 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004728 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004729
Torok Edwinc23197a2009-07-14 16:55:14 +00004730 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00004731 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004732}
4733
Evan Cheng0db9fe62006-04-25 20:13:52 +00004734
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004735/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004736/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004737SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004738 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004739 MVT VT = Op.getValueType();
4740 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004741 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004742 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004743 SDValue ShOpLo = Op.getOperand(0);
4744 SDValue ShOpHi = Op.getOperand(1);
4745 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00004746 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4747 DAG.getConstant(VTBits - 1, MVT::i8))
4748 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004749
Dan Gohman475871a2008-07-27 21:46:04 +00004750 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004751 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004752 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4753 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004754 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004755 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4756 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004757 }
Evan Chenge3413162006-01-09 18:33:28 +00004758
Dale Johannesenace16102009-02-03 19:33:06 +00004759 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Chris Lattner31dcfe62009-07-29 05:48:09 +00004760 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004761 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner31dcfe62009-07-29 05:48:09 +00004762 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004763
Dan Gohman475871a2008-07-27 21:46:04 +00004764 SDValue Hi, Lo;
4765 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4766 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4767 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004768
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004769 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004770 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4771 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004772 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004773 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4774 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004775 }
4776
Dan Gohman475871a2008-07-27 21:46:04 +00004777 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004778 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004779}
Evan Chenga3195e82006-01-12 22:54:21 +00004780
Dan Gohman475871a2008-07-27 21:46:04 +00004781SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004782 MVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004783
4784 if (SrcVT.isVector()) {
4785 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
4786 return Op;
4787 }
4788 return SDValue();
4789 }
4790
Duncan Sands8e4eb092008-06-08 20:54:56 +00004791 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004792 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004793
Eli Friedman36df4992009-05-27 00:47:34 +00004794 // These are really Legal; return the operand so the caller accepts it as
4795 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004796 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004797 return Op;
4798 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4799 Subtarget->is64Bit()) {
4800 return Op;
4801 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004802
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004803 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004804 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004805 MachineFunction &MF = DAG.getMachineFunction();
4806 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004807 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004808 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004809 StackSlot,
4810 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004811 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4812}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004813
Eli Friedman948e95a2009-05-23 09:59:16 +00004814SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4815 SDValue StackSlot,
4816 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004817 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004818 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004819 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004820 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004821 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004822 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4823 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004824 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004825 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826 Ops.push_back(Chain);
4827 Ops.push_back(StackSlot);
4828 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004829 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004830 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004832 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004833 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004834 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004835
4836 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4837 // shouldn't be necessary except that RFP cannot be live across
4838 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004839 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004840 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004841 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004842 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004843 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004844 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004845 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004846 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004847 Ops.push_back(DAG.getValueType(Op.getValueType()));
4848 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004849 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4850 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004851 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004852 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004853
Evan Cheng0db9fe62006-04-25 20:13:52 +00004854 return Result;
4855}
4856
Bill Wendling8b8a6362009-01-17 03:56:04 +00004857// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4858SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4859 // This algorithm is not obvious. Here it is in C code, more or less:
4860 /*
4861 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4862 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4863 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004864
Bill Wendling8b8a6362009-01-17 03:56:04 +00004865 // Copy ints to xmm registers.
4866 __m128i xh = _mm_cvtsi32_si128( hi );
4867 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004868
Bill Wendling8b8a6362009-01-17 03:56:04 +00004869 // Combine into low half of a single xmm register.
4870 __m128i x = _mm_unpacklo_epi32( xh, xl );
4871 __m128d d;
4872 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004873
Bill Wendling8b8a6362009-01-17 03:56:04 +00004874 // Merge in appropriate exponents to give the integer bits the right
4875 // magnitude.
4876 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004877
Bill Wendling8b8a6362009-01-17 03:56:04 +00004878 // Subtract away the biases to deal with the IEEE-754 double precision
4879 // implicit 1.
4880 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004881
Bill Wendling8b8a6362009-01-17 03:56:04 +00004882 // All conversions up to here are exact. The correctly rounded result is
4883 // calculated using the current rounding mode using the following
4884 // horizontal add.
4885 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4886 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4887 // store doesn't really need to be here (except
4888 // maybe to zero the other double)
4889 return sd;
4890 }
4891 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004892
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004893 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00004894 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00004895
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004896 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004897 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00004898 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
4899 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
4900 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
4901 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00004902 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004903 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004904
Bill Wendling8b8a6362009-01-17 03:56:04 +00004905 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00004906 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00004907 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00004908 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00004909 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00004910 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004911 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004912
Dale Johannesenace16102009-02-03 19:33:06 +00004913 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4914 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004915 Op.getOperand(0),
4916 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004917 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4918 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004919 Op.getOperand(0),
4920 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004921 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004922 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004923 PseudoSourceValue::getConstantPool(), 0,
4924 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004926 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4927 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004928 PseudoSourceValue::getConstantPool(), 0,
4929 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004930 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004931
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004932 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004933 int ShufMask[2] = { 1, -1 };
4934 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4935 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004936 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4937 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004938 DAG.getIntPtrConstant(0));
4939}
4940
Bill Wendling8b8a6362009-01-17 03:56:04 +00004941// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4942SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004943 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004944 // FP constant to bias correct the final result.
4945 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4946 MVT::f64);
4947
4948 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004949 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4950 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004951 Op.getOperand(0),
4952 DAG.getIntPtrConstant(0)));
4953
Dale Johannesenace16102009-02-03 19:33:06 +00004954 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4955 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004956 DAG.getIntPtrConstant(0));
4957
4958 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004959 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4960 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4961 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004962 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004963 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4964 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004965 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004966 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4967 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004968 DAG.getIntPtrConstant(0));
4969
4970 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004971 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004972
4973 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004974 MVT DestVT = Op.getValueType();
4975
4976 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004977 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004978 DAG.getIntPtrConstant(0));
4979 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004980 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004981 }
4982
4983 // Handle final rounding.
4984 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004985}
4986
4987SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004988 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004989 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004990
Evan Chenga06ec9e2009-01-19 08:08:22 +00004991 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4992 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4993 // the optimization here.
4994 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004995 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004996
4997 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004998 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004999 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005000 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005001 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005002
Bill Wendling8b8a6362009-01-17 03:56:04 +00005003 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005004 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005005 return LowerUINT_TO_FP_i32(Op, DAG);
5006 }
5007
Eli Friedman948e95a2009-05-23 09:59:16 +00005008 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
5009
5010 // Make a 64-bit buffer, and use it to build an FILD.
5011 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
5012 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5013 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5014 getPointerTy(), StackSlot, WordOff);
5015 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5016 StackSlot, NULL, 0);
5017 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5018 OffsetSlot, NULL, 0);
5019 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005020}
5021
Dan Gohman475871a2008-07-27 21:46:04 +00005022std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005023FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005024 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005025
5026 MVT DstTy = Op.getValueType();
5027
5028 if (!IsSigned) {
5029 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5030 DstTy = MVT::i64;
5031 }
5032
5033 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5034 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005035 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005036
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005037 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00005038 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005039 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005040 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005041 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00005042 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005043 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005044 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005045
Evan Cheng87c89352007-10-15 20:11:21 +00005046 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5047 // stack slot.
5048 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005049 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00005050 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00005051 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00005052
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00005054 switch (DstTy.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005055 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Chris Lattner27a6c732007-11-24 07:07:01 +00005056 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5057 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5058 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005059 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005060
Dan Gohman475871a2008-07-27 21:46:04 +00005061 SDValue Chain = DAG.getEntryNode();
5062 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005063 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00005064 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005065 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00005066 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00005067 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005068 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005069 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5070 };
Dale Johannesenace16102009-02-03 19:33:06 +00005071 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 Chain = Value.getValue(1);
5073 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5074 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5075 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005076
Evan Cheng0db9fe62006-04-25 20:13:52 +00005077 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005078 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00005079 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005080
Chris Lattner27a6c732007-11-24 07:07:01 +00005081 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005082}
5083
Dan Gohman475871a2008-07-27 21:46:04 +00005084SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005085 if (Op.getValueType().isVector()) {
5086 if (Op.getValueType() == MVT::v2i32 &&
5087 Op.getOperand(0).getValueType() == MVT::v2f64) {
5088 return Op;
5089 }
5090 return SDValue();
5091 }
5092
Eli Friedman948e95a2009-05-23 09:59:16 +00005093 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005094 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005095 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5096 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005097
Chris Lattner27a6c732007-11-24 07:07:01 +00005098 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005099 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005100 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005101}
5102
Eli Friedman948e95a2009-05-23 09:59:16 +00005103SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5104 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5105 SDValue FIST = Vals.first, StackSlot = Vals.second;
5106 assert(FIST.getNode() && "Unexpected failure");
5107
5108 // Load the result.
5109 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5110 FIST, StackSlot, NULL, 0);
5111}
5112
Dan Gohman475871a2008-07-27 21:46:04 +00005113SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005114 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005115 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005116 MVT VT = Op.getValueType();
5117 MVT EltVT = VT;
5118 if (VT.isVector())
5119 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005120 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005121 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005122 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005123 CV.push_back(C);
5124 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005125 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005126 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005127 CV.push_back(C);
5128 CV.push_back(C);
5129 CV.push_back(C);
5130 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005131 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005132 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005133 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005134 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005135 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005136 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005137 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005138}
5139
Dan Gohman475871a2008-07-27 21:46:04 +00005140SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005141 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005142 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005143 MVT VT = Op.getValueType();
5144 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00005145 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005146 if (VT.isVector()) {
5147 EltVT = VT.getVectorElementType();
5148 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00005149 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005150 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005151 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005152 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005153 CV.push_back(C);
5154 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005155 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005156 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005157 CV.push_back(C);
5158 CV.push_back(C);
5159 CV.push_back(C);
5160 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005162 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005163 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005164 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005165 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005166 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005167 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005168 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5169 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00005170 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005171 Op.getOperand(0)),
5172 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005173 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005174 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005175 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176}
5177
Dan Gohman475871a2008-07-27 21:46:04 +00005178SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005179 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005180 SDValue Op0 = Op.getOperand(0);
5181 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005182 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005183 MVT VT = Op.getValueType();
5184 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005185
5186 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005187 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005188 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005189 SrcVT = VT;
5190 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005191 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005192 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005193 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005194 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005195 }
5196
5197 // At this point the operands and the result should have the same
5198 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005199
Evan Cheng68c47cb2007-01-05 07:55:56 +00005200 // First get the sign bit of second operand.
5201 std::vector<Constant*> CV;
5202 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005203 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5204 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005205 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005206 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5207 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5208 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5209 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005210 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005211 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005212 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005213 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005214 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005215 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005216 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005217
5218 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005219 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005220 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005221 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5222 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005223 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005224 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5225 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005226 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005227 }
5228
Evan Cheng73d6cf12007-01-05 21:37:56 +00005229 // Clear first operand sign bit.
5230 CV.clear();
5231 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005232 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5233 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005234 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005235 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5236 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5237 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5238 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005239 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005240 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005241 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005242 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005243 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005244 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005245 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005246
5247 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005248 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005249}
5250
Dan Gohman076aee32009-03-04 19:44:21 +00005251/// Emit nodes that will be selected as "test Op0,Op0", or something
5252/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005253SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5254 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005255 DebugLoc dl = Op.getDebugLoc();
5256
Dan Gohman31125812009-03-07 01:58:32 +00005257 // CF and OF aren't always set the way we want. Determine which
5258 // of these we need.
5259 bool NeedCF = false;
5260 bool NeedOF = false;
5261 switch (X86CC) {
5262 case X86::COND_A: case X86::COND_AE:
5263 case X86::COND_B: case X86::COND_BE:
5264 NeedCF = true;
5265 break;
5266 case X86::COND_G: case X86::COND_GE:
5267 case X86::COND_L: case X86::COND_LE:
5268 case X86::COND_O: case X86::COND_NO:
5269 NeedOF = true;
5270 break;
5271 default: break;
5272 }
5273
Dan Gohman076aee32009-03-04 19:44:21 +00005274 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005275 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5276 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5277 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005278 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005279 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005280 switch (Op.getNode()->getOpcode()) {
5281 case ISD::ADD:
5282 // Due to an isel shortcoming, be conservative if this add is likely to
5283 // be selected as part of a load-modify-store instruction. When the root
5284 // node in a match is a store, isel doesn't know how to remap non-chain
5285 // non-flag uses of other nodes in the match, such as the ADD in this
5286 // case. This leads to the ADD being left around and reselected, with
5287 // the result being two adds in the output.
5288 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5289 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5290 if (UI->getOpcode() == ISD::STORE)
5291 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005292 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005293 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5294 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005295 if (C->getAPIntValue() == 1) {
5296 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005297 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005298 break;
5299 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005300 // An add of negative one (subtract of one) will be selected as a DEC.
5301 if (C->getAPIntValue().isAllOnesValue()) {
5302 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005303 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005304 break;
5305 }
5306 }
Dan Gohman076aee32009-03-04 19:44:21 +00005307 // Otherwise use a regular EFLAGS-setting add.
5308 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005309 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005310 break;
5311 case ISD::SUB:
5312 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5313 // likely to be selected as part of a load-modify-store instruction.
5314 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5315 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5316 if (UI->getOpcode() == ISD::STORE)
5317 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005318 // Otherwise use a regular EFLAGS-setting sub.
5319 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005320 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005321 break;
5322 case X86ISD::ADD:
5323 case X86ISD::SUB:
5324 case X86ISD::INC:
5325 case X86ISD::DEC:
5326 return SDValue(Op.getNode(), 1);
5327 default:
5328 default_case:
5329 break;
5330 }
5331 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005332 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005333 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005334 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005335 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005336 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005337 DAG.ReplaceAllUsesWith(Op, New);
5338 return SDValue(New.getNode(), 1);
5339 }
5340 }
5341
5342 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5343 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5344 DAG.getConstant(0, Op.getValueType()));
5345}
5346
5347/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5348/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005349SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5350 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005351 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5352 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005353 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005354
5355 DebugLoc dl = Op0.getDebugLoc();
5356 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5357}
5358
Dan Gohman475871a2008-07-27 21:46:04 +00005359SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005360 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005361 SDValue Op0 = Op.getOperand(0);
5362 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005363 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005364 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005365
Dan Gohmane5af2d32009-01-29 01:59:02 +00005366 // Lower (X & (1 << N)) == 0 to BT(X, N).
5367 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5368 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005369 if (Op0.getOpcode() == ISD::AND &&
5370 Op0.hasOneUse() &&
5371 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005372 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005373 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005374 SDValue LHS, RHS;
5375 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5376 if (ConstantSDNode *Op010C =
5377 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5378 if (Op010C->getZExtValue() == 1) {
5379 LHS = Op0.getOperand(0);
5380 RHS = Op0.getOperand(1).getOperand(1);
5381 }
5382 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5383 if (ConstantSDNode *Op000C =
5384 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5385 if (Op000C->getZExtValue() == 1) {
5386 LHS = Op0.getOperand(1);
5387 RHS = Op0.getOperand(0).getOperand(1);
5388 }
5389 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5390 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5391 SDValue AndLHS = Op0.getOperand(0);
5392 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5393 LHS = AndLHS.getOperand(0);
5394 RHS = AndLHS.getOperand(1);
5395 }
5396 }
Evan Cheng0488db92007-09-25 01:57:46 +00005397
Dan Gohmane5af2d32009-01-29 01:59:02 +00005398 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005399 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5400 // instruction. Since the shift amount is in-range-or-undefined, we know
5401 // that doing a bittest on the i16 value is ok. We extend to i32 because
5402 // the encoding for the i16 version is larger than the i32 version.
5403 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005404 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005405
5406 // If the operand types disagree, extend the shift amount to match. Since
5407 // BT ignores high bits (like shifts) we can use anyextend.
5408 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005409 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005410
Dale Johannesenace16102009-02-03 19:33:06 +00005411 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005412 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005413 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005414 DAG.getConstant(Cond, MVT::i8), BT);
5415 }
5416 }
5417
5418 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5419 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005420
Dan Gohman31125812009-03-07 01:58:32 +00005421 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005422 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005423 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005424}
5425
Dan Gohman475871a2008-07-27 21:46:04 +00005426SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5427 SDValue Cond;
5428 SDValue Op0 = Op.getOperand(0);
5429 SDValue Op1 = Op.getOperand(1);
5430 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005431 MVT VT = Op.getValueType();
5432 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5433 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005434 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005435
5436 if (isFP) {
5437 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005438 MVT VT0 = Op0.getValueType();
5439 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5440 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005441 bool Swap = false;
5442
5443 switch (SetCCOpcode) {
5444 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005445 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005446 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005447 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005448 case ISD::SETGT: Swap = true; // Fallthrough
5449 case ISD::SETLT:
5450 case ISD::SETOLT: SSECC = 1; break;
5451 case ISD::SETOGE:
5452 case ISD::SETGE: Swap = true; // Fallthrough
5453 case ISD::SETLE:
5454 case ISD::SETOLE: SSECC = 2; break;
5455 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005456 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005457 case ISD::SETNE: SSECC = 4; break;
5458 case ISD::SETULE: Swap = true;
5459 case ISD::SETUGE: SSECC = 5; break;
5460 case ISD::SETULT: Swap = true;
5461 case ISD::SETUGT: SSECC = 6; break;
5462 case ISD::SETO: SSECC = 7; break;
5463 }
5464 if (Swap)
5465 std::swap(Op0, Op1);
5466
Nate Begemanfb8ead02008-07-25 19:05:58 +00005467 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005468 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005469 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005470 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005471 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5472 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5473 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005474 }
5475 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005476 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005477 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5478 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5479 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005480 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005481 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005482 }
5483 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005484 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005485 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005486
Nate Begeman30a0de92008-07-17 16:51:19 +00005487 // We are handling one of the integer comparisons here. Since SSE only has
5488 // GT and EQ comparisons for integer, swapping operands and multiple
5489 // operations may be required for some comparisons.
5490 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5491 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005492
Nate Begeman30a0de92008-07-17 16:51:19 +00005493 switch (VT.getSimpleVT()) {
5494 default: break;
Eli Friedman3dae2842009-07-22 01:06:52 +00005495 case MVT::v8i8:
Nate Begeman30a0de92008-07-17 16:51:19 +00005496 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Eli Friedman3dae2842009-07-22 01:06:52 +00005497 case MVT::v4i16:
Nate Begeman30a0de92008-07-17 16:51:19 +00005498 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Eli Friedman3dae2842009-07-22 01:06:52 +00005499 case MVT::v2i32:
Nate Begeman30a0de92008-07-17 16:51:19 +00005500 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5501 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5502 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005503
Nate Begeman30a0de92008-07-17 16:51:19 +00005504 switch (SetCCOpcode) {
5505 default: break;
5506 case ISD::SETNE: Invert = true;
5507 case ISD::SETEQ: Opc = EQOpc; break;
5508 case ISD::SETLT: Swap = true;
5509 case ISD::SETGT: Opc = GTOpc; break;
5510 case ISD::SETGE: Swap = true;
5511 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5512 case ISD::SETULT: Swap = true;
5513 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5514 case ISD::SETUGE: Swap = true;
5515 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5516 }
5517 if (Swap)
5518 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005519
Nate Begeman30a0de92008-07-17 16:51:19 +00005520 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5521 // bits of the inputs before performing those operations.
5522 if (FlipSigns) {
5523 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005524 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5525 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005526 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005527 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5528 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005529 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5530 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005532
Dale Johannesenace16102009-02-03 19:33:06 +00005533 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005534
5535 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005536 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005537 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005538
Nate Begeman30a0de92008-07-17 16:51:19 +00005539 return Result;
5540}
Evan Cheng0488db92007-09-25 01:57:46 +00005541
Evan Cheng370e5342008-12-03 08:38:43 +00005542// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005543static bool isX86LogicalCmp(SDValue Op) {
5544 unsigned Opc = Op.getNode()->getOpcode();
5545 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5546 return true;
5547 if (Op.getResNo() == 1 &&
5548 (Opc == X86ISD::ADD ||
5549 Opc == X86ISD::SUB ||
5550 Opc == X86ISD::SMUL ||
5551 Opc == X86ISD::UMUL ||
5552 Opc == X86ISD::INC ||
5553 Opc == X86ISD::DEC))
5554 return true;
5555
5556 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005557}
5558
Dan Gohman475871a2008-07-27 21:46:04 +00005559SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005560 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005561 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005562 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005564
Evan Cheng734503b2006-09-11 02:19:56 +00005565 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005566 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005567
Evan Cheng3f41d662007-10-08 22:16:29 +00005568 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5569 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005570 if (Cond.getOpcode() == X86ISD::SETCC) {
5571 CC = Cond.getOperand(0);
5572
Dan Gohman475871a2008-07-27 21:46:04 +00005573 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005574 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005575 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005576
Evan Cheng3f41d662007-10-08 22:16:29 +00005577 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005578 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005579 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005580 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005581
Chris Lattnerd1980a52009-03-12 06:52:53 +00005582 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5583 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005584 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005585 addTest = false;
5586 }
5587 }
5588
5589 if (addTest) {
5590 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005591 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005592 }
5593
Dan Gohmanfc166572009-04-09 23:54:40 +00005594 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005595 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005596 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5597 // condition is true.
5598 Ops.push_back(Op.getOperand(2));
5599 Ops.push_back(Op.getOperand(1));
5600 Ops.push_back(CC);
5601 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005602 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005603}
5604
Evan Cheng370e5342008-12-03 08:38:43 +00005605// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5606// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5607// from the AND / OR.
5608static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5609 Opc = Op.getOpcode();
5610 if (Opc != ISD::OR && Opc != ISD::AND)
5611 return false;
5612 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5613 Op.getOperand(0).hasOneUse() &&
5614 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5615 Op.getOperand(1).hasOneUse());
5616}
5617
Evan Cheng961d6d42009-02-02 08:19:07 +00005618// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5619// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005620static bool isXor1OfSetCC(SDValue Op) {
5621 if (Op.getOpcode() != ISD::XOR)
5622 return false;
5623 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5624 if (N1C && N1C->getAPIntValue() == 1) {
5625 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5626 Op.getOperand(0).hasOneUse();
5627 }
5628 return false;
5629}
5630
Dan Gohman475871a2008-07-27 21:46:04 +00005631SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005632 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005633 SDValue Chain = Op.getOperand(0);
5634 SDValue Cond = Op.getOperand(1);
5635 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005636 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005637 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005638
Evan Cheng0db9fe62006-04-25 20:13:52 +00005639 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005640 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005641#if 0
5642 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005643 else if (Cond.getOpcode() == X86ISD::ADD ||
5644 Cond.getOpcode() == X86ISD::SUB ||
5645 Cond.getOpcode() == X86ISD::SMUL ||
5646 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005647 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005648#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005649
Evan Cheng3f41d662007-10-08 22:16:29 +00005650 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5651 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005652 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005653 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005654
Dan Gohman475871a2008-07-27 21:46:04 +00005655 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005656 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005657 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005658 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005659 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005660 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005661 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005662 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005663 default: break;
5664 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005665 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005666 // These can only come from an arithmetic instruction with overflow,
5667 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005668 Cond = Cond.getNode()->getOperand(1);
5669 addTest = false;
5670 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005671 }
Evan Cheng0488db92007-09-25 01:57:46 +00005672 }
Evan Cheng370e5342008-12-03 08:38:43 +00005673 } else {
5674 unsigned CondOpc;
5675 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5676 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005677 if (CondOpc == ISD::OR) {
5678 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5679 // two branches instead of an explicit OR instruction with a
5680 // separate test.
5681 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005682 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005683 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005684 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005685 Chain, Dest, CC, Cmp);
5686 CC = Cond.getOperand(1).getOperand(0);
5687 Cond = Cmp;
5688 addTest = false;
5689 }
5690 } else { // ISD::AND
5691 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5692 // two branches instead of an explicit AND instruction with a
5693 // separate test. However, we only do this if this block doesn't
5694 // have a fall-through edge, because this requires an explicit
5695 // jmp when the condition is false.
5696 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005697 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005698 Op.getNode()->hasOneUse()) {
5699 X86::CondCode CCode =
5700 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5701 CCode = X86::GetOppositeBranchCondition(CCode);
5702 CC = DAG.getConstant(CCode, MVT::i8);
5703 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5704 // Look for an unconditional branch following this conditional branch.
5705 // We need this because we need to reverse the successors in order
5706 // to implement FCMP_OEQ.
5707 if (User.getOpcode() == ISD::BR) {
5708 SDValue FalseBB = User.getOperand(1);
5709 SDValue NewBR =
5710 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5711 assert(NewBR == User);
5712 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005713
Dale Johannesene4d209d2009-02-03 20:21:25 +00005714 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005715 Chain, Dest, CC, Cmp);
5716 X86::CondCode CCode =
5717 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5718 CCode = X86::GetOppositeBranchCondition(CCode);
5719 CC = DAG.getConstant(CCode, MVT::i8);
5720 Cond = Cmp;
5721 addTest = false;
5722 }
5723 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005724 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005725 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5726 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5727 // It should be transformed during dag combiner except when the condition
5728 // is set by a arithmetics with overflow node.
5729 X86::CondCode CCode =
5730 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5731 CCode = X86::GetOppositeBranchCondition(CCode);
5732 CC = DAG.getConstant(CCode, MVT::i8);
5733 Cond = Cond.getOperand(0).getOperand(1);
5734 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005735 }
Evan Cheng0488db92007-09-25 01:57:46 +00005736 }
5737
5738 if (addTest) {
5739 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005740 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005741 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005742 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005743 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005744}
5745
Anton Korobeynikove060b532007-04-17 19:34:00 +00005746
5747// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5748// Calls to _alloca is needed to probe the stack when allocating more than 4k
5749// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5750// that the guard pages used by the OS virtual memory manager are allocated in
5751// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005752SDValue
5753X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005754 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005755 assert(Subtarget->isTargetCygMing() &&
5756 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005757 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005758
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005759 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005760 SDValue Chain = Op.getOperand(0);
5761 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005762 // FIXME: Ensure alignment here
5763
Dan Gohman475871a2008-07-27 21:46:04 +00005764 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005765
Duncan Sands83ec4b62008-06-06 12:08:01 +00005766 MVT IntPtr = getPointerTy();
5767 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005768
Chris Lattnere563bbc2008-10-11 22:08:30 +00005769 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005770
Dale Johannesendd64c412009-02-04 00:33:20 +00005771 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005772 Flag = Chain.getValue(1);
5773
5774 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005775 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005776 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005777 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005778 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005779 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005780 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005781 Flag = Chain.getValue(1);
5782
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005783 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005784 DAG.getIntPtrConstant(0, true),
5785 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005786 Flag);
5787
Dale Johannesendd64c412009-02-04 00:33:20 +00005788 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005789
Dan Gohman475871a2008-07-27 21:46:04 +00005790 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005791 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005792}
5793
Dan Gohman475871a2008-07-27 21:46:04 +00005794SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005795X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005796 SDValue Chain,
5797 SDValue Dst, SDValue Src,
5798 SDValue Size, unsigned Align,
5799 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005800 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005801 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005802
Bill Wendling6f287b22008-09-30 21:22:07 +00005803 // If not DWORD aligned or size is more than the threshold, call the library.
5804 // The libc version is likely to be faster for these cases. It can use the
5805 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005806 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005807 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005808 ConstantSize->getZExtValue() >
5809 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005810 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005811
5812 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005813 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005814
Bill Wendling6158d842008-10-01 00:59:58 +00005815 if (const char *bzeroEntry = V &&
5816 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5817 MVT IntPtr = getPointerTy();
5818 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005819 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005820 TargetLowering::ArgListEntry Entry;
5821 Entry.Node = Dst;
5822 Entry.Ty = IntPtrTy;
5823 Args.push_back(Entry);
5824 Entry.Node = Size;
5825 Args.push_back(Entry);
5826 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005827 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005828 0, CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005829 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005830 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005831 }
5832
Dan Gohman707e0182008-04-12 04:36:06 +00005833 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005834 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005835 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005836
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005837 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005838 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005839 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005840 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005841 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005842 unsigned BytesLeft = 0;
5843 bool TwoRepStos = false;
5844 if (ValC) {
5845 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005846 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005847
Evan Cheng0db9fe62006-04-25 20:13:52 +00005848 // If the value is a constant, then we can potentially use larger sets.
5849 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005850 case 2: // WORD aligned
5851 AVT = MVT::i16;
5852 ValReg = X86::AX;
5853 Val = (Val << 8) | Val;
5854 break;
5855 case 0: // DWORD aligned
5856 AVT = MVT::i32;
5857 ValReg = X86::EAX;
5858 Val = (Val << 8) | Val;
5859 Val = (Val << 16) | Val;
5860 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5861 AVT = MVT::i64;
5862 ValReg = X86::RAX;
5863 Val = (Val << 32) | Val;
5864 }
5865 break;
5866 default: // Byte aligned
5867 AVT = MVT::i8;
5868 ValReg = X86::AL;
5869 Count = DAG.getIntPtrConstant(SizeVal);
5870 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005871 }
5872
Duncan Sands8e4eb092008-06-08 20:54:56 +00005873 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005874 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005875 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5876 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005877 }
5878
Dale Johannesen0f502f62009-02-03 22:26:09 +00005879 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880 InFlag);
5881 InFlag = Chain.getValue(1);
5882 } else {
5883 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005884 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005885 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005886 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005887 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005888
Scott Michelfdc40a02009-02-17 22:15:04 +00005889 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005890 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005891 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005892 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005893 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005894 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005895 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005896 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005897
Chris Lattnerd96d0722007-02-25 06:40:16 +00005898 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005899 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005900 Ops.push_back(Chain);
5901 Ops.push_back(DAG.getValueType(AVT));
5902 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005903 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005904
Evan Cheng0db9fe62006-04-25 20:13:52 +00005905 if (TwoRepStos) {
5906 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005907 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005908 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005909 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005910 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005911 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005912 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005913 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005914 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005915 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005916 Ops.clear();
5917 Ops.push_back(Chain);
5918 Ops.push_back(DAG.getValueType(MVT::i8));
5919 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005920 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005921 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005922 // Handle the last 1 - 7 bytes.
5923 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005924 MVT AddrVT = Dst.getValueType();
5925 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005926
Dale Johannesen0f502f62009-02-03 22:26:09 +00005927 Chain = DAG.getMemset(Chain, dl,
5928 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005929 DAG.getConstant(Offset, AddrVT)),
5930 Src,
5931 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005932 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005933 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005934
Dan Gohman707e0182008-04-12 04:36:06 +00005935 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005936 return Chain;
5937}
Evan Cheng11e15b32006-04-03 20:53:28 +00005938
Dan Gohman475871a2008-07-27 21:46:04 +00005939SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005940X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005941 SDValue Chain, SDValue Dst, SDValue Src,
5942 SDValue Size, unsigned Align,
5943 bool AlwaysInline,
5944 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005945 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005946 // This requires the copy size to be a constant, preferrably
5947 // within a subtarget-specific limit.
5948 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5949 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005950 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005951 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005952 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005953 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005954
Evan Cheng1887c1c2008-08-21 21:00:15 +00005955 /// If not DWORD aligned, call the library.
5956 if ((Align & 3) != 0)
5957 return SDValue();
5958
5959 // DWORD aligned
5960 MVT AVT = MVT::i32;
5961 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005962 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005963
Duncan Sands83ec4b62008-06-06 12:08:01 +00005964 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005965 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005966 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005967 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005968
Dan Gohman475871a2008-07-27 21:46:04 +00005969 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005970 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005971 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005972 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005973 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005974 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005975 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005976 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005977 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005978 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005979 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005980 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005981 InFlag = Chain.getValue(1);
5982
Chris Lattnerd96d0722007-02-25 06:40:16 +00005983 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005984 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005985 Ops.push_back(Chain);
5986 Ops.push_back(DAG.getValueType(AVT));
5987 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005988 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005989
Dan Gohman475871a2008-07-27 21:46:04 +00005990 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005991 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005992 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005993 // Handle the last 1 - 7 bytes.
5994 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005995 MVT DstVT = Dst.getValueType();
5996 MVT SrcVT = Src.getValueType();
5997 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005998 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005999 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006000 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006001 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006002 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006003 DAG.getConstant(BytesLeft, SizeVT),
6004 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006005 DstSV, DstSVOff + Offset,
6006 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006007 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006008
Scott Michelfdc40a02009-02-17 22:15:04 +00006009 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006010 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006011}
6012
Dan Gohman475871a2008-07-27 21:46:04 +00006013SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006014 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006015 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006016
Evan Cheng25ab6902006-09-08 06:48:29 +00006017 if (!Subtarget->is64Bit()) {
6018 // vastart just stores the address of the VarArgsFrameIndex slot into the
6019 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006020 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006021 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006022 }
6023
6024 // __va_list_tag:
6025 // gp_offset (0 - 6 * 8)
6026 // fp_offset (48 - 48 + 8 * 16)
6027 // overflow_arg_area (point to parameters coming in memory).
6028 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006029 SmallVector<SDValue, 8> MemOps;
6030 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006031 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006032 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00006033 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006034 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006035 MemOps.push_back(Store);
6036
6037 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006038 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006039 FIN, DAG.getIntPtrConstant(4));
6040 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00006041 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006042 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006043 MemOps.push_back(Store);
6044
6045 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006046 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006047 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006048 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006049 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006050 MemOps.push_back(Store);
6051
6052 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006053 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006054 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006055 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006056 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006057 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00006058 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006059 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006060}
6061
Dan Gohman475871a2008-07-27 21:46:04 +00006062SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006063 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6064 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006065 SDValue Chain = Op.getOperand(0);
6066 SDValue SrcPtr = Op.getOperand(1);
6067 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006068
Torok Edwindac237e2009-07-08 20:53:28 +00006069 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006070 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006071}
6072
Dan Gohman475871a2008-07-27 21:46:04 +00006073SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006074 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006075 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006076 SDValue Chain = Op.getOperand(0);
6077 SDValue DstPtr = Op.getOperand(1);
6078 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006079 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6080 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006081 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006082
Dale Johannesendd64c412009-02-04 00:33:20 +00006083 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006084 DAG.getIntPtrConstant(24), 8, false,
6085 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006086}
6087
Dan Gohman475871a2008-07-27 21:46:04 +00006088SDValue
6089X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006090 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006091 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006092 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006093 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006094 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006095 case Intrinsic::x86_sse_comieq_ss:
6096 case Intrinsic::x86_sse_comilt_ss:
6097 case Intrinsic::x86_sse_comile_ss:
6098 case Intrinsic::x86_sse_comigt_ss:
6099 case Intrinsic::x86_sse_comige_ss:
6100 case Intrinsic::x86_sse_comineq_ss:
6101 case Intrinsic::x86_sse_ucomieq_ss:
6102 case Intrinsic::x86_sse_ucomilt_ss:
6103 case Intrinsic::x86_sse_ucomile_ss:
6104 case Intrinsic::x86_sse_ucomigt_ss:
6105 case Intrinsic::x86_sse_ucomige_ss:
6106 case Intrinsic::x86_sse_ucomineq_ss:
6107 case Intrinsic::x86_sse2_comieq_sd:
6108 case Intrinsic::x86_sse2_comilt_sd:
6109 case Intrinsic::x86_sse2_comile_sd:
6110 case Intrinsic::x86_sse2_comigt_sd:
6111 case Intrinsic::x86_sse2_comige_sd:
6112 case Intrinsic::x86_sse2_comineq_sd:
6113 case Intrinsic::x86_sse2_ucomieq_sd:
6114 case Intrinsic::x86_sse2_ucomilt_sd:
6115 case Intrinsic::x86_sse2_ucomile_sd:
6116 case Intrinsic::x86_sse2_ucomigt_sd:
6117 case Intrinsic::x86_sse2_ucomige_sd:
6118 case Intrinsic::x86_sse2_ucomineq_sd: {
6119 unsigned Opc = 0;
6120 ISD::CondCode CC = ISD::SETCC_INVALID;
6121 switch (IntNo) {
6122 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006123 case Intrinsic::x86_sse_comieq_ss:
6124 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006125 Opc = X86ISD::COMI;
6126 CC = ISD::SETEQ;
6127 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006128 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006129 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006130 Opc = X86ISD::COMI;
6131 CC = ISD::SETLT;
6132 break;
6133 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006134 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006135 Opc = X86ISD::COMI;
6136 CC = ISD::SETLE;
6137 break;
6138 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006139 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006140 Opc = X86ISD::COMI;
6141 CC = ISD::SETGT;
6142 break;
6143 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006144 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006145 Opc = X86ISD::COMI;
6146 CC = ISD::SETGE;
6147 break;
6148 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006149 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006150 Opc = X86ISD::COMI;
6151 CC = ISD::SETNE;
6152 break;
6153 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006154 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006155 Opc = X86ISD::UCOMI;
6156 CC = ISD::SETEQ;
6157 break;
6158 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006159 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006160 Opc = X86ISD::UCOMI;
6161 CC = ISD::SETLT;
6162 break;
6163 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006164 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006165 Opc = X86ISD::UCOMI;
6166 CC = ISD::SETLE;
6167 break;
6168 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006169 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006170 Opc = X86ISD::UCOMI;
6171 CC = ISD::SETGT;
6172 break;
6173 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006174 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006175 Opc = X86ISD::UCOMI;
6176 CC = ISD::SETGE;
6177 break;
6178 case Intrinsic::x86_sse_ucomineq_ss:
6179 case Intrinsic::x86_sse2_ucomineq_sd:
6180 Opc = X86ISD::UCOMI;
6181 CC = ISD::SETNE;
6182 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006183 }
Evan Cheng734503b2006-09-11 02:19:56 +00006184
Dan Gohman475871a2008-07-27 21:46:04 +00006185 SDValue LHS = Op.getOperand(1);
6186 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006187 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006188 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6189 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00006190 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006191 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006192 }
Eric Christopher71c67532009-07-29 00:28:05 +00006193 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006194 // an integer value, not just an instruction so lower it to the ptest
6195 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006196 case Intrinsic::x86_sse41_ptestz:
6197 case Intrinsic::x86_sse41_ptestc:
6198 case Intrinsic::x86_sse41_ptestnzc:{
6199 unsigned X86CC = 0;
6200 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006201 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006202 case Intrinsic::x86_sse41_ptestz:
6203 // ZF = 1
6204 X86CC = X86::COND_E;
6205 break;
6206 case Intrinsic::x86_sse41_ptestc:
6207 // CF = 1
6208 X86CC = X86::COND_B;
6209 break;
6210 case Intrinsic::x86_sse41_ptestnzc:
6211 // ZF and CF = 0
6212 X86CC = X86::COND_A;
6213 break;
6214 }
6215
6216 SDValue LHS = Op.getOperand(1);
6217 SDValue RHS = Op.getOperand(2);
6218 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6219 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6220 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6221 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
6222 }
Evan Cheng5759f972008-05-04 09:15:50 +00006223
6224 // Fix vector shift instructions where the last operand is a non-immediate
6225 // i32 value.
6226 case Intrinsic::x86_sse2_pslli_w:
6227 case Intrinsic::x86_sse2_pslli_d:
6228 case Intrinsic::x86_sse2_pslli_q:
6229 case Intrinsic::x86_sse2_psrli_w:
6230 case Intrinsic::x86_sse2_psrli_d:
6231 case Intrinsic::x86_sse2_psrli_q:
6232 case Intrinsic::x86_sse2_psrai_w:
6233 case Intrinsic::x86_sse2_psrai_d:
6234 case Intrinsic::x86_mmx_pslli_w:
6235 case Intrinsic::x86_mmx_pslli_d:
6236 case Intrinsic::x86_mmx_pslli_q:
6237 case Intrinsic::x86_mmx_psrli_w:
6238 case Intrinsic::x86_mmx_psrli_d:
6239 case Intrinsic::x86_mmx_psrli_q:
6240 case Intrinsic::x86_mmx_psrai_w:
6241 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006242 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006243 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006244 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006245
6246 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006247 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006248 switch (IntNo) {
6249 case Intrinsic::x86_sse2_pslli_w:
6250 NewIntNo = Intrinsic::x86_sse2_psll_w;
6251 break;
6252 case Intrinsic::x86_sse2_pslli_d:
6253 NewIntNo = Intrinsic::x86_sse2_psll_d;
6254 break;
6255 case Intrinsic::x86_sse2_pslli_q:
6256 NewIntNo = Intrinsic::x86_sse2_psll_q;
6257 break;
6258 case Intrinsic::x86_sse2_psrli_w:
6259 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6260 break;
6261 case Intrinsic::x86_sse2_psrli_d:
6262 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6263 break;
6264 case Intrinsic::x86_sse2_psrli_q:
6265 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6266 break;
6267 case Intrinsic::x86_sse2_psrai_w:
6268 NewIntNo = Intrinsic::x86_sse2_psra_w;
6269 break;
6270 case Intrinsic::x86_sse2_psrai_d:
6271 NewIntNo = Intrinsic::x86_sse2_psra_d;
6272 break;
6273 default: {
6274 ShAmtVT = MVT::v2i32;
6275 switch (IntNo) {
6276 case Intrinsic::x86_mmx_pslli_w:
6277 NewIntNo = Intrinsic::x86_mmx_psll_w;
6278 break;
6279 case Intrinsic::x86_mmx_pslli_d:
6280 NewIntNo = Intrinsic::x86_mmx_psll_d;
6281 break;
6282 case Intrinsic::x86_mmx_pslli_q:
6283 NewIntNo = Intrinsic::x86_mmx_psll_q;
6284 break;
6285 case Intrinsic::x86_mmx_psrli_w:
6286 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6287 break;
6288 case Intrinsic::x86_mmx_psrli_d:
6289 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6290 break;
6291 case Intrinsic::x86_mmx_psrli_q:
6292 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6293 break;
6294 case Intrinsic::x86_mmx_psrai_w:
6295 NewIntNo = Intrinsic::x86_mmx_psra_w;
6296 break;
6297 case Intrinsic::x86_mmx_psrai_d:
6298 NewIntNo = Intrinsic::x86_mmx_psra_d;
6299 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006300 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006301 }
6302 break;
6303 }
6304 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006305 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006306 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6307 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6308 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006309 DAG.getConstant(NewIntNo, MVT::i32),
6310 Op.getOperand(1), ShAmt);
6311 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006312 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006313}
Evan Cheng72261582005-12-20 06:22:03 +00006314
Dan Gohman475871a2008-07-27 21:46:04 +00006315SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006316 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006317 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006318
6319 if (Depth > 0) {
6320 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6321 SDValue Offset =
6322 DAG.getConstant(TD->getPointerSize(),
6323 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006324 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006325 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006326 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006327 NULL, 0);
6328 }
6329
6330 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006331 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006332 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006333 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006334}
6335
Dan Gohman475871a2008-07-27 21:46:04 +00006336SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006337 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6338 MFI->setFrameAddressIsTaken(true);
6339 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006340 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006341 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6342 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006343 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006344 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006345 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006346 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006347}
6348
Dan Gohman475871a2008-07-27 21:46:04 +00006349SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006350 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006351 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006352}
6353
Dan Gohman475871a2008-07-27 21:46:04 +00006354SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006355{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006356 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006357 SDValue Chain = Op.getOperand(0);
6358 SDValue Offset = Op.getOperand(1);
6359 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006360 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006361
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006362 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6363 getPointerTy());
6364 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006365
Dale Johannesene4d209d2009-02-03 20:21:25 +00006366 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006367 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006368 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6369 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006370 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006371 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006372
Dale Johannesene4d209d2009-02-03 20:21:25 +00006373 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006374 MVT::Other,
6375 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006376}
6377
Dan Gohman475871a2008-07-27 21:46:04 +00006378SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006379 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006380 SDValue Root = Op.getOperand(0);
6381 SDValue Trmp = Op.getOperand(1); // trampoline
6382 SDValue FPtr = Op.getOperand(2); // nested function
6383 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006384 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006385
Dan Gohman69de1932008-02-06 22:27:42 +00006386 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006387
Duncan Sands339e14f2008-01-16 22:55:25 +00006388 const X86InstrInfo *TII =
6389 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6390
Duncan Sandsb116fac2007-07-27 20:02:49 +00006391 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006392 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006393
6394 // Large code-model.
6395
6396 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6397 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6398
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006399 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6400 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006401
6402 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6403
6404 // Load the pointer to the nested function into R11.
6405 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006406 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006407 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6408 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006409
Scott Michelfdc40a02009-02-17 22:15:04 +00006410 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006411 DAG.getConstant(2, MVT::i64));
6412 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006413
6414 // Load the 'nest' parameter value into R10.
6415 // R10 is specified in X86CallingConv.td
6416 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006417 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006418 DAG.getConstant(10, MVT::i64));
6419 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6420 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006421
Scott Michelfdc40a02009-02-17 22:15:04 +00006422 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006423 DAG.getConstant(12, MVT::i64));
6424 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006425
6426 // Jump to the nested function.
6427 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006428 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006429 DAG.getConstant(20, MVT::i64));
6430 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6431 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006432
6433 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006434 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006435 DAG.getConstant(22, MVT::i64));
6436 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006437 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006438
Dan Gohman475871a2008-07-27 21:46:04 +00006439 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006440 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6441 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006442 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006443 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006444 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6445 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006446 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006447
6448 switch (CC) {
6449 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006450 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006451 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006452 case CallingConv::X86_StdCall: {
6453 // Pass 'nest' parameter in ECX.
6454 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006455 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006456
6457 // Check that ECX wasn't needed by an 'inreg' parameter.
6458 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006459 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006460
Chris Lattner58d74912008-03-12 17:45:29 +00006461 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006462 unsigned InRegCount = 0;
6463 unsigned Idx = 1;
6464
6465 for (FunctionType::param_iterator I = FTy->param_begin(),
6466 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006467 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006468 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006469 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006470
6471 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006472 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006473 }
6474 }
6475 break;
6476 }
6477 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006478 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006479 // Pass 'nest' parameter in EAX.
6480 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006481 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006482 break;
6483 }
6484
Dan Gohman475871a2008-07-27 21:46:04 +00006485 SDValue OutChains[4];
6486 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006487
Scott Michelfdc40a02009-02-17 22:15:04 +00006488 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006489 DAG.getConstant(10, MVT::i32));
6490 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006491
Duncan Sands339e14f2008-01-16 22:55:25 +00006492 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006493 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006494 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006495 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006496 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006497
Scott Michelfdc40a02009-02-17 22:15:04 +00006498 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006499 DAG.getConstant(1, MVT::i32));
6500 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006501
Duncan Sands339e14f2008-01-16 22:55:25 +00006502 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006503 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006504 DAG.getConstant(5, MVT::i32));
6505 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006506 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006507
Scott Michelfdc40a02009-02-17 22:15:04 +00006508 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006509 DAG.getConstant(6, MVT::i32));
6510 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006511
Dan Gohman475871a2008-07-27 21:46:04 +00006512 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006513 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6514 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006515 }
6516}
6517
Dan Gohman475871a2008-07-27 21:46:04 +00006518SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006519 /*
6520 The rounding mode is in bits 11:10 of FPSR, and has the following
6521 settings:
6522 00 Round to nearest
6523 01 Round to -inf
6524 10 Round to +inf
6525 11 Round to 0
6526
6527 FLT_ROUNDS, on the other hand, expects the following:
6528 -1 Undefined
6529 0 Round to 0
6530 1 Round to nearest
6531 2 Round to +inf
6532 3 Round to -inf
6533
6534 To perform the conversion, we do:
6535 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6536 */
6537
6538 MachineFunction &MF = DAG.getMachineFunction();
6539 const TargetMachine &TM = MF.getTarget();
6540 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6541 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006542 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006543 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006544
6545 // Save FP Control Word to stack slot
6546 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006547 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006548
Dale Johannesene4d209d2009-02-03 20:21:25 +00006549 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006550 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006551
6552 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006553 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006554
6555 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006556 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006557 DAG.getNode(ISD::SRL, dl, MVT::i16,
6558 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006559 CWD, DAG.getConstant(0x800, MVT::i16)),
6560 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006561 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006562 DAG.getNode(ISD::SRL, dl, MVT::i16,
6563 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006564 CWD, DAG.getConstant(0x400, MVT::i16)),
6565 DAG.getConstant(9, MVT::i8));
6566
Dan Gohman475871a2008-07-27 21:46:04 +00006567 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006568 DAG.getNode(ISD::AND, dl, MVT::i16,
6569 DAG.getNode(ISD::ADD, dl, MVT::i16,
6570 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006571 DAG.getConstant(1, MVT::i16)),
6572 DAG.getConstant(3, MVT::i16));
6573
6574
Duncan Sands83ec4b62008-06-06 12:08:01 +00006575 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006576 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006577}
6578
Dan Gohman475871a2008-07-27 21:46:04 +00006579SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006580 MVT VT = Op.getValueType();
6581 MVT OpVT = VT;
6582 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006583 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006584
6585 Op = Op.getOperand(0);
6586 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006587 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006588 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006589 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006590 }
Evan Cheng18efe262007-12-14 02:13:44 +00006591
Evan Cheng152804e2007-12-14 08:30:15 +00006592 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6593 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006594 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006595
6596 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006597 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006598 Ops.push_back(Op);
6599 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6600 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6601 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006602 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006603
6604 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006605 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006606
Evan Cheng18efe262007-12-14 02:13:44 +00006607 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006608 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006609 return Op;
6610}
6611
Dan Gohman475871a2008-07-27 21:46:04 +00006612SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006613 MVT VT = Op.getValueType();
6614 MVT OpVT = VT;
6615 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006616 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006617
6618 Op = Op.getOperand(0);
6619 if (VT == MVT::i8) {
6620 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006621 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006622 }
Evan Cheng152804e2007-12-14 08:30:15 +00006623
6624 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6625 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006626 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006627
6628 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006629 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006630 Ops.push_back(Op);
6631 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6632 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6633 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006634 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006635
Evan Cheng18efe262007-12-14 02:13:44 +00006636 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006637 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006638 return Op;
6639}
6640
Mon P Wangaf9b9522008-12-18 21:42:19 +00006641SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6642 MVT VT = Op.getValueType();
6643 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006644 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006645
Mon P Wangaf9b9522008-12-18 21:42:19 +00006646 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6647 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6648 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6649 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6650 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6651 //
6652 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6653 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6654 // return AloBlo + AloBhi + AhiBlo;
6655
6656 SDValue A = Op.getOperand(0);
6657 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006658
Dale Johannesene4d209d2009-02-03 20:21:25 +00006659 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006660 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6661 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006662 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006663 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6664 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006665 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006666 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6667 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006668 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006669 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6670 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006671 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006672 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6673 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006674 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006675 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6676 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006677 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006678 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6679 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006680 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6681 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006682 return Res;
6683}
6684
6685
Bill Wendling74c37652008-12-09 22:08:41 +00006686SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6687 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6688 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006689 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6690 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006691 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006692 SDValue LHS = N->getOperand(0);
6693 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006694 unsigned BaseOp = 0;
6695 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006696 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006697
6698 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006699 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00006700 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006701 // A subtract of one will be selected as a INC. Note that INC doesn't
6702 // set CF, so we can't do this for UADDO.
6703 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6704 if (C->getAPIntValue() == 1) {
6705 BaseOp = X86ISD::INC;
6706 Cond = X86::COND_O;
6707 break;
6708 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006709 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006710 Cond = X86::COND_O;
6711 break;
6712 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006713 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006714 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006715 break;
6716 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006717 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6718 // set CF, so we can't do this for USUBO.
6719 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6720 if (C->getAPIntValue() == 1) {
6721 BaseOp = X86ISD::DEC;
6722 Cond = X86::COND_O;
6723 break;
6724 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006725 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006726 Cond = X86::COND_O;
6727 break;
6728 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006729 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006730 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006731 break;
6732 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006733 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006734 Cond = X86::COND_O;
6735 break;
6736 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006737 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006738 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006739 break;
6740 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006741
Bill Wendling61edeb52008-12-02 01:06:39 +00006742 // Also sets EFLAGS.
6743 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006744 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006745
Bill Wendling61edeb52008-12-02 01:06:39 +00006746 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006747 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006748 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006749
Bill Wendling61edeb52008-12-02 01:06:39 +00006750 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6751 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006752}
6753
Dan Gohman475871a2008-07-27 21:46:04 +00006754SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006755 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006756 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006757 unsigned Reg = 0;
6758 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006759 switch(T.getSimpleVT()) {
6760 default:
6761 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006762 case MVT::i8: Reg = X86::AL; size = 1; break;
6763 case MVT::i16: Reg = X86::AX; size = 2; break;
6764 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006765 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006766 assert(Subtarget->is64Bit() && "Node not type legal!");
6767 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006768 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006769 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006770 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006771 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006772 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006773 Op.getOperand(1),
6774 Op.getOperand(3),
6775 DAG.getTargetConstant(size, MVT::i8),
6776 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006777 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006778 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006779 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006780 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006781 return cpOut;
6782}
6783
Duncan Sands1607f052008-12-01 11:39:25 +00006784SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006785 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006786 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006787 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006788 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006789 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006790 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006791 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6792 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006793 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006794 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006795 DAG.getConstant(32, MVT::i8));
6796 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006797 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006798 rdx.getValue(1)
6799 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006800 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006801}
6802
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006803SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6804 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006805 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006806 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006807 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006808 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006809 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006810 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006811 Node->getOperand(0),
6812 Node->getOperand(1), negOp,
6813 cast<AtomicSDNode>(Node)->getSrcValue(),
6814 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006815}
6816
Evan Cheng0db9fe62006-04-25 20:13:52 +00006817/// LowerOperation - Provide custom lowering hooks for some operations.
6818///
Dan Gohman475871a2008-07-27 21:46:04 +00006819SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006820 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006821 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006822 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6823 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006824 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6825 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6826 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6827 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6828 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6829 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6830 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006831 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006832 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006833 case ISD::SHL_PARTS:
6834 case ISD::SRA_PARTS:
6835 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6836 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006837 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006838 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006839 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006840 case ISD::FABS: return LowerFABS(Op, DAG);
6841 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006842 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006843 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006844 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006845 case ISD::SELECT: return LowerSELECT(Op, DAG);
6846 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006847 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006848 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006849 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006850 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006851 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006852 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006853 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006854 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006855 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6856 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006857 case ISD::FRAME_TO_ARGS_OFFSET:
6858 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006859 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006860 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006861 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006862 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006863 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6864 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006865 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006866 case ISD::SADDO:
6867 case ISD::UADDO:
6868 case ISD::SSUBO:
6869 case ISD::USUBO:
6870 case ISD::SMULO:
6871 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006872 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006873 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006874}
6875
Duncan Sands1607f052008-12-01 11:39:25 +00006876void X86TargetLowering::
6877ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6878 SelectionDAG &DAG, unsigned NewOp) {
6879 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006880 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006881 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6882
6883 SDValue Chain = Node->getOperand(0);
6884 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006885 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006886 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006887 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006888 Node->getOperand(2), DAG.getIntPtrConstant(1));
6889 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6890 // have a MemOperand. Pass the info through as a normal operand.
6891 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6892 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6893 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006894 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006895 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006896 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006897 Results.push_back(Result.getValue(2));
6898}
6899
Duncan Sands126d9072008-07-04 11:47:58 +00006900/// ReplaceNodeResults - Replace a node with an illegal result type
6901/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006902void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6903 SmallVectorImpl<SDValue>&Results,
6904 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006905 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006906 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006907 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006908 assert(false && "Do not know how to custom type legalize this operation!");
6909 return;
6910 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006911 std::pair<SDValue,SDValue> Vals =
6912 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006913 SDValue FIST = Vals.first, StackSlot = Vals.second;
6914 if (FIST.getNode() != 0) {
6915 MVT VT = N->getValueType(0);
6916 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006917 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006918 }
6919 return;
6920 }
6921 case ISD::READCYCLECOUNTER: {
6922 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6923 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006924 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006925 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006926 rd.getValue(1));
6927 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006928 eax.getValue(2));
6929 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6930 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006931 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006932 Results.push_back(edx.getValue(1));
6933 return;
6934 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006935 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006936 MVT T = N->getValueType(0);
6937 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6938 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006939 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006940 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006941 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006942 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006943 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6944 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006945 cpInL.getValue(1));
6946 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006947 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006948 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006949 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006950 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006951 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006952 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006953 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006954 swapInL.getValue(1));
6955 SDValue Ops[] = { swapInH.getValue(0),
6956 N->getOperand(1),
6957 swapInH.getValue(1) };
6958 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006959 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006960 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6961 MVT::i32, Result.getValue(1));
6962 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6963 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006964 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006965 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006966 Results.push_back(cpOutH.getValue(1));
6967 return;
6968 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006969 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006970 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6971 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006972 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006973 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6974 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006975 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006976 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6977 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006978 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006979 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6980 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006981 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006982 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6983 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006984 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006985 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6986 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006987 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006988 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6989 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006990 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991}
6992
Evan Cheng72261582005-12-20 06:22:03 +00006993const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6994 switch (Opcode) {
6995 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006996 case X86ISD::BSF: return "X86ISD::BSF";
6997 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006998 case X86ISD::SHLD: return "X86ISD::SHLD";
6999 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007000 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007001 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007002 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007003 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007004 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007005 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007006 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7007 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7008 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007009 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007010 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007011 case X86ISD::CALL: return "X86ISD::CALL";
7012 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
7013 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007014 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007015 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007016 case X86ISD::COMI: return "X86ISD::COMI";
7017 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007018 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00007019 case X86ISD::CMOV: return "X86ISD::CMOV";
7020 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007021 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007022 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7023 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007024 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007025 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007026 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007027 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007028 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007029 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7030 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007031 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007032 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007033 case X86ISD::FMAX: return "X86ISD::FMAX";
7034 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007035 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7036 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007037 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007038 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007039 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007040 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007041 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007042 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7043 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007044 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7045 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7046 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7047 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7048 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7049 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007050 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7051 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007052 case X86ISD::VSHL: return "X86ISD::VSHL";
7053 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007054 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7055 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7056 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7057 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7058 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7059 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7060 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7061 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7062 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7063 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007064 case X86ISD::ADD: return "X86ISD::ADD";
7065 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007066 case X86ISD::SMUL: return "X86ISD::SMUL";
7067 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007068 case X86ISD::INC: return "X86ISD::INC";
7069 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00007070 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007071 case X86ISD::PTEST: return "X86ISD::PTEST";
Evan Cheng72261582005-12-20 06:22:03 +00007072 }
7073}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007074
Chris Lattnerc9addb72007-03-30 23:15:24 +00007075// isLegalAddressingMode - Return true if the addressing mode represented
7076// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007077bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007078 const Type *Ty) const {
7079 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00007080
Chris Lattnerc9addb72007-03-30 23:15:24 +00007081 // X86 allows a sign-extended 32-bit immediate field as a displacement.
7082 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
7083 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007084
Chris Lattnerc9addb72007-03-30 23:15:24 +00007085 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007086 unsigned GVFlags =
7087 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
7088
7089 // If a reference to this global requires an extra load, we can't fold it.
7090 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007091 return false;
Chris Lattnerdfed4132009-07-10 07:38:24 +00007092
7093 // If BaseGV requires a register for the PIC base, we cannot also have a
7094 // BaseReg specified.
7095 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007096 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007097
7098 // X86-64 only supports addr of globals in small code model.
7099 if (Subtarget->is64Bit()) {
7100 if (getTargetMachine().getCodeModel() != CodeModel::Small)
7101 return false;
7102 // If lower 4G is not available, then we must use rip-relative addressing.
7103 if (AM.BaseOffs || AM.Scale > 1)
7104 return false;
7105 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00007106 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007107
Chris Lattnerc9addb72007-03-30 23:15:24 +00007108 switch (AM.Scale) {
7109 case 0:
7110 case 1:
7111 case 2:
7112 case 4:
7113 case 8:
7114 // These scales always work.
7115 break;
7116 case 3:
7117 case 5:
7118 case 9:
7119 // These scales are formed with basereg+scalereg. Only accept if there is
7120 // no basereg yet.
7121 if (AM.HasBaseReg)
7122 return false;
7123 break;
7124 default: // Other stuff never works.
7125 return false;
7126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007127
Chris Lattnerc9addb72007-03-30 23:15:24 +00007128 return true;
7129}
7130
7131
Evan Cheng2bd122c2007-10-26 01:56:11 +00007132bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7133 if (!Ty1->isInteger() || !Ty2->isInteger())
7134 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007135 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7136 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007137 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007138 return false;
7139 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007140}
7141
Duncan Sands83ec4b62008-06-06 12:08:01 +00007142bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
7143 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007144 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007145 unsigned NumBits1 = VT1.getSizeInBits();
7146 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007147 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007148 return false;
7149 return Subtarget->is64Bit() || NumBits1 < 64;
7150}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007151
Dan Gohman97121ba2009-04-08 00:15:30 +00007152bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007153 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00007154 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
7155}
7156
7157bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) 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 VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
7160}
7161
Evan Cheng8b944d32009-05-28 00:35:15 +00007162bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
7163 // i16 instructions are longer (0x66 prefix) and potentially slower.
7164 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
7165}
7166
Evan Cheng60c07e12006-07-05 22:17:51 +00007167/// isShuffleMaskLegal - Targets can use this to indicate that they only
7168/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7169/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7170/// are assumed to be legal.
7171bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007172X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
7173 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007174 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007175 if (VT.getSizeInBits() == 64)
7176 return false;
7177
7178 // FIXME: pshufb, blends, palignr, shifts.
7179 return (VT.getVectorNumElements() == 2 ||
7180 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7181 isMOVLMask(M, VT) ||
7182 isSHUFPMask(M, VT) ||
7183 isPSHUFDMask(M, VT) ||
7184 isPSHUFHWMask(M, VT) ||
7185 isPSHUFLWMask(M, VT) ||
7186 isUNPCKLMask(M, VT) ||
7187 isUNPCKHMask(M, VT) ||
7188 isUNPCKL_v_undef_Mask(M, VT) ||
7189 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007190}
7191
Dan Gohman7d8143f2008-04-09 20:09:42 +00007192bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007193X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00007194 MVT VT) const {
7195 unsigned NumElts = VT.getVectorNumElements();
7196 // FIXME: This collection of masks seems suspect.
7197 if (NumElts == 2)
7198 return true;
7199 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7200 return (isMOVLMask(Mask, VT) ||
7201 isCommutedMOVLMask(Mask, VT, true) ||
7202 isSHUFPMask(Mask, VT) ||
7203 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007204 }
7205 return false;
7206}
7207
7208//===----------------------------------------------------------------------===//
7209// X86 Scheduler Hooks
7210//===----------------------------------------------------------------------===//
7211
Mon P Wang63307c32008-05-05 19:05:59 +00007212// private utility function
7213MachineBasicBlock *
7214X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7215 MachineBasicBlock *MBB,
7216 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007217 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007218 unsigned LoadOpc,
7219 unsigned CXchgOpc,
7220 unsigned copyOpc,
7221 unsigned notOpc,
7222 unsigned EAXreg,
7223 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007224 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007225 // For the atomic bitwise operator, we generate
7226 // thisMBB:
7227 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007228 // ld t1 = [bitinstr.addr]
7229 // op t2 = t1, [bitinstr.val]
7230 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007231 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7232 // bz newMBB
7233 // fallthrough -->nextMBB
7234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7235 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007236 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007237 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007238
Mon P Wang63307c32008-05-05 19:05:59 +00007239 /// First build the CFG
7240 MachineFunction *F = MBB->getParent();
7241 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007242 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7243 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7244 F->insert(MBBIter, newMBB);
7245 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007246
Mon P Wang63307c32008-05-05 19:05:59 +00007247 // Move all successors to thisMBB to nextMBB
7248 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007249
Mon P Wang63307c32008-05-05 19:05:59 +00007250 // Update thisMBB to fall through to newMBB
7251 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007252
Mon P Wang63307c32008-05-05 19:05:59 +00007253 // newMBB jumps to itself and fall through to nextMBB
7254 newMBB->addSuccessor(nextMBB);
7255 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007256
Mon P Wang63307c32008-05-05 19:05:59 +00007257 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007258 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007259 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007260 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007261 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007262 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007263 int numArgs = bInstr->getNumOperands() - 1;
7264 for (int i=0; i < numArgs; ++i)
7265 argOpers[i] = &bInstr->getOperand(i+1);
7266
7267 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007268 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7269 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007270
Dale Johannesen140be2d2008-08-19 18:47:28 +00007271 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007272 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007273 for (int i=0; i <= lastAddrIndx; ++i)
7274 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007275
Dale Johannesen140be2d2008-08-19 18:47:28 +00007276 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007277 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007278 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007279 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007280 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007281 tt = t1;
7282
Dale Johannesen140be2d2008-08-19 18:47:28 +00007283 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007284 assert((argOpers[valArgIndx]->isReg() ||
7285 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007286 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007287 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007288 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007289 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007290 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007291 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007292 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007293
Dale Johannesene4d209d2009-02-03 20:21:25 +00007294 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007295 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007296
Dale Johannesene4d209d2009-02-03 20:21:25 +00007297 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007298 for (int i=0; i <= lastAddrIndx; ++i)
7299 (*MIB).addOperand(*argOpers[i]);
7300 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007301 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7302 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7303
Dale Johannesene4d209d2009-02-03 20:21:25 +00007304 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007305 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007306
Mon P Wang63307c32008-05-05 19:05:59 +00007307 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007308 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007309
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007310 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007311 return nextMBB;
7312}
7313
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007314// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007315MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007316X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7317 MachineBasicBlock *MBB,
7318 unsigned regOpcL,
7319 unsigned regOpcH,
7320 unsigned immOpcL,
7321 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007322 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007323 // For the atomic bitwise operator, we generate
7324 // thisMBB (instructions are in pairs, except cmpxchg8b)
7325 // ld t1,t2 = [bitinstr.addr]
7326 // newMBB:
7327 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7328 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007329 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007330 // mov ECX, EBX <- t5, t6
7331 // mov EAX, EDX <- t1, t2
7332 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7333 // mov t3, t4 <- EAX, EDX
7334 // bz newMBB
7335 // result in out1, out2
7336 // fallthrough -->nextMBB
7337
7338 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7339 const unsigned LoadOpc = X86::MOV32rm;
7340 const unsigned copyOpc = X86::MOV32rr;
7341 const unsigned NotOpc = X86::NOT32r;
7342 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7343 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7344 MachineFunction::iterator MBBIter = MBB;
7345 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007346
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007347 /// First build the CFG
7348 MachineFunction *F = MBB->getParent();
7349 MachineBasicBlock *thisMBB = MBB;
7350 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7351 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7352 F->insert(MBBIter, newMBB);
7353 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007354
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007355 // Move all successors to thisMBB to nextMBB
7356 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007357
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007358 // Update thisMBB to fall through to newMBB
7359 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007360
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007361 // newMBB jumps to itself and fall through to nextMBB
7362 newMBB->addSuccessor(nextMBB);
7363 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007364
Dale Johannesene4d209d2009-02-03 20:21:25 +00007365 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007366 // Insert instructions into newMBB based on incoming instruction
7367 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007368 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007369 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007370 MachineOperand& dest1Oper = bInstr->getOperand(0);
7371 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007372 MachineOperand* argOpers[2 + X86AddrNumOperands];
7373 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007374 argOpers[i] = &bInstr->getOperand(i+2);
7375
7376 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007377 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007378
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007379 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007380 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007381 for (int i=0; i <= lastAddrIndx; ++i)
7382 (*MIB).addOperand(*argOpers[i]);
7383 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007384 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007385 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007386 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007387 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007388 MachineOperand newOp3 = *(argOpers[3]);
7389 if (newOp3.isImm())
7390 newOp3.setImm(newOp3.getImm()+4);
7391 else
7392 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007393 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007394 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007395
7396 // t3/4 are defined later, at the bottom of the loop
7397 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7398 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007399 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007400 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007401 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007402 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7403
7404 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7405 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007406 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007407 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7408 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007409 } else {
7410 tt1 = t1;
7411 tt2 = t2;
7412 }
7413
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007414 int valArgIndx = lastAddrIndx + 1;
7415 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007416 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007417 "invalid operand");
7418 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7419 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007420 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007421 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007422 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007423 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007424 if (regOpcL != X86::MOV32rr)
7425 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007426 (*MIB).addOperand(*argOpers[valArgIndx]);
7427 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007428 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007429 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007430 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007431 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007432 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007433 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007434 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007435 if (regOpcH != X86::MOV32rr)
7436 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007437 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007438
Dale Johannesene4d209d2009-02-03 20:21:25 +00007439 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007440 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007441 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007442 MIB.addReg(t2);
7443
Dale Johannesene4d209d2009-02-03 20:21:25 +00007444 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007445 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007446 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007447 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007448
Dale Johannesene4d209d2009-02-03 20:21:25 +00007449 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007450 for (int i=0; i <= lastAddrIndx; ++i)
7451 (*MIB).addOperand(*argOpers[i]);
7452
7453 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7454 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7455
Dale Johannesene4d209d2009-02-03 20:21:25 +00007456 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007457 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007458 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007459 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007460
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007461 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007462 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007463
7464 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7465 return nextMBB;
7466}
7467
7468// private utility function
7469MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007470X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7471 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007472 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007473 // For the atomic min/max operator, we generate
7474 // thisMBB:
7475 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007476 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007477 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007478 // cmp t1, t2
7479 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007480 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007481 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7482 // bz newMBB
7483 // fallthrough -->nextMBB
7484 //
7485 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7486 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007487 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007488 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007489
Mon P Wang63307c32008-05-05 19:05:59 +00007490 /// First build the CFG
7491 MachineFunction *F = MBB->getParent();
7492 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007493 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7494 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7495 F->insert(MBBIter, newMBB);
7496 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007497
Mon P Wang63307c32008-05-05 19:05:59 +00007498 // Move all successors to thisMBB to nextMBB
7499 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007500
Mon P Wang63307c32008-05-05 19:05:59 +00007501 // Update thisMBB to fall through to newMBB
7502 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007503
Mon P Wang63307c32008-05-05 19:05:59 +00007504 // newMBB jumps to newMBB and fall through to nextMBB
7505 newMBB->addSuccessor(nextMBB);
7506 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007507
Dale Johannesene4d209d2009-02-03 20:21:25 +00007508 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007509 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007510 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007511 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007512 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007513 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007514 int numArgs = mInstr->getNumOperands() - 1;
7515 for (int i=0; i < numArgs; ++i)
7516 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007517
Mon P Wang63307c32008-05-05 19:05:59 +00007518 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007519 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7520 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007521
Mon P Wangab3e7472008-05-05 22:56:23 +00007522 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007523 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007524 for (int i=0; i <= lastAddrIndx; ++i)
7525 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007526
Mon P Wang63307c32008-05-05 19:05:59 +00007527 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007528 assert((argOpers[valArgIndx]->isReg() ||
7529 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007530 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007531
7532 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007533 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007534 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007535 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007536 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007537 (*MIB).addOperand(*argOpers[valArgIndx]);
7538
Dale Johannesene4d209d2009-02-03 20:21:25 +00007539 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007540 MIB.addReg(t1);
7541
Dale Johannesene4d209d2009-02-03 20:21:25 +00007542 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007543 MIB.addReg(t1);
7544 MIB.addReg(t2);
7545
7546 // Generate movc
7547 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007548 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007549 MIB.addReg(t2);
7550 MIB.addReg(t1);
7551
7552 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007553 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007554 for (int i=0; i <= lastAddrIndx; ++i)
7555 (*MIB).addOperand(*argOpers[i]);
7556 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007557 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7558 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007559
Dale Johannesene4d209d2009-02-03 20:21:25 +00007560 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007561 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007562
Mon P Wang63307c32008-05-05 19:05:59 +00007563 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007564 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007565
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007566 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007567 return nextMBB;
7568}
7569
7570
Evan Cheng60c07e12006-07-05 22:17:51 +00007571MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007572X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007573 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007574 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007575 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007576 switch (MI->getOpcode()) {
7577 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007578 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007579 case X86::CMOV_FR32:
7580 case X86::CMOV_FR64:
7581 case X86::CMOV_V4F32:
7582 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007583 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007584 // To "insert" a SELECT_CC instruction, we actually have to insert the
7585 // diamond control-flow pattern. The incoming instruction knows the
7586 // destination vreg to set, the condition code register to branch on, the
7587 // true/false values to select between, and a branch opcode to use.
7588 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007589 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007590 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007591
Evan Cheng60c07e12006-07-05 22:17:51 +00007592 // thisMBB:
7593 // ...
7594 // TrueVal = ...
7595 // cmpTY ccX, r1, r2
7596 // bCC copy1MBB
7597 // fallthrough --> copy0MBB
7598 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007599 MachineFunction *F = BB->getParent();
7600 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7601 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007602 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007603 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007604 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007605 F->insert(It, copy0MBB);
7606 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007607 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007608 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007609 sinkMBB->transferSuccessors(BB);
7610
7611 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007612 BB->addSuccessor(copy0MBB);
7613 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007614
Evan Cheng60c07e12006-07-05 22:17:51 +00007615 // copy0MBB:
7616 // %FalseValue = ...
7617 // # fallthrough to sinkMBB
7618 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007619
Evan Cheng60c07e12006-07-05 22:17:51 +00007620 // Update machine-CFG edges
7621 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007622
Evan Cheng60c07e12006-07-05 22:17:51 +00007623 // sinkMBB:
7624 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7625 // ...
7626 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007627 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007628 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7629 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7630
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007631 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007632 return BB;
7633 }
7634
Dale Johannesen849f2142007-07-03 00:53:03 +00007635 case X86::FP32_TO_INT16_IN_MEM:
7636 case X86::FP32_TO_INT32_IN_MEM:
7637 case X86::FP32_TO_INT64_IN_MEM:
7638 case X86::FP64_TO_INT16_IN_MEM:
7639 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007640 case X86::FP64_TO_INT64_IN_MEM:
7641 case X86::FP80_TO_INT16_IN_MEM:
7642 case X86::FP80_TO_INT32_IN_MEM:
7643 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007644 // Change the floating point control register to use "round towards zero"
7645 // mode when truncating to an integer value.
7646 MachineFunction *F = BB->getParent();
7647 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007648 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007649
7650 // Load the old value of the high byte of the control word...
7651 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007652 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007653 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007654 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007655
7656 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007657 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007658 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007659
7660 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007661 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007662
7663 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007664 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007665 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007666
7667 // Get the X86 opcode to use.
7668 unsigned Opc;
7669 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007670 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007671 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7672 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7673 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7674 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7675 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7676 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007677 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7678 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7679 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007680 }
7681
7682 X86AddressMode AM;
7683 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007684 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007685 AM.BaseType = X86AddressMode::RegBase;
7686 AM.Base.Reg = Op.getReg();
7687 } else {
7688 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007689 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007690 }
7691 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007692 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007693 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007694 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007695 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007696 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007697 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007698 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007699 AM.GV = Op.getGlobal();
7700 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007701 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007702 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007703 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007704 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007705
7706 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007707 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007708
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007709 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007710 return BB;
7711 }
Mon P Wang63307c32008-05-05 19:05:59 +00007712 case X86::ATOMAND32:
7713 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007714 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007715 X86::LCMPXCHG32, X86::MOV32rr,
7716 X86::NOT32r, X86::EAX,
7717 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007718 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007719 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7720 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007721 X86::LCMPXCHG32, X86::MOV32rr,
7722 X86::NOT32r, X86::EAX,
7723 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007724 case X86::ATOMXOR32:
7725 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007726 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007727 X86::LCMPXCHG32, X86::MOV32rr,
7728 X86::NOT32r, X86::EAX,
7729 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007730 case X86::ATOMNAND32:
7731 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007732 X86::AND32ri, X86::MOV32rm,
7733 X86::LCMPXCHG32, X86::MOV32rr,
7734 X86::NOT32r, X86::EAX,
7735 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007736 case X86::ATOMMIN32:
7737 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7738 case X86::ATOMMAX32:
7739 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7740 case X86::ATOMUMIN32:
7741 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7742 case X86::ATOMUMAX32:
7743 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007744
7745 case X86::ATOMAND16:
7746 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7747 X86::AND16ri, X86::MOV16rm,
7748 X86::LCMPXCHG16, X86::MOV16rr,
7749 X86::NOT16r, X86::AX,
7750 X86::GR16RegisterClass);
7751 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007752 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007753 X86::OR16ri, X86::MOV16rm,
7754 X86::LCMPXCHG16, X86::MOV16rr,
7755 X86::NOT16r, X86::AX,
7756 X86::GR16RegisterClass);
7757 case X86::ATOMXOR16:
7758 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7759 X86::XOR16ri, X86::MOV16rm,
7760 X86::LCMPXCHG16, X86::MOV16rr,
7761 X86::NOT16r, X86::AX,
7762 X86::GR16RegisterClass);
7763 case X86::ATOMNAND16:
7764 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7765 X86::AND16ri, X86::MOV16rm,
7766 X86::LCMPXCHG16, X86::MOV16rr,
7767 X86::NOT16r, X86::AX,
7768 X86::GR16RegisterClass, true);
7769 case X86::ATOMMIN16:
7770 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7771 case X86::ATOMMAX16:
7772 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7773 case X86::ATOMUMIN16:
7774 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7775 case X86::ATOMUMAX16:
7776 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7777
7778 case X86::ATOMAND8:
7779 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7780 X86::AND8ri, X86::MOV8rm,
7781 X86::LCMPXCHG8, X86::MOV8rr,
7782 X86::NOT8r, X86::AL,
7783 X86::GR8RegisterClass);
7784 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007785 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007786 X86::OR8ri, X86::MOV8rm,
7787 X86::LCMPXCHG8, X86::MOV8rr,
7788 X86::NOT8r, X86::AL,
7789 X86::GR8RegisterClass);
7790 case X86::ATOMXOR8:
7791 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7792 X86::XOR8ri, X86::MOV8rm,
7793 X86::LCMPXCHG8, X86::MOV8rr,
7794 X86::NOT8r, X86::AL,
7795 X86::GR8RegisterClass);
7796 case X86::ATOMNAND8:
7797 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7798 X86::AND8ri, X86::MOV8rm,
7799 X86::LCMPXCHG8, X86::MOV8rr,
7800 X86::NOT8r, X86::AL,
7801 X86::GR8RegisterClass, true);
7802 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007803 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007804 case X86::ATOMAND64:
7805 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007806 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007807 X86::LCMPXCHG64, X86::MOV64rr,
7808 X86::NOT64r, X86::RAX,
7809 X86::GR64RegisterClass);
7810 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007811 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7812 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007813 X86::LCMPXCHG64, X86::MOV64rr,
7814 X86::NOT64r, X86::RAX,
7815 X86::GR64RegisterClass);
7816 case X86::ATOMXOR64:
7817 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007818 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007819 X86::LCMPXCHG64, X86::MOV64rr,
7820 X86::NOT64r, X86::RAX,
7821 X86::GR64RegisterClass);
7822 case X86::ATOMNAND64:
7823 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7824 X86::AND64ri32, X86::MOV64rm,
7825 X86::LCMPXCHG64, X86::MOV64rr,
7826 X86::NOT64r, X86::RAX,
7827 X86::GR64RegisterClass, true);
7828 case X86::ATOMMIN64:
7829 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7830 case X86::ATOMMAX64:
7831 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7832 case X86::ATOMUMIN64:
7833 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7834 case X86::ATOMUMAX64:
7835 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007836
7837 // This group does 64-bit operations on a 32-bit host.
7838 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007839 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007840 X86::AND32rr, X86::AND32rr,
7841 X86::AND32ri, X86::AND32ri,
7842 false);
7843 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007844 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007845 X86::OR32rr, X86::OR32rr,
7846 X86::OR32ri, X86::OR32ri,
7847 false);
7848 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007849 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007850 X86::XOR32rr, X86::XOR32rr,
7851 X86::XOR32ri, X86::XOR32ri,
7852 false);
7853 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007854 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007855 X86::AND32rr, X86::AND32rr,
7856 X86::AND32ri, X86::AND32ri,
7857 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007858 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007859 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007860 X86::ADD32rr, X86::ADC32rr,
7861 X86::ADD32ri, X86::ADC32ri,
7862 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007863 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007864 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007865 X86::SUB32rr, X86::SBB32rr,
7866 X86::SUB32ri, X86::SBB32ri,
7867 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007868 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007869 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007870 X86::MOV32rr, X86::MOV32rr,
7871 X86::MOV32ri, X86::MOV32ri,
7872 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007873 }
7874}
7875
7876//===----------------------------------------------------------------------===//
7877// X86 Optimization Hooks
7878//===----------------------------------------------------------------------===//
7879
Dan Gohman475871a2008-07-27 21:46:04 +00007880void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007881 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007882 APInt &KnownZero,
7883 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007884 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007885 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007886 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007887 assert((Opc >= ISD::BUILTIN_OP_END ||
7888 Opc == ISD::INTRINSIC_WO_CHAIN ||
7889 Opc == ISD::INTRINSIC_W_CHAIN ||
7890 Opc == ISD::INTRINSIC_VOID) &&
7891 "Should use MaskedValueIsZero if you don't know whether Op"
7892 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007893
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007894 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007895 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007896 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007897 case X86ISD::ADD:
7898 case X86ISD::SUB:
7899 case X86ISD::SMUL:
7900 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007901 case X86ISD::INC:
7902 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007903 // These nodes' second result is a boolean.
7904 if (Op.getResNo() == 0)
7905 break;
7906 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007907 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007908 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7909 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007910 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007911 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007912}
Chris Lattner259e97c2006-01-31 19:43:35 +00007913
Evan Cheng206ee9d2006-07-07 08:33:52 +00007914/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007915/// node is a GlobalAddress + offset.
7916bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7917 GlobalValue* &GA, int64_t &Offset) const{
7918 if (N->getOpcode() == X86ISD::Wrapper) {
7919 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007920 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007921 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007922 return true;
7923 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007924 }
Evan Chengad4196b2008-05-12 19:56:52 +00007925 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007926}
7927
Evan Chengad4196b2008-05-12 19:56:52 +00007928static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7929 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007930 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007931 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007932 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007933 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007934 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007935 return false;
7936}
7937
Nate Begeman9008ca62009-04-27 18:41:29 +00007938static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Eli Friedman7a5e5552009-06-07 06:52:44 +00007939 MVT EVT, LoadSDNode *&LDBase,
7940 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00007941 SelectionDAG &DAG, MachineFrameInfo *MFI,
7942 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007943 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00007944 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007945 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007946 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007947 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00007948 return false;
7949 continue;
7950 }
7951
Dan Gohman475871a2008-07-27 21:46:04 +00007952 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007953 if (!Elt.getNode() ||
7954 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007955 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007956 if (!LDBase) {
7957 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00007958 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007959 LDBase = cast<LoadSDNode>(Elt.getNode());
7960 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007961 continue;
7962 }
7963 if (Elt.getOpcode() == ISD::UNDEF)
7964 continue;
7965
Nate Begemanabc01992009-06-05 21:37:30 +00007966 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Nate Begemanabc01992009-06-05 21:37:30 +00007967 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007968 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007969 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007970 }
7971 return true;
7972}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007973
7974/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7975/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7976/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007977/// order. In the case of v2i64, it will see if it can rewrite the
7978/// shuffle to be an appropriate build vector so it can take advantage of
7979// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007980static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007981 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007982 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007983 MVT VT = N->getValueType(0);
7984 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007985 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7986 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007987
Eli Friedman7a5e5552009-06-07 06:52:44 +00007988 if (VT.getSizeInBits() != 128)
7989 return SDValue();
7990
Mon P Wang1e955802009-04-03 02:43:30 +00007991 // Try to combine a vector_shuffle into a 128-bit load.
7992 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00007993 LoadSDNode *LD = NULL;
7994 unsigned LastLoadedElt;
7995 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG,
7996 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007997 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007998
Eli Friedman7a5e5552009-06-07 06:52:44 +00007999 if (LastLoadedElt == NumElems - 1) {
8000 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI))
8001 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8002 LD->getSrcValue(), LD->getSrcValueOffset(),
8003 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008004 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008005 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008006 LD->isVolatile(), LD->getAlignment());
8007 } else if (NumElems == 4 && LastLoadedElt == 1) {
8008 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008009 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8010 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008011 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8012 }
8013 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008014}
Evan Chengd880b972008-05-09 21:53:03 +00008015
Chris Lattner83e6c992006-10-04 06:57:07 +00008016/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008017static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008018 const X86Subtarget *Subtarget) {
8019 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008020 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008021 // Get the LHS/RHS of the select.
8022 SDValue LHS = N->getOperand(1);
8023 SDValue RHS = N->getOperand(2);
8024
Chris Lattner83e6c992006-10-04 06:57:07 +00008025 // If we have SSE[12] support, try to form min/max nodes.
8026 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008027 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
8028 Cond.getOpcode() == ISD::SETCC) {
8029 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008030
Chris Lattner47b4ce82009-03-11 05:48:52 +00008031 unsigned Opcode = 0;
8032 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8033 switch (CC) {
8034 default: break;
8035 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
8036 case ISD::SETULE:
8037 case ISD::SETLE:
8038 if (!UnsafeFPMath) break;
8039 // FALL THROUGH.
8040 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
8041 case ISD::SETLT:
8042 Opcode = X86ISD::FMIN;
8043 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008044
Chris Lattner47b4ce82009-03-11 05:48:52 +00008045 case ISD::SETOGT: // (X > Y) ? X : Y -> max
8046 case ISD::SETUGT:
8047 case ISD::SETGT:
8048 if (!UnsafeFPMath) break;
8049 // FALL THROUGH.
8050 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
8051 case ISD::SETGE:
8052 Opcode = X86ISD::FMAX;
8053 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008054 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008055 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8056 switch (CC) {
8057 default: break;
8058 case ISD::SETOGT: // (X > Y) ? Y : X -> min
8059 case ISD::SETUGT:
8060 case ISD::SETGT:
8061 if (!UnsafeFPMath) break;
8062 // FALL THROUGH.
8063 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
8064 case ISD::SETGE:
8065 Opcode = X86ISD::FMIN;
8066 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008067
Chris Lattner47b4ce82009-03-11 05:48:52 +00008068 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
8069 case ISD::SETULE:
8070 case ISD::SETLE:
8071 if (!UnsafeFPMath) break;
8072 // FALL THROUGH.
8073 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
8074 case ISD::SETLT:
8075 Opcode = X86ISD::FMAX;
8076 break;
8077 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008078 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008079
Chris Lattner47b4ce82009-03-11 05:48:52 +00008080 if (Opcode)
8081 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008082 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008083
Chris Lattnerd1980a52009-03-12 06:52:53 +00008084 // If this is a select between two integer constants, try to do some
8085 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008086 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8087 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008088 // Don't do this for crazy integer types.
8089 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8090 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008091 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008092 bool NeedsCondInvert = false;
8093
Chris Lattnercee56e72009-03-13 05:53:31 +00008094 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008095 // Efficiently invertible.
8096 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8097 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8098 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8099 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008100 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008101 }
8102
8103 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008104 if (FalseC->getAPIntValue() == 0 &&
8105 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008106 if (NeedsCondInvert) // Invert the condition if needed.
8107 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8108 DAG.getConstant(1, Cond.getValueType()));
8109
8110 // Zero extend the condition if needed.
8111 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
8112
Chris Lattnercee56e72009-03-13 05:53:31 +00008113 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008114 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
8115 DAG.getConstant(ShAmt, MVT::i8));
8116 }
Chris Lattner97a29a52009-03-13 05:22:11 +00008117
8118 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008119 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008120 if (NeedsCondInvert) // Invert the condition if needed.
8121 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8122 DAG.getConstant(1, Cond.getValueType()));
8123
8124 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008125 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8126 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008127 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008128 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008129 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008130
8131 // Optimize cases that will turn into an LEA instruction. This requires
8132 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8133 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8134 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8135 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8136
8137 bool isFastMultiplier = false;
8138 if (Diff < 10) {
8139 switch ((unsigned char)Diff) {
8140 default: break;
8141 case 1: // result = add base, cond
8142 case 2: // result = lea base( , cond*2)
8143 case 3: // result = lea base(cond, cond*2)
8144 case 4: // result = lea base( , cond*4)
8145 case 5: // result = lea base(cond, cond*4)
8146 case 8: // result = lea base( , cond*8)
8147 case 9: // result = lea base(cond, cond*8)
8148 isFastMultiplier = true;
8149 break;
8150 }
8151 }
8152
8153 if (isFastMultiplier) {
8154 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8155 if (NeedsCondInvert) // Invert the condition if needed.
8156 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8157 DAG.getConstant(1, Cond.getValueType()));
8158
8159 // Zero extend the condition if needed.
8160 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8161 Cond);
8162 // Scale the condition by the difference.
8163 if (Diff != 1)
8164 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8165 DAG.getConstant(Diff, Cond.getValueType()));
8166
8167 // Add the base if non-zero.
8168 if (FalseC->getAPIntValue() != 0)
8169 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8170 SDValue(FalseC, 0));
8171 return Cond;
8172 }
8173 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008174 }
8175 }
8176
Dan Gohman475871a2008-07-27 21:46:04 +00008177 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008178}
8179
Chris Lattnerd1980a52009-03-12 06:52:53 +00008180/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8181static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8182 TargetLowering::DAGCombinerInfo &DCI) {
8183 DebugLoc DL = N->getDebugLoc();
8184
8185 // If the flag operand isn't dead, don't touch this CMOV.
8186 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8187 return SDValue();
8188
8189 // If this is a select between two integer constants, try to do some
8190 // optimizations. Note that the operands are ordered the opposite of SELECT
8191 // operands.
8192 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8193 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8194 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8195 // larger than FalseC (the false value).
8196 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8197
8198 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8199 CC = X86::GetOppositeBranchCondition(CC);
8200 std::swap(TrueC, FalseC);
8201 }
8202
8203 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008204 // This is efficient for any integer data type (including i8/i16) and
8205 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008206 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8207 SDValue Cond = N->getOperand(3);
8208 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8209 DAG.getConstant(CC, MVT::i8), Cond);
8210
8211 // Zero extend the condition if needed.
8212 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8213
8214 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8215 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8216 DAG.getConstant(ShAmt, MVT::i8));
8217 if (N->getNumValues() == 2) // Dead flag value?
8218 return DCI.CombineTo(N, Cond, SDValue());
8219 return Cond;
8220 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008221
8222 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8223 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008224 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8225 SDValue Cond = N->getOperand(3);
8226 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8227 DAG.getConstant(CC, MVT::i8), Cond);
8228
8229 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008230 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8231 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008232 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8233 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008234
Chris Lattner97a29a52009-03-13 05:22:11 +00008235 if (N->getNumValues() == 2) // Dead flag value?
8236 return DCI.CombineTo(N, Cond, SDValue());
8237 return Cond;
8238 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008239
8240 // Optimize cases that will turn into an LEA instruction. This requires
8241 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8242 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8243 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8244 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8245
8246 bool isFastMultiplier = false;
8247 if (Diff < 10) {
8248 switch ((unsigned char)Diff) {
8249 default: break;
8250 case 1: // result = add base, cond
8251 case 2: // result = lea base( , cond*2)
8252 case 3: // result = lea base(cond, cond*2)
8253 case 4: // result = lea base( , cond*4)
8254 case 5: // result = lea base(cond, cond*4)
8255 case 8: // result = lea base( , cond*8)
8256 case 9: // result = lea base(cond, cond*8)
8257 isFastMultiplier = true;
8258 break;
8259 }
8260 }
8261
8262 if (isFastMultiplier) {
8263 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8264 SDValue Cond = N->getOperand(3);
8265 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8266 DAG.getConstant(CC, MVT::i8), Cond);
8267 // Zero extend the condition if needed.
8268 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8269 Cond);
8270 // Scale the condition by the difference.
8271 if (Diff != 1)
8272 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8273 DAG.getConstant(Diff, Cond.getValueType()));
8274
8275 // Add the base if non-zero.
8276 if (FalseC->getAPIntValue() != 0)
8277 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8278 SDValue(FalseC, 0));
8279 if (N->getNumValues() == 2) // Dead flag value?
8280 return DCI.CombineTo(N, Cond, SDValue());
8281 return Cond;
8282 }
8283 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008284 }
8285 }
8286 return SDValue();
8287}
8288
8289
Evan Cheng0b0cd912009-03-28 05:57:29 +00008290/// PerformMulCombine - Optimize a single multiply with constant into two
8291/// in order to implement it with two cheaper instructions, e.g.
8292/// LEA + SHL, LEA + LEA.
8293static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8294 TargetLowering::DAGCombinerInfo &DCI) {
8295 if (DAG.getMachineFunction().
8296 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8297 return SDValue();
8298
8299 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8300 return SDValue();
8301
8302 MVT VT = N->getValueType(0);
8303 if (VT != MVT::i64)
8304 return SDValue();
8305
8306 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8307 if (!C)
8308 return SDValue();
8309 uint64_t MulAmt = C->getZExtValue();
8310 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8311 return SDValue();
8312
8313 uint64_t MulAmt1 = 0;
8314 uint64_t MulAmt2 = 0;
8315 if ((MulAmt % 9) == 0) {
8316 MulAmt1 = 9;
8317 MulAmt2 = MulAmt / 9;
8318 } else if ((MulAmt % 5) == 0) {
8319 MulAmt1 = 5;
8320 MulAmt2 = MulAmt / 5;
8321 } else if ((MulAmt % 3) == 0) {
8322 MulAmt1 = 3;
8323 MulAmt2 = MulAmt / 3;
8324 }
8325 if (MulAmt2 &&
8326 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8327 DebugLoc DL = N->getDebugLoc();
8328
8329 if (isPowerOf2_64(MulAmt2) &&
8330 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8331 // If second multiplifer is pow2, issue it first. We want the multiply by
8332 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8333 // is an add.
8334 std::swap(MulAmt1, MulAmt2);
8335
8336 SDValue NewMul;
8337 if (isPowerOf2_64(MulAmt1))
8338 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8339 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8340 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008341 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008342 DAG.getConstant(MulAmt1, VT));
8343
8344 if (isPowerOf2_64(MulAmt2))
8345 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8346 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8347 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008348 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008349 DAG.getConstant(MulAmt2, VT));
8350
8351 // Do not add new nodes to DAG combiner worklist.
8352 DCI.CombineTo(N, NewMul, false);
8353 }
8354 return SDValue();
8355}
8356
8357
Nate Begeman740ab032009-01-26 00:52:55 +00008358/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8359/// when possible.
8360static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8361 const X86Subtarget *Subtarget) {
8362 // On X86 with SSE2 support, we can transform this to a vector shift if
8363 // all elements are shifted by the same amount. We can't do this in legalize
8364 // because the a constant vector is typically transformed to a constant pool
8365 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008366 if (!Subtarget->hasSSE2())
8367 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008368
Nate Begeman740ab032009-01-26 00:52:55 +00008369 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008370 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8371 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008372
Mon P Wang3becd092009-01-28 08:12:05 +00008373 SDValue ShAmtOp = N->getOperand(1);
8374 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008375 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008376 SDValue BaseShAmt;
8377 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8378 unsigned NumElts = VT.getVectorNumElements();
8379 unsigned i = 0;
8380 for (; i != NumElts; ++i) {
8381 SDValue Arg = ShAmtOp.getOperand(i);
8382 if (Arg.getOpcode() == ISD::UNDEF) continue;
8383 BaseShAmt = Arg;
8384 break;
8385 }
8386 for (; i != NumElts; ++i) {
8387 SDValue Arg = ShAmtOp.getOperand(i);
8388 if (Arg.getOpcode() == ISD::UNDEF) continue;
8389 if (Arg != BaseShAmt) {
8390 return SDValue();
8391 }
8392 }
8393 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008394 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8395 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8396 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008397 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008398 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008399
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008400 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008401 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008402 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008403 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008404
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008405 // The shift amount is identical so we can do a vector shift.
8406 SDValue ValOp = N->getOperand(0);
8407 switch (N->getOpcode()) {
8408 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008409 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008410 break;
8411 case ISD::SHL:
8412 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008414 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8415 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008416 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008418 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8419 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008420 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008421 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008422 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8423 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008424 break;
8425 case ISD::SRA:
8426 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008427 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008428 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8429 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008430 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008431 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008432 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8433 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008434 break;
8435 case ISD::SRL:
8436 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008437 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008438 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8439 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008440 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008442 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8443 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008444 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008445 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008446 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8447 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008448 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008449 }
8450 return SDValue();
8451}
8452
Chris Lattner149a4e52008-02-22 02:09:43 +00008453/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008454static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008455 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008456 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8457 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008458 // A preferable solution to the general problem is to figure out the right
8459 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008460
8461 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008462 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008463 MVT VT = St->getValue().getValueType();
8464 if (VT.getSizeInBits() != 64)
8465 return SDValue();
8466
Devang Patel578efa92009-06-05 21:57:13 +00008467 const Function *F = DAG.getMachineFunction().getFunction();
8468 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8469 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8470 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008471 if ((VT.isVector() ||
8472 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008473 isa<LoadSDNode>(St->getValue()) &&
8474 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8475 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008476 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008477 LoadSDNode *Ld = 0;
8478 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008479 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008480 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008481 // Must be a store of a load. We currently handle two cases: the load
8482 // is a direct child, and it's under an intervening TokenFactor. It is
8483 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008484 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008485 Ld = cast<LoadSDNode>(St->getChain());
8486 else if (St->getValue().hasOneUse() &&
8487 ChainVal->getOpcode() == ISD::TokenFactor) {
8488 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008489 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008490 TokenFactorIndex = i;
8491 Ld = cast<LoadSDNode>(St->getValue());
8492 } else
8493 Ops.push_back(ChainVal->getOperand(i));
8494 }
8495 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008496
Evan Cheng536e6672009-03-12 05:59:15 +00008497 if (!Ld || !ISD::isNormalLoad(Ld))
8498 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008499
Evan Cheng536e6672009-03-12 05:59:15 +00008500 // If this is not the MMX case, i.e. we are just turning i64 load/store
8501 // into f64 load/store, avoid the transformation if there are multiple
8502 // uses of the loaded value.
8503 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8504 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008505
Evan Cheng536e6672009-03-12 05:59:15 +00008506 DebugLoc LdDL = Ld->getDebugLoc();
8507 DebugLoc StDL = N->getDebugLoc();
8508 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8509 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8510 // pair instead.
8511 if (Subtarget->is64Bit() || F64IsLegal) {
8512 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8513 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8514 Ld->getBasePtr(), Ld->getSrcValue(),
8515 Ld->getSrcValueOffset(), Ld->isVolatile(),
8516 Ld->getAlignment());
8517 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008518 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008519 Ops.push_back(NewChain);
8520 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008521 Ops.size());
8522 }
Evan Cheng536e6672009-03-12 05:59:15 +00008523 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008524 St->getSrcValue(), St->getSrcValueOffset(),
8525 St->isVolatile(), St->getAlignment());
8526 }
Evan Cheng536e6672009-03-12 05:59:15 +00008527
8528 // Otherwise, lower to two pairs of 32-bit loads / stores.
8529 SDValue LoAddr = Ld->getBasePtr();
8530 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8531 DAG.getConstant(4, MVT::i32));
8532
8533 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8534 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8535 Ld->isVolatile(), Ld->getAlignment());
8536 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8537 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8538 Ld->isVolatile(),
8539 MinAlign(Ld->getAlignment(), 4));
8540
8541 SDValue NewChain = LoLd.getValue(1);
8542 if (TokenFactorIndex != -1) {
8543 Ops.push_back(LoLd);
8544 Ops.push_back(HiLd);
8545 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8546 Ops.size());
8547 }
8548
8549 LoAddr = St->getBasePtr();
8550 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8551 DAG.getConstant(4, MVT::i32));
8552
8553 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8554 St->getSrcValue(), St->getSrcValueOffset(),
8555 St->isVolatile(), St->getAlignment());
8556 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8557 St->getSrcValue(),
8558 St->getSrcValueOffset() + 4,
8559 St->isVolatile(),
8560 MinAlign(St->getAlignment(), 4));
8561 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008562 }
Dan Gohman475871a2008-07-27 21:46:04 +00008563 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008564}
8565
Chris Lattner6cf73262008-01-25 06:14:17 +00008566/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8567/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008568static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008569 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8570 // F[X]OR(0.0, x) -> x
8571 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008572 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8573 if (C->getValueAPF().isPosZero())
8574 return N->getOperand(1);
8575 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8576 if (C->getValueAPF().isPosZero())
8577 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008578 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008579}
8580
8581/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008582static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008583 // FAND(0.0, x) -> 0.0
8584 // FAND(x, 0.0) -> 0.0
8585 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8586 if (C->getValueAPF().isPosZero())
8587 return N->getOperand(0);
8588 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8589 if (C->getValueAPF().isPosZero())
8590 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008591 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008592}
8593
Dan Gohmane5af2d32009-01-29 01:59:02 +00008594static SDValue PerformBTCombine(SDNode *N,
8595 SelectionDAG &DAG,
8596 TargetLowering::DAGCombinerInfo &DCI) {
8597 // BT ignores high bits in the bit index operand.
8598 SDValue Op1 = N->getOperand(1);
8599 if (Op1.hasOneUse()) {
8600 unsigned BitWidth = Op1.getValueSizeInBits();
8601 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8602 APInt KnownZero, KnownOne;
8603 TargetLowering::TargetLoweringOpt TLO(DAG);
8604 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8605 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8606 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8607 DCI.CommitTargetLoweringOpt(TLO);
8608 }
8609 return SDValue();
8610}
Chris Lattner83e6c992006-10-04 06:57:07 +00008611
Eli Friedman7a5e5552009-06-07 06:52:44 +00008612static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
8613 SDValue Op = N->getOperand(0);
8614 if (Op.getOpcode() == ISD::BIT_CONVERT)
8615 Op = Op.getOperand(0);
8616 MVT VT = N->getValueType(0), OpVT = Op.getValueType();
8617 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
8618 VT.getVectorElementType().getSizeInBits() ==
8619 OpVT.getVectorElementType().getSizeInBits()) {
8620 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
8621 }
8622 return SDValue();
8623}
8624
Owen Anderson99177002009-06-29 18:04:45 +00008625// On X86 and X86-64, atomic operations are lowered to locked instructions.
8626// Locked instructions, in turn, have implicit fence semantics (all memory
8627// operations are flushed before issuing the locked instruction, and the
8628// are not buffered), so we can fold away the common pattern of
8629// fence-atomic-fence.
8630static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
8631 SDValue atomic = N->getOperand(0);
8632 switch (atomic.getOpcode()) {
8633 case ISD::ATOMIC_CMP_SWAP:
8634 case ISD::ATOMIC_SWAP:
8635 case ISD::ATOMIC_LOAD_ADD:
8636 case ISD::ATOMIC_LOAD_SUB:
8637 case ISD::ATOMIC_LOAD_AND:
8638 case ISD::ATOMIC_LOAD_OR:
8639 case ISD::ATOMIC_LOAD_XOR:
8640 case ISD::ATOMIC_LOAD_NAND:
8641 case ISD::ATOMIC_LOAD_MIN:
8642 case ISD::ATOMIC_LOAD_MAX:
8643 case ISD::ATOMIC_LOAD_UMIN:
8644 case ISD::ATOMIC_LOAD_UMAX:
8645 break;
8646 default:
8647 return SDValue();
8648 }
8649
8650 SDValue fence = atomic.getOperand(0);
8651 if (fence.getOpcode() != ISD::MEMBARRIER)
8652 return SDValue();
8653
8654 switch (atomic.getOpcode()) {
8655 case ISD::ATOMIC_CMP_SWAP:
8656 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8657 atomic.getOperand(1), atomic.getOperand(2),
8658 atomic.getOperand(3));
8659 case ISD::ATOMIC_SWAP:
8660 case ISD::ATOMIC_LOAD_ADD:
8661 case ISD::ATOMIC_LOAD_SUB:
8662 case ISD::ATOMIC_LOAD_AND:
8663 case ISD::ATOMIC_LOAD_OR:
8664 case ISD::ATOMIC_LOAD_XOR:
8665 case ISD::ATOMIC_LOAD_NAND:
8666 case ISD::ATOMIC_LOAD_MIN:
8667 case ISD::ATOMIC_LOAD_MAX:
8668 case ISD::ATOMIC_LOAD_UMIN:
8669 case ISD::ATOMIC_LOAD_UMAX:
8670 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8671 atomic.getOperand(1), atomic.getOperand(2));
8672 default:
8673 return SDValue();
8674 }
8675}
8676
Dan Gohman475871a2008-07-27 21:46:04 +00008677SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008678 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008679 SelectionDAG &DAG = DCI.DAG;
8680 switch (N->getOpcode()) {
8681 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008682 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008683 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008684 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008685 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008686 case ISD::SHL:
8687 case ISD::SRA:
8688 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008689 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008690 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008691 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8692 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008693 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00008694 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00008695 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008696 }
8697
Dan Gohman475871a2008-07-27 21:46:04 +00008698 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008699}
8700
Evan Cheng60c07e12006-07-05 22:17:51 +00008701//===----------------------------------------------------------------------===//
8702// X86 Inline Assembly Support
8703//===----------------------------------------------------------------------===//
8704
Chris Lattnerb8105652009-07-20 17:51:36 +00008705static bool LowerToBSwap(CallInst *CI) {
8706 // FIXME: this should verify that we are targetting a 486 or better. If not,
8707 // we will turn this bswap into something that will be lowered to logical ops
8708 // instead of emitting the bswap asm. For now, we don't support 486 or lower
8709 // so don't worry about this.
8710
8711 // Verify this is a simple bswap.
8712 if (CI->getNumOperands() != 2 ||
8713 CI->getType() != CI->getOperand(1)->getType() ||
8714 !CI->getType()->isInteger())
8715 return false;
8716
8717 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
8718 if (!Ty || Ty->getBitWidth() % 16 != 0)
8719 return false;
8720
8721 // Okay, we can do this xform, do so now.
8722 const Type *Tys[] = { Ty };
8723 Module *M = CI->getParent()->getParent()->getParent();
8724 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
8725
8726 Value *Op = CI->getOperand(1);
8727 Op = CallInst::Create(Int, Op, CI->getName(), CI);
8728
8729 CI->replaceAllUsesWith(Op);
8730 CI->eraseFromParent();
8731 return true;
8732}
8733
8734bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
8735 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8736 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
8737
8738 std::string AsmStr = IA->getAsmString();
8739
8740 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
8741 std::vector<std::string> AsmPieces;
8742 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
8743
8744 switch (AsmPieces.size()) {
8745 default: return false;
8746 case 1:
8747 AsmStr = AsmPieces[0];
8748 AsmPieces.clear();
8749 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
8750
8751 // bswap $0
8752 if (AsmPieces.size() == 2 &&
8753 (AsmPieces[0] == "bswap" ||
8754 AsmPieces[0] == "bswapq" ||
8755 AsmPieces[0] == "bswapl") &&
8756 (AsmPieces[1] == "$0" ||
8757 AsmPieces[1] == "${0:q}")) {
8758 // No need to check constraints, nothing other than the equivalent of
8759 // "=r,0" would be valid here.
8760 return LowerToBSwap(CI);
8761 }
8762 // rorw $$8, ${0:w} --> llvm.bswap.i16
8763 if (CI->getType() == Type::Int16Ty &&
8764 AsmPieces.size() == 3 &&
8765 AsmPieces[0] == "rorw" &&
8766 AsmPieces[1] == "$$8," &&
8767 AsmPieces[2] == "${0:w}" &&
8768 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
8769 return LowerToBSwap(CI);
8770 }
8771 break;
8772 case 3:
8773 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
8774 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
8775 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
8776 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
8777 std::vector<std::string> Words;
8778 SplitString(AsmPieces[0], Words, " \t");
8779 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
8780 Words.clear();
8781 SplitString(AsmPieces[1], Words, " \t");
8782 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
8783 Words.clear();
8784 SplitString(AsmPieces[2], Words, " \t,");
8785 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
8786 Words[2] == "%edx") {
8787 return LowerToBSwap(CI);
8788 }
8789 }
8790 }
8791 }
8792 break;
8793 }
8794 return false;
8795}
8796
8797
8798
Chris Lattnerf4dff842006-07-11 02:54:03 +00008799/// getConstraintType - Given a constraint letter, return the type of
8800/// constraint it is for this target.
8801X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008802X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8803 if (Constraint.size() == 1) {
8804 switch (Constraint[0]) {
8805 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008806 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008807 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008808 case 'r':
8809 case 'R':
8810 case 'l':
8811 case 'q':
8812 case 'Q':
8813 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008814 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008815 case 'Y':
8816 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008817 case 'e':
8818 case 'Z':
8819 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008820 default:
8821 break;
8822 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008823 }
Chris Lattner4234f572007-03-25 02:14:49 +00008824 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008825}
8826
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008827/// LowerXConstraint - try to replace an X constraint, which matches anything,
8828/// with another that has more specific requirements based on the type of the
8829/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008830const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008831LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008832 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8833 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008834 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008835 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008836 return "Y";
8837 if (Subtarget->hasSSE1())
8838 return "x";
8839 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008840
Chris Lattner5e764232008-04-26 23:02:14 +00008841 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008842}
8843
Chris Lattner48884cd2007-08-25 00:47:38 +00008844/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8845/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008846void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008847 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008848 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008849 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008850 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008851 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008852
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008853 switch (Constraint) {
8854 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008855 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008856 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008857 if (C->getZExtValue() <= 31) {
8858 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008859 break;
8860 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008861 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008862 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008863 case 'J':
8864 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008865 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00008866 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8867 break;
8868 }
8869 }
8870 return;
8871 case 'K':
8872 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008873 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00008874 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8875 break;
8876 }
8877 }
8878 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008879 case 'N':
8880 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008881 if (C->getZExtValue() <= 255) {
8882 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008883 break;
8884 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008885 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008886 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008887 case 'e': {
8888 // 32-bit signed value
8889 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8890 const ConstantInt *CI = C->getConstantIntValue();
8891 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8892 // Widen to 64 bits here to get it sign extended.
8893 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8894 break;
8895 }
8896 // FIXME gcc accepts some relocatable values here too, but only in certain
8897 // memory models; it's complicated.
8898 }
8899 return;
8900 }
8901 case 'Z': {
8902 // 32-bit unsigned value
8903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8904 const ConstantInt *CI = C->getConstantIntValue();
8905 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8906 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8907 break;
8908 }
8909 }
8910 // FIXME gcc accepts some relocatable values here too, but only in certain
8911 // memory models; it's complicated.
8912 return;
8913 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008914 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008915 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008916 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008917 // Widen to 64 bits here to get it sign extended.
8918 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008919 break;
8920 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008921
Chris Lattnerdc43a882007-05-03 16:52:29 +00008922 // If we are in non-pic codegen mode, we allow the address of a global (with
8923 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008924 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008925 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008926
Chris Lattner49921962009-05-08 18:23:14 +00008927 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8928 while (1) {
8929 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8930 Offset += GA->getOffset();
8931 break;
8932 } else if (Op.getOpcode() == ISD::ADD) {
8933 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8934 Offset += C->getZExtValue();
8935 Op = Op.getOperand(0);
8936 continue;
8937 }
8938 } else if (Op.getOpcode() == ISD::SUB) {
8939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8940 Offset += -C->getZExtValue();
8941 Op = Op.getOperand(0);
8942 continue;
8943 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008944 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008945
Chris Lattner49921962009-05-08 18:23:14 +00008946 // Otherwise, this isn't something we can handle, reject it.
8947 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008948 }
Chris Lattner3b6b36d2009-07-10 06:29:59 +00008949
Chris Lattner36c25012009-07-10 07:34:39 +00008950 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008951 // If we require an extra load to get this address, as in PIC mode, we
8952 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00008953 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
8954 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008955 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00008956
Dale Johannesen60b3ba02009-07-21 00:12:29 +00008957 if (hasMemory)
8958 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
8959 else
8960 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00008961 Result = Op;
8962 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008963 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008964 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008965
Gabor Greifba36cb52008-08-28 21:40:38 +00008966 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008967 Ops.push_back(Result);
8968 return;
8969 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008970 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8971 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008972}
8973
Chris Lattner259e97c2006-01-31 19:43:35 +00008974std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008975getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008976 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008977 if (Constraint.size() == 1) {
8978 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008979 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008980 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00008981 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
8982 if (Subtarget->is64Bit()) {
8983 if (VT == MVT::i32)
8984 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
8985 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
8986 X86::R10D,X86::R11D,X86::R12D,
8987 X86::R13D,X86::R14D,X86::R15D,
8988 X86::EBP, X86::ESP, 0);
8989 else if (VT == MVT::i16)
8990 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
8991 X86::SI, X86::DI, X86::R8W,X86::R9W,
8992 X86::R10W,X86::R11W,X86::R12W,
8993 X86::R13W,X86::R14W,X86::R15W,
8994 X86::BP, X86::SP, 0);
8995 else if (VT == MVT::i8)
8996 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
8997 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
8998 X86::R10B,X86::R11B,X86::R12B,
8999 X86::R13B,X86::R14B,X86::R15B,
9000 X86::BPL, X86::SPL, 0);
9001
9002 else if (VT == MVT::i64)
9003 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9004 X86::RSI, X86::RDI, X86::R8, X86::R9,
9005 X86::R10, X86::R11, X86::R12,
9006 X86::R13, X86::R14, X86::R15,
9007 X86::RBP, X86::RSP, 0);
9008
9009 break;
9010 }
9011 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009012 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009013 if (VT == MVT::i32)
9014 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
9015 else if (VT == MVT::i16)
9016 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
9017 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009018 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00009019 else if (VT == MVT::i64)
9020 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9021 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009022 }
9023 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009024
Chris Lattner1efa40f2006-02-22 00:56:39 +00009025 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009026}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009027
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009028std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009029X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00009030 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009031 // First, see if this is a constraint that directly corresponds to an LLVM
9032 // register class.
9033 if (Constraint.size() == 1) {
9034 // GCC Constraint Letters
9035 switch (Constraint[0]) {
9036 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009037 case 'r': // GENERAL_REGS
9038 case 'R': // LEGACY_REGS
9039 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00009040 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009041 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009042 if (VT == MVT::i16)
9043 return std::make_pair(0U, X86::GR16RegisterClass);
9044 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009045 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009046 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009047 case 'f': // FP Stack registers.
9048 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9049 // value to the correct fpstack register class.
9050 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
9051 return std::make_pair(0U, X86::RFP32RegisterClass);
9052 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
9053 return std::make_pair(0U, X86::RFP64RegisterClass);
9054 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009055 case 'y': // MMX_REGS if MMX allowed.
9056 if (!Subtarget->hasMMX()) break;
9057 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009058 case 'Y': // SSE_REGS if SSE2 allowed
9059 if (!Subtarget->hasSSE2()) break;
9060 // FALL THROUGH.
9061 case 'x': // SSE_REGS if SSE1 allowed
9062 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009063
9064 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009065 default: break;
9066 // Scalar SSE types.
9067 case MVT::f32:
9068 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009069 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009070 case MVT::f64:
9071 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009072 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009073 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00009074 case MVT::v16i8:
9075 case MVT::v8i16:
9076 case MVT::v4i32:
9077 case MVT::v2i64:
9078 case MVT::v4f32:
9079 case MVT::v2f64:
9080 return std::make_pair(0U, X86::VR128RegisterClass);
9081 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009082 break;
9083 }
9084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009085
Chris Lattnerf76d1802006-07-31 23:26:50 +00009086 // Use the default implementation in TargetLowering to convert the register
9087 // constraint into a member of a register class.
9088 std::pair<unsigned, const TargetRegisterClass*> Res;
9089 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009090
9091 // Not found as a standard register?
9092 if (Res.second == 0) {
9093 // GCC calls "st(0)" just plain "st".
9094 if (StringsEqualNoCase("{st}", Constraint)) {
9095 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00009096 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00009097 }
Dale Johannesen330169f2008-11-13 21:52:36 +00009098 // 'A' means EAX + EDX.
9099 if (Constraint == "A") {
9100 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +00009101 Res.second = X86::GR32_ADRegisterClass;
Dale Johannesen330169f2008-11-13 21:52:36 +00009102 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00009103 return Res;
9104 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009105
Chris Lattnerf76d1802006-07-31 23:26:50 +00009106 // Otherwise, check to see if this is a register class of the wrong value
9107 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
9108 // turn into {ax},{dx}.
9109 if (Res.second->hasType(VT))
9110 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009111
Chris Lattnerf76d1802006-07-31 23:26:50 +00009112 // All of the single-register GCC register classes map their values onto
9113 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
9114 // really want an 8-bit or 32-bit register, map to the appropriate register
9115 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00009116 if (Res.second == X86::GR16RegisterClass) {
9117 if (VT == MVT::i8) {
9118 unsigned DestReg = 0;
9119 switch (Res.first) {
9120 default: break;
9121 case X86::AX: DestReg = X86::AL; break;
9122 case X86::DX: DestReg = X86::DL; break;
9123 case X86::CX: DestReg = X86::CL; break;
9124 case X86::BX: DestReg = X86::BL; break;
9125 }
9126 if (DestReg) {
9127 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009128 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009129 }
9130 } else if (VT == MVT::i32) {
9131 unsigned DestReg = 0;
9132 switch (Res.first) {
9133 default: break;
9134 case X86::AX: DestReg = X86::EAX; break;
9135 case X86::DX: DestReg = X86::EDX; break;
9136 case X86::CX: DestReg = X86::ECX; break;
9137 case X86::BX: DestReg = X86::EBX; break;
9138 case X86::SI: DestReg = X86::ESI; break;
9139 case X86::DI: DestReg = X86::EDI; break;
9140 case X86::BP: DestReg = X86::EBP; break;
9141 case X86::SP: DestReg = X86::ESP; break;
9142 }
9143 if (DestReg) {
9144 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009145 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009146 }
9147 } else if (VT == MVT::i64) {
9148 unsigned DestReg = 0;
9149 switch (Res.first) {
9150 default: break;
9151 case X86::AX: DestReg = X86::RAX; break;
9152 case X86::DX: DestReg = X86::RDX; break;
9153 case X86::CX: DestReg = X86::RCX; break;
9154 case X86::BX: DestReg = X86::RBX; break;
9155 case X86::SI: DestReg = X86::RSI; break;
9156 case X86::DI: DestReg = X86::RDI; break;
9157 case X86::BP: DestReg = X86::RBP; break;
9158 case X86::SP: DestReg = X86::RSP; break;
9159 }
9160 if (DestReg) {
9161 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009162 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009163 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00009164 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00009165 } else if (Res.second == X86::FR32RegisterClass ||
9166 Res.second == X86::FR64RegisterClass ||
9167 Res.second == X86::VR128RegisterClass) {
9168 // Handle references to XMM physical registers that got mapped into the
9169 // wrong class. This can happen with constraints like {xmm0} where the
9170 // target independent register mapper will just pick the first match it can
9171 // find, ignoring the required type.
9172 if (VT == MVT::f32)
9173 Res.second = X86::FR32RegisterClass;
9174 else if (VT == MVT::f64)
9175 Res.second = X86::FR64RegisterClass;
9176 else if (X86::VR128RegisterClass->hasType(VT))
9177 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00009178 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009179
Chris Lattnerf76d1802006-07-31 23:26:50 +00009180 return Res;
9181}
Mon P Wang0c397192008-10-30 08:01:45 +00009182
9183//===----------------------------------------------------------------------===//
9184// X86 Widen vector type
9185//===----------------------------------------------------------------------===//
9186
9187/// getWidenVectorType: given a vector type, returns the type to widen
9188/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
9189/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00009190/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00009191/// scalarizing vs using the wider vector type.
9192
Dan Gohmanc13cf132009-01-15 17:34:08 +00009193MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00009194 assert(VT.isVector());
9195 if (isTypeLegal(VT))
9196 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009197
Mon P Wang0c397192008-10-30 08:01:45 +00009198 // TODO: In computeRegisterProperty, we can compute the list of legal vector
9199 // type based on element type. This would speed up our search (though
9200 // it may not be worth it since the size of the list is relatively
9201 // small).
9202 MVT EltVT = VT.getVectorElementType();
9203 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00009204
Mon P Wang0c397192008-10-30 08:01:45 +00009205 // On X86, it make sense to widen any vector wider than 1
9206 if (NElts <= 1)
9207 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00009208
9209 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00009210 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
9211 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009212
9213 if (isTypeLegal(SVT) &&
9214 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00009215 SVT.getVectorNumElements() > NElts)
9216 return SVT;
9217 }
9218 return MVT::Other;
9219}