blob: debfac486718969325447f8f2728c6967bef330e [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"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/CodeGen/SelectionDAG.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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042using namespace llvm;
43
Mon P Wang3c81d352008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000046
Evan Cheng10e86422008-04-25 19:11:04 +000047// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000048static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
49 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000050
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000051X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000053 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000054 X86ScalarSSEf64 = Subtarget->hasSSE2();
55 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000056 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000057
Chris Lattnerd43d00c2008-01-24 08:07:48 +000058 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000059
Anton Korobeynikov2365f512007-07-14 14:06:15 +000060 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000061 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000062
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000063 // Set up the TargetLowering object.
64
65 // X86 is weird, it always uses i8 for shift amounts and setcc results.
66 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000067 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000068 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000069 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000070 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000071
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000073 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000074 setUseUnderscoreSetJmp(false);
75 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000076 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000077 // MS runtime is weird: it exports _setjmp, but longjmp!
78 setUseUnderscoreSetJmp(true);
79 setUseUnderscoreLongJmp(false);
80 } else {
81 setUseUnderscoreSetJmp(true);
82 setUseUnderscoreLongJmp(true);
83 }
Scott Michelfdc40a02009-02-17 22:15:04 +000084
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000085 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000086 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
87 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
88 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000089 if (Subtarget->is64Bit())
90 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091
Evan Cheng03294662008-10-14 21:26:46 +000092 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000093
Scott Michelfdc40a02009-02-17 22:15:04 +000094 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +000095 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
97 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
98 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
99 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000100 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
101
102 // SETOEQ and SETUNE require checking two conditions.
103 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
105 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
108 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000109
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000110 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
111 // operation.
112 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
114 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000115
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000118 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000119 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000120 if (!UseSoftFloat && !NoImplicitFloat && X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000121 // We have an impenetrably clever algorithm for ui64->double only.
122 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling8b8a6362009-01-17 03:56:04 +0000123
124 // We have faster algorithm for ui32->single only.
125 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000126 } else {
Evan Cheng25ab6902006-09-08 06:48:29 +0000127 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000128 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000129 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000130
131 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
132 // this operation.
133 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
134 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000135
136 if (!UseSoftFloat && !NoImplicitFloat) {
137 // SSE has no i16 to fp conversion, only i32
138 if (X86ScalarSSEf32) {
139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
142 } else {
143 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
144 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
145 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000146 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000147 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
148 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000149 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000150
Dale Johannesen73328d12007-09-19 23:55:34 +0000151 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
152 // are Legal, f80 is custom lowered.
153 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
154 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000155
Evan Cheng02568ff2006-01-30 22:13:22 +0000156 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
157 // this operation.
158 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
159 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
160
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000161 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000162 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000163 // f32 and f64 cases are Legal, f80 case is not
164 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000165 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000167 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168 }
169
170 // Handle FP_TO_UINT by promoting the destination to a larger signed
171 // conversion.
172 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
173 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
174 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
175
Evan Cheng25ab6902006-09-08 06:48:29 +0000176 if (Subtarget->is64Bit()) {
177 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000178 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000179 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000180 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000181 // Expand FP_TO_UINT into a select.
182 // FIXME: We would like to use a Custom expander here eventually to do
183 // the optimal thing for SSE vs. the default expansion in the legalizer.
184 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
185 else
186 // With SSE3 we can use fisttpll to convert to a signed i64.
187 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
188 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189
Chris Lattner399610a2006-12-05 18:22:22 +0000190 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000191 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000192 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
193 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
194 }
Chris Lattner21f66852005-12-23 05:15:23 +0000195
Dan Gohmanb00ee212008-02-18 19:34:53 +0000196 // Scalar integer divide and remainder are lowered to use operations that
197 // produce two results, to match the available instructions. This exposes
198 // the two-result form to trivial CSE, which is able to combine x/y and x%y
199 // into a single instruction.
200 //
201 // Scalar integer multiply-high is also lowered to use two-result
202 // operations, to match the available instructions. However, plain multiply
203 // (low) operations are left as Legal, as there are single-result
204 // instructions for this in x86. Using the two-result multiply instructions
205 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000206 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
207 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
208 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
209 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
210 setOperationAction(ISD::SREM , MVT::i8 , Expand);
211 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000212 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
213 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
214 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
215 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
216 setOperationAction(ISD::SREM , MVT::i16 , Expand);
217 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000218 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
219 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
220 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
221 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
222 setOperationAction(ISD::SREM , MVT::i32 , Expand);
223 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000224 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
225 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
226 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
227 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
228 setOperationAction(ISD::SREM , MVT::i64 , Expand);
229 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000230
Evan Chengc35497f2006-10-30 08:02:39 +0000231 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000232 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000233 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
234 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000236 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
237 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
238 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000239 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
240 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000241 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000242 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000243 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000244 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000245
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000247 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
248 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000249 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000250 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
251 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000252 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000253 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
254 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000257 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
258 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000259 }
260
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000261 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000262 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000263
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000264 // These should be promoted to a larger select which is supported.
265 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
266 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000267 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000268 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
269 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
270 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
271 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000272 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000273 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
274 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
275 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
276 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
277 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000278 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000279 if (Subtarget->is64Bit()) {
280 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
281 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
282 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000283 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000285 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000286
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000287 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000288 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000289 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000290 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000291 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000292 if (Subtarget->is64Bit())
293 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000294 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000295 if (Subtarget->is64Bit()) {
296 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
297 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
298 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000299 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000300 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000301 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000302 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
303 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
304 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000305 if (Subtarget->is64Bit()) {
306 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
307 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
308 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
309 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Chengd2cde682008-03-10 19:38:10 +0000311 if (Subtarget->hasSSE1())
312 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000313
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000314 if (!Subtarget->hasSSE2())
315 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
316
Mon P Wang63307c32008-05-05 19:05:59 +0000317 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000318 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
319 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
320 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
321 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000322
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000323 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000327
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000328 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000329 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
330 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
331 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
332 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
333 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
334 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
335 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000336 }
337
Dan Gohman7f460202008-06-30 20:59:49 +0000338 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
339 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000340 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000341 if (!Subtarget->isTargetDarwin() &&
342 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000343 !Subtarget->isTargetCygMing()) {
344 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
345 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
346 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000347
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000348 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
349 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
350 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
351 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
352 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000353 setExceptionPointerRegister(X86::RAX);
354 setExceptionSelectorRegister(X86::RDX);
355 } else {
356 setExceptionPointerRegister(X86::EAX);
357 setExceptionSelectorRegister(X86::EDX);
358 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000359 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000360 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
361
Duncan Sandsf7331b32007-09-11 14:10:23 +0000362 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000363
Chris Lattnerda68d302008-01-15 21:58:22 +0000364 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000365
Nate Begemanacc398c2006-01-25 18:21:52 +0000366 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
367 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000368 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000369 if (Subtarget->is64Bit()) {
370 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000371 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000372 } else {
373 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000374 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000375 }
Evan Chengae642192007-03-02 23:16:35 +0000376
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000377 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000378 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000379 if (Subtarget->is64Bit())
380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000381 if (Subtarget->isTargetCygMing())
382 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
383 else
384 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000385
Evan Chengc7ce29b2009-02-13 22:36:38 +0000386 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000387 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000388 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000389 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
390 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000391
Evan Cheng223547a2006-01-31 22:28:30 +0000392 // Use ANDPD to simulate FABS.
393 setOperationAction(ISD::FABS , MVT::f64, Custom);
394 setOperationAction(ISD::FABS , MVT::f32, Custom);
395
396 // Use XORP to simulate FNEG.
397 setOperationAction(ISD::FNEG , MVT::f64, Custom);
398 setOperationAction(ISD::FNEG , MVT::f32, Custom);
399
Evan Cheng68c47cb2007-01-05 07:55:56 +0000400 // Use ANDPD and ORPD to simulate FCOPYSIGN.
401 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
402 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
403
Evan Chengd25e9e82006-02-02 00:28:23 +0000404 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405 setOperationAction(ISD::FSIN , MVT::f64, Expand);
406 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000407 setOperationAction(ISD::FSIN , MVT::f32, Expand);
408 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000409
Chris Lattnera54aa942006-01-29 06:26:08 +0000410 // Expand FP immediates into loads from the stack, except for the special
411 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000412 addLegalFPImmediate(APFloat(+0.0)); // xorpd
413 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000414
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000415 // Floating truncations from f80 and extensions to f80 go through memory.
416 // If optimizing, we lie about this though and handle it in
417 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
418 if (Fast) {
419 setConvertAction(MVT::f32, MVT::f80, Expand);
420 setConvertAction(MVT::f64, MVT::f80, Expand);
421 setConvertAction(MVT::f80, MVT::f32, Expand);
422 setConvertAction(MVT::f80, MVT::f64, Expand);
423 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000424 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000425 // Use SSE for f32, x87 for f64.
426 // Set up the FP register classes.
427 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
428 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
429
430 // Use ANDPS to simulate FABS.
431 setOperationAction(ISD::FABS , MVT::f32, Custom);
432
433 // Use XORP to simulate FNEG.
434 setOperationAction(ISD::FNEG , MVT::f32, Custom);
435
436 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
437
438 // Use ANDPS and ORPS to simulate FCOPYSIGN.
439 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
441
442 // We don't support sin/cos/fmod
443 setOperationAction(ISD::FSIN , MVT::f32, Expand);
444 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000445
Nate Begemane1795842008-02-14 08:57:00 +0000446 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000447 addLegalFPImmediate(APFloat(+0.0f)); // xorps
448 addLegalFPImmediate(APFloat(+0.0)); // FLD0
449 addLegalFPImmediate(APFloat(+1.0)); // FLD1
450 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
451 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
452
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000453 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
454 // this though and handle it in InstructionSelectPreprocess so that
455 // dagcombine2 can hack on these.
456 if (Fast) {
457 setConvertAction(MVT::f32, MVT::f64, Expand);
458 setConvertAction(MVT::f32, MVT::f80, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000459 setConvertAction(MVT::f80, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000460 setConvertAction(MVT::f64, MVT::f32, Expand);
461 // And x87->x87 truncations also.
462 setConvertAction(MVT::f80, MVT::f64, Expand);
463 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 if (!UnsafeFPMath) {
466 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
467 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
468 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000469 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000470 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000471 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000472 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
473 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000474
Evan Cheng68c47cb2007-01-05 07:55:56 +0000475 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000476 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000477 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
478 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000479
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000480 // Floating truncations go through memory. If optimizing, we lie about
481 // this though and handle it in InstructionSelectPreprocess so that
482 // dagcombine2 can hack on these.
483 if (Fast) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000484 setConvertAction(MVT::f80, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000485 setConvertAction(MVT::f64, MVT::f32, Expand);
486 setConvertAction(MVT::f80, MVT::f64, Expand);
487 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000488
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000489 if (!UnsafeFPMath) {
490 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
492 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000493 addLegalFPImmediate(APFloat(+0.0)); // FLD0
494 addLegalFPImmediate(APFloat(+1.0)); // FLD1
495 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
496 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000497 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
498 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
499 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
500 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000501 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000502
Dale Johannesen59a58732007-08-05 18:49:15 +0000503 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000504 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000505 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
506 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
507 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
508 {
509 bool ignored;
510 APFloat TmpFlt(+0.0);
511 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
512 &ignored);
513 addLegalFPImmediate(TmpFlt); // FLD0
514 TmpFlt.changeSign();
515 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
516 APFloat TmpFlt2(+1.0);
517 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
518 &ignored);
519 addLegalFPImmediate(TmpFlt2); // FLD1
520 TmpFlt2.changeSign();
521 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
522 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000523
Evan Chengc7ce29b2009-02-13 22:36:38 +0000524 if (!UnsafeFPMath) {
525 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
526 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
527 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000528 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000529
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000530 // Always use a library call for pow.
531 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
532 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
533 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
534
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000535 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000536 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000537 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000538 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000539 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
540
Mon P Wangf007a8b2008-11-06 05:31:54 +0000541 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000542 // (for widening) or expand (for scalarization). Then we will selectively
543 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000544 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
545 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000546 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000559 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
561 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000562 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000584 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000589 }
590
Evan Chengc7ce29b2009-02-13 22:36:38 +0000591 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
592 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000593 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000594 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
595 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
596 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000597 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000598 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000599
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000600 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
601 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
602 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000603 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000604
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000605 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
606 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
607 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000608 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000609
Bill Wendling74027e92007-03-15 21:24:36 +0000610 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
611 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
612
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000613 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000614 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000615 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000616 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
617 setOperationAction(ISD::AND, MVT::v2i32, Promote);
618 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
619 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000620
621 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000622 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000623 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000624 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
625 setOperationAction(ISD::OR, MVT::v2i32, Promote);
626 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
627 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000628
629 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000630 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000631 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000632 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
633 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
634 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
635 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000636
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000637 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000638 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000639 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000640 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
641 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
642 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000643 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
644 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000645 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000646
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000647 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
648 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
649 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000650 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000651 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000652
653 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
655 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000656 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000657
Evan Cheng52672b82008-07-22 18:39:19 +0000658 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000659 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
660 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000661 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000662
663 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000664
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000665 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000666 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
667 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
668 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
669 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
670 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000671 }
672
Evan Cheng92722532009-03-26 23:06:32 +0000673 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000674 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
675
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000676 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
677 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
678 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
679 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000680 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
681 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000682 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
683 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
684 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000685 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000686 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000687 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000688 }
689
Evan Cheng92722532009-03-26 23:06:32 +0000690 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000691 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000692
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000693 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
694 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000695 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
696 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
697 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
698 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
699
Evan Chengf7c378e2006-04-10 07:23:14 +0000700 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
701 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
702 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000703 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000704 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000705 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
706 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
707 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000708 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000709 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000710 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
711 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
712 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
713 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000714 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
715 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000716
Nate Begeman30a0de92008-07-17 16:51:19 +0000717 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
718 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
719 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
720 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000721
Evan Chengf7c378e2006-04-10 07:23:14 +0000722 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
723 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000724 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000725 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000726 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000727
Evan Cheng2c3ae372006-04-12 21:21:57 +0000728 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000729 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
730 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000731 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000732 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000733 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000734 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
735 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
736 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000737 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000738
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
740 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
741 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
742 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000745
Nate Begemancdd1eec2008-02-12 22:51:28 +0000746 if (Subtarget->is64Bit()) {
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000748 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000749 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000750
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000751 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000752 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000753 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
754 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
755 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
756 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
757 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
758 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
759 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
760 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
761 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
762 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)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
Evan Cheng470a6ad2006-02-22 02:26:30 +0000773 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000774
Nate Begeman14d12ca2008-02-11 04:19:36 +0000775 if (Subtarget->hasSSE41()) {
776 // FIXME: Do we need to handle scalar-to-vector here?
777 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
778
779 // i8 and i16 vectors are custom , because the source register and source
780 // source memory operand types are not the same width. f32 vectors are
781 // custom since the immediate controlling the insert encodes additional
782 // information.
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
784 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000786 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
787
788 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000791 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000792
793 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000794 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000796 }
797 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000798
Nate Begeman30a0de92008-07-17 16:51:19 +0000799 if (Subtarget->hasSSE42()) {
800 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
801 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000802
Evan Cheng6be2c582006-04-05 23:38:46 +0000803 // We want to custom lower some of our intrinsics.
804 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
805
Bill Wendling74c37652008-12-09 22:08:41 +0000806 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000807 setOperationAction(ISD::SADDO, MVT::i32, Custom);
808 setOperationAction(ISD::SADDO, MVT::i64, Custom);
809 setOperationAction(ISD::UADDO, MVT::i32, Custom);
810 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000811 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
812 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
813 setOperationAction(ISD::USUBO, MVT::i32, Custom);
814 setOperationAction(ISD::USUBO, MVT::i64, Custom);
815 setOperationAction(ISD::SMULO, MVT::i32, Custom);
816 setOperationAction(ISD::SMULO, MVT::i64, Custom);
817 setOperationAction(ISD::UMULO, MVT::i32, Custom);
818 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000819
Evan Chengd54f2d52009-03-31 19:38:51 +0000820 if (!Subtarget->is64Bit()) {
821 // These libcalls are not available in 32-bit.
822 setLibcallName(RTLIB::SHL_I128, 0);
823 setLibcallName(RTLIB::SRL_I128, 0);
824 setLibcallName(RTLIB::SRA_I128, 0);
825 }
826
Evan Cheng206ee9d2006-07-07 08:33:52 +0000827 // We have target-specific dag combine patterns for the following nodes:
828 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000829 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000830 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000831 setTargetDAGCombine(ISD::SHL);
832 setTargetDAGCombine(ISD::SRA);
833 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000834 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000835 if (Subtarget->is64Bit())
836 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000837
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000838 computeRegisterProperties();
839
Evan Cheng87ed7162006-02-14 08:25:08 +0000840 // FIXME: These should be based on subtarget info. Plus, the values should
841 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000842 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
843 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
844 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000845 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000846 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000847 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000848}
849
Scott Michel5b8f82e2008-03-10 15:42:14 +0000850
Duncan Sands5480c042009-01-01 15:52:00 +0000851MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000852 return MVT::i8;
853}
854
855
Evan Cheng29286502008-01-23 23:17:41 +0000856/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
857/// the desired ByVal argument alignment.
858static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
859 if (MaxAlign == 16)
860 return;
861 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
862 if (VTy->getBitWidth() == 128)
863 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000864 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
865 unsigned EltAlign = 0;
866 getMaxByValAlign(ATy->getElementType(), EltAlign);
867 if (EltAlign > MaxAlign)
868 MaxAlign = EltAlign;
869 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
870 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
871 unsigned EltAlign = 0;
872 getMaxByValAlign(STy->getElementType(i), EltAlign);
873 if (EltAlign > MaxAlign)
874 MaxAlign = EltAlign;
875 if (MaxAlign == 16)
876 break;
877 }
878 }
879 return;
880}
881
882/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
883/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000884/// that contain SSE vectors are placed at 16-byte boundaries while the rest
885/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000886unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000887 if (Subtarget->is64Bit()) {
888 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000889 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000890 if (TyAlign > 8)
891 return TyAlign;
892 return 8;
893 }
894
Evan Cheng29286502008-01-23 23:17:41 +0000895 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000896 if (Subtarget->hasSSE1())
897 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000898 return Align;
899}
Chris Lattner2b02a442007-02-25 08:29:00 +0000900
Evan Chengf0df0312008-05-15 08:39:06 +0000901/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000902/// and store operations as a result of memset, memcpy, and memmove
903/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000904/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000905MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000906X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
907 bool isSrcConst, bool isSrcStr) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000908 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
909 // linux. This is because the stack realignment code can't handle certain
910 // cases like PR2962. This should be removed when PR2962 is fixed.
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000911 if (!NoImplicitFloat && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000912 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
913 return MVT::v4i32;
914 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
915 return MVT::v4f32;
916 }
Evan Chengf0df0312008-05-15 08:39:06 +0000917 if (Subtarget->is64Bit() && Size >= 8)
918 return MVT::i64;
919 return MVT::i32;
920}
921
Evan Chengcc415862007-11-09 01:32:10 +0000922/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
923/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000924SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000925 SelectionDAG &DAG) const {
926 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000927 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000928 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000929 // This doesn't have DebugLoc associated with it, but is not really the
930 // same as a Register.
931 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
932 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000933 return Table;
934}
935
Chris Lattner2b02a442007-02-25 08:29:00 +0000936//===----------------------------------------------------------------------===//
937// Return Value Calling Convention Implementation
938//===----------------------------------------------------------------------===//
939
Chris Lattner59ed56b2007-02-28 04:55:35 +0000940#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000941
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000942/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000943SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000944 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000945 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000946
Chris Lattner9774c912007-02-27 05:28:59 +0000947 SmallVector<CCValAssign, 16> RVLocs;
948 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000949 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
950 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000951 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000952
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000953 // If this is the first return lowered for this function, add the regs to the
954 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000955 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000956 for (unsigned i = 0; i != RVLocs.size(); ++i)
957 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000958 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000959 }
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000961
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000962 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000963 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000964 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000965 SDValue TailCall = Chain;
966 SDValue TargetAddress = TailCall.getOperand(1);
967 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000968 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000969 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000970 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000971 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000972 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000973 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000974 assert(StackAdjustment.getOpcode() == ISD::Constant &&
975 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000976
Dan Gohman475871a2008-07-27 21:46:04 +0000977 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000978 Operands.push_back(Chain.getOperand(0));
979 Operands.push_back(TargetAddress);
980 Operands.push_back(StackAdjustment);
981 // Copy registers used by the call. Last operand is a flag so it is not
982 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000983 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000984 Operands.push_back(Chain.getOperand(i));
985 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000986 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000987 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000988 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000989
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000990 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000991 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000992
Dan Gohman475871a2008-07-27 21:46:04 +0000993 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000994 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
995 // Operand #1 = Bytes To Pop
996 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000997
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000998 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000999 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1000 CCValAssign &VA = RVLocs[i];
1001 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +00001002 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001003
Chris Lattner447ff682008-03-11 03:23:40 +00001004 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1005 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001006 if (VA.getLocReg() == X86::ST0 ||
1007 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001008 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1009 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001010 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +00001011 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001012 RetOps.push_back(ValToCopy);
1013 // Don't emit a copytoreg.
1014 continue;
1015 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001016
Evan Cheng242b38b2009-02-23 09:03:22 +00001017 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1018 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001019 if (Subtarget->is64Bit()) {
1020 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001021 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +00001022 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001023 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1024 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1025 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001026 }
1027
Dale Johannesendd64c412009-02-04 00:33:20 +00001028 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001029 Flag = Chain.getValue(1);
1030 }
Dan Gohman61a92132008-04-21 23:59:07 +00001031
1032 // The x86-64 ABI for returning structs by value requires that we copy
1033 // the sret argument into %rax for the return. We saved the argument into
1034 // a virtual register in the entry block, so now we copy the value out
1035 // and into %rax.
1036 if (Subtarget->is64Bit() &&
1037 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1038 MachineFunction &MF = DAG.getMachineFunction();
1039 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1040 unsigned Reg = FuncInfo->getSRetReturnReg();
1041 if (!Reg) {
1042 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1043 FuncInfo->setSRetReturnReg(Reg);
1044 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001045 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001046
Dale Johannesendd64c412009-02-04 00:33:20 +00001047 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001048 Flag = Chain.getValue(1);
1049 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001050
Chris Lattner447ff682008-03-11 03:23:40 +00001051 RetOps[0] = Chain; // Update chain.
1052
1053 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001054 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001055 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
1057 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001058 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001059}
1060
1061
Chris Lattner3085e152007-02-25 08:59:22 +00001062/// LowerCallResult - Lower the result values of an ISD::CALL into the
1063/// appropriate copies out of appropriate physical registers. This assumes that
1064/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1065/// being lowered. The returns a SDNode with the same number of values as the
1066/// ISD::CALL.
1067SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001068LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001069 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001070
Scott Michelfdc40a02009-02-17 22:15:04 +00001071 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001072 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001073 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001074 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001075 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001076 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001077 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1078
Dan Gohman475871a2008-07-27 21:46:04 +00001079 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001080
Chris Lattner3085e152007-02-25 08:59:22 +00001081 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001082 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001083 CCValAssign &VA = RVLocs[i];
1084 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001085
Torok Edwin3f142c32009-02-01 18:15:56 +00001086 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001087 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001088 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1089 cerr << "SSE register return with SSE disabled\n";
1090 exit(1);
1091 }
1092
Chris Lattner8e6da152008-03-10 21:08:41 +00001093 // If this is a call to a function that returns an fp value on the floating
1094 // point stack, but where we prefer to use the value in xmm registers, copy
1095 // 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 +00001096 if ((VA.getLocReg() == X86::ST0 ||
1097 VA.getLocReg() == X86::ST1) &&
1098 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001099 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001100 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001101
Evan Cheng79fb3b42009-02-20 20:43:02 +00001102 SDValue Val;
1103 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001104 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1105 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1106 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1107 MVT::v2i64, InFlag).getValue(1);
1108 Val = Chain.getValue(0);
1109 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1110 Val, DAG.getConstant(0, MVT::i64));
1111 } else {
1112 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1113 MVT::i64, InFlag).getValue(1);
1114 Val = Chain.getValue(0);
1115 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001116 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1117 } else {
1118 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1119 CopyVT, InFlag).getValue(1);
1120 Val = Chain.getValue(0);
1121 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001122 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001123
Dan Gohman37eed792009-02-04 17:28:58 +00001124 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001125 // Round the F80 the right size, which also moves to the appropriate xmm
1126 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001127 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001128 // This truncation won't change the value.
1129 DAG.getIntPtrConstant(1));
1130 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001131
Chris Lattner8e6da152008-03-10 21:08:41 +00001132 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001133 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001134
Chris Lattner3085e152007-02-25 08:59:22 +00001135 // Merge everything together with a MERGE_VALUES node.
1136 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001137 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1138 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001139}
1140
1141
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001142//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001143// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001144//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001145// StdCall calling convention seems to be standard for many Windows' API
1146// routines and around. It differs from C calling convention just a little:
1147// callee should clean up the stack, not caller. Symbols should be also
1148// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001149// For info on fast calling convention see Fast Calling Convention (tail call)
1150// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001151
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001152/// CallIsStructReturn - Determines whether a CALL node uses struct return
1153/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001154static bool CallIsStructReturn(CallSDNode *TheCall) {
1155 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001156 if (!NumOps)
1157 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001158
Dan Gohman095cc292008-09-13 01:54:27 +00001159 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001160}
1161
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001162/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1163/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001164static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001165 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001166 if (!NumArgs)
1167 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001168
1169 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001170}
1171
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001172/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1173/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001174/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001175bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001176 if (IsVarArg)
1177 return false;
1178
Dan Gohman095cc292008-09-13 01:54:27 +00001179 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001180 default:
1181 return false;
1182 case CallingConv::X86_StdCall:
1183 return !Subtarget->is64Bit();
1184 case CallingConv::X86_FastCall:
1185 return !Subtarget->is64Bit();
1186 case CallingConv::Fast:
1187 return PerformTailCallOpt;
1188 }
1189}
1190
Dan Gohman095cc292008-09-13 01:54:27 +00001191/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1192/// given CallingConvention value.
1193CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001194 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001195 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001196 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001197 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1198 return CC_X86_64_TailCall;
1199 else
1200 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001201 }
1202
Gordon Henriksen86737662008-01-05 16:56:59 +00001203 if (CC == CallingConv::X86_FastCall)
1204 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001205 else if (CC == CallingConv::Fast)
1206 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001207 else
1208 return CC_X86_32_C;
1209}
1210
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001211/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1212/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001213NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001214X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001215 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001216 if (CC == CallingConv::X86_FastCall)
1217 return FastCall;
1218 else if (CC == CallingConv::X86_StdCall)
1219 return StdCall;
1220 return None;
1221}
1222
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001223
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001224/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1225/// in a register before calling.
1226bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1227 return !IsTailCall && !Is64Bit &&
1228 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1229 Subtarget->isPICStyleGOT();
1230}
1231
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001232/// CallRequiresFnAddressInReg - Check whether the call requires the function
1233/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001234bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001235X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001236 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001237 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1238 Subtarget->isPICStyleGOT();
1239}
1240
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001241/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1242/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001243/// the specific parameter attribute. The copy will be passed as a byval
1244/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001245static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001246CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001247 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1248 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001249 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001250 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001251 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001252}
1253
Dan Gohman475871a2008-07-27 21:46:04 +00001254SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001255 const CCValAssign &VA,
1256 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001257 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001258 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001259 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001260 ISD::ArgFlagsTy Flags =
1261 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001262 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001263 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001264
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001265 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001266 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001267 // In case of tail call optimization mark all arguments mutable. Since they
1268 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001269 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001270 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001272 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001273 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001274 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001275 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001276}
1277
Dan Gohman475871a2008-07-27 21:46:04 +00001278SDValue
1279X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001280 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001281 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001282 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Gordon Henriksen86737662008-01-05 16:56:59 +00001284 const Function* Fn = MF.getFunction();
1285 if (Fn->hasExternalLinkage() &&
1286 Subtarget->isTargetCygMing() &&
1287 Fn->getName() == "main")
1288 FuncInfo->setForceFramePointer(true);
1289
1290 // Decorate the function name.
1291 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Evan Cheng1bc78042006-04-26 01:20:17 +00001293 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001295 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001296 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001297 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001298 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001299
1300 assert(!(isVarArg && CC == CallingConv::Fast) &&
1301 "Var args not supported with calling convention fastcc");
1302
Chris Lattner638402b2007-02-28 07:00:42 +00001303 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001304 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001305 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001306 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001307
Dan Gohman475871a2008-07-27 21:46:04 +00001308 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001309 unsigned LastVal = ~0U;
1310 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1311 CCValAssign &VA = ArgLocs[i];
1312 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1313 // places.
1314 assert(VA.getValNo() != LastVal &&
1315 "Don't support value assigned to multiple locs yet");
1316 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001317
Chris Lattnerf39f7712007-02-28 05:46:49 +00001318 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001319 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001320 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001321 if (RegVT == MVT::i32)
1322 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001323 else if (Is64Bit && RegVT == MVT::i64)
1324 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001325 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001326 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001327 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001328 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001329 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001330 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001331 else if (RegVT.isVector()) {
1332 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001333 if (!Is64Bit)
1334 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1335 else {
1336 // Darwin calling convention passes MMX values in either GPRs or
1337 // XMMs in x86-64. Other targets pass them in memory.
1338 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1339 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1340 RegVT = MVT::v2i64;
1341 } else {
1342 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1343 RegVT = MVT::i64;
1344 }
1345 }
1346 } else {
1347 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001348 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001349
Bob Wilson998e1252009-04-20 18:36:57 +00001350 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001351 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Chris Lattnerf39f7712007-02-28 05:46:49 +00001353 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1354 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1355 // right size.
1356 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001357 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001358 DAG.getValueType(VA.getValVT()));
1359 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001360 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001361 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Chris Lattnerf39f7712007-02-28 05:46:49 +00001363 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001364 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001365
Gordon Henriksen86737662008-01-05 16:56:59 +00001366 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001367 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001368 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001369 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001370 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001371 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1372 ArgValue, DAG.getConstant(0, MVT::i64));
1373 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001374 }
1375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001376
Chris Lattnerf39f7712007-02-28 05:46:49 +00001377 ArgValues.push_back(ArgValue);
1378 } else {
1379 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001380 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001381 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001382 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001383
Dan Gohman61a92132008-04-21 23:59:07 +00001384 // The x86-64 ABI for returning structs by value requires that we copy
1385 // the sret argument into %rax for the return. Save the argument into
1386 // a virtual register so that we can access it from the return points.
1387 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1388 MachineFunction &MF = DAG.getMachineFunction();
1389 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1390 unsigned Reg = FuncInfo->getSRetReturnReg();
1391 if (!Reg) {
1392 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1393 FuncInfo->setSRetReturnReg(Reg);
1394 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001395 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001396 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001397 }
1398
Chris Lattnerf39f7712007-02-28 05:46:49 +00001399 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001400 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001401 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001402 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001403
Evan Cheng1bc78042006-04-26 01:20:17 +00001404 // If the function takes variable number of arguments, make a frame index for
1405 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001406 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001407 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1408 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1409 }
1410 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001411 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1412
1413 // FIXME: We should really autogenerate these arrays
1414 static const unsigned GPR64ArgRegsWin64[] = {
1415 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001416 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001417 static const unsigned XMMArgRegsWin64[] = {
1418 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1419 };
1420 static const unsigned GPR64ArgRegs64Bit[] = {
1421 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1422 };
1423 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001424 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1425 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1426 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001427 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1428
1429 if (IsWin64) {
1430 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1431 GPR64ArgRegs = GPR64ArgRegsWin64;
1432 XMMArgRegs = XMMArgRegsWin64;
1433 } else {
1434 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1435 GPR64ArgRegs = GPR64ArgRegs64Bit;
1436 XMMArgRegs = XMMArgRegs64Bit;
1437 }
1438 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1439 TotalNumIntRegs);
1440 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1441 TotalNumXMMRegs);
1442
Evan Chengc7ce29b2009-02-13 22:36:38 +00001443 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001444 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001445 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloat) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001446 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001447 if (UseSoftFloat || NoImplicitFloat || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001448 // Kernel mode asks for SSE to be disabled, so don't push them
1449 // on the stack.
1450 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001451
Gordon Henriksen86737662008-01-05 16:56:59 +00001452 // For X86-64, if there are vararg parameters that are passed via
1453 // registers, then we must store them to their spots on the stack so they
1454 // may be loaded by deferencing the result of va_next.
1455 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001456 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1457 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1458 TotalNumXMMRegs * 16, 16);
1459
Gordon Henriksen86737662008-01-05 16:56:59 +00001460 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001461 SmallVector<SDValue, 8> MemOps;
1462 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001463 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001464 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001465 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001466 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1467 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001468 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001469 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001470 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001471 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001472 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001473 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001474 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001475 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001476
Gordon Henriksen86737662008-01-05 16:56:59 +00001477 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001478 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001479 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001480 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001481 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1482 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001483 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001484 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001485 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001486 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001487 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001488 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001489 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001490 }
1491 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001492 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001493 &MemOps[0], MemOps.size());
1494 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001495 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001496
Gordon Henriksenae636f82008-01-03 16:47:34 +00001497 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001498
Gordon Henriksen86737662008-01-05 16:56:59 +00001499 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001500 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001501 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001502 BytesCallerReserves = 0;
1503 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001504 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001505 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001506 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001507 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001508 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001509 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001510
Gordon Henriksen86737662008-01-05 16:56:59 +00001511 if (!Is64Bit) {
1512 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1513 if (CC == CallingConv::X86_FastCall)
1514 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1515 }
Evan Cheng25caf632006-05-23 21:06:34 +00001516
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001517 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001518
Evan Cheng25caf632006-05-23 21:06:34 +00001519 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001520 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001521 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001522}
1523
Dan Gohman475871a2008-07-27 21:46:04 +00001524SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001525X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001526 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001527 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001528 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001529 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001530 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001531 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001532 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001533 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001534 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001535 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001536 }
Dale Johannesenace16102009-02-03 19:33:06 +00001537 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001538 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001539}
1540
Bill Wendling64e87322009-01-16 19:25:27 +00001541/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001542/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001543SDValue
1544X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001545 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001546 SDValue Chain,
1547 bool IsTailCall,
1548 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001549 int FPDiff,
1550 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001551 if (!IsTailCall || FPDiff==0) return Chain;
1552
1553 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001554 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001555 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001556
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001557 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001558 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001559 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001560}
1561
1562/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1563/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001564static SDValue
1565EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001566 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001567 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001568 // Store the return address to the appropriate stack slot.
1569 if (!FPDiff) return Chain;
1570 // Calculate the new stack slot for the return address.
1571 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001572 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001573 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001574 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001575 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001577 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001578 return Chain;
1579}
1580
Dan Gohman475871a2008-07-27 21:46:04 +00001581SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001582 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001583 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1584 SDValue Chain = TheCall->getChain();
1585 unsigned CC = TheCall->getCallingConv();
1586 bool isVarArg = TheCall->isVarArg();
1587 bool IsTailCall = TheCall->isTailCall() &&
1588 CC == CallingConv::Fast && PerformTailCallOpt;
1589 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001590 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001591 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001592 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001593
1594 assert(!(isVarArg && CC == CallingConv::Fast) &&
1595 "Var args not supported with calling convention fastcc");
1596
Chris Lattner638402b2007-02-28 07:00:42 +00001597 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001598 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001599 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001600 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner423c5f42007-02-28 05:31:48 +00001602 // Get a count of how many bytes are to be pushed on the stack.
1603 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001604 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001605 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001606
Gordon Henriksen86737662008-01-05 16:56:59 +00001607 int FPDiff = 0;
1608 if (IsTailCall) {
1609 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001610 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001611 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1612 FPDiff = NumBytesCallerPushed - NumBytes;
1613
1614 // Set the delta of movement of the returnaddr stackslot.
1615 // But only set if delta is greater than previous delta.
1616 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1617 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1618 }
1619
Chris Lattnere563bbc2008-10-11 22:08:30 +00001620 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001621
Dan Gohman475871a2008-07-27 21:46:04 +00001622 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001623 // Load return adress for tail calls.
1624 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001625 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001626
Dan Gohman475871a2008-07-27 21:46:04 +00001627 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1628 SmallVector<SDValue, 8> MemOpChains;
1629 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001630
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001631 // Walk the register/memloc assignments, inserting copies/loads. In the case
1632 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001633 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1634 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001635 SDValue Arg = TheCall->getArg(i);
1636 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1637 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001638
Chris Lattner423c5f42007-02-28 05:31:48 +00001639 // Promote the value if needed.
1640 switch (VA.getLocInfo()) {
1641 default: assert(0 && "Unknown loc info!");
1642 case CCValAssign::Full: break;
1643 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001644 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001645 break;
1646 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001647 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001648 break;
1649 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001650 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001651 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001653
Chris Lattner423c5f42007-02-28 05:31:48 +00001654 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001655 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001656 MVT RegVT = VA.getLocVT();
1657 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001658 switch (VA.getLocReg()) {
1659 default:
1660 break;
1661 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1662 case X86::R8: {
1663 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001664 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001665 break;
1666 }
1667 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1668 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1669 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001670 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1671 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001672 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001673 break;
1674 }
1675 }
1676 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001677 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1678 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001679 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001680 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001681 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001682 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001683
Dan Gohman095cc292008-09-13 01:54:27 +00001684 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1685 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001686 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001687 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001688 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Evan Cheng32fe1032006-05-25 00:59:30 +00001690 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001691 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001692 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001693
Evan Cheng347d5f72006-04-28 21:29:37 +00001694 // Build a sequence of copy-to-reg nodes chained together with token chain
1695 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001696 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001697 // Tail call byval lowering might overwrite argument registers so in case of
1698 // tail call optimization the copies to registers are lowered later.
1699 if (!IsTailCall)
1700 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001701 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001702 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001703 InFlag = Chain.getValue(1);
1704 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001705
Evan Chengf4684712007-02-21 21:18:14 +00001706 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001707 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001708 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001709 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001710 DAG.getNode(X86ISD::GlobalBaseReg,
1711 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001712 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001713 InFlag);
1714 InFlag = Chain.getValue(1);
1715 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001716 // If we are tail calling and generating PIC/GOT style code load the address
1717 // of the callee into ecx. The value in ecx is used as target of the tail
1718 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1719 // calls on PIC/GOT architectures. Normally we would just put the address of
1720 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1721 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001722 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001723 // Note: The actual moving to ecx is done further down.
1724 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001725 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001726 !G->getGlobal()->hasProtectedVisibility())
1727 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001728 else if (isa<ExternalSymbolSDNode>(Callee))
1729 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001730 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001731
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 if (Is64Bit && isVarArg) {
1733 // From AMD64 ABI document:
1734 // For calls that may call functions that use varargs or stdargs
1735 // (prototype-less calls or calls to functions containing ellipsis (...) in
1736 // the declaration) %al is used as hidden argument to specify the number
1737 // of SSE registers used. The contents of %al do not need to match exactly
1738 // the number of registers, but must be an ubound on the number of SSE
1739 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001740
1741 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001742 // Count the number of XMM registers allocated.
1743 static const unsigned XMMArgRegs[] = {
1744 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1745 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1746 };
1747 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001748 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001749 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001750
Dale Johannesendd64c412009-02-04 00:33:20 +00001751 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001752 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1753 InFlag = Chain.getValue(1);
1754 }
1755
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001756
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001757 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001758 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SmallVector<SDValue, 8> MemOpChains2;
1760 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001761 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001762 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001763 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1765 CCValAssign &VA = ArgLocs[i];
1766 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001767 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001768 SDValue Arg = TheCall->getArg(i);
1769 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 // Create frame index.
1771 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001772 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001773 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001774 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001775
Duncan Sands276dcbd2008-03-21 09:14:45 +00001776 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001777 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001778 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001779 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001780 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001781 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001782 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001783
1784 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001785 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001786 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001787 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001788 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001789 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001790 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001791 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 }
1793 }
1794
1795 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001796 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001797 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001798
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001799 // Copy arguments to their registers.
1800 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001801 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001802 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001803 InFlag = Chain.getValue(1);
1804 }
Dan Gohman475871a2008-07-27 21:46:04 +00001805 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001806
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001808 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001809 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 }
1811
Evan Cheng32fe1032006-05-25 00:59:30 +00001812 // If the callee is a GlobalAddress node (quite common, every direct call is)
1813 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001814 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001815 // We should use extra load for direct calls to dllimported functions in
1816 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001817 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1818 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001819 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1820 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001821 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1822 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001823 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001824 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001825
Dale Johannesendd64c412009-02-04 00:33:20 +00001826 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001827 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 Callee,InFlag);
1829 Callee = DAG.getRegister(Opc, getPointerTy());
1830 // Add register as live out.
1831 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001832 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001833
Chris Lattnerd96d0722007-02-25 06:40:16 +00001834 // Returns a chain & a flag for retval copy to use.
1835 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001837
1838 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001839 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1840 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001841 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001842
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 // Returns a chain & a flag for retval copy to use.
1844 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1845 Ops.clear();
1846 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001847
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001848 Ops.push_back(Chain);
1849 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001850
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 if (IsTailCall)
1852 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001853
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 // Add argument registers to the end of the list so that they are known live
1855 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001856 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1857 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1858 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001859
Evan Cheng586ccac2008-03-18 23:36:35 +00001860 // Add an implicit use GOT pointer in EBX.
1861 if (!IsTailCall && !Is64Bit &&
1862 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1863 Subtarget->isPICStyleGOT())
1864 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1865
1866 // Add an implicit use of AL for x86 vararg functions.
1867 if (Is64Bit && isVarArg)
1868 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1869
Gabor Greifba36cb52008-08-28 21:40:38 +00001870 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001871 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001872
Gordon Henriksen86737662008-01-05 16:56:59 +00001873 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001874 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001875 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001876 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001877 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001878
Gabor Greifba36cb52008-08-28 21:40:38 +00001879 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001880 }
1881
Dale Johannesenace16102009-02-03 19:33:06 +00001882 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001883 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001884
Chris Lattner2d297092006-05-23 18:50:38 +00001885 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001886 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001887 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001888 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001889 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001890 // If this is is a call to a struct-return function, the callee
1891 // pops the hidden struct pointer, so we have to push it back.
1892 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001893 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001894 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001895 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001896
Gordon Henriksenae636f82008-01-03 16:47:34 +00001897 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001898 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001899 DAG.getIntPtrConstant(NumBytes, true),
1900 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1901 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001902 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001903 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001904
Chris Lattner3085e152007-02-25 08:59:22 +00001905 // Handle result values, copying them out of physregs into vregs that we
1906 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001907 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001908 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001909}
1910
Evan Cheng25ab6902006-09-08 06:48:29 +00001911
1912//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001913// Fast Calling Convention (tail call) implementation
1914//===----------------------------------------------------------------------===//
1915
1916// Like std call, callee cleans arguments, convention except that ECX is
1917// reserved for storing the tail called function address. Only 2 registers are
1918// free for argument passing (inreg). Tail call optimization is performed
1919// provided:
1920// * tailcallopt is enabled
1921// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001922// On X86_64 architecture with GOT-style position independent code only local
1923// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001924// To keep the stack aligned according to platform abi the function
1925// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1926// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001927// If a tail called function callee has more arguments than the caller the
1928// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001929// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001930// original REtADDR, but before the saved framepointer or the spilled registers
1931// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1932// stack layout:
1933// arg1
1934// arg2
1935// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001936// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001937// move area ]
1938// (possible EBP)
1939// ESI
1940// EDI
1941// local1 ..
1942
1943/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1944/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001945unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001946 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001947 MachineFunction &MF = DAG.getMachineFunction();
1948 const TargetMachine &TM = MF.getTarget();
1949 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1950 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001951 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001952 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001953 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001954 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1955 // Number smaller than 12 so just add the difference.
1956 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1957 } else {
1958 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001959 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001960 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001961 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001962 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001963}
1964
1965/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001966/// following the call is a return. A function is eligible if caller/callee
1967/// calling conventions match, currently only fastcc supports tail calls, and
1968/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001969bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001971 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001972 if (!PerformTailCallOpt)
1973 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001974
Dan Gohman095cc292008-09-13 01:54:27 +00001975 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001976 MachineFunction &MF = DAG.getMachineFunction();
1977 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001978 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001979 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001980 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001981 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001982 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001983 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001984 return true;
1985
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001986 // Can only do local tail calls (in same module, hidden or protected) on
1987 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001988 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1989 return G->getGlobal()->hasHiddenVisibility()
1990 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001991 }
1992 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001993
1994 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001995}
1996
Dan Gohman3df24e62008-09-03 23:12:08 +00001997FastISel *
1998X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001999 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002000 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002001 DenseMap<const Value *, unsigned> &vm,
2002 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002003 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002004 DenseMap<const AllocaInst *, int> &am
2005#ifndef NDEBUG
2006 , SmallSet<Instruction*, 8> &cil
2007#endif
2008 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002009 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002010#ifndef NDEBUG
2011 , cil
2012#endif
2013 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002014}
2015
2016
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002017//===----------------------------------------------------------------------===//
2018// Other Lowering Hooks
2019//===----------------------------------------------------------------------===//
2020
2021
Dan Gohman475871a2008-07-27 21:46:04 +00002022SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002023 MachineFunction &MF = DAG.getMachineFunction();
2024 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2025 int ReturnAddrIndex = FuncInfo->getRAIndex();
2026
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002027 if (ReturnAddrIndex == 0) {
2028 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002029 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002030 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002031 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002032 }
2033
Evan Cheng25ab6902006-09-08 06:48:29 +00002034 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002035}
2036
2037
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002038/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2039/// specific condition code, returning the condition code and the LHS/RHS of the
2040/// comparison to make.
2041static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2042 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002043 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002044 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2045 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2046 // X > -1 -> X == 0, jump !sign.
2047 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002048 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002049 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2050 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002051 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002052 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002053 // X < 1 -> X <= 0
2054 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002055 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002056 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002057 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002058
Evan Chengd9558e02006-01-06 00:43:03 +00002059 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002060 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002061 case ISD::SETEQ: return X86::COND_E;
2062 case ISD::SETGT: return X86::COND_G;
2063 case ISD::SETGE: return X86::COND_GE;
2064 case ISD::SETLT: return X86::COND_L;
2065 case ISD::SETLE: return X86::COND_LE;
2066 case ISD::SETNE: return X86::COND_NE;
2067 case ISD::SETULT: return X86::COND_B;
2068 case ISD::SETUGT: return X86::COND_A;
2069 case ISD::SETULE: return X86::COND_BE;
2070 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002071 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
Chris Lattner4c78e022008-12-23 23:42:27 +00002074 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002075
Chris Lattner4c78e022008-12-23 23:42:27 +00002076 // If LHS is a foldable load, but RHS is not, flip the condition.
2077 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2078 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2079 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2080 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002081 }
2082
Chris Lattner4c78e022008-12-23 23:42:27 +00002083 switch (SetCCOpcode) {
2084 default: break;
2085 case ISD::SETOLT:
2086 case ISD::SETOLE:
2087 case ISD::SETUGT:
2088 case ISD::SETUGE:
2089 std::swap(LHS, RHS);
2090 break;
2091 }
2092
2093 // On a floating point condition, the flags are set as follows:
2094 // ZF PF CF op
2095 // 0 | 0 | 0 | X > Y
2096 // 0 | 0 | 1 | X < Y
2097 // 1 | 0 | 0 | X == Y
2098 // 1 | 1 | 1 | unordered
2099 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002100 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002101 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002102 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002103 case ISD::SETOLT: // flipped
2104 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002105 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002106 case ISD::SETOLE: // flipped
2107 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002108 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002109 case ISD::SETUGT: // flipped
2110 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002111 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002112 case ISD::SETUGE: // flipped
2113 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002114 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002115 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002116 case ISD::SETNE: return X86::COND_NE;
2117 case ISD::SETUO: return X86::COND_P;
2118 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002119 }
Evan Chengd9558e02006-01-06 00:43:03 +00002120}
2121
Evan Cheng4a460802006-01-11 00:33:36 +00002122/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2123/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002124/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002125static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002126 switch (X86CC) {
2127 default:
2128 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002129 case X86::COND_B:
2130 case X86::COND_BE:
2131 case X86::COND_E:
2132 case X86::COND_P:
2133 case X86::COND_A:
2134 case X86::COND_AE:
2135 case X86::COND_NE:
2136 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002137 return true;
2138 }
2139}
2140
Nate Begeman9008ca62009-04-27 18:41:29 +00002141/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2142/// the specified range (L, H].
2143static bool isUndefOrInRange(int Val, int Low, int Hi) {
2144 return (Val < 0) || (Val >= Low && Val < Hi);
2145}
2146
2147/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2148/// specified value.
2149static bool isUndefOrEqual(int Val, int CmpVal) {
2150 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002151 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002152 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002153}
2154
Nate Begeman9008ca62009-04-27 18:41:29 +00002155/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2156/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2157/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002158static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002159 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2160 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2161 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2162 return (Mask[0] < 2 && Mask[1] < 2);
2163 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002164}
2165
Nate Begeman9008ca62009-04-27 18:41:29 +00002166bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2167 SmallVector<int, 8> M;
2168 N->getMask(M);
2169 return ::isPSHUFDMask(M, N->getValueType(0));
2170}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002171
Nate Begeman9008ca62009-04-27 18:41:29 +00002172/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2173/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002174static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002175 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002176 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002177
2178 // Lower quadword copied in order or undef.
2179 for (int i = 0; i != 4; ++i)
2180 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002181 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002182
Evan Cheng506d3df2006-03-29 23:07:14 +00002183 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002184 for (int i = 4; i != 8; ++i)
2185 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002186 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002187
Evan Cheng506d3df2006-03-29 23:07:14 +00002188 return true;
2189}
2190
Nate Begeman9008ca62009-04-27 18:41:29 +00002191bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2192 SmallVector<int, 8> M;
2193 N->getMask(M);
2194 return ::isPSHUFHWMask(M, N->getValueType(0));
2195}
Evan Cheng506d3df2006-03-29 23:07:14 +00002196
Nate Begeman9008ca62009-04-27 18:41:29 +00002197/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2198/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002199static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002200 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002201 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002202
Rafael Espindola15684b22009-04-24 12:40:33 +00002203 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002204 for (int i = 4; i != 8; ++i)
2205 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002206 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002207
Rafael Espindola15684b22009-04-24 12:40:33 +00002208 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002209 for (int i = 0; i != 4; ++i)
2210 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002211 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002212
Rafael Espindola15684b22009-04-24 12:40:33 +00002213 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002214}
2215
Nate Begeman9008ca62009-04-27 18:41:29 +00002216bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2217 SmallVector<int, 8> M;
2218 N->getMask(M);
2219 return ::isPSHUFLWMask(M, N->getValueType(0));
2220}
2221
Evan Cheng14aed5e2006-03-24 01:18:28 +00002222/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2223/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002224static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002225 int NumElems = VT.getVectorNumElements();
2226 if (NumElems != 2 && NumElems != 4)
2227 return false;
2228
2229 int Half = NumElems / 2;
2230 for (int i = 0; i < Half; ++i)
2231 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002232 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002233 for (int i = Half; i < NumElems; ++i)
2234 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002235 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002236
Evan Cheng14aed5e2006-03-24 01:18:28 +00002237 return true;
2238}
2239
Nate Begeman9008ca62009-04-27 18:41:29 +00002240bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2241 SmallVector<int, 8> M;
2242 N->getMask(M);
2243 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002244}
2245
Evan Cheng213d2cf2007-05-17 18:45:50 +00002246/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002247/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2248/// half elements to come from vector 1 (which would equal the dest.) and
2249/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002250static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002251 int NumElems = VT.getVectorNumElements();
2252
2253 if (NumElems != 2 && NumElems != 4)
2254 return false;
2255
2256 int Half = NumElems / 2;
2257 for (int i = 0; i < Half; ++i)
2258 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002259 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002260 for (int i = Half; i < NumElems; ++i)
2261 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002262 return false;
2263 return true;
2264}
2265
Nate Begeman9008ca62009-04-27 18:41:29 +00002266static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2267 SmallVector<int, 8> M;
2268 N->getMask(M);
2269 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002270}
2271
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002272/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2273/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002274bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2275 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002276 return false;
2277
Evan Cheng2064a2b2006-03-28 06:50:32 +00002278 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002279 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2280 isUndefOrEqual(N->getMaskElt(1), 7) &&
2281 isUndefOrEqual(N->getMaskElt(2), 2) &&
2282 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002283}
2284
Evan Cheng5ced1d82006-04-06 23:23:56 +00002285/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2286/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002287bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2288 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002289
Evan Cheng5ced1d82006-04-06 23:23:56 +00002290 if (NumElems != 2 && NumElems != 4)
2291 return false;
2292
Evan Chengc5cdff22006-04-07 21:53:05 +00002293 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002294 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002295 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002296
Evan Chengc5cdff22006-04-07 21:53:05 +00002297 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002298 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002299 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002300
2301 return true;
2302}
2303
2304/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002305/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2306/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002307bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2308 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002309
Evan Cheng5ced1d82006-04-06 23:23:56 +00002310 if (NumElems != 2 && NumElems != 4)
2311 return false;
2312
Evan Chengc5cdff22006-04-07 21:53:05 +00002313 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002314 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002315 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002316
Nate Begeman9008ca62009-04-27 18:41:29 +00002317 for (unsigned i = 0; i < NumElems/2; ++i)
2318 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002319 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002320
2321 return true;
2322}
2323
Nate Begeman9008ca62009-04-27 18:41:29 +00002324/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2325/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2326/// <2, 3, 2, 3>
2327bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2328 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2329
2330 if (NumElems != 4)
2331 return false;
2332
2333 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2334 isUndefOrEqual(N->getMaskElt(1), 3) &&
2335 isUndefOrEqual(N->getMaskElt(2), 2) &&
2336 isUndefOrEqual(N->getMaskElt(3), 3);
2337}
2338
Evan Cheng0038e592006-03-28 00:39:58 +00002339/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2340/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002341static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002342 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002343 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002344 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002345 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002346
2347 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2348 int BitI = Mask[i];
2349 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002350 if (!isUndefOrEqual(BitI, j))
2351 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002352 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002353 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002354 return false;
2355 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002356 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002357 return false;
2358 }
Evan Cheng0038e592006-03-28 00:39:58 +00002359 }
Evan Cheng0038e592006-03-28 00:39:58 +00002360 return true;
2361}
2362
Nate Begeman9008ca62009-04-27 18:41:29 +00002363bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2364 SmallVector<int, 8> M;
2365 N->getMask(M);
2366 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002367}
2368
Evan Cheng4fcb9222006-03-28 02:43:26 +00002369/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2370/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002371static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002372 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002373 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002374 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002375 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002376
2377 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2378 int BitI = Mask[i];
2379 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002380 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002381 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002382 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002383 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002384 return false;
2385 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002386 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002387 return false;
2388 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002389 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002390 return true;
2391}
2392
Nate Begeman9008ca62009-04-27 18:41:29 +00002393bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2394 SmallVector<int, 8> M;
2395 N->getMask(M);
2396 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002397}
2398
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002399/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2400/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2401/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002402static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002403 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002404 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002405 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002406
2407 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2408 int BitI = Mask[i];
2409 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002410 if (!isUndefOrEqual(BitI, j))
2411 return false;
2412 if (!isUndefOrEqual(BitI1, j))
2413 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002414 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002415 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002416}
2417
Nate Begeman9008ca62009-04-27 18:41:29 +00002418bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2419 SmallVector<int, 8> M;
2420 N->getMask(M);
2421 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2422}
2423
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002424/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2425/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2426/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002427static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002428 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002429 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2430 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002431
2432 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2433 int BitI = Mask[i];
2434 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002435 if (!isUndefOrEqual(BitI, j))
2436 return false;
2437 if (!isUndefOrEqual(BitI1, j))
2438 return false;
2439 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002440 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002441}
2442
Nate Begeman9008ca62009-04-27 18:41:29 +00002443bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2444 SmallVector<int, 8> M;
2445 N->getMask(M);
2446 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2447}
2448
Evan Cheng017dcc62006-04-21 01:05:10 +00002449/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2450/// specifies a shuffle of elements that is suitable for input to MOVSS,
2451/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002452static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002453 int NumElts = VT.getVectorNumElements();
Evan Cheng10762102007-12-06 22:14:22 +00002454 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002455 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002456
2457 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002458 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002459
2460 for (int i = 1; i < NumElts; ++i)
2461 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002462 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002463
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002464 return true;
2465}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002466
Nate Begeman9008ca62009-04-27 18:41:29 +00002467bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2468 SmallVector<int, 8> M;
2469 N->getMask(M);
2470 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002471}
2472
Evan Cheng017dcc62006-04-21 01:05:10 +00002473/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2474/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002475/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002476static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002477 bool V2IsSplat = false, bool V2IsUndef = false) {
2478 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002479 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002480 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002481
2482 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002483 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002484
2485 for (int i = 1; i < NumOps; ++i)
2486 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2487 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2488 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002489 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002490
Evan Cheng39623da2006-04-20 08:58:49 +00002491 return true;
2492}
2493
Nate Begeman9008ca62009-04-27 18:41:29 +00002494static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002495 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002496 SmallVector<int, 8> M;
2497 N->getMask(M);
2498 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002499}
2500
Evan Chengd9539472006-04-14 21:59:03 +00002501/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2502/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002503bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2504 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002505 return false;
2506
2507 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002508 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002509 int Elt = N->getMaskElt(i);
2510 if (Elt >= 0 && Elt != 1)
2511 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002512 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002513
2514 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002515 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002516 int Elt = N->getMaskElt(i);
2517 if (Elt >= 0 && Elt != 3)
2518 return false;
2519 if (Elt == 3)
2520 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002521 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002522 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002523 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002524 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002525}
2526
2527/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2528/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002529bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2530 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002531 return false;
2532
2533 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002534 for (unsigned i = 0; i < 2; ++i)
2535 if (N->getMaskElt(i) > 0)
2536 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002537
2538 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002539 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002540 int Elt = N->getMaskElt(i);
2541 if (Elt >= 0 && Elt != 2)
2542 return false;
2543 if (Elt == 2)
2544 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002545 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002547 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002548}
2549
Evan Cheng0b457f02008-09-25 20:50:48 +00002550/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2551/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002552bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2553 int e = N->getValueType(0).getVectorNumElements() / 2;
2554
2555 for (int i = 0; i < e; ++i)
2556 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002557 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002558 for (int i = 0; i < e; ++i)
2559 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002560 return false;
2561 return true;
2562}
2563
Evan Cheng63d33002006-03-22 08:01:21 +00002564/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2565/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2566/// instructions.
2567unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002568 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2569 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2570
Evan Chengb9df0ca2006-03-22 02:53:00 +00002571 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2572 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002573 for (int i = 0; i < NumOperands; ++i) {
2574 int Val = SVOp->getMaskElt(NumOperands-i-1);
2575 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002576 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002577 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002578 if (i != NumOperands - 1)
2579 Mask <<= Shift;
2580 }
Evan Cheng63d33002006-03-22 08:01:21 +00002581 return Mask;
2582}
2583
Evan Cheng506d3df2006-03-29 23:07:14 +00002584/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2585/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2586/// instructions.
2587unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002588 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002589 unsigned Mask = 0;
2590 // 8 nodes, but we only care about the last 4.
2591 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002592 int Val = SVOp->getMaskElt(i);
2593 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002594 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002595 if (i != 4)
2596 Mask <<= 2;
2597 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002598 return Mask;
2599}
2600
2601/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2602/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2603/// instructions.
2604unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002606 unsigned Mask = 0;
2607 // 8 nodes, but we only care about the first 4.
2608 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002609 int Val = SVOp->getMaskElt(i);
2610 if (Val >= 0)
2611 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002612 if (i != 0)
2613 Mask <<= 2;
2614 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002615 return Mask;
2616}
2617
Nate Begeman9008ca62009-04-27 18:41:29 +00002618/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2619/// their permute mask.
2620static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2621 SelectionDAG &DAG) {
2622 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002623 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 SmallVector<int, 8> MaskVec;
2625
Nate Begeman5a5ca152009-04-29 05:20:52 +00002626 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002627 int idx = SVOp->getMaskElt(i);
2628 if (idx < 0)
2629 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002630 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002631 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002632 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002634 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002635 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2636 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002637}
2638
Evan Cheng779ccea2007-12-07 21:30:01 +00002639/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2640/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002641static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002642 unsigned NumElems = VT.getVectorNumElements();
2643 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002644 int idx = Mask[i];
2645 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002646 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002647 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002648 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002649 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002650 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002651 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002652}
2653
Evan Cheng533a0aa2006-04-19 20:35:22 +00002654/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2655/// match movhlps. The lower half elements should come from upper half of
2656/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002657/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002658static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2659 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002660 return false;
2661 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002663 return false;
2664 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002665 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002666 return false;
2667 return true;
2668}
2669
Evan Cheng5ced1d82006-04-06 23:23:56 +00002670/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002671/// is promoted to a vector. It also returns the LoadSDNode by reference if
2672/// required.
2673static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002674 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2675 return false;
2676 N = N->getOperand(0).getNode();
2677 if (!ISD::isNON_EXTLoad(N))
2678 return false;
2679 if (LD)
2680 *LD = cast<LoadSDNode>(N);
2681 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002682}
2683
Evan Cheng533a0aa2006-04-19 20:35:22 +00002684/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2685/// match movlp{s|d}. The lower half elements should come from lower half of
2686/// V1 (and in order), and the upper half elements should come from the upper
2687/// half of V2 (and in order). And since V1 will become the source of the
2688/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002689static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2690 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002691 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002692 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002693 // Is V2 is a vector load, don't do this transformation. We will try to use
2694 // load folding shufps op.
2695 if (ISD::isNON_EXTLoad(V2))
2696 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002697
Nate Begeman5a5ca152009-04-29 05:20:52 +00002698 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002699
Evan Cheng533a0aa2006-04-19 20:35:22 +00002700 if (NumElems != 2 && NumElems != 4)
2701 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002702 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002703 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002704 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002705 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002706 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002707 return false;
2708 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002709}
2710
Evan Cheng39623da2006-04-20 08:58:49 +00002711/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2712/// all the same.
2713static bool isSplatVector(SDNode *N) {
2714 if (N->getOpcode() != ISD::BUILD_VECTOR)
2715 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002716
Dan Gohman475871a2008-07-27 21:46:04 +00002717 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002718 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2719 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002720 return false;
2721 return true;
2722}
2723
Evan Cheng213d2cf2007-05-17 18:45:50 +00002724/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2725/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002726static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002727 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002728 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002729 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002730 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002731}
2732
2733/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002734/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002735/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002736static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002737 SDValue V1 = N->getOperand(0);
2738 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002739 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2740 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002741 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002742 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002743 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002744 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2745 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002746 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2747 return false;
2748 } else if (Idx >= 0) {
2749 unsigned Opc = V1.getOpcode();
2750 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2751 continue;
2752 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002753 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002754 }
2755 }
2756 return true;
2757}
2758
2759/// getZeroVector - Returns a vector of specified type with all zero elements.
2760///
Dale Johannesenace16102009-02-03 19:33:06 +00002761static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2762 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002763 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002764
Chris Lattner8a594482007-11-25 00:24:49 +00002765 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2766 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002767 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002768 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002769 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002770 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002771 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002772 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002773 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002774 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002775 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002776 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002777 }
Dale Johannesenace16102009-02-03 19:33:06 +00002778 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002779}
2780
Chris Lattner8a594482007-11-25 00:24:49 +00002781/// getOnesVector - Returns a vector of specified type with all bits set.
2782///
Dale Johannesenace16102009-02-03 19:33:06 +00002783static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002784 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002785
Chris Lattner8a594482007-11-25 00:24:49 +00002786 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2787 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002788 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2789 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002790 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002791 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002792 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002793 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002794 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002795}
2796
2797
Evan Cheng39623da2006-04-20 08:58:49 +00002798/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2799/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002800static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2801 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002802 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002803
Evan Cheng39623da2006-04-20 08:58:49 +00002804 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 SmallVector<int, 8> MaskVec;
2806 SVOp->getMask(MaskVec);
2807
Nate Begeman5a5ca152009-04-29 05:20:52 +00002808 for (unsigned i = 0; i != NumElems; ++i) {
2809 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002810 MaskVec[i] = NumElems;
2811 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002812 }
Evan Cheng39623da2006-04-20 08:58:49 +00002813 }
Evan Cheng39623da2006-04-20 08:58:49 +00002814 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002815 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2816 SVOp->getOperand(1), &MaskVec[0]);
2817 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002818}
2819
Evan Cheng017dcc62006-04-21 01:05:10 +00002820/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2821/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002822static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2823 SDValue V2) {
2824 unsigned NumElems = VT.getVectorNumElements();
2825 SmallVector<int, 8> Mask;
2826 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002827 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 Mask.push_back(i);
2829 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002830}
2831
Nate Begeman9008ca62009-04-27 18:41:29 +00002832/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2833static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2834 SDValue V2) {
2835 unsigned NumElems = VT.getVectorNumElements();
2836 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002837 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002838 Mask.push_back(i);
2839 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002840 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002841 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002842}
2843
Nate Begeman9008ca62009-04-27 18:41:29 +00002844/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2845static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2846 SDValue V2) {
2847 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002848 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002850 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002851 Mask.push_back(i + Half);
2852 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002853 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002855}
2856
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002857/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002858static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2859 bool HasSSE2) {
2860 if (SV->getValueType(0).getVectorNumElements() <= 4)
2861 return SDValue(SV, 0);
2862
2863 MVT PVT = MVT::v4f32;
2864 MVT VT = SV->getValueType(0);
2865 DebugLoc dl = SV->getDebugLoc();
2866 SDValue V1 = SV->getOperand(0);
2867 int NumElems = VT.getVectorNumElements();
2868 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002869
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 // unpack elements to the correct location
2871 while (NumElems > 4) {
2872 if (EltNo < NumElems/2) {
2873 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2874 } else {
2875 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2876 EltNo -= NumElems/2;
2877 }
2878 NumElems >>= 1;
2879 }
2880
2881 // Perform the splat.
2882 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002883 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002884 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2885 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002886}
2887
Evan Chengba05f722006-04-21 23:03:30 +00002888/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002889/// vector of zero or undef vector. This produces a shuffle where the low
2890/// element of V2 is swizzled into the zero/undef vector, landing at element
2891/// 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 +00002892static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002893 bool isZero, bool HasSSE2,
2894 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002896 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002897 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2898 unsigned NumElems = VT.getVectorNumElements();
2899 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002900 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002901 // If this is the insertion idx, put the low elt of V2 here.
2902 MaskVec.push_back(i == Idx ? NumElems : i);
2903 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002904}
2905
Evan Chengf26ffe92008-05-29 08:22:04 +00002906/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2907/// a shuffle that is zero.
2908static
Nate Begeman9008ca62009-04-27 18:41:29 +00002909unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2910 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002911 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002912 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002913 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 int Idx = SVOp->getMaskElt(Index);
2915 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002916 ++NumZeros;
2917 continue;
2918 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002919 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002920 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002921 ++NumZeros;
2922 else
2923 break;
2924 }
2925 return NumZeros;
2926}
2927
2928/// isVectorShift - Returns true if the shuffle can be implemented as a
2929/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002930/// FIXME: split into pslldqi, psrldqi, palignr variants.
2931static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002932 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002933 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002934
2935 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002936 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002937 if (!NumZeros) {
2938 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002940 if (!NumZeros)
2941 return false;
2942 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002943 bool SeenV1 = false;
2944 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002945 for (int i = NumZeros; i < NumElems; ++i) {
2946 int Val = isLeft ? (i - NumZeros) : i;
2947 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2948 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002949 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002951 SeenV1 = true;
2952 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002954 SeenV2 = true;
2955 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002956 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002957 return false;
2958 }
2959 if (SeenV1 && SeenV2)
2960 return false;
2961
Nate Begeman9008ca62009-04-27 18:41:29 +00002962 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002963 ShAmt = NumZeros;
2964 return true;
2965}
2966
2967
Evan Chengc78d3b42006-04-24 18:01:45 +00002968/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2969///
Dan Gohman475871a2008-07-27 21:46:04 +00002970static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002971 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002972 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002973 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002974 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002975
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002976 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002977 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002978 bool First = true;
2979 for (unsigned i = 0; i < 16; ++i) {
2980 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2981 if (ThisIsNonZero && First) {
2982 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002983 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002984 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002985 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002986 First = false;
2987 }
2988
2989 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002991 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2992 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002993 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002994 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002995 }
2996 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002997 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2998 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002999 ThisElt, DAG.getConstant(8, MVT::i8));
3000 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003001 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003002 } else
3003 ThisElt = LastElt;
3004
Gabor Greifba36cb52008-08-28 21:40:38 +00003005 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003006 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003007 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003008 }
3009 }
3010
Dale Johannesenace16102009-02-03 19:33:06 +00003011 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003012}
3013
Bill Wendlinga348c562007-03-22 18:42:45 +00003014/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003015///
Dan Gohman475871a2008-07-27 21:46:04 +00003016static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003017 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003018 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003019 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003020 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003021
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003022 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003023 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003024 bool First = true;
3025 for (unsigned i = 0; i < 8; ++i) {
3026 bool isNonZero = (NonZeros & (1 << i)) != 0;
3027 if (isNonZero) {
3028 if (First) {
3029 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003030 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003031 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003032 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003033 First = false;
3034 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003035 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003036 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003037 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003038 }
3039 }
3040
3041 return V;
3042}
3043
Evan Chengf26ffe92008-05-29 08:22:04 +00003044/// getVShift - Return a vector logical shift node.
3045///
Dan Gohman475871a2008-07-27 21:46:04 +00003046static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 unsigned NumBits, SelectionDAG &DAG,
3048 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003049 bool isMMX = VT.getSizeInBits() == 64;
3050 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003051 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003052 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3053 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3054 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003055 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003056}
3057
Dan Gohman475871a2008-07-27 21:46:04 +00003058SDValue
3059X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003060 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003061 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003062 if (ISD::isBuildVectorAllZeros(Op.getNode())
3063 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003064 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3065 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3066 // eliminated on x86-32 hosts.
3067 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3068 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003069
Gabor Greifba36cb52008-08-28 21:40:38 +00003070 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003071 return getOnesVector(Op.getValueType(), DAG, dl);
3072 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003073 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003074
Duncan Sands83ec4b62008-06-06 12:08:01 +00003075 MVT VT = Op.getValueType();
3076 MVT EVT = VT.getVectorElementType();
3077 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003078
3079 unsigned NumElems = Op.getNumOperands();
3080 unsigned NumZero = 0;
3081 unsigned NumNonZero = 0;
3082 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003083 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003085 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003086 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003087 if (Elt.getOpcode() == ISD::UNDEF)
3088 continue;
3089 Values.insert(Elt);
3090 if (Elt.getOpcode() != ISD::Constant &&
3091 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003092 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003093 if (isZeroNode(Elt))
3094 NumZero++;
3095 else {
3096 NonZeros |= (1 << i);
3097 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003098 }
3099 }
3100
Dan Gohman7f321562007-06-25 16:23:39 +00003101 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003102 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003103 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003104 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003105
Chris Lattner67f453a2008-03-09 05:42:06 +00003106 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003107 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003108 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003109 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003110
Chris Lattner62098042008-03-09 01:05:04 +00003111 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3112 // the value are obviously zero, truncate the value to i32 and do the
3113 // insertion that way. Only do this if the value is non-constant or if the
3114 // value is a constant being inserted into element 0. It is cheaper to do
3115 // a constant pool load than it is to do a movd + shuffle.
3116 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3117 (!IsAllConstants || Idx == 0)) {
3118 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3119 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003120 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3121 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003122
Chris Lattner62098042008-03-09 01:05:04 +00003123 // Truncate the value (which may itself be a constant) to i32, and
3124 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003125 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3126 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003127 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3128 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003129
Chris Lattner62098042008-03-09 01:05:04 +00003130 // Now we have our 32-bit value zero extended in the low element of
3131 // a vector. If Idx != 0, swizzle it into place.
3132 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003133 SmallVector<int, 4> Mask;
3134 Mask.push_back(Idx);
3135 for (unsigned i = 1; i != VecElts; ++i)
3136 Mask.push_back(i);
3137 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3138 DAG.getUNDEF(Item.getValueType()),
3139 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003140 }
Dale Johannesenace16102009-02-03 19:33:06 +00003141 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003142 }
3143 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003144
Chris Lattner19f79692008-03-08 22:59:52 +00003145 // If we have a constant or non-constant insertion into the low element of
3146 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3147 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3148 // depending on what the source datatype is. Because we can only get here
3149 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3150 if (Idx == 0 &&
3151 // Don't do this for i64 values on x86-32.
3152 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003153 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003154 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003155 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3156 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003157 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003158
3159 // Is it a vector logical left shift?
3160 if (NumElems == 2 && Idx == 1 &&
3161 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003162 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003163 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003164 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003165 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003166 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003168
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003169 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003170 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003171
Chris Lattner19f79692008-03-08 22:59:52 +00003172 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3173 // is a non-constant being inserted into an element other than the low one,
3174 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3175 // movd/movss) to move this into the low element, then shuffle it into
3176 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003177 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003178 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003179
Evan Cheng0db9fe62006-04-25 20:13:52 +00003180 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003181 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3182 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003184 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 MaskVec.push_back(i == Idx ? 0 : 1);
3186 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003187 }
3188 }
3189
Chris Lattner67f453a2008-03-09 05:42:06 +00003190 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3191 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003192 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003193
Dan Gohmana3941172007-07-24 22:55:08 +00003194 // A vector full of immediates; various special cases are already
3195 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003196 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003197 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003198
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003199 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003200 if (EVTBits == 64) {
3201 if (NumNonZero == 1) {
3202 // One half is zero or undef.
3203 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003204 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003205 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003206 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3207 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003208 }
Dan Gohman475871a2008-07-27 21:46:04 +00003209 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003210 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003211
3212 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003213 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003214 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003215 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003216 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003217 }
3218
Bill Wendling826f36f2007-03-28 00:57:11 +00003219 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003220 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003221 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003222 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223 }
3224
3225 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003226 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003227 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003228 if (NumElems == 4 && NumZero > 0) {
3229 for (unsigned i = 0; i < 4; ++i) {
3230 bool isZero = !(NonZeros & (1 << i));
3231 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003232 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003233 else
Dale Johannesenace16102009-02-03 19:33:06 +00003234 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003235 }
3236
3237 for (unsigned i = 0; i < 2; ++i) {
3238 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3239 default: break;
3240 case 0:
3241 V[i] = V[i*2]; // Must be a zero vector.
3242 break;
3243 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003245 break;
3246 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003248 break;
3249 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003251 break;
3252 }
3253 }
3254
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003256 bool Reverse = (NonZeros & 0x3) == 2;
3257 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003259 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3260 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003261 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3262 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003263 }
3264
3265 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003266 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3267 // values to be inserted is equal to the number of elements, in which case
3268 // use the unpack code below in the hopes of matching the consecutive elts
3269 // load merge pattern for shuffles.
3270 // FIXME: We could probably just check that here directly.
3271 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3272 getSubtarget()->hasSSE41()) {
3273 V[0] = DAG.getUNDEF(VT);
3274 for (unsigned i = 0; i < NumElems; ++i)
3275 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3276 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3277 Op.getOperand(i), DAG.getIntPtrConstant(i));
3278 return V[0];
3279 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003280 // Expand into a number of unpckl*.
3281 // e.g. for v4f32
3282 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3283 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3284 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003285 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003286 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003287 NumElems >>= 1;
3288 while (NumElems != 0) {
3289 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003290 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003291 NumElems >>= 1;
3292 }
3293 return V[0];
3294 }
3295
Dan Gohman475871a2008-07-27 21:46:04 +00003296 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003297}
3298
Nate Begemanb9a47b82009-02-23 08:49:38 +00003299// v8i16 shuffles - Prefer shuffles in the following order:
3300// 1. [all] pshuflw, pshufhw, optional move
3301// 2. [ssse3] 1 x pshufb
3302// 3. [ssse3] 2 x pshufb + 1 x por
3303// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003304static
Nate Begeman9008ca62009-04-27 18:41:29 +00003305SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3306 SelectionDAG &DAG, X86TargetLowering &TLI) {
3307 SDValue V1 = SVOp->getOperand(0);
3308 SDValue V2 = SVOp->getOperand(1);
3309 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003310 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003311
Nate Begemanb9a47b82009-02-23 08:49:38 +00003312 // Determine if more than 1 of the words in each of the low and high quadwords
3313 // of the result come from the same quadword of one of the two inputs. Undef
3314 // mask values count as coming from any quadword, for better codegen.
3315 SmallVector<unsigned, 4> LoQuad(4);
3316 SmallVector<unsigned, 4> HiQuad(4);
3317 BitVector InputQuads(4);
3318 for (unsigned i = 0; i < 8; ++i) {
3319 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003320 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003321 MaskVals.push_back(EltIdx);
3322 if (EltIdx < 0) {
3323 ++Quad[0];
3324 ++Quad[1];
3325 ++Quad[2];
3326 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003327 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003328 }
3329 ++Quad[EltIdx / 4];
3330 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003331 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003332
Nate Begemanb9a47b82009-02-23 08:49:38 +00003333 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003334 unsigned MaxQuad = 1;
3335 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003336 if (LoQuad[i] > MaxQuad) {
3337 BestLoQuad = i;
3338 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003339 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003340 }
3341
Nate Begemanb9a47b82009-02-23 08:49:38 +00003342 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003343 MaxQuad = 1;
3344 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003345 if (HiQuad[i] > MaxQuad) {
3346 BestHiQuad = i;
3347 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003348 }
3349 }
3350
Nate Begemanb9a47b82009-02-23 08:49:38 +00003351 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3352 // of the two input vectors, shuffle them into one input vector so only a
3353 // single pshufb instruction is necessary. If There are more than 2 input
3354 // quads, disable the next transformation since it does not help SSSE3.
3355 bool V1Used = InputQuads[0] || InputQuads[1];
3356 bool V2Used = InputQuads[2] || InputQuads[3];
3357 if (TLI.getSubtarget()->hasSSSE3()) {
3358 if (InputQuads.count() == 2 && V1Used && V2Used) {
3359 BestLoQuad = InputQuads.find_first();
3360 BestHiQuad = InputQuads.find_next(BestLoQuad);
3361 }
3362 if (InputQuads.count() > 2) {
3363 BestLoQuad = -1;
3364 BestHiQuad = -1;
3365 }
3366 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003367
Nate Begemanb9a47b82009-02-23 08:49:38 +00003368 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3369 // the shuffle mask. If a quad is scored as -1, that means that it contains
3370 // words from all 4 input quadwords.
3371 SDValue NewV;
3372 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003373 SmallVector<int, 8> MaskV;
3374 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3375 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3376 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3377 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3378 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003379 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003380
Nate Begemanb9a47b82009-02-23 08:49:38 +00003381 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3382 // source words for the shuffle, to aid later transformations.
3383 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003384 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003385 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003386 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003387 if (idx != (int)i)
3388 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003389 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003390 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003391 AllWordsInNewV = false;
3392 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003393 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003394
Nate Begemanb9a47b82009-02-23 08:49:38 +00003395 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3396 if (AllWordsInNewV) {
3397 for (int i = 0; i != 8; ++i) {
3398 int idx = MaskVals[i];
3399 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003400 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003401 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3402 if ((idx != i) && idx < 4)
3403 pshufhw = false;
3404 if ((idx != i) && idx > 3)
3405 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003406 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003407 V1 = NewV;
3408 V2Used = false;
3409 BestLoQuad = 0;
3410 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003411 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003412
Nate Begemanb9a47b82009-02-23 08:49:38 +00003413 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3414 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003415 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003416 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3417 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003418 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003419 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003420
3421 // If we have SSSE3, and all words of the result are from 1 input vector,
3422 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3423 // is present, fall back to case 4.
3424 if (TLI.getSubtarget()->hasSSSE3()) {
3425 SmallVector<SDValue,16> pshufbMask;
3426
3427 // If we have elements from both input vectors, set the high bit of the
3428 // shuffle mask element to zero out elements that come from V2 in the V1
3429 // mask, and elements that come from V1 in the V2 mask, so that the two
3430 // results can be OR'd together.
3431 bool TwoInputs = V1Used && V2Used;
3432 for (unsigned i = 0; i != 8; ++i) {
3433 int EltIdx = MaskVals[i] * 2;
3434 if (TwoInputs && (EltIdx >= 16)) {
3435 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3436 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3437 continue;
3438 }
3439 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3440 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3441 }
3442 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3443 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003444 DAG.getNode(ISD::BUILD_VECTOR, dl,
3445 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003446 if (!TwoInputs)
3447 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3448
3449 // Calculate the shuffle mask for the second input, shuffle it, and
3450 // OR it with the first shuffled input.
3451 pshufbMask.clear();
3452 for (unsigned i = 0; i != 8; ++i) {
3453 int EltIdx = MaskVals[i] * 2;
3454 if (EltIdx < 16) {
3455 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3456 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3457 continue;
3458 }
3459 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3460 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3461 }
3462 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3463 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003464 DAG.getNode(ISD::BUILD_VECTOR, dl,
3465 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003466 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3467 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3468 }
3469
3470 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3471 // and update MaskVals with new element order.
3472 BitVector InOrder(8);
3473 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003475 for (int i = 0; i != 4; ++i) {
3476 int idx = MaskVals[i];
3477 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003479 InOrder.set(i);
3480 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003481 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003482 InOrder.set(i);
3483 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003484 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003485 }
3486 }
3487 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 MaskV.push_back(i);
3489 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3490 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003491 }
3492
3493 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3494 // and update MaskVals with the new element order.
3495 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003496 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003497 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003498 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003499 for (unsigned i = 4; i != 8; ++i) {
3500 int idx = MaskVals[i];
3501 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003502 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003503 InOrder.set(i);
3504 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003505 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003506 InOrder.set(i);
3507 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003509 }
3510 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003511 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3512 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003513 }
3514
3515 // In case BestHi & BestLo were both -1, which means each quadword has a word
3516 // from each of the four input quadwords, calculate the InOrder bitvector now
3517 // before falling through to the insert/extract cleanup.
3518 if (BestLoQuad == -1 && BestHiQuad == -1) {
3519 NewV = V1;
3520 for (int i = 0; i != 8; ++i)
3521 if (MaskVals[i] < 0 || MaskVals[i] == i)
3522 InOrder.set(i);
3523 }
3524
3525 // The other elements are put in the right place using pextrw and pinsrw.
3526 for (unsigned i = 0; i != 8; ++i) {
3527 if (InOrder[i])
3528 continue;
3529 int EltIdx = MaskVals[i];
3530 if (EltIdx < 0)
3531 continue;
3532 SDValue ExtOp = (EltIdx < 8)
3533 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3534 DAG.getIntPtrConstant(EltIdx))
3535 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3536 DAG.getIntPtrConstant(EltIdx - 8));
3537 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3538 DAG.getIntPtrConstant(i));
3539 }
3540 return NewV;
3541}
3542
3543// v16i8 shuffles - Prefer shuffles in the following order:
3544// 1. [ssse3] 1 x pshufb
3545// 2. [ssse3] 2 x pshufb + 1 x por
3546// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3547static
Nate Begeman9008ca62009-04-27 18:41:29 +00003548SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3549 SelectionDAG &DAG, X86TargetLowering &TLI) {
3550 SDValue V1 = SVOp->getOperand(0);
3551 SDValue V2 = SVOp->getOperand(1);
3552 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003553 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003554 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003555
3556 // If we have SSSE3, case 1 is generated when all result bytes come from
3557 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3558 // present, fall back to case 3.
3559 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3560 bool V1Only = true;
3561 bool V2Only = true;
3562 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003564 if (EltIdx < 0)
3565 continue;
3566 if (EltIdx < 16)
3567 V2Only = false;
3568 else
3569 V1Only = false;
3570 }
3571
3572 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3573 if (TLI.getSubtarget()->hasSSSE3()) {
3574 SmallVector<SDValue,16> pshufbMask;
3575
3576 // If all result elements are from one input vector, then only translate
3577 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3578 //
3579 // Otherwise, we have elements from both input vectors, and must zero out
3580 // elements that come from V2 in the first mask, and V1 in the second mask
3581 // so that we can OR them together.
3582 bool TwoInputs = !(V1Only || V2Only);
3583 for (unsigned i = 0; i != 16; ++i) {
3584 int EltIdx = MaskVals[i];
3585 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3586 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3587 continue;
3588 }
3589 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3590 }
3591 // If all the elements are from V2, assign it to V1 and return after
3592 // building the first pshufb.
3593 if (V2Only)
3594 V1 = V2;
3595 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003596 DAG.getNode(ISD::BUILD_VECTOR, dl,
3597 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003598 if (!TwoInputs)
3599 return V1;
3600
3601 // Calculate the shuffle mask for the second input, shuffle it, and
3602 // OR it with the first shuffled input.
3603 pshufbMask.clear();
3604 for (unsigned i = 0; i != 16; ++i) {
3605 int EltIdx = MaskVals[i];
3606 if (EltIdx < 16) {
3607 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3608 continue;
3609 }
3610 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3611 }
3612 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003613 DAG.getNode(ISD::BUILD_VECTOR, dl,
3614 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003615 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3616 }
3617
3618 // No SSSE3 - Calculate in place words and then fix all out of place words
3619 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3620 // the 16 different words that comprise the two doublequadword input vectors.
3621 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3622 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3623 SDValue NewV = V2Only ? V2 : V1;
3624 for (int i = 0; i != 8; ++i) {
3625 int Elt0 = MaskVals[i*2];
3626 int Elt1 = MaskVals[i*2+1];
3627
3628 // This word of the result is all undef, skip it.
3629 if (Elt0 < 0 && Elt1 < 0)
3630 continue;
3631
3632 // This word of the result is already in the correct place, skip it.
3633 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3634 continue;
3635 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3636 continue;
3637
3638 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3639 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3640 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003641
3642 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3643 // using a single extract together, load it and store it.
3644 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3645 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3646 DAG.getIntPtrConstant(Elt1 / 2));
3647 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3648 DAG.getIntPtrConstant(i));
3649 continue;
3650 }
3651
Nate Begemanb9a47b82009-02-23 08:49:38 +00003652 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003653 // source byte is not also odd, shift the extracted word left 8 bits
3654 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003655 if (Elt1 >= 0) {
3656 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3657 DAG.getIntPtrConstant(Elt1 / 2));
3658 if ((Elt1 & 1) == 0)
3659 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3660 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003661 else if (Elt0 >= 0)
3662 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3663 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003664 }
3665 // If Elt0 is defined, extract it from the appropriate source. If the
3666 // source byte is not also even, shift the extracted word right 8 bits. If
3667 // Elt1 was also defined, OR the extracted values together before
3668 // inserting them in the result.
3669 if (Elt0 >= 0) {
3670 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3671 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3672 if ((Elt0 & 1) != 0)
3673 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3674 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003675 else if (Elt1 >= 0)
3676 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3677 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003678 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3679 : InsElt0;
3680 }
3681 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3682 DAG.getIntPtrConstant(i));
3683 }
3684 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003685}
3686
Evan Cheng7a831ce2007-12-15 03:00:47 +00003687/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3688/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3689/// done when every pair / quad of shuffle mask elements point to elements in
3690/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003691/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3692static
Nate Begeman9008ca62009-04-27 18:41:29 +00003693SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3694 SelectionDAG &DAG,
3695 TargetLowering &TLI, DebugLoc dl) {
3696 MVT VT = SVOp->getValueType(0);
3697 SDValue V1 = SVOp->getOperand(0);
3698 SDValue V2 = SVOp->getOperand(1);
3699 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003700 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003701 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003702 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003703 MVT NewVT = MaskVT;
3704 switch (VT.getSimpleVT()) {
3705 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003706 case MVT::v4f32: NewVT = MVT::v2f64; break;
3707 case MVT::v4i32: NewVT = MVT::v2i64; break;
3708 case MVT::v8i16: NewVT = MVT::v4i32; break;
3709 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003710 }
3711
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003712 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003713 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003714 NewVT = MVT::v2i64;
3715 else
3716 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003717 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003718 int Scale = NumElems / NewWidth;
3719 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003720 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003721 int StartIdx = -1;
3722 for (int j = 0; j < Scale; ++j) {
3723 int EltIdx = SVOp->getMaskElt(i+j);
3724 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003725 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003726 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003727 StartIdx = EltIdx - (EltIdx % Scale);
3728 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003729 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003730 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003731 if (StartIdx == -1)
3732 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003733 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003734 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003735 }
3736
Dale Johannesenace16102009-02-03 19:33:06 +00003737 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3738 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003739 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003740}
3741
Evan Chengd880b972008-05-09 21:53:03 +00003742/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003743///
Dan Gohman475871a2008-07-27 21:46:04 +00003744static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003745 SDValue SrcOp, SelectionDAG &DAG,
3746 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003747 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3748 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003749 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003750 LD = dyn_cast<LoadSDNode>(SrcOp);
3751 if (!LD) {
3752 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3753 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003754 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003755 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3756 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3757 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3758 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3759 // PR2108
3760 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003761 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3762 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3763 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3764 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003765 SrcOp.getOperand(0)
3766 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003767 }
3768 }
3769 }
3770
Dale Johannesenace16102009-02-03 19:33:06 +00003771 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3772 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003773 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003774 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003775}
3776
Evan Chengace3c172008-07-22 21:13:36 +00003777/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3778/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003779static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003780LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3781 SDValue V1 = SVOp->getOperand(0);
3782 SDValue V2 = SVOp->getOperand(1);
3783 DebugLoc dl = SVOp->getDebugLoc();
3784 MVT VT = SVOp->getValueType(0);
3785
Evan Chengace3c172008-07-22 21:13:36 +00003786 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003787 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 SmallVector<int, 8> Mask1(4U, -1);
3789 SmallVector<int, 8> PermMask;
3790 SVOp->getMask(PermMask);
3791
Evan Chengace3c172008-07-22 21:13:36 +00003792 unsigned NumHi = 0;
3793 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003794 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 int Idx = PermMask[i];
3796 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003797 Locs[i] = std::make_pair(-1, -1);
3798 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3800 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003801 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003802 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003803 NumLo++;
3804 } else {
3805 Locs[i] = std::make_pair(1, NumHi);
3806 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003807 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003808 NumHi++;
3809 }
3810 }
3811 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003812
Evan Chengace3c172008-07-22 21:13:36 +00003813 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003814 // If no more than two elements come from either vector. This can be
3815 // implemented with two shuffles. First shuffle gather the elements.
3816 // The second shuffle, which takes the first shuffle as both of its
3817 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003819
Nate Begeman9008ca62009-04-27 18:41:29 +00003820 SmallVector<int, 8> Mask2(4U, -1);
3821
Evan Chengace3c172008-07-22 21:13:36 +00003822 for (unsigned i = 0; i != 4; ++i) {
3823 if (Locs[i].first == -1)
3824 continue;
3825 else {
3826 unsigned Idx = (i < 2) ? 0 : 4;
3827 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003829 }
3830 }
3831
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003833 } else if (NumLo == 3 || NumHi == 3) {
3834 // Otherwise, we must have three elements from one vector, call it X, and
3835 // one element from the other, call it Y. First, use a shufps to build an
3836 // intermediate vector with the one element from Y and the element from X
3837 // that will be in the same half in the final destination (the indexes don't
3838 // matter). Then, use a shufps to build the final vector, taking the half
3839 // containing the element from Y from the intermediate, and the other half
3840 // from X.
3841 if (NumHi == 3) {
3842 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003844 std::swap(V1, V2);
3845 }
3846
3847 // Find the element from V2.
3848 unsigned HiIndex;
3849 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003850 int Val = PermMask[HiIndex];
3851 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003852 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003853 if (Val >= 4)
3854 break;
3855 }
3856
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 Mask1[0] = PermMask[HiIndex];
3858 Mask1[1] = -1;
3859 Mask1[2] = PermMask[HiIndex^1];
3860 Mask1[3] = -1;
3861 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003862
3863 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003864 Mask1[0] = PermMask[0];
3865 Mask1[1] = PermMask[1];
3866 Mask1[2] = HiIndex & 1 ? 6 : 4;
3867 Mask1[3] = HiIndex & 1 ? 4 : 6;
3868 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003869 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 Mask1[0] = HiIndex & 1 ? 2 : 0;
3871 Mask1[1] = HiIndex & 1 ? 0 : 2;
3872 Mask1[2] = PermMask[2];
3873 Mask1[3] = PermMask[3];
3874 if (Mask1[2] >= 0)
3875 Mask1[2] += 4;
3876 if (Mask1[3] >= 0)
3877 Mask1[3] += 4;
3878 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003879 }
Evan Chengace3c172008-07-22 21:13:36 +00003880 }
3881
3882 // Break it into (shuffle shuffle_hi, shuffle_lo).
3883 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 SmallVector<int,8> LoMask(4U, -1);
3885 SmallVector<int,8> HiMask(4U, -1);
3886
3887 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003888 unsigned MaskIdx = 0;
3889 unsigned LoIdx = 0;
3890 unsigned HiIdx = 2;
3891 for (unsigned i = 0; i != 4; ++i) {
3892 if (i == 2) {
3893 MaskPtr = &HiMask;
3894 MaskIdx = 1;
3895 LoIdx = 0;
3896 HiIdx = 2;
3897 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 int Idx = PermMask[i];
3899 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003900 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003902 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003904 LoIdx++;
3905 } else {
3906 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003908 HiIdx++;
3909 }
3910 }
3911
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3913 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3914 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003915 for (unsigned i = 0; i != 4; ++i) {
3916 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003917 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003918 } else {
3919 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003920 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003921 }
3922 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003924}
3925
Dan Gohman475871a2008-07-27 21:46:04 +00003926SDValue
3927X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003929 SDValue V1 = Op.getOperand(0);
3930 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003931 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003932 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003934 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003935 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3936 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003937 bool V1IsSplat = false;
3938 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003939
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003941 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003942
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 // Promote splats to v4f32.
3944 if (SVOp->isSplat()) {
3945 if (isMMX || NumElems < 4)
3946 return Op;
3947 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003948 }
3949
Evan Cheng7a831ce2007-12-15 03:00:47 +00003950 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3951 // do it!
3952 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003954 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003955 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003956 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003957 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3958 // FIXME: Figure out a cleaner way to do this.
3959 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003960 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003962 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3964 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3965 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003966 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003967 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3969 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003970 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003972 }
3973 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003974
3975 if (X86::isPSHUFDMask(SVOp))
3976 return Op;
3977
Evan Chengf26ffe92008-05-29 08:22:04 +00003978 // Check if this can be converted into a logical shift.
3979 bool isLeft = false;
3980 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003981 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003982 bool isShift = getSubtarget()->hasSSE2() &&
3983 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003984 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003985 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003986 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003987 MVT EVT = VT.getVectorElementType();
3988 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003989 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003990 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003991
3992 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003993 if (V1IsUndef)
3994 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003995 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003996 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003997 if (!isMMX)
3998 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003999 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004000
4001 // FIXME: fold these into legal mask.
4002 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4003 X86::isMOVSLDUPMask(SVOp) ||
4004 X86::isMOVHLPSMask(SVOp) ||
4005 X86::isMOVHPMask(SVOp) ||
4006 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004007 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004008
Nate Begeman9008ca62009-04-27 18:41:29 +00004009 if (ShouldXformToMOVHLPS(SVOp) ||
4010 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4011 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004012
Evan Chengf26ffe92008-05-29 08:22:04 +00004013 if (isShift) {
4014 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004015 MVT EVT = VT.getVectorElementType();
4016 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004017 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004018 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004019
Evan Cheng9eca5e82006-10-25 21:49:50 +00004020 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004021 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4022 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004023 V1IsSplat = isSplatVector(V1.getNode());
4024 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004025
Chris Lattner8a594482007-11-25 00:24:49 +00004026 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004027 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004028 Op = CommuteVectorShuffle(SVOp, DAG);
4029 SVOp = cast<ShuffleVectorSDNode>(Op);
4030 V1 = SVOp->getOperand(0);
4031 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004032 std::swap(V1IsSplat, V2IsSplat);
4033 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004034 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004035 }
4036
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4038 // Shuffling low element of v1 into undef, just return v1.
4039 if (V2IsUndef)
4040 return V1;
4041 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4042 // the instruction selector will not match, so get a canonical MOVL with
4043 // swapped operands to undo the commute.
4044 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004045 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004046
Nate Begeman9008ca62009-04-27 18:41:29 +00004047 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4048 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4049 X86::isUNPCKLMask(SVOp) ||
4050 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004051 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004052
Evan Cheng9bbbb982006-10-25 20:48:19 +00004053 if (V2IsSplat) {
4054 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004055 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004056 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 SDValue NewMask = NormalizeMask(SVOp, DAG);
4058 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4059 if (NSVOp != SVOp) {
4060 if (X86::isUNPCKLMask(NSVOp, true)) {
4061 return NewMask;
4062 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4063 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004064 }
4065 }
4066 }
4067
Evan Cheng9eca5e82006-10-25 21:49:50 +00004068 if (Commuted) {
4069 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004070 // FIXME: this seems wrong.
4071 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4072 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4073 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4074 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4075 X86::isUNPCKLMask(NewSVOp) ||
4076 X86::isUNPCKHMask(NewSVOp))
4077 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004078 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004079
Nate Begemanb9a47b82009-02-23 08:49:38 +00004080 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004081
4082 // Normalize the node to match x86 shuffle ops if needed
4083 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4084 return CommuteVectorShuffle(SVOp, DAG);
4085
4086 // Check for legal shuffle and return?
4087 SmallVector<int, 16> PermMask;
4088 SVOp->getMask(PermMask);
4089 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004090 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004091
Evan Cheng14b32e12007-12-11 01:46:18 +00004092 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4093 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004094 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004095 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004096 return NewOp;
4097 }
4098
Nate Begemanb9a47b82009-02-23 08:49:38 +00004099 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004100 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004101 if (NewOp.getNode())
4102 return NewOp;
4103 }
4104
Evan Chengace3c172008-07-22 21:13:36 +00004105 // Handle all 4 wide cases with a number of shuffles except for MMX.
4106 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004107 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004108
Dan Gohman475871a2008-07-27 21:46:04 +00004109 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004110}
4111
Dan Gohman475871a2008-07-27 21:46:04 +00004112SDValue
4113X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004114 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004115 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004116 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004117 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004118 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004119 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004120 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004121 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004122 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004124 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4125 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4126 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004127 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4128 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4129 DAG.getNode(ISD::BIT_CONVERT, dl,
4130 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004131 Op.getOperand(0)),
4132 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004133 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004134 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004135 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004136 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004137 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004138 } else if (VT == MVT::f32) {
4139 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4140 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004141 // result has a single use which is a store or a bitcast to i32. And in
4142 // the case of a store, it's not worth it if the index is a constant 0,
4143 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004144 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004145 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004146 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004147 if ((User->getOpcode() != ISD::STORE ||
4148 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4149 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004150 (User->getOpcode() != ISD::BIT_CONVERT ||
4151 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004152 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004153 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004154 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004155 Op.getOperand(0)),
4156 Op.getOperand(1));
4157 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004158 } else if (VT == MVT::i32) {
4159 // ExtractPS works with constant index.
4160 if (isa<ConstantSDNode>(Op.getOperand(1)))
4161 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004162 }
Dan Gohman475871a2008-07-27 21:46:04 +00004163 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004164}
4165
4166
Dan Gohman475871a2008-07-27 21:46:04 +00004167SDValue
4168X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004169 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004170 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004171
Evan Cheng62a3f152008-03-24 21:52:23 +00004172 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004173 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004174 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004175 return Res;
4176 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004177
Duncan Sands83ec4b62008-06-06 12:08:01 +00004178 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004179 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004180 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004182 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004183 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004184 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004185 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4186 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004187 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004188 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004189 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004190 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004191 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004192 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004193 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004194 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004195 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004196 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004197 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004198 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004199 if (Idx == 0)
4200 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004201
Evan Cheng0db9fe62006-04-25 20:13:52 +00004202 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004203 int Mask[4] = { Idx, -1, -1, -1 };
4204 MVT VVT = Op.getOperand(0).getValueType();
4205 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4206 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004207 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004208 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004209 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004210 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4211 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4212 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004213 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004214 if (Idx == 0)
4215 return Op;
4216
4217 // UNPCKHPD the element to the lowest double word, then movsd.
4218 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4219 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004220 int Mask[2] = { 1, -1 };
4221 MVT VVT = Op.getOperand(0).getValueType();
4222 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4223 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004224 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004225 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004226 }
4227
Dan Gohman475871a2008-07-27 21:46:04 +00004228 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004229}
4230
Dan Gohman475871a2008-07-27 21:46:04 +00004231SDValue
4232X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004233 MVT VT = Op.getValueType();
4234 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004235 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004236
Dan Gohman475871a2008-07-27 21:46:04 +00004237 SDValue N0 = Op.getOperand(0);
4238 SDValue N1 = Op.getOperand(1);
4239 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004240
Dan Gohmanef521f12008-08-14 22:53:18 +00004241 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4242 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004244 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004245 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4246 // argument.
4247 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004248 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004249 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004250 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004251 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004252 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004253 // Bits [7:6] of the constant are the source select. This will always be
4254 // zero here. The DAG Combiner may combine an extract_elt index into these
4255 // bits. For example (insert (extract, 3), 2) could be matched by putting
4256 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004257 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004258 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004259 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004260 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004261 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004262 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004263 } else if (EVT == MVT::i32) {
4264 // InsertPS works with constant index.
4265 if (isa<ConstantSDNode>(N2))
4266 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004267 }
Dan Gohman475871a2008-07-27 21:46:04 +00004268 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004269}
4270
Dan Gohman475871a2008-07-27 21:46:04 +00004271SDValue
4272X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004273 MVT VT = Op.getValueType();
4274 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004275
4276 if (Subtarget->hasSSE41())
4277 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4278
Evan Cheng794405e2007-12-12 07:55:34 +00004279 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004281
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004282 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004283 SDValue N0 = Op.getOperand(0);
4284 SDValue N1 = Op.getOperand(1);
4285 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004286
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004288 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4289 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004290 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004291 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004292 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004293 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004294 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004295 }
Dan Gohman475871a2008-07-27 21:46:04 +00004296 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004297}
4298
Dan Gohman475871a2008-07-27 21:46:04 +00004299SDValue
4300X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004301 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004302 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004303 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4304 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4305 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004306 Op.getOperand(0))));
4307
Dale Johannesenace16102009-02-03 19:33:06 +00004308 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309 MVT VT = MVT::v2i32;
4310 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004311 default: break;
4312 case MVT::v16i8:
4313 case MVT::v8i16:
4314 VT = MVT::v4i32;
4315 break;
4316 }
Dale Johannesenace16102009-02-03 19:33:06 +00004317 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4318 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004319}
4320
Bill Wendling056292f2008-09-16 21:48:12 +00004321// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4322// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4323// one of the above mentioned nodes. It has to be wrapped because otherwise
4324// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4325// be used to form addressing mode. These wrapped nodes will be selected
4326// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004327SDValue
4328X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004329 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004330 // FIXME there isn't really any debug info here, should come from the parent
4331 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004332 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4333 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004334 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004335 // With PIC, the address is actually $g + Offset.
4336 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4337 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004338 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004339 DAG.getNode(X86ISD::GlobalBaseReg,
4340 DebugLoc::getUnknownLoc(),
4341 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004342 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004343 }
4344
4345 return Result;
4346}
4347
Dan Gohman475871a2008-07-27 21:46:04 +00004348SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004349X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004350 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004351 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004352 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4353 bool ExtraLoadRequired =
4354 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4355
4356 // Create the TargetGlobalAddress node, folding in the constant
4357 // offset if it is legal.
4358 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004359 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004360 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4361 Offset = 0;
4362 } else
4363 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004364 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004365
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004366 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004367 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004368 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4369 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004370 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004372
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004373 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4374 // load the value at address GV, not the value of GV itself. This means that
4375 // the GlobalAddress must be in the base or index register of the address, not
4376 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004377 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004378 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004379 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004380 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004381
Dan Gohman6520e202008-10-18 02:06:02 +00004382 // If there was a non-zero offset that we didn't fold, create an explicit
4383 // addition for it.
4384 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004385 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004386 DAG.getConstant(Offset, getPointerTy()));
4387
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388 return Result;
4389}
4390
Evan Chengda43bcf2008-09-24 00:05:32 +00004391SDValue
4392X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4393 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004394 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004395 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004396}
4397
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004398static SDValue
4399GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004400 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004401 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4402 DebugLoc dl = GA->getDebugLoc();
4403 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4404 GA->getValueType(0),
4405 GA->getOffset());
4406 if (InFlag) {
4407 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004408 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004409 } else {
4410 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004411 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004412 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004413 SDValue Flag = Chain.getValue(1);
4414 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004415}
4416
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004417// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004418static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004419LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004420 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004421 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004422 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4423 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004424 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004425 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004426 PtrVT), InFlag);
4427 InFlag = Chain.getValue(1);
4428
Rafael Espindola15f1b662009-04-24 12:59:40 +00004429 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004430}
4431
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004432// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004433static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004434LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004435 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004436 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004437}
4438
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004439// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4440// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004441static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004442 const MVT PtrVT, TLSModel::Model model,
4443 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004444 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004445 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004446 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4447 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004448 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4449 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004450
4451 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4452 NULL, 0);
4453
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004454 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4455 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004456 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004457 GA->getValueType(0),
4458 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004459 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004460
Rafael Espindola9a580232009-02-27 13:37:18 +00004461 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004462 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004463 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004464
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004465 // The address of the thread local variable is the add of the thread
4466 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004467 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004468}
4469
Dan Gohman475871a2008-07-27 21:46:04 +00004470SDValue
4471X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004472 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004473 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004474 assert(Subtarget->isTargetELF() &&
4475 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004476 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004477 GlobalValue *GV = GA->getGlobal();
4478 TLSModel::Model model =
4479 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004480 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004481 switch (model) {
4482 case TLSModel::GeneralDynamic:
4483 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004484 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004485
4486 case TLSModel::InitialExec:
4487 case TLSModel::LocalExec:
4488 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004489 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004490 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004491 switch (model) {
4492 case TLSModel::GeneralDynamic:
4493 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004494 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004495
4496 case TLSModel::InitialExec:
4497 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004498 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004499 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004500 }
Chris Lattner5867de12009-04-01 22:14:45 +00004501 assert(0 && "Unreachable");
4502 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004503}
4504
Dan Gohman475871a2008-07-27 21:46:04 +00004505SDValue
4506X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004507 // FIXME there isn't really any debug info here
4508 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004509 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4510 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004511 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004512 // With PIC, the address is actually $g + Offset.
4513 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4514 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004515 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004516 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004517 DebugLoc::getUnknownLoc(),
4518 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004519 Result);
4520 }
4521
4522 return Result;
4523}
4524
Dan Gohman475871a2008-07-27 21:46:04 +00004525SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004526 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004527 // FIXME there isn't really any debug into here
4528 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004529 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004530 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004531 // With PIC, the address is actually $g + Offset.
4532 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4533 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004534 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004535 DAG.getNode(X86ISD::GlobalBaseReg,
4536 DebugLoc::getUnknownLoc(),
4537 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004538 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004539 }
4540
4541 return Result;
4542}
4543
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004544/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004545/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004546SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004547 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004548 MVT VT = Op.getValueType();
4549 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004550 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004551 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004552 SDValue ShOpLo = Op.getOperand(0);
4553 SDValue ShOpHi = Op.getOperand(1);
4554 SDValue ShAmt = Op.getOperand(2);
4555 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004556 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004557 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004558 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004559
Dan Gohman475871a2008-07-27 21:46:04 +00004560 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004561 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004562 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4563 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004564 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004565 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4566 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004567 }
Evan Chenge3413162006-01-09 18:33:28 +00004568
Dale Johannesenace16102009-02-03 19:33:06 +00004569 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004570 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004571 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004572 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004573
Dan Gohman475871a2008-07-27 21:46:04 +00004574 SDValue Hi, Lo;
4575 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4576 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4577 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004578
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004579 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004580 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4581 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004582 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004583 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4584 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004585 }
4586
Dan Gohman475871a2008-07-27 21:46:04 +00004587 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004588 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004589}
Evan Chenga3195e82006-01-12 22:54:21 +00004590
Dan Gohman475871a2008-07-27 21:46:04 +00004591SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004592 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004593 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004594 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004595
Chris Lattnerb09916b2008-02-27 05:57:41 +00004596 // These are really Legal; caller falls through into that case.
4597 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004598 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00004599 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004600 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004601 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00004602
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004603 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004604 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 MachineFunction &MF = DAG.getMachineFunction();
4606 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004607 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004608 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004609 StackSlot,
4610 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611
4612 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004613 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004614 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004615 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004616 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4617 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004618 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004619 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 Ops.push_back(Chain);
4621 Ops.push_back(StackSlot);
4622 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004623 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004624 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004625
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004626 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004627 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004628 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004629
4630 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4631 // shouldn't be necessary except that RFP cannot be live across
4632 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004633 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004634 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004635 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004636 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004637 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004638 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004640 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004641 Ops.push_back(DAG.getValueType(Op.getValueType()));
4642 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004643 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4644 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004645 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004646 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004647
Evan Cheng0db9fe62006-04-25 20:13:52 +00004648 return Result;
4649}
4650
Bill Wendling8b8a6362009-01-17 03:56:04 +00004651// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4652SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4653 // This algorithm is not obvious. Here it is in C code, more or less:
4654 /*
4655 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4656 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4657 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004658
Bill Wendling8b8a6362009-01-17 03:56:04 +00004659 // Copy ints to xmm registers.
4660 __m128i xh = _mm_cvtsi32_si128( hi );
4661 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004662
Bill Wendling8b8a6362009-01-17 03:56:04 +00004663 // Combine into low half of a single xmm register.
4664 __m128i x = _mm_unpacklo_epi32( xh, xl );
4665 __m128d d;
4666 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004667
Bill Wendling8b8a6362009-01-17 03:56:04 +00004668 // Merge in appropriate exponents to give the integer bits the right
4669 // magnitude.
4670 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004671
Bill Wendling8b8a6362009-01-17 03:56:04 +00004672 // Subtract away the biases to deal with the IEEE-754 double precision
4673 // implicit 1.
4674 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004675
Bill Wendling8b8a6362009-01-17 03:56:04 +00004676 // All conversions up to here are exact. The correctly rounded result is
4677 // calculated using the current rounding mode using the following
4678 // horizontal add.
4679 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4680 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4681 // store doesn't really need to be here (except
4682 // maybe to zero the other double)
4683 return sd;
4684 }
4685 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004686
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004687 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004688
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004689 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004690 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004691 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4692 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4693 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4694 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4695 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004696 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004697
Bill Wendling8b8a6362009-01-17 03:56:04 +00004698 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004699 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4700 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4701 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004702 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004703
Dale Johannesenace16102009-02-03 19:33:06 +00004704 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4705 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004706 Op.getOperand(0),
4707 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004708 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4709 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004710 Op.getOperand(0),
4711 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004712 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004713 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004714 PseudoSourceValue::getConstantPool(), 0,
4715 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004716 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004717 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4718 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004719 PseudoSourceValue::getConstantPool(), 0,
4720 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004721 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004722
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004723 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004724 int ShufMask[2] = { 1, -1 };
4725 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4726 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004727 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4728 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004729 DAG.getIntPtrConstant(0));
4730}
4731
Bill Wendling8b8a6362009-01-17 03:56:04 +00004732// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4733SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004734 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004735 // FP constant to bias correct the final result.
4736 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4737 MVT::f64);
4738
4739 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004740 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4741 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004742 Op.getOperand(0),
4743 DAG.getIntPtrConstant(0)));
4744
Dale Johannesenace16102009-02-03 19:33:06 +00004745 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4746 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004747 DAG.getIntPtrConstant(0));
4748
4749 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004750 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4751 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4752 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004753 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004754 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4755 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004756 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004757 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4758 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004759 DAG.getIntPtrConstant(0));
4760
4761 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004762 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004763
4764 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004765 MVT DestVT = Op.getValueType();
4766
4767 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004768 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004769 DAG.getIntPtrConstant(0));
4770 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004771 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004772 }
4773
4774 // Handle final rounding.
4775 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004776}
4777
4778SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004779 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004780 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004781
Evan Chenga06ec9e2009-01-19 08:08:22 +00004782 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4783 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4784 // the optimization here.
4785 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004786 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004787
4788 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004789 if (SrcVT == MVT::i64) {
4790 // We only handle SSE2 f64 target here; caller can handle the rest.
4791 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4792 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004793
Bill Wendling8b8a6362009-01-17 03:56:04 +00004794 return LowerUINT_TO_FP_i64(Op, DAG);
4795 } else if (SrcVT == MVT::i32) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004796 return LowerUINT_TO_FP_i32(Op, DAG);
4797 }
4798
4799 assert(0 && "Unknown UINT_TO_FP to lower!");
4800 return SDValue();
4801}
4802
Dan Gohman475871a2008-07-27 21:46:04 +00004803std::pair<SDValue,SDValue> X86TargetLowering::
4804FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004805 DebugLoc dl = Op.getDebugLoc();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004806 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4807 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004808 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004810 // These are really Legal.
Scott Michelfdc40a02009-02-17 22:15:04 +00004811 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004812 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004813 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004814 if (Subtarget->is64Bit() &&
4815 Op.getValueType() == MVT::i64 &&
4816 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00004817 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004818
Evan Cheng87c89352007-10-15 20:11:21 +00004819 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4820 // stack slot.
4821 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004823 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004824 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004825 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004826 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004827 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4828 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4829 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4830 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004832
Dan Gohman475871a2008-07-27 21:46:04 +00004833 SDValue Chain = DAG.getEntryNode();
4834 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004835 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004837 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004838 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004839 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004840 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004841 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4842 };
Dale Johannesenace16102009-02-03 19:33:06 +00004843 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844 Chain = Value.getValue(1);
4845 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4846 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4847 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004848
Evan Cheng0db9fe62006-04-25 20:13:52 +00004849 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004850 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004851 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004852
Chris Lattner27a6c732007-11-24 07:07:01 +00004853 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004854}
4855
Dan Gohman475871a2008-07-27 21:46:04 +00004856SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4857 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4858 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00004859 if (FIST.getNode() == 0) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00004860
Chris Lattner27a6c732007-11-24 07:07:01 +00004861 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004862 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004863 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004864}
4865
Dan Gohman475871a2008-07-27 21:46:04 +00004866SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004867 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004868 MVT VT = Op.getValueType();
4869 MVT EltVT = VT;
4870 if (VT.isVector())
4871 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004872 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004873 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004874 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004875 CV.push_back(C);
4876 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004877 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004878 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004879 CV.push_back(C);
4880 CV.push_back(C);
4881 CV.push_back(C);
4882 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004883 }
Dan Gohmand3006222007-07-27 17:16:43 +00004884 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004885 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004886 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004887 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004888 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004889 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004890}
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004893 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004894 MVT VT = Op.getValueType();
4895 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004896 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004897 if (VT.isVector()) {
4898 EltVT = VT.getVectorElementType();
4899 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004900 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004901 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004902 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004903 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004904 CV.push_back(C);
4905 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004906 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004907 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004908 CV.push_back(C);
4909 CV.push_back(C);
4910 CV.push_back(C);
4911 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004912 }
Dan Gohmand3006222007-07-27 17:16:43 +00004913 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004914 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004915 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004916 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004917 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004918 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004919 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4920 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004921 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004922 Op.getOperand(0)),
4923 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004924 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004925 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004926 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004927}
4928
Dan Gohman475871a2008-07-27 21:46:04 +00004929SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4930 SDValue Op0 = Op.getOperand(0);
4931 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004932 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004933 MVT VT = Op.getValueType();
4934 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004935
4936 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004937 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004938 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004939 SrcVT = VT;
4940 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004941 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004942 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004943 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004944 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004945 }
4946
4947 // At this point the operands and the result should have the same
4948 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004949
Evan Cheng68c47cb2007-01-05 07:55:56 +00004950 // First get the sign bit of second operand.
4951 std::vector<Constant*> CV;
4952 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004953 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4954 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004955 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004956 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4957 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4958 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4959 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004960 }
Dan Gohmand3006222007-07-27 17:16:43 +00004961 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004962 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004963 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004964 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004965 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004966 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004967
4968 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004969 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004970 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00004971 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
4972 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00004973 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004974 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
4975 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004976 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004977 }
4978
Evan Cheng73d6cf12007-01-05 21:37:56 +00004979 // Clear first operand sign bit.
4980 CV.clear();
4981 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004982 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4983 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004984 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004985 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4986 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4987 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4988 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004989 }
Dan Gohmand3006222007-07-27 17:16:43 +00004990 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004991 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004992 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004993 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004994 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004995 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004996
4997 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00004998 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004999}
5000
Dan Gohman076aee32009-03-04 19:44:21 +00005001/// Emit nodes that will be selected as "test Op0,Op0", or something
5002/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005003SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5004 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005005 DebugLoc dl = Op.getDebugLoc();
5006
Dan Gohman31125812009-03-07 01:58:32 +00005007 // CF and OF aren't always set the way we want. Determine which
5008 // of these we need.
5009 bool NeedCF = false;
5010 bool NeedOF = false;
5011 switch (X86CC) {
5012 case X86::COND_A: case X86::COND_AE:
5013 case X86::COND_B: case X86::COND_BE:
5014 NeedCF = true;
5015 break;
5016 case X86::COND_G: case X86::COND_GE:
5017 case X86::COND_L: case X86::COND_LE:
5018 case X86::COND_O: case X86::COND_NO:
5019 NeedOF = true;
5020 break;
5021 default: break;
5022 }
5023
Dan Gohman076aee32009-03-04 19:44:21 +00005024 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005025 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5026 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5027 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005028 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005029 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005030 switch (Op.getNode()->getOpcode()) {
5031 case ISD::ADD:
5032 // Due to an isel shortcoming, be conservative if this add is likely to
5033 // be selected as part of a load-modify-store instruction. When the root
5034 // node in a match is a store, isel doesn't know how to remap non-chain
5035 // non-flag uses of other nodes in the match, such as the ADD in this
5036 // case. This leads to the ADD being left around and reselected, with
5037 // the result being two adds in the output.
5038 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5039 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5040 if (UI->getOpcode() == ISD::STORE)
5041 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005042 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005043 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5044 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005045 if (C->getAPIntValue() == 1) {
5046 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005047 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005048 break;
5049 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005050 // An add of negative one (subtract of one) will be selected as a DEC.
5051 if (C->getAPIntValue().isAllOnesValue()) {
5052 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005053 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005054 break;
5055 }
5056 }
Dan Gohman076aee32009-03-04 19:44:21 +00005057 // Otherwise use a regular EFLAGS-setting add.
5058 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005059 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005060 break;
5061 case ISD::SUB:
5062 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5063 // likely to be selected as part of a load-modify-store instruction.
5064 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5065 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5066 if (UI->getOpcode() == ISD::STORE)
5067 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005068 // Otherwise use a regular EFLAGS-setting sub.
5069 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005070 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005071 break;
5072 case X86ISD::ADD:
5073 case X86ISD::SUB:
5074 case X86ISD::INC:
5075 case X86ISD::DEC:
5076 return SDValue(Op.getNode(), 1);
5077 default:
5078 default_case:
5079 break;
5080 }
5081 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005082 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005083 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005084 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005085 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005086 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005087 DAG.ReplaceAllUsesWith(Op, New);
5088 return SDValue(New.getNode(), 1);
5089 }
5090 }
5091
5092 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5093 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5094 DAG.getConstant(0, Op.getValueType()));
5095}
5096
5097/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5098/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005099SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5100 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005101 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5102 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005103 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005104
5105 DebugLoc dl = Op0.getDebugLoc();
5106 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5107}
5108
Dan Gohman475871a2008-07-27 21:46:04 +00005109SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005110 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005111 SDValue Op0 = Op.getOperand(0);
5112 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005113 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005114 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005115
Dan Gohmane5af2d32009-01-29 01:59:02 +00005116 // Lower (X & (1 << N)) == 0 to BT(X, N).
5117 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5118 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005119 if (Op0.getOpcode() == ISD::AND &&
5120 Op0.hasOneUse() &&
5121 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005122 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005123 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005124 SDValue LHS, RHS;
5125 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5126 if (ConstantSDNode *Op010C =
5127 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5128 if (Op010C->getZExtValue() == 1) {
5129 LHS = Op0.getOperand(0);
5130 RHS = Op0.getOperand(1).getOperand(1);
5131 }
5132 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5133 if (ConstantSDNode *Op000C =
5134 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5135 if (Op000C->getZExtValue() == 1) {
5136 LHS = Op0.getOperand(1);
5137 RHS = Op0.getOperand(0).getOperand(1);
5138 }
5139 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5140 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5141 SDValue AndLHS = Op0.getOperand(0);
5142 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5143 LHS = AndLHS.getOperand(0);
5144 RHS = AndLHS.getOperand(1);
5145 }
5146 }
Evan Cheng0488db92007-09-25 01:57:46 +00005147
Dan Gohmane5af2d32009-01-29 01:59:02 +00005148 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005149 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5150 // instruction. Since the shift amount is in-range-or-undefined, we know
5151 // that doing a bittest on the i16 value is ok. We extend to i32 because
5152 // the encoding for the i16 version is larger than the i32 version.
5153 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005154 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005155
5156 // If the operand types disagree, extend the shift amount to match. Since
5157 // BT ignores high bits (like shifts) we can use anyextend.
5158 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005159 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005160
Dale Johannesenace16102009-02-03 19:33:06 +00005161 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005162 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005163 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005164 DAG.getConstant(Cond, MVT::i8), BT);
5165 }
5166 }
5167
5168 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5169 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005170
Dan Gohman31125812009-03-07 01:58:32 +00005171 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005172 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005173 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005174}
5175
Dan Gohman475871a2008-07-27 21:46:04 +00005176SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5177 SDValue Cond;
5178 SDValue Op0 = Op.getOperand(0);
5179 SDValue Op1 = Op.getOperand(1);
5180 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005181 MVT VT = Op.getValueType();
5182 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5183 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005184 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005185
5186 if (isFP) {
5187 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005188 MVT VT0 = Op0.getValueType();
5189 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5190 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005191 bool Swap = false;
5192
5193 switch (SetCCOpcode) {
5194 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005195 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005196 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005197 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005198 case ISD::SETGT: Swap = true; // Fallthrough
5199 case ISD::SETLT:
5200 case ISD::SETOLT: SSECC = 1; break;
5201 case ISD::SETOGE:
5202 case ISD::SETGE: Swap = true; // Fallthrough
5203 case ISD::SETLE:
5204 case ISD::SETOLE: SSECC = 2; break;
5205 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005206 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005207 case ISD::SETNE: SSECC = 4; break;
5208 case ISD::SETULE: Swap = true;
5209 case ISD::SETUGE: SSECC = 5; break;
5210 case ISD::SETULT: Swap = true;
5211 case ISD::SETUGT: SSECC = 6; break;
5212 case ISD::SETO: SSECC = 7; break;
5213 }
5214 if (Swap)
5215 std::swap(Op0, Op1);
5216
Nate Begemanfb8ead02008-07-25 19:05:58 +00005217 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005218 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005219 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005220 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005221 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5222 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5223 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005224 }
5225 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005227 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5228 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5229 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005230 }
5231 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005232 }
5233 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005234 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005236
Nate Begeman30a0de92008-07-17 16:51:19 +00005237 // We are handling one of the integer comparisons here. Since SSE only has
5238 // GT and EQ comparisons for integer, swapping operands and multiple
5239 // operations may be required for some comparisons.
5240 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5241 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005242
Nate Begeman30a0de92008-07-17 16:51:19 +00005243 switch (VT.getSimpleVT()) {
5244 default: break;
5245 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5246 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5247 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5248 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005250
Nate Begeman30a0de92008-07-17 16:51:19 +00005251 switch (SetCCOpcode) {
5252 default: break;
5253 case ISD::SETNE: Invert = true;
5254 case ISD::SETEQ: Opc = EQOpc; break;
5255 case ISD::SETLT: Swap = true;
5256 case ISD::SETGT: Opc = GTOpc; break;
5257 case ISD::SETGE: Swap = true;
5258 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5259 case ISD::SETULT: Swap = true;
5260 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5261 case ISD::SETUGE: Swap = true;
5262 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5263 }
5264 if (Swap)
5265 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005266
Nate Begeman30a0de92008-07-17 16:51:19 +00005267 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5268 // bits of the inputs before performing those operations.
5269 if (FlipSigns) {
5270 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005271 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5272 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005273 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005274 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5275 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005276 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5277 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005278 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005279
Dale Johannesenace16102009-02-03 19:33:06 +00005280 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005281
5282 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005283 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005284 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005285
Nate Begeman30a0de92008-07-17 16:51:19 +00005286 return Result;
5287}
Evan Cheng0488db92007-09-25 01:57:46 +00005288
Evan Cheng370e5342008-12-03 08:38:43 +00005289// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005290static bool isX86LogicalCmp(SDValue Op) {
5291 unsigned Opc = Op.getNode()->getOpcode();
5292 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5293 return true;
5294 if (Op.getResNo() == 1 &&
5295 (Opc == X86ISD::ADD ||
5296 Opc == X86ISD::SUB ||
5297 Opc == X86ISD::SMUL ||
5298 Opc == X86ISD::UMUL ||
5299 Opc == X86ISD::INC ||
5300 Opc == X86ISD::DEC))
5301 return true;
5302
5303 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005304}
5305
Dan Gohman475871a2008-07-27 21:46:04 +00005306SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005307 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005308 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005309 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005310 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005311
Evan Cheng734503b2006-09-11 02:19:56 +00005312 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005313 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005314
Evan Cheng3f41d662007-10-08 22:16:29 +00005315 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5316 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005317 if (Cond.getOpcode() == X86ISD::SETCC) {
5318 CC = Cond.getOperand(0);
5319
Dan Gohman475871a2008-07-27 21:46:04 +00005320 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005321 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005322 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005323
Evan Cheng3f41d662007-10-08 22:16:29 +00005324 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005325 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005326 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005327 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Chris Lattnerd1980a52009-03-12 06:52:53 +00005329 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5330 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005331 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005332 addTest = false;
5333 }
5334 }
5335
5336 if (addTest) {
5337 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005338 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005339 }
5340
Dan Gohmanfc166572009-04-09 23:54:40 +00005341 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005342 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005343 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5344 // condition is true.
5345 Ops.push_back(Op.getOperand(2));
5346 Ops.push_back(Op.getOperand(1));
5347 Ops.push_back(CC);
5348 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005349 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005350}
5351
Evan Cheng370e5342008-12-03 08:38:43 +00005352// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5353// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5354// from the AND / OR.
5355static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5356 Opc = Op.getOpcode();
5357 if (Opc != ISD::OR && Opc != ISD::AND)
5358 return false;
5359 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5360 Op.getOperand(0).hasOneUse() &&
5361 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5362 Op.getOperand(1).hasOneUse());
5363}
5364
Evan Cheng961d6d42009-02-02 08:19:07 +00005365// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5366// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005367static bool isXor1OfSetCC(SDValue Op) {
5368 if (Op.getOpcode() != ISD::XOR)
5369 return false;
5370 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5371 if (N1C && N1C->getAPIntValue() == 1) {
5372 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5373 Op.getOperand(0).hasOneUse();
5374 }
5375 return false;
5376}
5377
Dan Gohman475871a2008-07-27 21:46:04 +00005378SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005379 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005380 SDValue Chain = Op.getOperand(0);
5381 SDValue Cond = Op.getOperand(1);
5382 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005383 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005384 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005385
Evan Cheng0db9fe62006-04-25 20:13:52 +00005386 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005387 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005388#if 0
5389 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005390 else if (Cond.getOpcode() == X86ISD::ADD ||
5391 Cond.getOpcode() == X86ISD::SUB ||
5392 Cond.getOpcode() == X86ISD::SMUL ||
5393 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005394 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005395#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005396
Evan Cheng3f41d662007-10-08 22:16:29 +00005397 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5398 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005399 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005400 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005401
Dan Gohman475871a2008-07-27 21:46:04 +00005402 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005403 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005404 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005405 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005406 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005407 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005408 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005409 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005410 default: break;
5411 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005412 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005413 // These can only come from an arithmetic instruction with overflow,
5414 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005415 Cond = Cond.getNode()->getOperand(1);
5416 addTest = false;
5417 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005418 }
Evan Cheng0488db92007-09-25 01:57:46 +00005419 }
Evan Cheng370e5342008-12-03 08:38:43 +00005420 } else {
5421 unsigned CondOpc;
5422 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5423 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005424 if (CondOpc == ISD::OR) {
5425 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5426 // two branches instead of an explicit OR instruction with a
5427 // separate test.
5428 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005429 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005430 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005431 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005432 Chain, Dest, CC, Cmp);
5433 CC = Cond.getOperand(1).getOperand(0);
5434 Cond = Cmp;
5435 addTest = false;
5436 }
5437 } else { // ISD::AND
5438 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5439 // two branches instead of an explicit AND instruction with a
5440 // separate test. However, we only do this if this block doesn't
5441 // have a fall-through edge, because this requires an explicit
5442 // jmp when the condition is false.
5443 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005444 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005445 Op.getNode()->hasOneUse()) {
5446 X86::CondCode CCode =
5447 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5448 CCode = X86::GetOppositeBranchCondition(CCode);
5449 CC = DAG.getConstant(CCode, MVT::i8);
5450 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5451 // Look for an unconditional branch following this conditional branch.
5452 // We need this because we need to reverse the successors in order
5453 // to implement FCMP_OEQ.
5454 if (User.getOpcode() == ISD::BR) {
5455 SDValue FalseBB = User.getOperand(1);
5456 SDValue NewBR =
5457 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5458 assert(NewBR == User);
5459 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005460
Dale Johannesene4d209d2009-02-03 20:21:25 +00005461 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005462 Chain, Dest, CC, Cmp);
5463 X86::CondCode CCode =
5464 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5465 CCode = X86::GetOppositeBranchCondition(CCode);
5466 CC = DAG.getConstant(CCode, MVT::i8);
5467 Cond = Cmp;
5468 addTest = false;
5469 }
5470 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005471 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005472 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5473 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5474 // It should be transformed during dag combiner except when the condition
5475 // is set by a arithmetics with overflow node.
5476 X86::CondCode CCode =
5477 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5478 CCode = X86::GetOppositeBranchCondition(CCode);
5479 CC = DAG.getConstant(CCode, MVT::i8);
5480 Cond = Cond.getOperand(0).getOperand(1);
5481 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005482 }
Evan Cheng0488db92007-09-25 01:57:46 +00005483 }
5484
5485 if (addTest) {
5486 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005487 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005488 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005489 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005490 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005491}
5492
Anton Korobeynikove060b532007-04-17 19:34:00 +00005493
5494// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5495// Calls to _alloca is needed to probe the stack when allocating more than 4k
5496// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5497// that the guard pages used by the OS virtual memory manager are allocated in
5498// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005499SDValue
5500X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005501 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005502 assert(Subtarget->isTargetCygMing() &&
5503 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005504 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005505
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005506 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005507 SDValue Chain = Op.getOperand(0);
5508 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005509 // FIXME: Ensure alignment here
5510
Dan Gohman475871a2008-07-27 21:46:04 +00005511 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005512
Duncan Sands83ec4b62008-06-06 12:08:01 +00005513 MVT IntPtr = getPointerTy();
5514 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005515
Chris Lattnere563bbc2008-10-11 22:08:30 +00005516 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005517
Dale Johannesendd64c412009-02-04 00:33:20 +00005518 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005519 Flag = Chain.getValue(1);
5520
5521 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005522 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005523 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005524 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005525 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005526 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005527 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005528 Flag = Chain.getValue(1);
5529
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005530 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005531 DAG.getIntPtrConstant(0, true),
5532 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005533 Flag);
5534
Dale Johannesendd64c412009-02-04 00:33:20 +00005535 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005536
Dan Gohman475871a2008-07-27 21:46:04 +00005537 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005538 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005539}
5540
Dan Gohman475871a2008-07-27 21:46:04 +00005541SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005542X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005543 SDValue Chain,
5544 SDValue Dst, SDValue Src,
5545 SDValue Size, unsigned Align,
5546 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005547 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005548 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005549
Bill Wendling6f287b22008-09-30 21:22:07 +00005550 // If not DWORD aligned or size is more than the threshold, call the library.
5551 // The libc version is likely to be faster for these cases. It can use the
5552 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005553 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005554 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005555 ConstantSize->getZExtValue() >
5556 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005557 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005558
5559 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005560 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005561
Bill Wendling6158d842008-10-01 00:59:58 +00005562 if (const char *bzeroEntry = V &&
5563 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5564 MVT IntPtr = getPointerTy();
5565 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005566 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005567 TargetLowering::ArgListEntry Entry;
5568 Entry.Node = Dst;
5569 Entry.Ty = IntPtrTy;
5570 Args.push_back(Entry);
5571 Entry.Node = Size;
5572 Args.push_back(Entry);
5573 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005574 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5575 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005576 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005577 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005578 }
5579
Dan Gohman707e0182008-04-12 04:36:06 +00005580 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005581 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005582 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005583
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005584 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005585 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005586 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005587 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005588 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005589 unsigned BytesLeft = 0;
5590 bool TwoRepStos = false;
5591 if (ValC) {
5592 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005593 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005594
Evan Cheng0db9fe62006-04-25 20:13:52 +00005595 // If the value is a constant, then we can potentially use larger sets.
5596 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005597 case 2: // WORD aligned
5598 AVT = MVT::i16;
5599 ValReg = X86::AX;
5600 Val = (Val << 8) | Val;
5601 break;
5602 case 0: // DWORD aligned
5603 AVT = MVT::i32;
5604 ValReg = X86::EAX;
5605 Val = (Val << 8) | Val;
5606 Val = (Val << 16) | Val;
5607 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5608 AVT = MVT::i64;
5609 ValReg = X86::RAX;
5610 Val = (Val << 32) | Val;
5611 }
5612 break;
5613 default: // Byte aligned
5614 AVT = MVT::i8;
5615 ValReg = X86::AL;
5616 Count = DAG.getIntPtrConstant(SizeVal);
5617 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005618 }
5619
Duncan Sands8e4eb092008-06-08 20:54:56 +00005620 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005621 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005622 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5623 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005624 }
5625
Dale Johannesen0f502f62009-02-03 22:26:09 +00005626 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627 InFlag);
5628 InFlag = Chain.getValue(1);
5629 } else {
5630 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005631 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005632 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005633 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005634 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005635
Scott Michelfdc40a02009-02-17 22:15:04 +00005636 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005637 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005638 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005639 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005640 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005641 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005642 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005643 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005644
Chris Lattnerd96d0722007-02-25 06:40:16 +00005645 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005647 Ops.push_back(Chain);
5648 Ops.push_back(DAG.getValueType(AVT));
5649 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005650 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005651
Evan Cheng0db9fe62006-04-25 20:13:52 +00005652 if (TwoRepStos) {
5653 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005654 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005655 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005656 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005657 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005658 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005659 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005660 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005661 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005662 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005663 Ops.clear();
5664 Ops.push_back(Chain);
5665 Ops.push_back(DAG.getValueType(MVT::i8));
5666 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005667 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005668 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005669 // Handle the last 1 - 7 bytes.
5670 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005671 MVT AddrVT = Dst.getValueType();
5672 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005673
Dale Johannesen0f502f62009-02-03 22:26:09 +00005674 Chain = DAG.getMemset(Chain, dl,
5675 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005676 DAG.getConstant(Offset, AddrVT)),
5677 Src,
5678 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005679 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005680 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005681
Dan Gohman707e0182008-04-12 04:36:06 +00005682 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005683 return Chain;
5684}
Evan Cheng11e15b32006-04-03 20:53:28 +00005685
Dan Gohman475871a2008-07-27 21:46:04 +00005686SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005687X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005688 SDValue Chain, SDValue Dst, SDValue Src,
5689 SDValue Size, unsigned Align,
5690 bool AlwaysInline,
5691 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005692 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005693 // This requires the copy size to be a constant, preferrably
5694 // within a subtarget-specific limit.
5695 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5696 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005697 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005698 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005699 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005700 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005701
Evan Cheng1887c1c2008-08-21 21:00:15 +00005702 /// If not DWORD aligned, call the library.
5703 if ((Align & 3) != 0)
5704 return SDValue();
5705
5706 // DWORD aligned
5707 MVT AVT = MVT::i32;
5708 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005709 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005710
Duncan Sands83ec4b62008-06-06 12:08:01 +00005711 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005712 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005713 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005714 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005715
Dan Gohman475871a2008-07-27 21:46:04 +00005716 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005717 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005718 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005719 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005721 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005722 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005723 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005724 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005725 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005726 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005727 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005728 InFlag = Chain.getValue(1);
5729
Chris Lattnerd96d0722007-02-25 06:40:16 +00005730 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005731 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005732 Ops.push_back(Chain);
5733 Ops.push_back(DAG.getValueType(AVT));
5734 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005735 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005736
Dan Gohman475871a2008-07-27 21:46:04 +00005737 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005738 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005739 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005740 // Handle the last 1 - 7 bytes.
5741 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005742 MVT DstVT = Dst.getValueType();
5743 MVT SrcVT = Src.getValueType();
5744 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005745 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005746 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005747 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005748 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005749 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005750 DAG.getConstant(BytesLeft, SizeVT),
5751 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005752 DstSV, DstSVOff + Offset,
5753 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005754 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005755
Scott Michelfdc40a02009-02-17 22:15:04 +00005756 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005757 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005758}
5759
Dan Gohman475871a2008-07-27 21:46:04 +00005760SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005761 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005762 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005763
Evan Cheng25ab6902006-09-08 06:48:29 +00005764 if (!Subtarget->is64Bit()) {
5765 // vastart just stores the address of the VarArgsFrameIndex slot into the
5766 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005767 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005768 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005769 }
5770
5771 // __va_list_tag:
5772 // gp_offset (0 - 6 * 8)
5773 // fp_offset (48 - 48 + 8 * 16)
5774 // overflow_arg_area (point to parameters coming in memory).
5775 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005776 SmallVector<SDValue, 8> MemOps;
5777 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005778 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005779 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005780 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005781 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005782 MemOps.push_back(Store);
5783
5784 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005785 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005786 FIN, DAG.getIntPtrConstant(4));
5787 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005788 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005789 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005790 MemOps.push_back(Store);
5791
5792 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005793 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005794 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005795 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005796 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005797 MemOps.push_back(Store);
5798
5799 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005800 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005801 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005802 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005803 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005804 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005805 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005806 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005807}
5808
Dan Gohman475871a2008-07-27 21:46:04 +00005809SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005810 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5811 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005812 SDValue Chain = Op.getOperand(0);
5813 SDValue SrcPtr = Op.getOperand(1);
5814 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005815
5816 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5817 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005818 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005819}
5820
Dan Gohman475871a2008-07-27 21:46:04 +00005821SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005822 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005823 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005824 SDValue Chain = Op.getOperand(0);
5825 SDValue DstPtr = Op.getOperand(1);
5826 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005827 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5828 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005829 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005830
Dale Johannesendd64c412009-02-04 00:33:20 +00005831 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005832 DAG.getIntPtrConstant(24), 8, false,
5833 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005834}
5835
Dan Gohman475871a2008-07-27 21:46:04 +00005836SDValue
5837X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005838 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005839 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005840 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005841 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005842 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005843 case Intrinsic::x86_sse_comieq_ss:
5844 case Intrinsic::x86_sse_comilt_ss:
5845 case Intrinsic::x86_sse_comile_ss:
5846 case Intrinsic::x86_sse_comigt_ss:
5847 case Intrinsic::x86_sse_comige_ss:
5848 case Intrinsic::x86_sse_comineq_ss:
5849 case Intrinsic::x86_sse_ucomieq_ss:
5850 case Intrinsic::x86_sse_ucomilt_ss:
5851 case Intrinsic::x86_sse_ucomile_ss:
5852 case Intrinsic::x86_sse_ucomigt_ss:
5853 case Intrinsic::x86_sse_ucomige_ss:
5854 case Intrinsic::x86_sse_ucomineq_ss:
5855 case Intrinsic::x86_sse2_comieq_sd:
5856 case Intrinsic::x86_sse2_comilt_sd:
5857 case Intrinsic::x86_sse2_comile_sd:
5858 case Intrinsic::x86_sse2_comigt_sd:
5859 case Intrinsic::x86_sse2_comige_sd:
5860 case Intrinsic::x86_sse2_comineq_sd:
5861 case Intrinsic::x86_sse2_ucomieq_sd:
5862 case Intrinsic::x86_sse2_ucomilt_sd:
5863 case Intrinsic::x86_sse2_ucomile_sd:
5864 case Intrinsic::x86_sse2_ucomigt_sd:
5865 case Intrinsic::x86_sse2_ucomige_sd:
5866 case Intrinsic::x86_sse2_ucomineq_sd: {
5867 unsigned Opc = 0;
5868 ISD::CondCode CC = ISD::SETCC_INVALID;
5869 switch (IntNo) {
5870 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005871 case Intrinsic::x86_sse_comieq_ss:
5872 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005873 Opc = X86ISD::COMI;
5874 CC = ISD::SETEQ;
5875 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005876 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005877 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005878 Opc = X86ISD::COMI;
5879 CC = ISD::SETLT;
5880 break;
5881 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005882 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005883 Opc = X86ISD::COMI;
5884 CC = ISD::SETLE;
5885 break;
5886 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005887 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005888 Opc = X86ISD::COMI;
5889 CC = ISD::SETGT;
5890 break;
5891 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005892 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005893 Opc = X86ISD::COMI;
5894 CC = ISD::SETGE;
5895 break;
5896 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005897 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005898 Opc = X86ISD::COMI;
5899 CC = ISD::SETNE;
5900 break;
5901 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005902 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005903 Opc = X86ISD::UCOMI;
5904 CC = ISD::SETEQ;
5905 break;
5906 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005907 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005908 Opc = X86ISD::UCOMI;
5909 CC = ISD::SETLT;
5910 break;
5911 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005912 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913 Opc = X86ISD::UCOMI;
5914 CC = ISD::SETLE;
5915 break;
5916 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005917 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005918 Opc = X86ISD::UCOMI;
5919 CC = ISD::SETGT;
5920 break;
5921 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005922 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005923 Opc = X86ISD::UCOMI;
5924 CC = ISD::SETGE;
5925 break;
5926 case Intrinsic::x86_sse_ucomineq_ss:
5927 case Intrinsic::x86_sse2_ucomineq_sd:
5928 Opc = X86ISD::UCOMI;
5929 CC = ISD::SETNE;
5930 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005931 }
Evan Cheng734503b2006-09-11 02:19:56 +00005932
Dan Gohman475871a2008-07-27 21:46:04 +00005933 SDValue LHS = Op.getOperand(1);
5934 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005935 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005936 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5937 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005938 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005939 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005940 }
Evan Cheng5759f972008-05-04 09:15:50 +00005941
5942 // Fix vector shift instructions where the last operand is a non-immediate
5943 // i32 value.
5944 case Intrinsic::x86_sse2_pslli_w:
5945 case Intrinsic::x86_sse2_pslli_d:
5946 case Intrinsic::x86_sse2_pslli_q:
5947 case Intrinsic::x86_sse2_psrli_w:
5948 case Intrinsic::x86_sse2_psrli_d:
5949 case Intrinsic::x86_sse2_psrli_q:
5950 case Intrinsic::x86_sse2_psrai_w:
5951 case Intrinsic::x86_sse2_psrai_d:
5952 case Intrinsic::x86_mmx_pslli_w:
5953 case Intrinsic::x86_mmx_pslli_d:
5954 case Intrinsic::x86_mmx_pslli_q:
5955 case Intrinsic::x86_mmx_psrli_w:
5956 case Intrinsic::x86_mmx_psrli_d:
5957 case Intrinsic::x86_mmx_psrli_q:
5958 case Intrinsic::x86_mmx_psrai_w:
5959 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005961 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005962 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005963
5964 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005965 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005966 switch (IntNo) {
5967 case Intrinsic::x86_sse2_pslli_w:
5968 NewIntNo = Intrinsic::x86_sse2_psll_w;
5969 break;
5970 case Intrinsic::x86_sse2_pslli_d:
5971 NewIntNo = Intrinsic::x86_sse2_psll_d;
5972 break;
5973 case Intrinsic::x86_sse2_pslli_q:
5974 NewIntNo = Intrinsic::x86_sse2_psll_q;
5975 break;
5976 case Intrinsic::x86_sse2_psrli_w:
5977 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5978 break;
5979 case Intrinsic::x86_sse2_psrli_d:
5980 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5981 break;
5982 case Intrinsic::x86_sse2_psrli_q:
5983 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5984 break;
5985 case Intrinsic::x86_sse2_psrai_w:
5986 NewIntNo = Intrinsic::x86_sse2_psra_w;
5987 break;
5988 case Intrinsic::x86_sse2_psrai_d:
5989 NewIntNo = Intrinsic::x86_sse2_psra_d;
5990 break;
5991 default: {
5992 ShAmtVT = MVT::v2i32;
5993 switch (IntNo) {
5994 case Intrinsic::x86_mmx_pslli_w:
5995 NewIntNo = Intrinsic::x86_mmx_psll_w;
5996 break;
5997 case Intrinsic::x86_mmx_pslli_d:
5998 NewIntNo = Intrinsic::x86_mmx_psll_d;
5999 break;
6000 case Intrinsic::x86_mmx_pslli_q:
6001 NewIntNo = Intrinsic::x86_mmx_psll_q;
6002 break;
6003 case Intrinsic::x86_mmx_psrli_w:
6004 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6005 break;
6006 case Intrinsic::x86_mmx_psrli_d:
6007 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6008 break;
6009 case Intrinsic::x86_mmx_psrli_q:
6010 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6011 break;
6012 case Intrinsic::x86_mmx_psrai_w:
6013 NewIntNo = Intrinsic::x86_mmx_psra_w;
6014 break;
6015 case Intrinsic::x86_mmx_psrai_d:
6016 NewIntNo = Intrinsic::x86_mmx_psra_d;
6017 break;
6018 default: abort(); // Can't reach here.
6019 }
6020 break;
6021 }
6022 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006023 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006024 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6025 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6026 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006027 DAG.getConstant(NewIntNo, MVT::i32),
6028 Op.getOperand(1), ShAmt);
6029 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006030 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006031}
Evan Cheng72261582005-12-20 06:22:03 +00006032
Dan Gohman475871a2008-07-27 21:46:04 +00006033SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006034 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006035 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006036
6037 if (Depth > 0) {
6038 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6039 SDValue Offset =
6040 DAG.getConstant(TD->getPointerSize(),
6041 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006042 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006043 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006044 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006045 NULL, 0);
6046 }
6047
6048 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006049 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006050 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006051 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006052}
6053
Dan Gohman475871a2008-07-27 21:46:04 +00006054SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006055 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6056 MFI->setFrameAddressIsTaken(true);
6057 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006058 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006059 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6060 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006061 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006062 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006063 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006064 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006065}
6066
Dan Gohman475871a2008-07-27 21:46:04 +00006067SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006068 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006069 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006070}
6071
Dan Gohman475871a2008-07-27 21:46:04 +00006072SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006073{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006074 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006075 SDValue Chain = Op.getOperand(0);
6076 SDValue Offset = Op.getOperand(1);
6077 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006078 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006079
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006080 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6081 getPointerTy());
6082 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006083
Dale Johannesene4d209d2009-02-03 20:21:25 +00006084 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006085 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006086 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6087 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006088 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006089 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006090
Dale Johannesene4d209d2009-02-03 20:21:25 +00006091 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006092 MVT::Other,
6093 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006094}
6095
Dan Gohman475871a2008-07-27 21:46:04 +00006096SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006097 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006098 SDValue Root = Op.getOperand(0);
6099 SDValue Trmp = Op.getOperand(1); // trampoline
6100 SDValue FPtr = Op.getOperand(2); // nested function
6101 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006102 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006103
Dan Gohman69de1932008-02-06 22:27:42 +00006104 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006105
Duncan Sands339e14f2008-01-16 22:55:25 +00006106 const X86InstrInfo *TII =
6107 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6108
Duncan Sandsb116fac2007-07-27 20:02:49 +00006109 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006110 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006111
6112 // Large code-model.
6113
6114 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6115 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6116
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006117 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6118 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006119
6120 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6121
6122 // Load the pointer to the nested function into R11.
6123 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006124 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006125 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6126 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006127
Scott Michelfdc40a02009-02-17 22:15:04 +00006128 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006129 DAG.getConstant(2, MVT::i64));
6130 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006131
6132 // Load the 'nest' parameter value into R10.
6133 // R10 is specified in X86CallingConv.td
6134 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006135 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006136 DAG.getConstant(10, MVT::i64));
6137 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6138 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006139
Scott Michelfdc40a02009-02-17 22:15:04 +00006140 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006141 DAG.getConstant(12, MVT::i64));
6142 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006143
6144 // Jump to the nested function.
6145 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006146 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006147 DAG.getConstant(20, MVT::i64));
6148 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6149 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006150
6151 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006152 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006153 DAG.getConstant(22, MVT::i64));
6154 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006155 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006156
Dan Gohman475871a2008-07-27 21:46:04 +00006157 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006158 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6159 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006160 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006161 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006162 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6163 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006164 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006165
6166 switch (CC) {
6167 default:
6168 assert(0 && "Unsupported calling convention");
6169 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006170 case CallingConv::X86_StdCall: {
6171 // Pass 'nest' parameter in ECX.
6172 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006173 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006174
6175 // Check that ECX wasn't needed by an 'inreg' parameter.
6176 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006177 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006178
Chris Lattner58d74912008-03-12 17:45:29 +00006179 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006180 unsigned InRegCount = 0;
6181 unsigned Idx = 1;
6182
6183 for (FunctionType::param_iterator I = FTy->param_begin(),
6184 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006185 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006186 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006187 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006188
6189 if (InRegCount > 2) {
6190 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6191 abort();
6192 }
6193 }
6194 break;
6195 }
6196 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006197 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006198 // Pass 'nest' parameter in EAX.
6199 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006200 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006201 break;
6202 }
6203
Dan Gohman475871a2008-07-27 21:46:04 +00006204 SDValue OutChains[4];
6205 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006206
Scott Michelfdc40a02009-02-17 22:15:04 +00006207 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006208 DAG.getConstant(10, MVT::i32));
6209 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006210
Duncan Sands339e14f2008-01-16 22:55:25 +00006211 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006212 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006213 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006214 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006215 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006216
Scott Michelfdc40a02009-02-17 22:15:04 +00006217 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006218 DAG.getConstant(1, MVT::i32));
6219 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006220
Duncan Sands339e14f2008-01-16 22:55:25 +00006221 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006222 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006223 DAG.getConstant(5, MVT::i32));
6224 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006225 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006226
Scott Michelfdc40a02009-02-17 22:15:04 +00006227 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006228 DAG.getConstant(6, MVT::i32));
6229 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006230
Dan Gohman475871a2008-07-27 21:46:04 +00006231 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006232 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6233 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006234 }
6235}
6236
Dan Gohman475871a2008-07-27 21:46:04 +00006237SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006238 /*
6239 The rounding mode is in bits 11:10 of FPSR, and has the following
6240 settings:
6241 00 Round to nearest
6242 01 Round to -inf
6243 10 Round to +inf
6244 11 Round to 0
6245
6246 FLT_ROUNDS, on the other hand, expects the following:
6247 -1 Undefined
6248 0 Round to 0
6249 1 Round to nearest
6250 2 Round to +inf
6251 3 Round to -inf
6252
6253 To perform the conversion, we do:
6254 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6255 */
6256
6257 MachineFunction &MF = DAG.getMachineFunction();
6258 const TargetMachine &TM = MF.getTarget();
6259 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6260 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006261 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006262 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006263
6264 // Save FP Control Word to stack slot
6265 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006266 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006267
Dale Johannesene4d209d2009-02-03 20:21:25 +00006268 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006269 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006270
6271 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006272 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006273
6274 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006275 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006276 DAG.getNode(ISD::SRL, dl, MVT::i16,
6277 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006278 CWD, DAG.getConstant(0x800, MVT::i16)),
6279 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006280 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006281 DAG.getNode(ISD::SRL, dl, MVT::i16,
6282 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006283 CWD, DAG.getConstant(0x400, MVT::i16)),
6284 DAG.getConstant(9, MVT::i8));
6285
Dan Gohman475871a2008-07-27 21:46:04 +00006286 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006287 DAG.getNode(ISD::AND, dl, MVT::i16,
6288 DAG.getNode(ISD::ADD, dl, MVT::i16,
6289 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006290 DAG.getConstant(1, MVT::i16)),
6291 DAG.getConstant(3, MVT::i16));
6292
6293
Duncan Sands83ec4b62008-06-06 12:08:01 +00006294 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006295 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006296}
6297
Dan Gohman475871a2008-07-27 21:46:04 +00006298SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006299 MVT VT = Op.getValueType();
6300 MVT OpVT = VT;
6301 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006302 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006303
6304 Op = Op.getOperand(0);
6305 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006306 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006307 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006308 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006309 }
Evan Cheng18efe262007-12-14 02:13:44 +00006310
Evan Cheng152804e2007-12-14 08:30:15 +00006311 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6312 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006313 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006314
6315 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006316 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006317 Ops.push_back(Op);
6318 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6319 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6320 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006321 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006322
6323 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006324 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006325
Evan Cheng18efe262007-12-14 02:13:44 +00006326 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006327 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006328 return Op;
6329}
6330
Dan Gohman475871a2008-07-27 21:46:04 +00006331SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006332 MVT VT = Op.getValueType();
6333 MVT OpVT = VT;
6334 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006335 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006336
6337 Op = Op.getOperand(0);
6338 if (VT == MVT::i8) {
6339 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006340 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006341 }
Evan Cheng152804e2007-12-14 08:30:15 +00006342
6343 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6344 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006345 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006346
6347 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006348 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006349 Ops.push_back(Op);
6350 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6351 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6352 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006353 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006354
Evan Cheng18efe262007-12-14 02:13:44 +00006355 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006356 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006357 return Op;
6358}
6359
Mon P Wangaf9b9522008-12-18 21:42:19 +00006360SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6361 MVT VT = Op.getValueType();
6362 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006363 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006364
Mon P Wangaf9b9522008-12-18 21:42:19 +00006365 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6366 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6367 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6368 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6369 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6370 //
6371 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6372 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6373 // return AloBlo + AloBhi + AhiBlo;
6374
6375 SDValue A = Op.getOperand(0);
6376 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006377
Dale Johannesene4d209d2009-02-03 20:21:25 +00006378 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006379 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6380 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006381 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006382 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6383 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006384 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006385 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6386 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006387 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006388 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6389 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006390 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006391 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6392 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006393 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006394 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6395 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006396 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006397 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6398 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006399 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6400 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006401 return Res;
6402}
6403
6404
Bill Wendling74c37652008-12-09 22:08:41 +00006405SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6406 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6407 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006408 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6409 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006410 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006411 SDValue LHS = N->getOperand(0);
6412 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006413 unsigned BaseOp = 0;
6414 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006415 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006416
6417 switch (Op.getOpcode()) {
6418 default: assert(0 && "Unknown ovf instruction!");
6419 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006420 // A subtract of one will be selected as a INC. Note that INC doesn't
6421 // set CF, so we can't do this for UADDO.
6422 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6423 if (C->getAPIntValue() == 1) {
6424 BaseOp = X86ISD::INC;
6425 Cond = X86::COND_O;
6426 break;
6427 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006428 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006429 Cond = X86::COND_O;
6430 break;
6431 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006432 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006433 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006434 break;
6435 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006436 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6437 // set CF, so we can't do this for USUBO.
6438 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6439 if (C->getAPIntValue() == 1) {
6440 BaseOp = X86ISD::DEC;
6441 Cond = X86::COND_O;
6442 break;
6443 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006444 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006445 Cond = X86::COND_O;
6446 break;
6447 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006448 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006449 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006450 break;
6451 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006452 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006453 Cond = X86::COND_O;
6454 break;
6455 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006456 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006457 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006458 break;
6459 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006460
Bill Wendling61edeb52008-12-02 01:06:39 +00006461 // Also sets EFLAGS.
6462 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006463 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006464
Bill Wendling61edeb52008-12-02 01:06:39 +00006465 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006466 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006467 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006468
Bill Wendling61edeb52008-12-02 01:06:39 +00006469 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6470 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006471}
6472
Dan Gohman475871a2008-07-27 21:46:04 +00006473SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006474 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006475 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006476 unsigned Reg = 0;
6477 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006478 switch(T.getSimpleVT()) {
6479 default:
6480 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006481 case MVT::i8: Reg = X86::AL; size = 1; break;
6482 case MVT::i16: Reg = X86::AX; size = 2; break;
6483 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006484 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006485 assert(Subtarget->is64Bit() && "Node not type legal!");
6486 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006487 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006488 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006489 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006490 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006491 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006492 Op.getOperand(1),
6493 Op.getOperand(3),
6494 DAG.getTargetConstant(size, MVT::i8),
6495 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006496 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006497 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006498 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006499 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006500 return cpOut;
6501}
6502
Duncan Sands1607f052008-12-01 11:39:25 +00006503SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006504 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006505 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006506 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006507 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006508 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006509 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006510 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6511 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006512 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006513 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006514 DAG.getConstant(32, MVT::i8));
6515 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006516 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006517 rdx.getValue(1)
6518 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006519 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006520}
6521
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006522SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6523 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006524 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006525 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006526 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006527 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006528 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006529 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006530 Node->getOperand(0),
6531 Node->getOperand(1), negOp,
6532 cast<AtomicSDNode>(Node)->getSrcValue(),
6533 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006534}
6535
Evan Cheng0db9fe62006-04-25 20:13:52 +00006536/// LowerOperation - Provide custom lowering hooks for some operations.
6537///
Dan Gohman475871a2008-07-27 21:46:04 +00006538SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006539 switch (Op.getOpcode()) {
6540 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006541 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6542 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006543 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6544 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6545 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6546 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6547 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6548 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6549 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006550 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006551 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006552 case ISD::SHL_PARTS:
6553 case ISD::SRA_PARTS:
6554 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6555 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006556 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006557 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6558 case ISD::FABS: return LowerFABS(Op, DAG);
6559 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006560 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006561 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006562 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006563 case ISD::SELECT: return LowerSELECT(Op, DAG);
6564 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006565 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006566 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006568 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006569 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006570 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006571 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006572 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006573 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6574 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006575 case ISD::FRAME_TO_ARGS_OFFSET:
6576 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006577 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006578 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006579 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006580 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006581 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6582 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006583 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006584 case ISD::SADDO:
6585 case ISD::UADDO:
6586 case ISD::SSUBO:
6587 case ISD::USUBO:
6588 case ISD::SMULO:
6589 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006590 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006591 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006592}
6593
Duncan Sands1607f052008-12-01 11:39:25 +00006594void X86TargetLowering::
6595ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6596 SelectionDAG &DAG, unsigned NewOp) {
6597 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006598 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006599 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6600
6601 SDValue Chain = Node->getOperand(0);
6602 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006603 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006604 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006605 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006606 Node->getOperand(2), DAG.getIntPtrConstant(1));
6607 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6608 // have a MemOperand. Pass the info through as a normal operand.
6609 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6610 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6611 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006612 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006613 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006614 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006615 Results.push_back(Result.getValue(2));
6616}
6617
Duncan Sands126d9072008-07-04 11:47:58 +00006618/// ReplaceNodeResults - Replace a node with an illegal result type
6619/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006620void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6621 SmallVectorImpl<SDValue>&Results,
6622 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006623 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006624 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006625 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006626 assert(false && "Do not know how to custom type legalize this operation!");
6627 return;
6628 case ISD::FP_TO_SINT: {
6629 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6630 SDValue FIST = Vals.first, StackSlot = Vals.second;
6631 if (FIST.getNode() != 0) {
6632 MVT VT = N->getValueType(0);
6633 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006634 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006635 }
6636 return;
6637 }
6638 case ISD::READCYCLECOUNTER: {
6639 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6640 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006641 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006642 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006643 rd.getValue(1));
6644 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006645 eax.getValue(2));
6646 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6647 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006648 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006649 Results.push_back(edx.getValue(1));
6650 return;
6651 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006652 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006653 MVT T = N->getValueType(0);
6654 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6655 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006656 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006657 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006658 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006659 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006660 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6661 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006662 cpInL.getValue(1));
6663 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006664 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006665 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006666 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006667 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006668 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006669 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006670 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006671 swapInL.getValue(1));
6672 SDValue Ops[] = { swapInH.getValue(0),
6673 N->getOperand(1),
6674 swapInH.getValue(1) };
6675 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006676 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006677 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6678 MVT::i32, Result.getValue(1));
6679 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6680 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006681 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006682 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006683 Results.push_back(cpOutH.getValue(1));
6684 return;
6685 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006686 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006687 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6688 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006689 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006690 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6691 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006692 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006693 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6694 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006695 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006696 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6697 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006698 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006699 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6700 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006701 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006702 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6703 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006704 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006705 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6706 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006707 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006708}
6709
Evan Cheng72261582005-12-20 06:22:03 +00006710const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6711 switch (Opcode) {
6712 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006713 case X86ISD::BSF: return "X86ISD::BSF";
6714 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006715 case X86ISD::SHLD: return "X86ISD::SHLD";
6716 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006717 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006718 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006719 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006720 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006721 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006722 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006723 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6724 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6725 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006726 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006727 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006728 case X86ISD::CALL: return "X86ISD::CALL";
6729 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6730 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006731 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006732 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006733 case X86ISD::COMI: return "X86ISD::COMI";
6734 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006735 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006736 case X86ISD::CMOV: return "X86ISD::CMOV";
6737 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006738 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006739 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6740 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006741 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006742 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006743 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006744 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006745 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6746 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006747 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006748 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006749 case X86ISD::FMAX: return "X86ISD::FMAX";
6750 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006751 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6752 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006753 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006754 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006755 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006756 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006757 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006758 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6759 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006760 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6761 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6762 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6763 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6764 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6765 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006766 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6767 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006768 case X86ISD::VSHL: return "X86ISD::VSHL";
6769 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006770 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6771 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6772 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6773 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6774 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6775 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6776 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6777 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6778 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6779 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006780 case X86ISD::ADD: return "X86ISD::ADD";
6781 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006782 case X86ISD::SMUL: return "X86ISD::SMUL";
6783 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006784 case X86ISD::INC: return "X86ISD::INC";
6785 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006786 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006787 }
6788}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006789
Chris Lattnerc9addb72007-03-30 23:15:24 +00006790// isLegalAddressingMode - Return true if the addressing mode represented
6791// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006792bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006793 const Type *Ty) const {
6794 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006795
Chris Lattnerc9addb72007-03-30 23:15:24 +00006796 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6797 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6798 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006799
Chris Lattnerc9addb72007-03-30 23:15:24 +00006800 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006801 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006802 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6803 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006804 // If BaseGV requires a register, we cannot also have a BaseReg.
6805 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6806 AM.HasBaseReg)
6807 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006808
6809 // X86-64 only supports addr of globals in small code model.
6810 if (Subtarget->is64Bit()) {
6811 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6812 return false;
6813 // If lower 4G is not available, then we must use rip-relative addressing.
6814 if (AM.BaseOffs || AM.Scale > 1)
6815 return false;
6816 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006818
Chris Lattnerc9addb72007-03-30 23:15:24 +00006819 switch (AM.Scale) {
6820 case 0:
6821 case 1:
6822 case 2:
6823 case 4:
6824 case 8:
6825 // These scales always work.
6826 break;
6827 case 3:
6828 case 5:
6829 case 9:
6830 // These scales are formed with basereg+scalereg. Only accept if there is
6831 // no basereg yet.
6832 if (AM.HasBaseReg)
6833 return false;
6834 break;
6835 default: // Other stuff never works.
6836 return false;
6837 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006838
Chris Lattnerc9addb72007-03-30 23:15:24 +00006839 return true;
6840}
6841
6842
Evan Cheng2bd122c2007-10-26 01:56:11 +00006843bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6844 if (!Ty1->isInteger() || !Ty2->isInteger())
6845 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006846 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6847 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006848 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006849 return false;
6850 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006851}
6852
Duncan Sands83ec4b62008-06-06 12:08:01 +00006853bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6854 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006855 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006856 unsigned NumBits1 = VT1.getSizeInBits();
6857 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006858 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006859 return false;
6860 return Subtarget->is64Bit() || NumBits1 < 64;
6861}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006862
Dan Gohman97121ba2009-04-08 00:15:30 +00006863bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006864 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006865 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6866}
6867
6868bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006869 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006870 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6871}
6872
Evan Cheng60c07e12006-07-05 22:17:51 +00006873/// isShuffleMaskLegal - Targets can use this to indicate that they only
6874/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6875/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6876/// are assumed to be legal.
6877bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006878X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6879 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006880 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006881 if (VT.getSizeInBits() == 64)
6882 return false;
6883
6884 // FIXME: pshufb, blends, palignr, shifts.
6885 return (VT.getVectorNumElements() == 2 ||
6886 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6887 isMOVLMask(M, VT) ||
6888 isSHUFPMask(M, VT) ||
6889 isPSHUFDMask(M, VT) ||
6890 isPSHUFHWMask(M, VT) ||
6891 isPSHUFLWMask(M, VT) ||
6892 isUNPCKLMask(M, VT) ||
6893 isUNPCKHMask(M, VT) ||
6894 isUNPCKL_v_undef_Mask(M, VT) ||
6895 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006896}
6897
Dan Gohman7d8143f2008-04-09 20:09:42 +00006898bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006899X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006900 MVT VT) const {
6901 unsigned NumElts = VT.getVectorNumElements();
6902 // FIXME: This collection of masks seems suspect.
6903 if (NumElts == 2)
6904 return true;
6905 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6906 return (isMOVLMask(Mask, VT) ||
6907 isCommutedMOVLMask(Mask, VT, true) ||
6908 isSHUFPMask(Mask, VT) ||
6909 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006910 }
6911 return false;
6912}
6913
6914//===----------------------------------------------------------------------===//
6915// X86 Scheduler Hooks
6916//===----------------------------------------------------------------------===//
6917
Mon P Wang63307c32008-05-05 19:05:59 +00006918// private utility function
6919MachineBasicBlock *
6920X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6921 MachineBasicBlock *MBB,
6922 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006923 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006924 unsigned LoadOpc,
6925 unsigned CXchgOpc,
6926 unsigned copyOpc,
6927 unsigned notOpc,
6928 unsigned EAXreg,
6929 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006930 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006931 // For the atomic bitwise operator, we generate
6932 // thisMBB:
6933 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006934 // ld t1 = [bitinstr.addr]
6935 // op t2 = t1, [bitinstr.val]
6936 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006937 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6938 // bz newMBB
6939 // fallthrough -->nextMBB
6940 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6941 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006942 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006943 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006944
Mon P Wang63307c32008-05-05 19:05:59 +00006945 /// First build the CFG
6946 MachineFunction *F = MBB->getParent();
6947 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006948 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6949 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6950 F->insert(MBBIter, newMBB);
6951 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006952
Mon P Wang63307c32008-05-05 19:05:59 +00006953 // Move all successors to thisMBB to nextMBB
6954 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006955
Mon P Wang63307c32008-05-05 19:05:59 +00006956 // Update thisMBB to fall through to newMBB
6957 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006958
Mon P Wang63307c32008-05-05 19:05:59 +00006959 // newMBB jumps to itself and fall through to nextMBB
6960 newMBB->addSuccessor(nextMBB);
6961 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006962
Mon P Wang63307c32008-05-05 19:05:59 +00006963 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006964 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
6965 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00006966 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00006967 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006968 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00006969 int numArgs = bInstr->getNumOperands() - 1;
6970 for (int i=0; i < numArgs; ++i)
6971 argOpers[i] = &bInstr->getOperand(i+1);
6972
6973 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006974 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
6975 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00006976
Dale Johannesen140be2d2008-08-19 18:47:28 +00006977 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006978 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006979 for (int i=0; i <= lastAddrIndx; ++i)
6980 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006981
Dale Johannesen140be2d2008-08-19 18:47:28 +00006982 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006983 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006984 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006985 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006986 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006987 tt = t1;
6988
Dale Johannesen140be2d2008-08-19 18:47:28 +00006989 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00006990 assert((argOpers[valArgIndx]->isReg() ||
6991 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00006992 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00006993 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00006994 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00006995 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00006996 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006997 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006998 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006999
Dale Johannesene4d209d2009-02-03 20:21:25 +00007000 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007001 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007002
Dale Johannesene4d209d2009-02-03 20:21:25 +00007003 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007004 for (int i=0; i <= lastAddrIndx; ++i)
7005 (*MIB).addOperand(*argOpers[i]);
7006 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007007 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7008 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7009
Dale Johannesene4d209d2009-02-03 20:21:25 +00007010 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007011 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007012
Mon P Wang63307c32008-05-05 19:05:59 +00007013 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007014 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007015
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007016 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007017 return nextMBB;
7018}
7019
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007020// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007021MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007022X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7023 MachineBasicBlock *MBB,
7024 unsigned regOpcL,
7025 unsigned regOpcH,
7026 unsigned immOpcL,
7027 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007028 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007029 // For the atomic bitwise operator, we generate
7030 // thisMBB (instructions are in pairs, except cmpxchg8b)
7031 // ld t1,t2 = [bitinstr.addr]
7032 // newMBB:
7033 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7034 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007035 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007036 // mov ECX, EBX <- t5, t6
7037 // mov EAX, EDX <- t1, t2
7038 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7039 // mov t3, t4 <- EAX, EDX
7040 // bz newMBB
7041 // result in out1, out2
7042 // fallthrough -->nextMBB
7043
7044 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7045 const unsigned LoadOpc = X86::MOV32rm;
7046 const unsigned copyOpc = X86::MOV32rr;
7047 const unsigned NotOpc = X86::NOT32r;
7048 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7049 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7050 MachineFunction::iterator MBBIter = MBB;
7051 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007052
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007053 /// First build the CFG
7054 MachineFunction *F = MBB->getParent();
7055 MachineBasicBlock *thisMBB = MBB;
7056 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7057 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7058 F->insert(MBBIter, newMBB);
7059 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007060
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007061 // Move all successors to thisMBB to nextMBB
7062 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007063
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007064 // Update thisMBB to fall through to newMBB
7065 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007066
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007067 // newMBB jumps to itself and fall through to nextMBB
7068 newMBB->addSuccessor(nextMBB);
7069 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007070
Dale Johannesene4d209d2009-02-03 20:21:25 +00007071 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007072 // Insert instructions into newMBB based on incoming instruction
7073 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007074 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
7075 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007076 MachineOperand& dest1Oper = bInstr->getOperand(0);
7077 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007078 MachineOperand* argOpers[2 + X86AddrNumOperands];
7079 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007080 argOpers[i] = &bInstr->getOperand(i+2);
7081
7082 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007083 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007084
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007085 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007086 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007087 for (int i=0; i <= lastAddrIndx; ++i)
7088 (*MIB).addOperand(*argOpers[i]);
7089 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007090 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007091 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007092 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007093 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007094 MachineOperand newOp3 = *(argOpers[3]);
7095 if (newOp3.isImm())
7096 newOp3.setImm(newOp3.getImm()+4);
7097 else
7098 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007099 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007100 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007101
7102 // t3/4 are defined later, at the bottom of the loop
7103 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7104 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007105 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007106 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007107 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007108 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7109
7110 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7111 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007112 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007113 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7114 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007115 } else {
7116 tt1 = t1;
7117 tt2 = t2;
7118 }
7119
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007120 int valArgIndx = lastAddrIndx + 1;
7121 assert((argOpers[valArgIndx]->isReg() ||
7122 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007123 "invalid operand");
7124 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7125 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007126 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007127 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007128 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007129 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007130 if (regOpcL != X86::MOV32rr)
7131 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007132 (*MIB).addOperand(*argOpers[valArgIndx]);
7133 assert(argOpers[valArgIndx + 1]->isReg() ==
7134 argOpers[valArgIndx]->isReg());
7135 assert(argOpers[valArgIndx + 1]->isImm() ==
7136 argOpers[valArgIndx]->isImm());
7137 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007138 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007139 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007140 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007141 if (regOpcH != X86::MOV32rr)
7142 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007143 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007144
Dale Johannesene4d209d2009-02-03 20:21:25 +00007145 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007146 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007147 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007148 MIB.addReg(t2);
7149
Dale Johannesene4d209d2009-02-03 20:21:25 +00007150 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007151 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007152 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007153 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007154
Dale Johannesene4d209d2009-02-03 20:21:25 +00007155 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007156 for (int i=0; i <= lastAddrIndx; ++i)
7157 (*MIB).addOperand(*argOpers[i]);
7158
7159 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7160 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7161
Dale Johannesene4d209d2009-02-03 20:21:25 +00007162 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007163 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007164 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007165 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007166
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007167 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007168 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007169
7170 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7171 return nextMBB;
7172}
7173
7174// private utility function
7175MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007176X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7177 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007178 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007179 // For the atomic min/max operator, we generate
7180 // thisMBB:
7181 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007182 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007183 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007184 // cmp t1, t2
7185 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007186 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007187 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7188 // bz newMBB
7189 // fallthrough -->nextMBB
7190 //
7191 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7192 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007193 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007194 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007195
Mon P Wang63307c32008-05-05 19:05:59 +00007196 /// First build the CFG
7197 MachineFunction *F = MBB->getParent();
7198 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007199 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7200 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7201 F->insert(MBBIter, newMBB);
7202 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007203
Mon P Wang63307c32008-05-05 19:05:59 +00007204 // Move all successors to thisMBB to nextMBB
7205 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007206
Mon P Wang63307c32008-05-05 19:05:59 +00007207 // Update thisMBB to fall through to newMBB
7208 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007209
Mon P Wang63307c32008-05-05 19:05:59 +00007210 // newMBB jumps to newMBB and fall through to nextMBB
7211 newMBB->addSuccessor(nextMBB);
7212 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007213
Dale Johannesene4d209d2009-02-03 20:21:25 +00007214 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007215 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007216 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
7217 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007218 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007219 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007220 int numArgs = mInstr->getNumOperands() - 1;
7221 for (int i=0; i < numArgs; ++i)
7222 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007223
Mon P Wang63307c32008-05-05 19:05:59 +00007224 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007225 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7226 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007227
Mon P Wangab3e7472008-05-05 22:56:23 +00007228 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007229 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007230 for (int i=0; i <= lastAddrIndx; ++i)
7231 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007232
Mon P Wang63307c32008-05-05 19:05:59 +00007233 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007234 assert((argOpers[valArgIndx]->isReg() ||
7235 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007236 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007237
7238 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007239 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007240 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007241 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007242 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007243 (*MIB).addOperand(*argOpers[valArgIndx]);
7244
Dale Johannesene4d209d2009-02-03 20:21:25 +00007245 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007246 MIB.addReg(t1);
7247
Dale Johannesene4d209d2009-02-03 20:21:25 +00007248 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007249 MIB.addReg(t1);
7250 MIB.addReg(t2);
7251
7252 // Generate movc
7253 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007254 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007255 MIB.addReg(t2);
7256 MIB.addReg(t1);
7257
7258 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007259 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007260 for (int i=0; i <= lastAddrIndx; ++i)
7261 (*MIB).addOperand(*argOpers[i]);
7262 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007263 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7264 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007265
Dale Johannesene4d209d2009-02-03 20:21:25 +00007266 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007267 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007268
Mon P Wang63307c32008-05-05 19:05:59 +00007269 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007270 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007271
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007272 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007273 return nextMBB;
7274}
7275
7276
Evan Cheng60c07e12006-07-05 22:17:51 +00007277MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007278X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007279 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007280 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007281 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007282 switch (MI->getOpcode()) {
7283 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007284 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007285 case X86::CMOV_FR32:
7286 case X86::CMOV_FR64:
7287 case X86::CMOV_V4F32:
7288 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007289 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007290 // To "insert" a SELECT_CC instruction, we actually have to insert the
7291 // diamond control-flow pattern. The incoming instruction knows the
7292 // destination vreg to set, the condition code register to branch on, the
7293 // true/false values to select between, and a branch opcode to use.
7294 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007295 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007296 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007297
Evan Cheng60c07e12006-07-05 22:17:51 +00007298 // thisMBB:
7299 // ...
7300 // TrueVal = ...
7301 // cmpTY ccX, r1, r2
7302 // bCC copy1MBB
7303 // fallthrough --> copy0MBB
7304 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007305 MachineFunction *F = BB->getParent();
7306 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7307 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007308 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007309 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007310 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007311 F->insert(It, copy0MBB);
7312 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007313 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007314 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007315 sinkMBB->transferSuccessors(BB);
7316
7317 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007318 BB->addSuccessor(copy0MBB);
7319 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007320
Evan Cheng60c07e12006-07-05 22:17:51 +00007321 // copy0MBB:
7322 // %FalseValue = ...
7323 // # fallthrough to sinkMBB
7324 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007325
Evan Cheng60c07e12006-07-05 22:17:51 +00007326 // Update machine-CFG edges
7327 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007328
Evan Cheng60c07e12006-07-05 22:17:51 +00007329 // sinkMBB:
7330 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7331 // ...
7332 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007333 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007334 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7335 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7336
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007337 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007338 return BB;
7339 }
7340
Dale Johannesen849f2142007-07-03 00:53:03 +00007341 case X86::FP32_TO_INT16_IN_MEM:
7342 case X86::FP32_TO_INT32_IN_MEM:
7343 case X86::FP32_TO_INT64_IN_MEM:
7344 case X86::FP64_TO_INT16_IN_MEM:
7345 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007346 case X86::FP64_TO_INT64_IN_MEM:
7347 case X86::FP80_TO_INT16_IN_MEM:
7348 case X86::FP80_TO_INT32_IN_MEM:
7349 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007350 // Change the floating point control register to use "round towards zero"
7351 // mode when truncating to an integer value.
7352 MachineFunction *F = BB->getParent();
7353 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007354 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007355
7356 // Load the old value of the high byte of the control word...
7357 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007358 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007359 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007360 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007361
7362 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007363 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007364 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007365
7366 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007367 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007368
7369 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007370 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007371 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007372
7373 // Get the X86 opcode to use.
7374 unsigned Opc;
7375 switch (MI->getOpcode()) {
7376 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007377 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7378 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7379 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7380 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7381 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7382 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007383 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7384 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7385 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007386 }
7387
7388 X86AddressMode AM;
7389 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007390 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007391 AM.BaseType = X86AddressMode::RegBase;
7392 AM.Base.Reg = Op.getReg();
7393 } else {
7394 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007395 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007396 }
7397 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007398 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007399 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007400 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007401 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007402 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007403 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007404 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007405 AM.GV = Op.getGlobal();
7406 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007407 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007408 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007409 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007410 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007411
7412 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007413 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007414
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007415 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007416 return BB;
7417 }
Mon P Wang63307c32008-05-05 19:05:59 +00007418 case X86::ATOMAND32:
7419 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007420 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007421 X86::LCMPXCHG32, X86::MOV32rr,
7422 X86::NOT32r, X86::EAX,
7423 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007424 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007425 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7426 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007427 X86::LCMPXCHG32, X86::MOV32rr,
7428 X86::NOT32r, X86::EAX,
7429 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007430 case X86::ATOMXOR32:
7431 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007432 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007433 X86::LCMPXCHG32, X86::MOV32rr,
7434 X86::NOT32r, X86::EAX,
7435 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007436 case X86::ATOMNAND32:
7437 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007438 X86::AND32ri, X86::MOV32rm,
7439 X86::LCMPXCHG32, X86::MOV32rr,
7440 X86::NOT32r, X86::EAX,
7441 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007442 case X86::ATOMMIN32:
7443 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7444 case X86::ATOMMAX32:
7445 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7446 case X86::ATOMUMIN32:
7447 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7448 case X86::ATOMUMAX32:
7449 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007450
7451 case X86::ATOMAND16:
7452 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7453 X86::AND16ri, X86::MOV16rm,
7454 X86::LCMPXCHG16, X86::MOV16rr,
7455 X86::NOT16r, X86::AX,
7456 X86::GR16RegisterClass);
7457 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007458 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007459 X86::OR16ri, X86::MOV16rm,
7460 X86::LCMPXCHG16, X86::MOV16rr,
7461 X86::NOT16r, X86::AX,
7462 X86::GR16RegisterClass);
7463 case X86::ATOMXOR16:
7464 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7465 X86::XOR16ri, X86::MOV16rm,
7466 X86::LCMPXCHG16, X86::MOV16rr,
7467 X86::NOT16r, X86::AX,
7468 X86::GR16RegisterClass);
7469 case X86::ATOMNAND16:
7470 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7471 X86::AND16ri, X86::MOV16rm,
7472 X86::LCMPXCHG16, X86::MOV16rr,
7473 X86::NOT16r, X86::AX,
7474 X86::GR16RegisterClass, true);
7475 case X86::ATOMMIN16:
7476 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7477 case X86::ATOMMAX16:
7478 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7479 case X86::ATOMUMIN16:
7480 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7481 case X86::ATOMUMAX16:
7482 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7483
7484 case X86::ATOMAND8:
7485 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7486 X86::AND8ri, X86::MOV8rm,
7487 X86::LCMPXCHG8, X86::MOV8rr,
7488 X86::NOT8r, X86::AL,
7489 X86::GR8RegisterClass);
7490 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007491 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007492 X86::OR8ri, X86::MOV8rm,
7493 X86::LCMPXCHG8, X86::MOV8rr,
7494 X86::NOT8r, X86::AL,
7495 X86::GR8RegisterClass);
7496 case X86::ATOMXOR8:
7497 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7498 X86::XOR8ri, X86::MOV8rm,
7499 X86::LCMPXCHG8, X86::MOV8rr,
7500 X86::NOT8r, X86::AL,
7501 X86::GR8RegisterClass);
7502 case X86::ATOMNAND8:
7503 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7504 X86::AND8ri, X86::MOV8rm,
7505 X86::LCMPXCHG8, X86::MOV8rr,
7506 X86::NOT8r, X86::AL,
7507 X86::GR8RegisterClass, true);
7508 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007509 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007510 case X86::ATOMAND64:
7511 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007512 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007513 X86::LCMPXCHG64, X86::MOV64rr,
7514 X86::NOT64r, X86::RAX,
7515 X86::GR64RegisterClass);
7516 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007517 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7518 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007519 X86::LCMPXCHG64, X86::MOV64rr,
7520 X86::NOT64r, X86::RAX,
7521 X86::GR64RegisterClass);
7522 case X86::ATOMXOR64:
7523 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007524 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007525 X86::LCMPXCHG64, X86::MOV64rr,
7526 X86::NOT64r, X86::RAX,
7527 X86::GR64RegisterClass);
7528 case X86::ATOMNAND64:
7529 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7530 X86::AND64ri32, X86::MOV64rm,
7531 X86::LCMPXCHG64, X86::MOV64rr,
7532 X86::NOT64r, X86::RAX,
7533 X86::GR64RegisterClass, true);
7534 case X86::ATOMMIN64:
7535 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7536 case X86::ATOMMAX64:
7537 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7538 case X86::ATOMUMIN64:
7539 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7540 case X86::ATOMUMAX64:
7541 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007542
7543 // This group does 64-bit operations on a 32-bit host.
7544 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007545 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007546 X86::AND32rr, X86::AND32rr,
7547 X86::AND32ri, X86::AND32ri,
7548 false);
7549 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007550 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007551 X86::OR32rr, X86::OR32rr,
7552 X86::OR32ri, X86::OR32ri,
7553 false);
7554 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007555 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007556 X86::XOR32rr, X86::XOR32rr,
7557 X86::XOR32ri, X86::XOR32ri,
7558 false);
7559 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007560 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007561 X86::AND32rr, X86::AND32rr,
7562 X86::AND32ri, X86::AND32ri,
7563 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007564 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007565 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007566 X86::ADD32rr, X86::ADC32rr,
7567 X86::ADD32ri, X86::ADC32ri,
7568 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007569 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007570 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007571 X86::SUB32rr, X86::SBB32rr,
7572 X86::SUB32ri, X86::SBB32ri,
7573 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007574 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007575 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007576 X86::MOV32rr, X86::MOV32rr,
7577 X86::MOV32ri, X86::MOV32ri,
7578 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007579 }
7580}
7581
7582//===----------------------------------------------------------------------===//
7583// X86 Optimization Hooks
7584//===----------------------------------------------------------------------===//
7585
Dan Gohman475871a2008-07-27 21:46:04 +00007586void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007587 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007588 APInt &KnownZero,
7589 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007590 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007591 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007592 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007593 assert((Opc >= ISD::BUILTIN_OP_END ||
7594 Opc == ISD::INTRINSIC_WO_CHAIN ||
7595 Opc == ISD::INTRINSIC_W_CHAIN ||
7596 Opc == ISD::INTRINSIC_VOID) &&
7597 "Should use MaskedValueIsZero if you don't know whether Op"
7598 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007599
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007600 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007601 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007602 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007603 case X86ISD::ADD:
7604 case X86ISD::SUB:
7605 case X86ISD::SMUL:
7606 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007607 case X86ISD::INC:
7608 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007609 // These nodes' second result is a boolean.
7610 if (Op.getResNo() == 0)
7611 break;
7612 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007613 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007614 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7615 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007616 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007617 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007618}
Chris Lattner259e97c2006-01-31 19:43:35 +00007619
Evan Cheng206ee9d2006-07-07 08:33:52 +00007620/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007621/// node is a GlobalAddress + offset.
7622bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7623 GlobalValue* &GA, int64_t &Offset) const{
7624 if (N->getOpcode() == X86ISD::Wrapper) {
7625 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007626 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007627 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007628 return true;
7629 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007630 }
Evan Chengad4196b2008-05-12 19:56:52 +00007631 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007632}
7633
Evan Chengad4196b2008-05-12 19:56:52 +00007634static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7635 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007636 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007637 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007638 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007639 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007640 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007641 return false;
7642}
7643
Nate Begeman9008ca62009-04-27 18:41:29 +00007644static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7645 MVT EVT, SDNode *&Base,
Evan Chengad4196b2008-05-12 19:56:52 +00007646 SelectionDAG &DAG, MachineFrameInfo *MFI,
7647 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007648 Base = NULL;
7649 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007650 if (N->getMaskElt(i) < 0) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007651 if (!Base)
7652 return false;
7653 continue;
7654 }
7655
Dan Gohman475871a2008-07-27 21:46:04 +00007656 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007657 if (!Elt.getNode() ||
7658 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007659 return false;
7660 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007661 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007662 if (Base->getOpcode() == ISD::UNDEF)
7663 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007664 continue;
7665 }
7666 if (Elt.getOpcode() == ISD::UNDEF)
7667 continue;
7668
Gabor Greifba36cb52008-08-28 21:40:38 +00007669 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007670 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007671 return false;
7672 }
7673 return true;
7674}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007675
7676/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7677/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7678/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007679/// order. In the case of v2i64, it will see if it can rewrite the
7680/// shuffle to be an appropriate build vector so it can take advantage of
7681// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007682static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007683 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007684 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007685 MVT VT = N->getValueType(0);
7686 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007687 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7688 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007689
7690 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7691 // where the upper half is 0, it is advantageous to rewrite it as a build
7692 // vector of (0, val) so it can use movq.
7693 if (VT == MVT::v2i64) {
7694 SDValue In[2];
7695 In[0] = N->getOperand(0);
7696 In[1] = N->getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00007697 int Idx0 = SVN->getMaskElt(0);
7698 int Idx1 = SVN->getMaskElt(1);
7699 // FIXME: can we take advantage of undef index?
7700 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang1e955802009-04-03 02:43:30 +00007701 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7702 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7703 ConstantSDNode* InsertVecIdx =
7704 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7705 if (InsertVecIdx &&
Nate Begeman9008ca62009-04-27 18:41:29 +00007706 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang1e955802009-04-03 02:43:30 +00007707 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7708 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7709 In[Idx0/2].getOperand(1),
7710 In[Idx1/2].getOperand(Idx1 % 2));
7711 }
7712 }
7713 }
7714
7715 // Try to combine a vector_shuffle into a 128-bit load.
7716 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007717 SDNode *Base = NULL;
Nate Begeman9008ca62009-04-27 18:41:29 +00007718 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007719 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007720
Dan Gohmand3006222007-07-27 17:16:43 +00007721 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007722 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007723 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007724 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007725 LD->isVolatile());
7726 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7727 LD->getSrcValue(), LD->getSrcValueOffset(),
7728 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007729}
7730
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007731/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007732static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007733 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007734 const X86Subtarget *Subtarget,
7735 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007736 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007737 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007738
Evan Chengd880b972008-05-09 21:53:03 +00007739 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007740 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007741 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007742
Duncan Sands83ec4b62008-06-06 12:08:01 +00007743 MVT VT = N->getValueType(0);
7744 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00007745 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7746 // We are looking for load i64 and zero extend. We want to transform
7747 // it before legalizer has a chance to expand it. Also look for i64
7748 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00007749 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007750 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00007751 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00007752 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007753 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007754
7755 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00007756 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00007757 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007758 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00007759 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007760 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007761 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00007762 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007763 }
Evan Chengd880b972008-05-09 21:53:03 +00007764
7765 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007766 LoadSDNode *LD = cast<LoadSDNode>(Base);
Scott Michelfdc40a02009-02-17 22:15:04 +00007767
Nate Begemanf7333bf2008-05-28 00:24:25 +00007768 // Load must not be an extload.
7769 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00007770 return SDValue();
Mon P Wang7ad9b512009-01-30 07:07:40 +00007771
7772 // Load type should legal type so we don't have to legalize it.
7773 if (!TLI.isTypeLegal(VT))
7774 return SDValue();
7775
Evan Cheng8a186ae2008-09-24 23:26:36 +00007776 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7777 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007778 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007779 TargetLowering::TargetLoweringOpt TLO(DAG);
7780 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7781 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng8a186ae2008-09-24 23:26:36 +00007782 return ResNode;
Scott Michelfdc40a02009-02-17 22:15:04 +00007783}
Evan Chengd880b972008-05-09 21:53:03 +00007784
Chris Lattner83e6c992006-10-04 06:57:07 +00007785/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007786static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007787 const X86Subtarget *Subtarget) {
7788 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007789 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007790 // Get the LHS/RHS of the select.
7791 SDValue LHS = N->getOperand(1);
7792 SDValue RHS = N->getOperand(2);
7793
Chris Lattner83e6c992006-10-04 06:57:07 +00007794 // If we have SSE[12] support, try to form min/max nodes.
7795 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007796 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7797 Cond.getOpcode() == ISD::SETCC) {
7798 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007799
Chris Lattner47b4ce82009-03-11 05:48:52 +00007800 unsigned Opcode = 0;
7801 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7802 switch (CC) {
7803 default: break;
7804 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7805 case ISD::SETULE:
7806 case ISD::SETLE:
7807 if (!UnsafeFPMath) break;
7808 // FALL THROUGH.
7809 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7810 case ISD::SETLT:
7811 Opcode = X86ISD::FMIN;
7812 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007813
Chris Lattner47b4ce82009-03-11 05:48:52 +00007814 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7815 case ISD::SETUGT:
7816 case ISD::SETGT:
7817 if (!UnsafeFPMath) break;
7818 // FALL THROUGH.
7819 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7820 case ISD::SETGE:
7821 Opcode = X86ISD::FMAX;
7822 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007823 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007824 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7825 switch (CC) {
7826 default: break;
7827 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7828 case ISD::SETUGT:
7829 case ISD::SETGT:
7830 if (!UnsafeFPMath) break;
7831 // FALL THROUGH.
7832 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7833 case ISD::SETGE:
7834 Opcode = X86ISD::FMIN;
7835 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007836
Chris Lattner47b4ce82009-03-11 05:48:52 +00007837 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7838 case ISD::SETULE:
7839 case ISD::SETLE:
7840 if (!UnsafeFPMath) break;
7841 // FALL THROUGH.
7842 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7843 case ISD::SETLT:
7844 Opcode = X86ISD::FMAX;
7845 break;
7846 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007847 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007848
Chris Lattner47b4ce82009-03-11 05:48:52 +00007849 if (Opcode)
7850 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007851 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007852
Chris Lattnerd1980a52009-03-12 06:52:53 +00007853 // If this is a select between two integer constants, try to do some
7854 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007855 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7856 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007857 // Don't do this for crazy integer types.
7858 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7859 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007860 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007861 bool NeedsCondInvert = false;
7862
Chris Lattnercee56e72009-03-13 05:53:31 +00007863 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007864 // Efficiently invertible.
7865 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7866 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7867 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7868 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007869 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007870 }
7871
7872 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007873 if (FalseC->getAPIntValue() == 0 &&
7874 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007875 if (NeedsCondInvert) // Invert the condition if needed.
7876 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7877 DAG.getConstant(1, Cond.getValueType()));
7878
7879 // Zero extend the condition if needed.
7880 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7881
Chris Lattnercee56e72009-03-13 05:53:31 +00007882 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007883 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7884 DAG.getConstant(ShAmt, MVT::i8));
7885 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007886
7887 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007888 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007889 if (NeedsCondInvert) // Invert the condition if needed.
7890 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7891 DAG.getConstant(1, Cond.getValueType()));
7892
7893 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007894 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7895 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007896 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007897 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007898 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007899
7900 // Optimize cases that will turn into an LEA instruction. This requires
7901 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7902 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7903 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7904 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7905
7906 bool isFastMultiplier = false;
7907 if (Diff < 10) {
7908 switch ((unsigned char)Diff) {
7909 default: break;
7910 case 1: // result = add base, cond
7911 case 2: // result = lea base( , cond*2)
7912 case 3: // result = lea base(cond, cond*2)
7913 case 4: // result = lea base( , cond*4)
7914 case 5: // result = lea base(cond, cond*4)
7915 case 8: // result = lea base( , cond*8)
7916 case 9: // result = lea base(cond, cond*8)
7917 isFastMultiplier = true;
7918 break;
7919 }
7920 }
7921
7922 if (isFastMultiplier) {
7923 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7924 if (NeedsCondInvert) // Invert the condition if needed.
7925 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7926 DAG.getConstant(1, Cond.getValueType()));
7927
7928 // Zero extend the condition if needed.
7929 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7930 Cond);
7931 // Scale the condition by the difference.
7932 if (Diff != 1)
7933 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7934 DAG.getConstant(Diff, Cond.getValueType()));
7935
7936 // Add the base if non-zero.
7937 if (FalseC->getAPIntValue() != 0)
7938 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7939 SDValue(FalseC, 0));
7940 return Cond;
7941 }
7942 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007943 }
7944 }
7945
Dan Gohman475871a2008-07-27 21:46:04 +00007946 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007947}
7948
Chris Lattnerd1980a52009-03-12 06:52:53 +00007949/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7950static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7951 TargetLowering::DAGCombinerInfo &DCI) {
7952 DebugLoc DL = N->getDebugLoc();
7953
7954 // If the flag operand isn't dead, don't touch this CMOV.
7955 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
7956 return SDValue();
7957
7958 // If this is a select between two integer constants, try to do some
7959 // optimizations. Note that the operands are ordered the opposite of SELECT
7960 // operands.
7961 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
7962 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
7963 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
7964 // larger than FalseC (the false value).
7965 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
7966
7967 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
7968 CC = X86::GetOppositeBranchCondition(CC);
7969 std::swap(TrueC, FalseC);
7970 }
7971
7972 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007973 // This is efficient for any integer data type (including i8/i16) and
7974 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007975 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
7976 SDValue Cond = N->getOperand(3);
7977 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7978 DAG.getConstant(CC, MVT::i8), Cond);
7979
7980 // Zero extend the condition if needed.
7981 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
7982
7983 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
7984 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
7985 DAG.getConstant(ShAmt, MVT::i8));
7986 if (N->getNumValues() == 2) // Dead flag value?
7987 return DCI.CombineTo(N, Cond, SDValue());
7988 return Cond;
7989 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007990
7991 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
7992 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00007993 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
7994 SDValue Cond = N->getOperand(3);
7995 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7996 DAG.getConstant(CC, MVT::i8), Cond);
7997
7998 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007999 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8000 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008001 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8002 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008003
Chris Lattner97a29a52009-03-13 05:22:11 +00008004 if (N->getNumValues() == 2) // Dead flag value?
8005 return DCI.CombineTo(N, Cond, SDValue());
8006 return Cond;
8007 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008008
8009 // Optimize cases that will turn into an LEA instruction. This requires
8010 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8011 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8012 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8013 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8014
8015 bool isFastMultiplier = false;
8016 if (Diff < 10) {
8017 switch ((unsigned char)Diff) {
8018 default: break;
8019 case 1: // result = add base, cond
8020 case 2: // result = lea base( , cond*2)
8021 case 3: // result = lea base(cond, cond*2)
8022 case 4: // result = lea base( , cond*4)
8023 case 5: // result = lea base(cond, cond*4)
8024 case 8: // result = lea base( , cond*8)
8025 case 9: // result = lea base(cond, cond*8)
8026 isFastMultiplier = true;
8027 break;
8028 }
8029 }
8030
8031 if (isFastMultiplier) {
8032 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8033 SDValue Cond = N->getOperand(3);
8034 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8035 DAG.getConstant(CC, MVT::i8), Cond);
8036 // Zero extend the condition if needed.
8037 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8038 Cond);
8039 // Scale the condition by the difference.
8040 if (Diff != 1)
8041 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8042 DAG.getConstant(Diff, Cond.getValueType()));
8043
8044 // Add the base if non-zero.
8045 if (FalseC->getAPIntValue() != 0)
8046 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8047 SDValue(FalseC, 0));
8048 if (N->getNumValues() == 2) // Dead flag value?
8049 return DCI.CombineTo(N, Cond, SDValue());
8050 return Cond;
8051 }
8052 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008053 }
8054 }
8055 return SDValue();
8056}
8057
8058
Evan Cheng0b0cd912009-03-28 05:57:29 +00008059/// PerformMulCombine - Optimize a single multiply with constant into two
8060/// in order to implement it with two cheaper instructions, e.g.
8061/// LEA + SHL, LEA + LEA.
8062static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8063 TargetLowering::DAGCombinerInfo &DCI) {
8064 if (DAG.getMachineFunction().
8065 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8066 return SDValue();
8067
8068 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8069 return SDValue();
8070
8071 MVT VT = N->getValueType(0);
8072 if (VT != MVT::i64)
8073 return SDValue();
8074
8075 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8076 if (!C)
8077 return SDValue();
8078 uint64_t MulAmt = C->getZExtValue();
8079 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8080 return SDValue();
8081
8082 uint64_t MulAmt1 = 0;
8083 uint64_t MulAmt2 = 0;
8084 if ((MulAmt % 9) == 0) {
8085 MulAmt1 = 9;
8086 MulAmt2 = MulAmt / 9;
8087 } else if ((MulAmt % 5) == 0) {
8088 MulAmt1 = 5;
8089 MulAmt2 = MulAmt / 5;
8090 } else if ((MulAmt % 3) == 0) {
8091 MulAmt1 = 3;
8092 MulAmt2 = MulAmt / 3;
8093 }
8094 if (MulAmt2 &&
8095 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8096 DebugLoc DL = N->getDebugLoc();
8097
8098 if (isPowerOf2_64(MulAmt2) &&
8099 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8100 // If second multiplifer is pow2, issue it first. We want the multiply by
8101 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8102 // is an add.
8103 std::swap(MulAmt1, MulAmt2);
8104
8105 SDValue NewMul;
8106 if (isPowerOf2_64(MulAmt1))
8107 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8108 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8109 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008110 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008111 DAG.getConstant(MulAmt1, VT));
8112
8113 if (isPowerOf2_64(MulAmt2))
8114 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8115 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8116 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008117 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008118 DAG.getConstant(MulAmt2, VT));
8119
8120 // Do not add new nodes to DAG combiner worklist.
8121 DCI.CombineTo(N, NewMul, false);
8122 }
8123 return SDValue();
8124}
8125
8126
Nate Begeman740ab032009-01-26 00:52:55 +00008127/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8128/// when possible.
8129static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8130 const X86Subtarget *Subtarget) {
8131 // On X86 with SSE2 support, we can transform this to a vector shift if
8132 // all elements are shifted by the same amount. We can't do this in legalize
8133 // because the a constant vector is typically transformed to a constant pool
8134 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008135 if (!Subtarget->hasSSE2())
8136 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008137
Nate Begeman740ab032009-01-26 00:52:55 +00008138 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008139 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8140 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008141
Mon P Wang3becd092009-01-28 08:12:05 +00008142 SDValue ShAmtOp = N->getOperand(1);
8143 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008144 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008145 SDValue BaseShAmt;
8146 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8147 unsigned NumElts = VT.getVectorNumElements();
8148 unsigned i = 0;
8149 for (; i != NumElts; ++i) {
8150 SDValue Arg = ShAmtOp.getOperand(i);
8151 if (Arg.getOpcode() == ISD::UNDEF) continue;
8152 BaseShAmt = Arg;
8153 break;
8154 }
8155 for (; i != NumElts; ++i) {
8156 SDValue Arg = ShAmtOp.getOperand(i);
8157 if (Arg.getOpcode() == ISD::UNDEF) continue;
8158 if (Arg != BaseShAmt) {
8159 return SDValue();
8160 }
8161 }
8162 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008163 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8164 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8165 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008166 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008167 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008168
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008169 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008170 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008171 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008172 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008173
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008174 // The shift amount is identical so we can do a vector shift.
8175 SDValue ValOp = N->getOperand(0);
8176 switch (N->getOpcode()) {
8177 default:
8178 assert(0 && "Unknown shift opcode!");
8179 break;
8180 case ISD::SHL:
8181 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008182 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008183 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8184 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008185 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008186 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008187 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8188 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008189 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008190 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008191 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8192 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008193 break;
8194 case ISD::SRA:
8195 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008196 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008197 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8198 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008199 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008200 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008201 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8202 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008203 break;
8204 case ISD::SRL:
8205 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008206 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008207 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8208 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008209 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008210 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008211 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8212 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008213 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008215 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8216 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008217 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008218 }
8219 return SDValue();
8220}
8221
Chris Lattner149a4e52008-02-22 02:09:43 +00008222/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008223static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008224 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008225 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8226 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008227 // A preferable solution to the general problem is to figure out the right
8228 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008229
8230 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008231 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008232 MVT VT = St->getValue().getValueType();
8233 if (VT.getSizeInBits() != 64)
8234 return SDValue();
8235
8236 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloat && Subtarget->hasSSE2();
8237 if ((VT.isVector() ||
8238 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008239 isa<LoadSDNode>(St->getValue()) &&
8240 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8241 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008242 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008243 LoadSDNode *Ld = 0;
8244 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008245 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008246 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008247 // Must be a store of a load. We currently handle two cases: the load
8248 // is a direct child, and it's under an intervening TokenFactor. It is
8249 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008250 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008251 Ld = cast<LoadSDNode>(St->getChain());
8252 else if (St->getValue().hasOneUse() &&
8253 ChainVal->getOpcode() == ISD::TokenFactor) {
8254 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008255 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008256 TokenFactorIndex = i;
8257 Ld = cast<LoadSDNode>(St->getValue());
8258 } else
8259 Ops.push_back(ChainVal->getOperand(i));
8260 }
8261 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008262
Evan Cheng536e6672009-03-12 05:59:15 +00008263 if (!Ld || !ISD::isNormalLoad(Ld))
8264 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008265
Evan Cheng536e6672009-03-12 05:59:15 +00008266 // If this is not the MMX case, i.e. we are just turning i64 load/store
8267 // into f64 load/store, avoid the transformation if there are multiple
8268 // uses of the loaded value.
8269 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8270 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008271
Evan Cheng536e6672009-03-12 05:59:15 +00008272 DebugLoc LdDL = Ld->getDebugLoc();
8273 DebugLoc StDL = N->getDebugLoc();
8274 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8275 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8276 // pair instead.
8277 if (Subtarget->is64Bit() || F64IsLegal) {
8278 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8279 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8280 Ld->getBasePtr(), Ld->getSrcValue(),
8281 Ld->getSrcValueOffset(), Ld->isVolatile(),
8282 Ld->getAlignment());
8283 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008284 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008285 Ops.push_back(NewChain);
8286 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008287 Ops.size());
8288 }
Evan Cheng536e6672009-03-12 05:59:15 +00008289 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008290 St->getSrcValue(), St->getSrcValueOffset(),
8291 St->isVolatile(), St->getAlignment());
8292 }
Evan Cheng536e6672009-03-12 05:59:15 +00008293
8294 // Otherwise, lower to two pairs of 32-bit loads / stores.
8295 SDValue LoAddr = Ld->getBasePtr();
8296 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8297 DAG.getConstant(4, MVT::i32));
8298
8299 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8300 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8301 Ld->isVolatile(), Ld->getAlignment());
8302 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8303 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8304 Ld->isVolatile(),
8305 MinAlign(Ld->getAlignment(), 4));
8306
8307 SDValue NewChain = LoLd.getValue(1);
8308 if (TokenFactorIndex != -1) {
8309 Ops.push_back(LoLd);
8310 Ops.push_back(HiLd);
8311 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8312 Ops.size());
8313 }
8314
8315 LoAddr = St->getBasePtr();
8316 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8317 DAG.getConstant(4, MVT::i32));
8318
8319 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8320 St->getSrcValue(), St->getSrcValueOffset(),
8321 St->isVolatile(), St->getAlignment());
8322 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8323 St->getSrcValue(),
8324 St->getSrcValueOffset() + 4,
8325 St->isVolatile(),
8326 MinAlign(St->getAlignment(), 4));
8327 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008328 }
Dan Gohman475871a2008-07-27 21:46:04 +00008329 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008330}
8331
Chris Lattner6cf73262008-01-25 06:14:17 +00008332/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8333/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008334static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008335 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8336 // F[X]OR(0.0, x) -> x
8337 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008338 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8339 if (C->getValueAPF().isPosZero())
8340 return N->getOperand(1);
8341 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8342 if (C->getValueAPF().isPosZero())
8343 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008344 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008345}
8346
8347/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008348static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008349 // FAND(0.0, x) -> 0.0
8350 // FAND(x, 0.0) -> 0.0
8351 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8352 if (C->getValueAPF().isPosZero())
8353 return N->getOperand(0);
8354 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8355 if (C->getValueAPF().isPosZero())
8356 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008357 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008358}
8359
Dan Gohmane5af2d32009-01-29 01:59:02 +00008360static SDValue PerformBTCombine(SDNode *N,
8361 SelectionDAG &DAG,
8362 TargetLowering::DAGCombinerInfo &DCI) {
8363 // BT ignores high bits in the bit index operand.
8364 SDValue Op1 = N->getOperand(1);
8365 if (Op1.hasOneUse()) {
8366 unsigned BitWidth = Op1.getValueSizeInBits();
8367 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8368 APInt KnownZero, KnownOne;
8369 TargetLowering::TargetLoweringOpt TLO(DAG);
8370 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8371 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8372 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8373 DCI.CommitTargetLoweringOpt(TLO);
8374 }
8375 return SDValue();
8376}
Chris Lattner83e6c992006-10-04 06:57:07 +00008377
Dan Gohman475871a2008-07-27 21:46:04 +00008378SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008379 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008380 SelectionDAG &DAG = DCI.DAG;
8381 switch (N->getOpcode()) {
8382 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008383 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8384 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008385 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008386 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008387 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008388 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008389 case ISD::SHL:
8390 case ISD::SRA:
8391 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008392 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008393 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008394 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8395 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008396 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008397 }
8398
Dan Gohman475871a2008-07-27 21:46:04 +00008399 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008400}
8401
Evan Cheng60c07e12006-07-05 22:17:51 +00008402//===----------------------------------------------------------------------===//
8403// X86 Inline Assembly Support
8404//===----------------------------------------------------------------------===//
8405
Chris Lattnerf4dff842006-07-11 02:54:03 +00008406/// getConstraintType - Given a constraint letter, return the type of
8407/// constraint it is for this target.
8408X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008409X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8410 if (Constraint.size() == 1) {
8411 switch (Constraint[0]) {
8412 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008413 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008414 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008415 case 'r':
8416 case 'R':
8417 case 'l':
8418 case 'q':
8419 case 'Q':
8420 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008421 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008422 case 'Y':
8423 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008424 case 'e':
8425 case 'Z':
8426 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008427 default:
8428 break;
8429 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008430 }
Chris Lattner4234f572007-03-25 02:14:49 +00008431 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008432}
8433
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008434/// LowerXConstraint - try to replace an X constraint, which matches anything,
8435/// with another that has more specific requirements based on the type of the
8436/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008437const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008438LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008439 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8440 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008441 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008442 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008443 return "Y";
8444 if (Subtarget->hasSSE1())
8445 return "x";
8446 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008447
Chris Lattner5e764232008-04-26 23:02:14 +00008448 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008449}
8450
Chris Lattner48884cd2007-08-25 00:47:38 +00008451/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8452/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008453void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008454 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008455 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008456 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008457 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008458 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008459
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008460 switch (Constraint) {
8461 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008462 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008464 if (C->getZExtValue() <= 31) {
8465 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008466 break;
8467 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008468 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008469 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008470 case 'J':
8471 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8472 if (C->getZExtValue() <= 63) {
8473 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8474 break;
8475 }
8476 }
8477 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008478 case 'N':
8479 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008480 if (C->getZExtValue() <= 255) {
8481 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008482 break;
8483 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008484 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008485 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008486 case 'e': {
8487 // 32-bit signed value
8488 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8489 const ConstantInt *CI = C->getConstantIntValue();
8490 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8491 // Widen to 64 bits here to get it sign extended.
8492 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8493 break;
8494 }
8495 // FIXME gcc accepts some relocatable values here too, but only in certain
8496 // memory models; it's complicated.
8497 }
8498 return;
8499 }
8500 case 'Z': {
8501 // 32-bit unsigned value
8502 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8503 const ConstantInt *CI = C->getConstantIntValue();
8504 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8505 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8506 break;
8507 }
8508 }
8509 // FIXME gcc accepts some relocatable values here too, but only in certain
8510 // memory models; it's complicated.
8511 return;
8512 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008513 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008514 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008515 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008516 // Widen to 64 bits here to get it sign extended.
8517 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008518 break;
8519 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008520
Chris Lattnerdc43a882007-05-03 16:52:29 +00008521 // If we are in non-pic codegen mode, we allow the address of a global (with
8522 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008523 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008524 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008525
Chris Lattner49921962009-05-08 18:23:14 +00008526 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8527 while (1) {
8528 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8529 Offset += GA->getOffset();
8530 break;
8531 } else if (Op.getOpcode() == ISD::ADD) {
8532 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8533 Offset += C->getZExtValue();
8534 Op = Op.getOperand(0);
8535 continue;
8536 }
8537 } else if (Op.getOpcode() == ISD::SUB) {
8538 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8539 Offset += -C->getZExtValue();
8540 Op = Op.getOperand(0);
8541 continue;
8542 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008543 }
Chris Lattner49921962009-05-08 18:23:14 +00008544
8545 // Otherwise, this isn't something we can handle, reject it.
8546 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008547 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008548
Chris Lattner49921962009-05-08 18:23:14 +00008549 if (hasMemory)
8550 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8551 else
8552 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8553 Offset);
8554 Result = Op;
8555 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008556 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008558
Gabor Greifba36cb52008-08-28 21:40:38 +00008559 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008560 Ops.push_back(Result);
8561 return;
8562 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008563 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8564 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008565}
8566
Chris Lattner259e97c2006-01-31 19:43:35 +00008567std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008568getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008569 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008570 if (Constraint.size() == 1) {
8571 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008572 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008573 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008574 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8575 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008576 if (VT == MVT::i32)
8577 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8578 else if (VT == MVT::i16)
8579 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8580 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008581 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008582 else if (VT == MVT::i64)
8583 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8584 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008585 }
8586 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008587
Chris Lattner1efa40f2006-02-22 00:56:39 +00008588 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008589}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008590
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008591std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008592X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008593 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008594 // First, see if this is a constraint that directly corresponds to an LLVM
8595 // register class.
8596 if (Constraint.size() == 1) {
8597 // GCC Constraint Letters
8598 switch (Constraint[0]) {
8599 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008600 case 'r': // GENERAL_REGS
8601 case 'R': // LEGACY_REGS
8602 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008603 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008604 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008605 if (VT == MVT::i16)
8606 return std::make_pair(0U, X86::GR16RegisterClass);
8607 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008608 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008609 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008610 case 'f': // FP Stack registers.
8611 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8612 // value to the correct fpstack register class.
8613 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8614 return std::make_pair(0U, X86::RFP32RegisterClass);
8615 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8616 return std::make_pair(0U, X86::RFP64RegisterClass);
8617 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008618 case 'y': // MMX_REGS if MMX allowed.
8619 if (!Subtarget->hasMMX()) break;
8620 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008621 case 'Y': // SSE_REGS if SSE2 allowed
8622 if (!Subtarget->hasSSE2()) break;
8623 // FALL THROUGH.
8624 case 'x': // SSE_REGS if SSE1 allowed
8625 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008626
8627 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008628 default: break;
8629 // Scalar SSE types.
8630 case MVT::f32:
8631 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008632 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008633 case MVT::f64:
8634 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008635 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008636 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008637 case MVT::v16i8:
8638 case MVT::v8i16:
8639 case MVT::v4i32:
8640 case MVT::v2i64:
8641 case MVT::v4f32:
8642 case MVT::v2f64:
8643 return std::make_pair(0U, X86::VR128RegisterClass);
8644 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008645 break;
8646 }
8647 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008648
Chris Lattnerf76d1802006-07-31 23:26:50 +00008649 // Use the default implementation in TargetLowering to convert the register
8650 // constraint into a member of a register class.
8651 std::pair<unsigned, const TargetRegisterClass*> Res;
8652 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008653
8654 // Not found as a standard register?
8655 if (Res.second == 0) {
8656 // GCC calls "st(0)" just plain "st".
8657 if (StringsEqualNoCase("{st}", Constraint)) {
8658 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008659 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008660 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008661 // 'A' means EAX + EDX.
8662 if (Constraint == "A") {
8663 Res.first = X86::EAX;
8664 Res.second = X86::GRADRegisterClass;
8665 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008666 return Res;
8667 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008668
Chris Lattnerf76d1802006-07-31 23:26:50 +00008669 // Otherwise, check to see if this is a register class of the wrong value
8670 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8671 // turn into {ax},{dx}.
8672 if (Res.second->hasType(VT))
8673 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008674
Chris Lattnerf76d1802006-07-31 23:26:50 +00008675 // All of the single-register GCC register classes map their values onto
8676 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8677 // really want an 8-bit or 32-bit register, map to the appropriate register
8678 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008679 if (Res.second == X86::GR16RegisterClass) {
8680 if (VT == MVT::i8) {
8681 unsigned DestReg = 0;
8682 switch (Res.first) {
8683 default: break;
8684 case X86::AX: DestReg = X86::AL; break;
8685 case X86::DX: DestReg = X86::DL; break;
8686 case X86::CX: DestReg = X86::CL; break;
8687 case X86::BX: DestReg = X86::BL; break;
8688 }
8689 if (DestReg) {
8690 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008691 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008692 }
8693 } else if (VT == MVT::i32) {
8694 unsigned DestReg = 0;
8695 switch (Res.first) {
8696 default: break;
8697 case X86::AX: DestReg = X86::EAX; break;
8698 case X86::DX: DestReg = X86::EDX; break;
8699 case X86::CX: DestReg = X86::ECX; break;
8700 case X86::BX: DestReg = X86::EBX; break;
8701 case X86::SI: DestReg = X86::ESI; break;
8702 case X86::DI: DestReg = X86::EDI; break;
8703 case X86::BP: DestReg = X86::EBP; break;
8704 case X86::SP: DestReg = X86::ESP; break;
8705 }
8706 if (DestReg) {
8707 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008708 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008709 }
8710 } else if (VT == MVT::i64) {
8711 unsigned DestReg = 0;
8712 switch (Res.first) {
8713 default: break;
8714 case X86::AX: DestReg = X86::RAX; break;
8715 case X86::DX: DestReg = X86::RDX; break;
8716 case X86::CX: DestReg = X86::RCX; break;
8717 case X86::BX: DestReg = X86::RBX; break;
8718 case X86::SI: DestReg = X86::RSI; break;
8719 case X86::DI: DestReg = X86::RDI; break;
8720 case X86::BP: DestReg = X86::RBP; break;
8721 case X86::SP: DestReg = X86::RSP; break;
8722 }
8723 if (DestReg) {
8724 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008725 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008726 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008727 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008728 } else if (Res.second == X86::FR32RegisterClass ||
8729 Res.second == X86::FR64RegisterClass ||
8730 Res.second == X86::VR128RegisterClass) {
8731 // Handle references to XMM physical registers that got mapped into the
8732 // wrong class. This can happen with constraints like {xmm0} where the
8733 // target independent register mapper will just pick the first match it can
8734 // find, ignoring the required type.
8735 if (VT == MVT::f32)
8736 Res.second = X86::FR32RegisterClass;
8737 else if (VT == MVT::f64)
8738 Res.second = X86::FR64RegisterClass;
8739 else if (X86::VR128RegisterClass->hasType(VT))
8740 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008741 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008742
Chris Lattnerf76d1802006-07-31 23:26:50 +00008743 return Res;
8744}
Mon P Wang0c397192008-10-30 08:01:45 +00008745
8746//===----------------------------------------------------------------------===//
8747// X86 Widen vector type
8748//===----------------------------------------------------------------------===//
8749
8750/// getWidenVectorType: given a vector type, returns the type to widen
8751/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8752/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008753/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008754/// scalarizing vs using the wider vector type.
8755
Dan Gohmanc13cf132009-01-15 17:34:08 +00008756MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008757 assert(VT.isVector());
8758 if (isTypeLegal(VT))
8759 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008760
Mon P Wang0c397192008-10-30 08:01:45 +00008761 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8762 // type based on element type. This would speed up our search (though
8763 // it may not be worth it since the size of the list is relatively
8764 // small).
8765 MVT EltVT = VT.getVectorElementType();
8766 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008767
Mon P Wang0c397192008-10-30 08:01:45 +00008768 // On X86, it make sense to widen any vector wider than 1
8769 if (NElts <= 1)
8770 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008771
8772 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008773 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8774 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008775
8776 if (isTypeLegal(SVT) &&
8777 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008778 SVT.getVectorNumElements() > NElts)
8779 return SVT;
8780 }
8781 return MVT::Other;
8782}