blob: fda8cba2a1a1a7ca43b90b4f4daab6b64dcb2f44 [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);
Eli Friedman948e95a2009-05-23 09:59:16 +0000119 } else if (!UseSoftFloat) {
120 if (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 Wendling105be5a2009-03-13 08:41:47 +0000123 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000124 // We have an algorithm for SSE2, and we turn this into a 64-bit
125 // FILD for other targets.
126 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000127 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128
129 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
130 // this operation.
131 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
132 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000133
134 if (!UseSoftFloat && !NoImplicitFloat) {
135 // SSE has no i16 to fp conversion, only i32
136 if (X86ScalarSSEf32) {
137 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
138 // f32 and f64 cases are Legal, f80 case is not
139 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
140 } else {
141 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
142 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
143 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000144 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000145 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
146 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000147 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000148
Dale Johannesen73328d12007-09-19 23:55:34 +0000149 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
150 // are Legal, f80 is custom lowered.
151 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
152 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000153
Evan Cheng02568ff2006-01-30 22:13:22 +0000154 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
155 // this operation.
156 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
157 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
158
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000159 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000160 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000161 // f32 and f64 cases are Legal, f80 case is not
162 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000163 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000164 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000165 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 }
167
168 // Handle FP_TO_UINT by promoting the destination to a larger signed
169 // conversion.
170 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
171 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
172 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
173
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 if (Subtarget->is64Bit()) {
175 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000176 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000177 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000178 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000179 // Expand FP_TO_UINT into a select.
180 // FIXME: We would like to use a Custom expander here eventually to do
181 // the optimal thing for SSE vs. the default expansion in the legalizer.
182 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
183 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000184 // With SSE3 we can use fisttpll to convert to a signed i64; without
185 // SSE, we're stuck with a fistpll.
186 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000187 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
Chris Lattner399610a2006-12-05 18:22:22 +0000189 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000190 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000191 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
192 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
193 }
Chris Lattner21f66852005-12-23 05:15:23 +0000194
Dan Gohmanb00ee212008-02-18 19:34:53 +0000195 // Scalar integer divide and remainder are lowered to use operations that
196 // produce two results, to match the available instructions. This exposes
197 // the two-result form to trivial CSE, which is able to combine x/y and x%y
198 // into a single instruction.
199 //
200 // Scalar integer multiply-high is also lowered to use two-result
201 // operations, to match the available instructions. However, plain multiply
202 // (low) operations are left as Legal, as there are single-result
203 // instructions for this in x86. Using the two-result multiply instructions
204 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000205 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
206 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
207 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
208 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
209 setOperationAction(ISD::SREM , MVT::i8 , Expand);
210 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000211 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
212 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
213 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
214 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
215 setOperationAction(ISD::SREM , MVT::i16 , Expand);
216 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000217 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
218 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
219 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
220 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
221 setOperationAction(ISD::SREM , MVT::i32 , Expand);
222 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000223 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
224 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
225 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
226 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
227 setOperationAction(ISD::SREM , MVT::i64 , Expand);
228 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000229
Evan Chengc35497f2006-10-30 08:02:39 +0000230 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000231 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000232 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
233 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
236 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
237 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
239 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000240 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000241 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000242 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000243 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000244
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000245 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000246 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
247 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000249 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
250 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000252 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
253 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
255 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000256 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
257 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 }
259
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000260 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000261 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000262
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000263 // These should be promoted to a larger select which is supported.
264 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
265 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000266 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
268 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
269 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
270 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000271 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000272 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
273 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
274 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
275 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
276 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000277 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000278 if (Subtarget->is64Bit()) {
279 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
280 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
281 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000282 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000283 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000284 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000285
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000286 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000287 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000288 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000289 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000290 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000291 if (Subtarget->is64Bit())
292 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000293 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000294 if (Subtarget->is64Bit()) {
295 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
296 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
297 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000298 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000299 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000300 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000301 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
302 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
303 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000304 if (Subtarget->is64Bit()) {
305 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
306 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
307 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
308 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000309
Evan Chengd2cde682008-03-10 19:38:10 +0000310 if (Subtarget->hasSSE1())
311 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000312
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000313 if (!Subtarget->hasSSE2())
314 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
315
Mon P Wang63307c32008-05-05 19:05:59 +0000316 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000317 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
318 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
319 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
320 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000321
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000326
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000327 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000328 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
330 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
331 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
332 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
333 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
334 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000335 }
336
Dan Gohman7f460202008-06-30 20:59:49 +0000337 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
338 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000339 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000340 if (!Subtarget->isTargetDarwin() &&
341 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000342 !Subtarget->isTargetCygMing()) {
343 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
344 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
345 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000346
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000347 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
348 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
349 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
350 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
351 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000352 setExceptionPointerRegister(X86::RAX);
353 setExceptionSelectorRegister(X86::RDX);
354 } else {
355 setExceptionPointerRegister(X86::EAX);
356 setExceptionSelectorRegister(X86::EDX);
357 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000358 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000359 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
360
Duncan Sandsf7331b32007-09-11 14:10:23 +0000361 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000362
Chris Lattnerda68d302008-01-15 21:58:22 +0000363 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000364
Nate Begemanacc398c2006-01-25 18:21:52 +0000365 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
366 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000367 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000368 if (Subtarget->is64Bit()) {
369 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000370 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000371 } else {
372 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000373 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000374 }
Evan Chengae642192007-03-02 23:16:35 +0000375
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000376 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000377 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000378 if (Subtarget->is64Bit())
379 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000380 if (Subtarget->isTargetCygMing())
381 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
382 else
383 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000384
Evan Chengc7ce29b2009-02-13 22:36:38 +0000385 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000386 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000387 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000388 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
389 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000390
Evan Cheng223547a2006-01-31 22:28:30 +0000391 // Use ANDPD to simulate FABS.
392 setOperationAction(ISD::FABS , MVT::f64, Custom);
393 setOperationAction(ISD::FABS , MVT::f32, Custom);
394
395 // Use XORP to simulate FNEG.
396 setOperationAction(ISD::FNEG , MVT::f64, Custom);
397 setOperationAction(ISD::FNEG , MVT::f32, Custom);
398
Evan Cheng68c47cb2007-01-05 07:55:56 +0000399 // Use ANDPD and ORPD to simulate FCOPYSIGN.
400 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
401 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
402
Evan Chengd25e9e82006-02-02 00:28:23 +0000403 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000404 setOperationAction(ISD::FSIN , MVT::f64, Expand);
405 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000406 setOperationAction(ISD::FSIN , MVT::f32, Expand);
407 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000408
Chris Lattnera54aa942006-01-29 06:26:08 +0000409 // Expand FP immediates into loads from the stack, except for the special
410 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000411 addLegalFPImmediate(APFloat(+0.0)); // xorpd
412 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000413
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000414 // Floating truncations from f80 and extensions to f80 go through memory.
415 // If optimizing, we lie about this though and handle it in
416 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
417 if (Fast) {
418 setConvertAction(MVT::f32, MVT::f80, Expand);
419 setConvertAction(MVT::f64, MVT::f80, Expand);
420 setConvertAction(MVT::f80, MVT::f32, Expand);
421 setConvertAction(MVT::f80, MVT::f64, Expand);
422 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000423 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000424 // Use SSE for f32, x87 for f64.
425 // Set up the FP register classes.
426 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
427 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
428
429 // Use ANDPS to simulate FABS.
430 setOperationAction(ISD::FABS , MVT::f32, Custom);
431
432 // Use XORP to simulate FNEG.
433 setOperationAction(ISD::FNEG , MVT::f32, Custom);
434
435 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
436
437 // Use ANDPS and ORPS to simulate FCOPYSIGN.
438 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
440
441 // We don't support sin/cos/fmod
442 setOperationAction(ISD::FSIN , MVT::f32, Expand);
443 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000444
Nate Begemane1795842008-02-14 08:57:00 +0000445 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000446 addLegalFPImmediate(APFloat(+0.0f)); // xorps
447 addLegalFPImmediate(APFloat(+0.0)); // FLD0
448 addLegalFPImmediate(APFloat(+1.0)); // FLD1
449 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
450 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
451
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000452 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
453 // this though and handle it in InstructionSelectPreprocess so that
454 // dagcombine2 can hack on these.
455 if (Fast) {
456 setConvertAction(MVT::f32, MVT::f64, Expand);
457 setConvertAction(MVT::f32, MVT::f80, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000458 setConvertAction(MVT::f80, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000459 setConvertAction(MVT::f64, MVT::f32, Expand);
460 // And x87->x87 truncations also.
461 setConvertAction(MVT::f80, MVT::f64, Expand);
462 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463
464 if (!UnsafeFPMath) {
465 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
466 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
467 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000468 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000470 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000471 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
472 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000473
Evan Cheng68c47cb2007-01-05 07:55:56 +0000474 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000475 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000476 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
477 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000478
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000479 // Floating truncations go through memory. If optimizing, we lie about
480 // this though and handle it in InstructionSelectPreprocess so that
481 // dagcombine2 can hack on these.
482 if (Fast) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000483 setConvertAction(MVT::f80, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000484 setConvertAction(MVT::f64, MVT::f32, Expand);
485 setConvertAction(MVT::f80, MVT::f64, Expand);
486 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000487
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000488 if (!UnsafeFPMath) {
489 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
490 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
491 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000492 addLegalFPImmediate(APFloat(+0.0)); // FLD0
493 addLegalFPImmediate(APFloat(+1.0)); // FLD1
494 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
495 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000496 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
497 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
498 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
499 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000501
Dale Johannesen59a58732007-08-05 18:49:15 +0000502 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000503 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000504 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
505 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
506 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
507 {
508 bool ignored;
509 APFloat TmpFlt(+0.0);
510 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
511 &ignored);
512 addLegalFPImmediate(TmpFlt); // FLD0
513 TmpFlt.changeSign();
514 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
515 APFloat TmpFlt2(+1.0);
516 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
517 &ignored);
518 addLegalFPImmediate(TmpFlt2); // FLD1
519 TmpFlt2.changeSign();
520 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
521 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000522
Evan Chengc7ce29b2009-02-13 22:36:38 +0000523 if (!UnsafeFPMath) {
524 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
525 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
526 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000527 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000528
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000529 // Always use a library call for pow.
530 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
531 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
532 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
533
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000534 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000535 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000536 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000537 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000538 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
539
Mon P Wangf007a8b2008-11-06 05:31:54 +0000540 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000541 // (for widening) or expand (for scalarization). Then we will selectively
542 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000543 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
544 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000545 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000558 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
560 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000561 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000583 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000588 }
589
Evan Chengc7ce29b2009-02-13 22:36:38 +0000590 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
591 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000592 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000593 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
594 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
595 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000596 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000597 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000598
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000599 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
600 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
601 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000602 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000603
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000604 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
605 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
606 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000607 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000608
Bill Wendling74027e92007-03-15 21:24:36 +0000609 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
610 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
611
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000612 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000613 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000614 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000615 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
616 setOperationAction(ISD::AND, MVT::v2i32, Promote);
617 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
618 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000619
620 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000621 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000622 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000623 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
624 setOperationAction(ISD::OR, MVT::v2i32, Promote);
625 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
626 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000627
628 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000629 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000630 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000631 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
632 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
633 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
634 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000635
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000636 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000637 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000638 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000639 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
640 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
641 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000642 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
643 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000644 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000645
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000646 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
648 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000649 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000650 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000651
652 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
653 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000655 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000656
Evan Cheng52672b82008-07-22 18:39:19 +0000657 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000658 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
659 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000660 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000661
662 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000663
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000664 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000665 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
666 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
667 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
668 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
669 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000670 }
671
Evan Cheng92722532009-03-26 23:06:32 +0000672 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000673 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
674
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000675 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
676 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
677 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
678 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000679 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
680 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000681 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
682 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
683 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000684 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000685 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000686 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000687 }
688
Evan Cheng92722532009-03-26 23:06:32 +0000689 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000690 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000691
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000692 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
693 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000694 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
695 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
696 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
697 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
698
Evan Chengf7c378e2006-04-10 07:23:14 +0000699 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
700 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
701 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000702 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000703 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000704 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
705 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
706 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000707 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000708 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000709 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
710 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
711 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
712 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000713 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
714 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000715
Nate Begeman30a0de92008-07-17 16:51:19 +0000716 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
717 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
718 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
719 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000720
Evan Chengf7c378e2006-04-10 07:23:14 +0000721 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
722 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000723 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000724 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000725 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000726
Evan Cheng2c3ae372006-04-12 21:21:57 +0000727 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000728 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
729 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000730 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000731 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000732 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000733 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
734 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
735 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000736 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000737
Evan Cheng2c3ae372006-04-12 21:21:57 +0000738 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
739 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
740 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
741 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000742 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000743 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000744
Nate Begemancdd1eec2008-02-12 22:51:28 +0000745 if (Subtarget->is64Bit()) {
746 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000747 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000748 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000749
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000750 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000751 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000752 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
753 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
754 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
755 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
756 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
757 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
758 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
759 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
760 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
761 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000762 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000763
Chris Lattnerddf89562008-01-17 19:59:44 +0000764 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000765
Evan Cheng2c3ae372006-04-12 21:21:57 +0000766 // Custom lower v2i64 and v2f64 selects.
767 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000768 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000769 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000770 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000771
Evan Cheng470a6ad2006-02-22 02:26:30 +0000772 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000773
Nate Begeman14d12ca2008-02-11 04:19:36 +0000774 if (Subtarget->hasSSE41()) {
775 // FIXME: Do we need to handle scalar-to-vector here?
776 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
777
778 // i8 and i16 vectors are custom , because the source register and source
779 // source memory operand types are not the same width. f32 vectors are
780 // custom since the immediate controlling the insert encodes additional
781 // information.
782 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
783 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000784 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
786
787 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
788 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000791
792 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000793 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
794 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000795 }
796 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000797
Nate Begeman30a0de92008-07-17 16:51:19 +0000798 if (Subtarget->hasSSE42()) {
799 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
800 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000801
Evan Cheng6be2c582006-04-05 23:38:46 +0000802 // We want to custom lower some of our intrinsics.
803 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
804
Bill Wendling74c37652008-12-09 22:08:41 +0000805 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000806 setOperationAction(ISD::SADDO, MVT::i32, Custom);
807 setOperationAction(ISD::SADDO, MVT::i64, Custom);
808 setOperationAction(ISD::UADDO, MVT::i32, Custom);
809 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000810 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
811 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
812 setOperationAction(ISD::USUBO, MVT::i32, Custom);
813 setOperationAction(ISD::USUBO, MVT::i64, Custom);
814 setOperationAction(ISD::SMULO, MVT::i32, Custom);
815 setOperationAction(ISD::SMULO, MVT::i64, Custom);
816 setOperationAction(ISD::UMULO, MVT::i32, Custom);
817 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000818
Evan Chengd54f2d52009-03-31 19:38:51 +0000819 if (!Subtarget->is64Bit()) {
820 // These libcalls are not available in 32-bit.
821 setLibcallName(RTLIB::SHL_I128, 0);
822 setLibcallName(RTLIB::SRL_I128, 0);
823 setLibcallName(RTLIB::SRA_I128, 0);
824 }
825
Evan Cheng206ee9d2006-07-07 08:33:52 +0000826 // We have target-specific dag combine patterns for the following nodes:
827 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000828 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000829 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000830 setTargetDAGCombine(ISD::SHL);
831 setTargetDAGCombine(ISD::SRA);
832 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000833 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000834 if (Subtarget->is64Bit())
835 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000836
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000837 computeRegisterProperties();
838
Evan Cheng87ed7162006-02-14 08:25:08 +0000839 // FIXME: These should be based on subtarget info. Plus, the values should
840 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000841 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
842 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
843 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000844 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000845 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000846 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000847}
848
Scott Michel5b8f82e2008-03-10 15:42:14 +0000849
Duncan Sands5480c042009-01-01 15:52:00 +0000850MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000851 return MVT::i8;
852}
853
854
Evan Cheng29286502008-01-23 23:17:41 +0000855/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
856/// the desired ByVal argument alignment.
857static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
858 if (MaxAlign == 16)
859 return;
860 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
861 if (VTy->getBitWidth() == 128)
862 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000863 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
864 unsigned EltAlign = 0;
865 getMaxByValAlign(ATy->getElementType(), EltAlign);
866 if (EltAlign > MaxAlign)
867 MaxAlign = EltAlign;
868 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
869 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
870 unsigned EltAlign = 0;
871 getMaxByValAlign(STy->getElementType(i), EltAlign);
872 if (EltAlign > MaxAlign)
873 MaxAlign = EltAlign;
874 if (MaxAlign == 16)
875 break;
876 }
877 }
878 return;
879}
880
881/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
882/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000883/// that contain SSE vectors are placed at 16-byte boundaries while the rest
884/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000885unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000886 if (Subtarget->is64Bit()) {
887 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000888 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000889 if (TyAlign > 8)
890 return TyAlign;
891 return 8;
892 }
893
Evan Cheng29286502008-01-23 23:17:41 +0000894 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000895 if (Subtarget->hasSSE1())
896 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000897 return Align;
898}
Chris Lattner2b02a442007-02-25 08:29:00 +0000899
Evan Chengf0df0312008-05-15 08:39:06 +0000900/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000901/// and store operations as a result of memset, memcpy, and memmove
902/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000903/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000904MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000905X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
906 bool isSrcConst, bool isSrcStr) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000907 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
908 // linux. This is because the stack realignment code can't handle certain
909 // cases like PR2962. This should be removed when PR2962 is fixed.
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000910 if (!NoImplicitFloat && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000911 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
912 return MVT::v4i32;
913 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
914 return MVT::v4f32;
915 }
Evan Chengf0df0312008-05-15 08:39:06 +0000916 if (Subtarget->is64Bit() && Size >= 8)
917 return MVT::i64;
918 return MVT::i32;
919}
920
Evan Chengcc415862007-11-09 01:32:10 +0000921/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
922/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000923SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000924 SelectionDAG &DAG) const {
925 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000926 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000927 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000928 // This doesn't have DebugLoc associated with it, but is not really the
929 // same as a Register.
930 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
931 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000932 return Table;
933}
934
Chris Lattner2b02a442007-02-25 08:29:00 +0000935//===----------------------------------------------------------------------===//
936// Return Value Calling Convention Implementation
937//===----------------------------------------------------------------------===//
938
Chris Lattner59ed56b2007-02-28 04:55:35 +0000939#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000940
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000941/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000942SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000943 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000944 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000945
Chris Lattner9774c912007-02-27 05:28:59 +0000946 SmallVector<CCValAssign, 16> RVLocs;
947 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000948 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
949 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000951
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000952 // If this is the first return lowered for this function, add the regs to the
953 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000954 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000955 for (unsigned i = 0; i != RVLocs.size(); ++i)
956 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000957 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000958 }
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000960
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000961 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000962 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000963 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000964 SDValue TailCall = Chain;
965 SDValue TargetAddress = TailCall.getOperand(1);
966 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000967 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000968 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000969 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000970 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000971 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000972 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000973 assert(StackAdjustment.getOpcode() == ISD::Constant &&
974 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000975
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000977 Operands.push_back(Chain.getOperand(0));
978 Operands.push_back(TargetAddress);
979 Operands.push_back(StackAdjustment);
980 // Copy registers used by the call. Last operand is a flag so it is not
981 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000982 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000983 Operands.push_back(Chain.getOperand(i));
984 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000985 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000986 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000987 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000988
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000989 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000990 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000991
Dan Gohman475871a2008-07-27 21:46:04 +0000992 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000993 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
994 // Operand #1 = Bytes To Pop
995 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000996
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000997 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000998 for (unsigned i = 0; i != RVLocs.size(); ++i) {
999 CCValAssign &VA = RVLocs[i];
1000 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +00001001 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001002
Chris Lattner447ff682008-03-11 03:23:40 +00001003 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1004 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001005 if (VA.getLocReg() == X86::ST0 ||
1006 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001007 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1008 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001009 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +00001010 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001011 RetOps.push_back(ValToCopy);
1012 // Don't emit a copytoreg.
1013 continue;
1014 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001015
Evan Cheng242b38b2009-02-23 09:03:22 +00001016 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1017 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001018 if (Subtarget->is64Bit()) {
1019 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001020 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +00001021 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001022 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1023 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1024 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001025 }
1026
Dale Johannesendd64c412009-02-04 00:33:20 +00001027 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001028 Flag = Chain.getValue(1);
1029 }
Dan Gohman61a92132008-04-21 23:59:07 +00001030
1031 // The x86-64 ABI for returning structs by value requires that we copy
1032 // the sret argument into %rax for the return. We saved the argument into
1033 // a virtual register in the entry block, so now we copy the value out
1034 // and into %rax.
1035 if (Subtarget->is64Bit() &&
1036 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1037 MachineFunction &MF = DAG.getMachineFunction();
1038 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1039 unsigned Reg = FuncInfo->getSRetReturnReg();
1040 if (!Reg) {
1041 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1042 FuncInfo->setSRetReturnReg(Reg);
1043 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001044 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001045
Dale Johannesendd64c412009-02-04 00:33:20 +00001046 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001047 Flag = Chain.getValue(1);
1048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001049
Chris Lattner447ff682008-03-11 03:23:40 +00001050 RetOps[0] = Chain; // Update chain.
1051
1052 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001053 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001054 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001055
1056 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001057 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001058}
1059
1060
Chris Lattner3085e152007-02-25 08:59:22 +00001061/// LowerCallResult - Lower the result values of an ISD::CALL into the
1062/// appropriate copies out of appropriate physical registers. This assumes that
1063/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1064/// being lowered. The returns a SDNode with the same number of values as the
1065/// ISD::CALL.
1066SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001067LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001068 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001069
Scott Michelfdc40a02009-02-17 22:15:04 +00001070 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001071 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001072 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001073 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001074 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001075 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001076 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1077
Dan Gohman475871a2008-07-27 21:46:04 +00001078 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Chris Lattner3085e152007-02-25 08:59:22 +00001080 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001081 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001082 CCValAssign &VA = RVLocs[i];
1083 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001084
Torok Edwin3f142c32009-02-01 18:15:56 +00001085 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001086 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001087 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1088 cerr << "SSE register return with SSE disabled\n";
1089 exit(1);
1090 }
1091
Chris Lattner8e6da152008-03-10 21:08:41 +00001092 // If this is a call to a function that returns an fp value on the floating
1093 // point stack, but where we prefer to use the value in xmm registers, copy
1094 // 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 +00001095 if ((VA.getLocReg() == X86::ST0 ||
1096 VA.getLocReg() == X86::ST1) &&
1097 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001098 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001100
Evan Cheng79fb3b42009-02-20 20:43:02 +00001101 SDValue Val;
1102 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001103 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1104 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1105 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1106 MVT::v2i64, InFlag).getValue(1);
1107 Val = Chain.getValue(0);
1108 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1109 Val, DAG.getConstant(0, MVT::i64));
1110 } else {
1111 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1112 MVT::i64, InFlag).getValue(1);
1113 Val = Chain.getValue(0);
1114 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001115 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1116 } else {
1117 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1118 CopyVT, InFlag).getValue(1);
1119 Val = Chain.getValue(0);
1120 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001121 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001122
Dan Gohman37eed792009-02-04 17:28:58 +00001123 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001124 // Round the F80 the right size, which also moves to the appropriate xmm
1125 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001126 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001127 // This truncation won't change the value.
1128 DAG.getIntPtrConstant(1));
1129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001130
Chris Lattner8e6da152008-03-10 21:08:41 +00001131 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001132 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001133
Chris Lattner3085e152007-02-25 08:59:22 +00001134 // Merge everything together with a MERGE_VALUES node.
1135 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001136 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1137 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001138}
1139
1140
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001141//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001142// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001143//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001144// StdCall calling convention seems to be standard for many Windows' API
1145// routines and around. It differs from C calling convention just a little:
1146// callee should clean up the stack, not caller. Symbols should be also
1147// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001148// For info on fast calling convention see Fast Calling Convention (tail call)
1149// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001150
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001151/// CallIsStructReturn - Determines whether a CALL node uses struct return
1152/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001153static bool CallIsStructReturn(CallSDNode *TheCall) {
1154 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001155 if (!NumOps)
1156 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001157
Dan Gohman095cc292008-09-13 01:54:27 +00001158 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001159}
1160
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001161/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1162/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001163static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001164 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001165 if (!NumArgs)
1166 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001167
1168 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001169}
1170
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001171/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1172/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001173/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001174bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001175 if (IsVarArg)
1176 return false;
1177
Dan Gohman095cc292008-09-13 01:54:27 +00001178 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001179 default:
1180 return false;
1181 case CallingConv::X86_StdCall:
1182 return !Subtarget->is64Bit();
1183 case CallingConv::X86_FastCall:
1184 return !Subtarget->is64Bit();
1185 case CallingConv::Fast:
1186 return PerformTailCallOpt;
1187 }
1188}
1189
Dan Gohman095cc292008-09-13 01:54:27 +00001190/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1191/// given CallingConvention value.
1192CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001193 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001194 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001195 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001196 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1197 return CC_X86_64_TailCall;
1198 else
1199 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001200 }
1201
Gordon Henriksen86737662008-01-05 16:56:59 +00001202 if (CC == CallingConv::X86_FastCall)
1203 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001204 else if (CC == CallingConv::Fast)
1205 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001206 else
1207 return CC_X86_32_C;
1208}
1209
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001210/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1211/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001212NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001213X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001214 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001215 if (CC == CallingConv::X86_FastCall)
1216 return FastCall;
1217 else if (CC == CallingConv::X86_StdCall)
1218 return StdCall;
1219 return None;
1220}
1221
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001222
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001223/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1224/// in a register before calling.
1225bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1226 return !IsTailCall && !Is64Bit &&
1227 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1228 Subtarget->isPICStyleGOT();
1229}
1230
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001231/// CallRequiresFnAddressInReg - Check whether the call requires the function
1232/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001233bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001234X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001235 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001236 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1237 Subtarget->isPICStyleGOT();
1238}
1239
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001240/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1241/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001242/// the specific parameter attribute. The copy will be passed as a byval
1243/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001244static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001245CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001246 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1247 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001248 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001249 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001250 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001251}
1252
Dan Gohman475871a2008-07-27 21:46:04 +00001253SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001254 const CCValAssign &VA,
1255 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001256 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001257 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001258 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001259 ISD::ArgFlagsTy Flags =
1260 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001261 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001262 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001263
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001264 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001265 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001266 // In case of tail call optimization mark all arguments mutable. Since they
1267 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001268 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001269 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001270 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001271 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001272 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001273 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001274 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001275}
1276
Dan Gohman475871a2008-07-27 21:46:04 +00001277SDValue
1278X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001279 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001280 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001281 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001282
Gordon Henriksen86737662008-01-05 16:56:59 +00001283 const Function* Fn = MF.getFunction();
1284 if (Fn->hasExternalLinkage() &&
1285 Subtarget->isTargetCygMing() &&
1286 Fn->getName() == "main")
1287 FuncInfo->setForceFramePointer(true);
1288
1289 // Decorate the function name.
1290 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Evan Cheng1bc78042006-04-26 01:20:17 +00001292 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001294 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001295 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001296 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001297 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001298
1299 assert(!(isVarArg && CC == CallingConv::Fast) &&
1300 "Var args not supported with calling convention fastcc");
1301
Chris Lattner638402b2007-02-28 07:00:42 +00001302 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001303 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001304 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001305 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001306
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001308 unsigned LastVal = ~0U;
1309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1310 CCValAssign &VA = ArgLocs[i];
1311 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1312 // places.
1313 assert(VA.getValNo() != LastVal &&
1314 "Don't support value assigned to multiple locs yet");
1315 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001316
Chris Lattnerf39f7712007-02-28 05:46:49 +00001317 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001318 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001319 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001320 if (RegVT == MVT::i32)
1321 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001322 else if (Is64Bit && RegVT == MVT::i64)
1323 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001324 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001325 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001326 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001327 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001328 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001329 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001330 else if (RegVT.isVector()) {
1331 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001332 if (!Is64Bit)
1333 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1334 else {
1335 // Darwin calling convention passes MMX values in either GPRs or
1336 // XMMs in x86-64. Other targets pass them in memory.
1337 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1338 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1339 RegVT = MVT::v2i64;
1340 } else {
1341 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1342 RegVT = MVT::i64;
1343 }
1344 }
1345 } else {
1346 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001347 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001348
Bob Wilson998e1252009-04-20 18:36:57 +00001349 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001350 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Chris Lattnerf39f7712007-02-28 05:46:49 +00001352 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1353 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1354 // right size.
1355 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001356 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001357 DAG.getValueType(VA.getValVT()));
1358 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001359 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001360 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Chris Lattnerf39f7712007-02-28 05:46:49 +00001362 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001363 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001364
Gordon Henriksen86737662008-01-05 16:56:59 +00001365 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001366 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001367 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001368 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001369 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001370 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1371 ArgValue, DAG.getConstant(0, MVT::i64));
1372 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001373 }
1374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001375
Chris Lattnerf39f7712007-02-28 05:46:49 +00001376 ArgValues.push_back(ArgValue);
1377 } else {
1378 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001379 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001380 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001381 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001382
Dan Gohman61a92132008-04-21 23:59:07 +00001383 // The x86-64 ABI for returning structs by value requires that we copy
1384 // the sret argument into %rax for the return. Save the argument into
1385 // a virtual register so that we can access it from the return points.
1386 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1387 MachineFunction &MF = DAG.getMachineFunction();
1388 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1389 unsigned Reg = FuncInfo->getSRetReturnReg();
1390 if (!Reg) {
1391 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1392 FuncInfo->setSRetReturnReg(Reg);
1393 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001394 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001395 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001396 }
1397
Chris Lattnerf39f7712007-02-28 05:46:49 +00001398 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001399 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001400 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001401 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001402
Evan Cheng1bc78042006-04-26 01:20:17 +00001403 // If the function takes variable number of arguments, make a frame index for
1404 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001405 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001406 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1407 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1408 }
1409 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001410 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1411
1412 // FIXME: We should really autogenerate these arrays
1413 static const unsigned GPR64ArgRegsWin64[] = {
1414 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001415 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001416 static const unsigned XMMArgRegsWin64[] = {
1417 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1418 };
1419 static const unsigned GPR64ArgRegs64Bit[] = {
1420 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1421 };
1422 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001423 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1424 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1425 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001426 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1427
1428 if (IsWin64) {
1429 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1430 GPR64ArgRegs = GPR64ArgRegsWin64;
1431 XMMArgRegs = XMMArgRegsWin64;
1432 } else {
1433 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1434 GPR64ArgRegs = GPR64ArgRegs64Bit;
1435 XMMArgRegs = XMMArgRegs64Bit;
1436 }
1437 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1438 TotalNumIntRegs);
1439 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1440 TotalNumXMMRegs);
1441
Evan Chengc7ce29b2009-02-13 22:36:38 +00001442 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001443 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001444 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloat) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001445 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001446 if (UseSoftFloat || NoImplicitFloat || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001447 // Kernel mode asks for SSE to be disabled, so don't push them
1448 // on the stack.
1449 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001450
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 // For X86-64, if there are vararg parameters that are passed via
1452 // registers, then we must store them to their spots on the stack so they
1453 // may be loaded by deferencing the result of va_next.
1454 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001455 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1456 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1457 TotalNumXMMRegs * 16, 16);
1458
Gordon Henriksen86737662008-01-05 16:56:59 +00001459 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001460 SmallVector<SDValue, 8> MemOps;
1461 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001462 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001463 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001464 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001465 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1466 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001467 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001468 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001469 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001470 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001471 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001472 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001473 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001474 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001475
Gordon Henriksen86737662008-01-05 16:56:59 +00001476 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001477 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001478 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001479 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001480 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1481 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001482 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001483 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001484 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001485 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001486 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001487 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001488 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001489 }
1490 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001491 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001492 &MemOps[0], MemOps.size());
1493 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Gordon Henriksenae636f82008-01-03 16:47:34 +00001496 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001497
Gordon Henriksen86737662008-01-05 16:56:59 +00001498 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001499 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001500 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001501 BytesCallerReserves = 0;
1502 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001503 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001504 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001505 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001506 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001507 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001508 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001509
Gordon Henriksen86737662008-01-05 16:56:59 +00001510 if (!Is64Bit) {
1511 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1512 if (CC == CallingConv::X86_FastCall)
1513 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1514 }
Evan Cheng25caf632006-05-23 21:06:34 +00001515
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001516 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001517
Evan Cheng25caf632006-05-23 21:06:34 +00001518 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001519 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001520 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001521}
1522
Dan Gohman475871a2008-07-27 21:46:04 +00001523SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001524X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001525 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001526 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001527 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001528 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001529 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001530 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001531 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001532 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001533 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001534 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001535 }
Dale Johannesenace16102009-02-03 19:33:06 +00001536 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001537 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001538}
1539
Bill Wendling64e87322009-01-16 19:25:27 +00001540/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001541/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001542SDValue
1543X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001544 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001545 SDValue Chain,
1546 bool IsTailCall,
1547 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001548 int FPDiff,
1549 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001550 if (!IsTailCall || FPDiff==0) return Chain;
1551
1552 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001553 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001554 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001555
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001556 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001557 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001558 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001559}
1560
1561/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1562/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001563static SDValue
1564EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001565 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001566 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001567 // Store the return address to the appropriate stack slot.
1568 if (!FPDiff) return Chain;
1569 // Calculate the new stack slot for the return address.
1570 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001571 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001572 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001573 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001574 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001575 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001576 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001577 return Chain;
1578}
1579
Dan Gohman475871a2008-07-27 21:46:04 +00001580SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001581 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001582 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1583 SDValue Chain = TheCall->getChain();
1584 unsigned CC = TheCall->getCallingConv();
1585 bool isVarArg = TheCall->isVarArg();
1586 bool IsTailCall = TheCall->isTailCall() &&
1587 CC == CallingConv::Fast && PerformTailCallOpt;
1588 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001590 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001591 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001592
1593 assert(!(isVarArg && CC == CallingConv::Fast) &&
1594 "Var args not supported with calling convention fastcc");
1595
Chris Lattner638402b2007-02-28 07:00:42 +00001596 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001597 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001598 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001599 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner423c5f42007-02-28 05:31:48 +00001601 // Get a count of how many bytes are to be pushed on the stack.
1602 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001603 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001604 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001605
Gordon Henriksen86737662008-01-05 16:56:59 +00001606 int FPDiff = 0;
1607 if (IsTailCall) {
1608 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001609 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001610 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1611 FPDiff = NumBytesCallerPushed - NumBytes;
1612
1613 // Set the delta of movement of the returnaddr stackslot.
1614 // But only set if delta is greater than previous delta.
1615 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1616 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1617 }
1618
Chris Lattnere563bbc2008-10-11 22:08:30 +00001619 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001620
Dan Gohman475871a2008-07-27 21:46:04 +00001621 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001622 // Load return adress for tail calls.
1623 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001624 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001625
Dan Gohman475871a2008-07-27 21:46:04 +00001626 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1627 SmallVector<SDValue, 8> MemOpChains;
1628 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001629
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001630 // Walk the register/memloc assignments, inserting copies/loads. In the case
1631 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001632 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1633 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001634 SDValue Arg = TheCall->getArg(i);
1635 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1636 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001637
Chris Lattner423c5f42007-02-28 05:31:48 +00001638 // Promote the value if needed.
1639 switch (VA.getLocInfo()) {
1640 default: assert(0 && "Unknown loc info!");
1641 case CCValAssign::Full: break;
1642 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001643 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001644 break;
1645 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001646 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001647 break;
1648 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001649 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001650 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001651 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001652
Chris Lattner423c5f42007-02-28 05:31:48 +00001653 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001654 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001655 MVT RegVT = VA.getLocVT();
1656 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001657 switch (VA.getLocReg()) {
1658 default:
1659 break;
1660 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1661 case X86::R8: {
1662 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001663 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001664 break;
1665 }
1666 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1667 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1668 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001669 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1670 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001671 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001672 break;
1673 }
1674 }
1675 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001676 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1677 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001678 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001679 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001680 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001681 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001682
Dan Gohman095cc292008-09-13 01:54:27 +00001683 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1684 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001685 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001686 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001687 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001688
Evan Cheng32fe1032006-05-25 00:59:30 +00001689 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001690 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001691 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001692
Evan Cheng347d5f72006-04-28 21:29:37 +00001693 // Build a sequence of copy-to-reg nodes chained together with token chain
1694 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001695 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001696 // Tail call byval lowering might overwrite argument registers so in case of
1697 // tail call optimization the copies to registers are lowered later.
1698 if (!IsTailCall)
1699 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001700 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001701 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001702 InFlag = Chain.getValue(1);
1703 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001704
Evan Chengf4684712007-02-21 21:18:14 +00001705 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001706 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001707 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001708 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001709 DAG.getNode(X86ISD::GlobalBaseReg,
1710 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001711 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001712 InFlag);
1713 InFlag = Chain.getValue(1);
1714 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001715 // If we are tail calling and generating PIC/GOT style code load the address
1716 // of the callee into ecx. The value in ecx is used as target of the tail
1717 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1718 // calls on PIC/GOT architectures. Normally we would just put the address of
1719 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1720 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001721 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001722 // Note: The actual moving to ecx is done further down.
1723 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001724 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001725 !G->getGlobal()->hasProtectedVisibility())
1726 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001727 else if (isa<ExternalSymbolSDNode>(Callee))
1728 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001729 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001730
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 if (Is64Bit && isVarArg) {
1732 // From AMD64 ABI document:
1733 // For calls that may call functions that use varargs or stdargs
1734 // (prototype-less calls or calls to functions containing ellipsis (...) in
1735 // the declaration) %al is used as hidden argument to specify the number
1736 // of SSE registers used. The contents of %al do not need to match exactly
1737 // the number of registers, but must be an ubound on the number of SSE
1738 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001739
1740 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001741 // Count the number of XMM registers allocated.
1742 static const unsigned XMMArgRegs[] = {
1743 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1744 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1745 };
1746 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001747 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001748 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001749
Dale Johannesendd64c412009-02-04 00:33:20 +00001750 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1752 InFlag = Chain.getValue(1);
1753 }
1754
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001755
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001756 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SmallVector<SDValue, 8> MemOpChains2;
1759 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001760 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001761 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001762 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001763 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1764 CCValAssign &VA = ArgLocs[i];
1765 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001766 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001767 SDValue Arg = TheCall->getArg(i);
1768 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001769 // Create frame index.
1770 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001771 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001772 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001773 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001774
Duncan Sands276dcbd2008-03-21 09:14:45 +00001775 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001776 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001777 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001778 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001779 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001780 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001781 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001782
1783 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001784 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001786 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001787 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001788 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001789 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001790 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 }
1792 }
1793
1794 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001795 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001796 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001797
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001798 // Copy arguments to their registers.
1799 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001800 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001801 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001802 InFlag = Chain.getValue(1);
1803 }
Dan Gohman475871a2008-07-27 21:46:04 +00001804 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001805
Gordon Henriksen86737662008-01-05 16:56:59 +00001806 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001807 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001808 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 }
1810
Evan Cheng32fe1032006-05-25 00:59:30 +00001811 // If the callee is a GlobalAddress node (quite common, every direct call is)
1812 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001813 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001814 // We should use extra load for direct calls to dllimported functions in
1815 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001816 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1817 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001818 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1819 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001820 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1821 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001822 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001823 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001824
Dale Johannesendd64c412009-02-04 00:33:20 +00001825 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001826 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001827 Callee,InFlag);
1828 Callee = DAG.getRegister(Opc, getPointerTy());
1829 // Add register as live out.
1830 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001831 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001832
Chris Lattnerd96d0722007-02-25 06:40:16 +00001833 // Returns a chain & a flag for retval copy to use.
1834 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001836
1837 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001838 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1839 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001841
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 // Returns a chain & a flag for retval copy to use.
1843 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1844 Ops.clear();
1845 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001846
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001847 Ops.push_back(Chain);
1848 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001849
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 if (IsTailCall)
1851 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001852
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 // Add argument registers to the end of the list so that they are known live
1854 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001855 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1856 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1857 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001858
Evan Cheng586ccac2008-03-18 23:36:35 +00001859 // Add an implicit use GOT pointer in EBX.
1860 if (!IsTailCall && !Is64Bit &&
1861 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1862 Subtarget->isPICStyleGOT())
1863 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1864
1865 // Add an implicit use of AL for x86 vararg functions.
1866 if (Is64Bit && isVarArg)
1867 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1868
Gabor Greifba36cb52008-08-28 21:40:38 +00001869 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001870 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001871
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001873 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001874 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001875 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001876 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001877
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001879 }
1880
Dale Johannesenace16102009-02-03 19:33:06 +00001881 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001882 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001883
Chris Lattner2d297092006-05-23 18:50:38 +00001884 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001886 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001887 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001888 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001889 // If this is is a call to a struct-return function, the callee
1890 // pops the hidden struct pointer, so we have to push it back.
1891 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001892 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001893 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001894 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Gordon Henriksenae636f82008-01-03 16:47:34 +00001896 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001897 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001898 DAG.getIntPtrConstant(NumBytes, true),
1899 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1900 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001901 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001902 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001903
Chris Lattner3085e152007-02-25 08:59:22 +00001904 // Handle result values, copying them out of physregs into vregs that we
1905 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001906 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001907 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001908}
1909
Evan Cheng25ab6902006-09-08 06:48:29 +00001910
1911//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001912// Fast Calling Convention (tail call) implementation
1913//===----------------------------------------------------------------------===//
1914
1915// Like std call, callee cleans arguments, convention except that ECX is
1916// reserved for storing the tail called function address. Only 2 registers are
1917// free for argument passing (inreg). Tail call optimization is performed
1918// provided:
1919// * tailcallopt is enabled
1920// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001921// On X86_64 architecture with GOT-style position independent code only local
1922// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001923// To keep the stack aligned according to platform abi the function
1924// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1925// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001926// If a tail called function callee has more arguments than the caller the
1927// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001928// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001929// original REtADDR, but before the saved framepointer or the spilled registers
1930// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1931// stack layout:
1932// arg1
1933// arg2
1934// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001935// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001936// move area ]
1937// (possible EBP)
1938// ESI
1939// EDI
1940// local1 ..
1941
1942/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1943/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001944unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001945 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001946 MachineFunction &MF = DAG.getMachineFunction();
1947 const TargetMachine &TM = MF.getTarget();
1948 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1949 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001950 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001951 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001952 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001953 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1954 // Number smaller than 12 so just add the difference.
1955 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1956 } else {
1957 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001958 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001959 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001960 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001961 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001962}
1963
1964/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001965/// following the call is a return. A function is eligible if caller/callee
1966/// calling conventions match, currently only fastcc supports tail calls, and
1967/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001968bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001970 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001971 if (!PerformTailCallOpt)
1972 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001973
Dan Gohman095cc292008-09-13 01:54:27 +00001974 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001975 MachineFunction &MF = DAG.getMachineFunction();
1976 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001977 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001978 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001979 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001980 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001981 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001982 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001983 return true;
1984
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001985 // Can only do local tail calls (in same module, hidden or protected) on
1986 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1988 return G->getGlobal()->hasHiddenVisibility()
1989 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001990 }
1991 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001992
1993 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001994}
1995
Dan Gohman3df24e62008-09-03 23:12:08 +00001996FastISel *
1997X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001998 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001999 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002000 DenseMap<const Value *, unsigned> &vm,
2001 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002002 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002003 DenseMap<const AllocaInst *, int> &am
2004#ifndef NDEBUG
2005 , SmallSet<Instruction*, 8> &cil
2006#endif
2007 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002008 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002009#ifndef NDEBUG
2010 , cil
2011#endif
2012 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002013}
2014
2015
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002016//===----------------------------------------------------------------------===//
2017// Other Lowering Hooks
2018//===----------------------------------------------------------------------===//
2019
2020
Dan Gohman475871a2008-07-27 21:46:04 +00002021SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002022 MachineFunction &MF = DAG.getMachineFunction();
2023 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2024 int ReturnAddrIndex = FuncInfo->getRAIndex();
2025
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002026 if (ReturnAddrIndex == 0) {
2027 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002028 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002029 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002030 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002031 }
2032
Evan Cheng25ab6902006-09-08 06:48:29 +00002033 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002034}
2035
2036
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002037/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2038/// specific condition code, returning the condition code and the LHS/RHS of the
2039/// comparison to make.
2040static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2041 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002042 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002043 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2044 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2045 // X > -1 -> X == 0, jump !sign.
2046 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002047 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002048 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2049 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002050 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002051 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002052 // X < 1 -> X <= 0
2053 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002054 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002055 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002056 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002057
Evan Chengd9558e02006-01-06 00:43:03 +00002058 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002059 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002060 case ISD::SETEQ: return X86::COND_E;
2061 case ISD::SETGT: return X86::COND_G;
2062 case ISD::SETGE: return X86::COND_GE;
2063 case ISD::SETLT: return X86::COND_L;
2064 case ISD::SETLE: return X86::COND_LE;
2065 case ISD::SETNE: return X86::COND_NE;
2066 case ISD::SETULT: return X86::COND_B;
2067 case ISD::SETUGT: return X86::COND_A;
2068 case ISD::SETULE: return X86::COND_BE;
2069 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002070 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002072
Chris Lattner4c78e022008-12-23 23:42:27 +00002073 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002074
Chris Lattner4c78e022008-12-23 23:42:27 +00002075 // If LHS is a foldable load, but RHS is not, flip the condition.
2076 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2077 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2078 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2079 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002080 }
2081
Chris Lattner4c78e022008-12-23 23:42:27 +00002082 switch (SetCCOpcode) {
2083 default: break;
2084 case ISD::SETOLT:
2085 case ISD::SETOLE:
2086 case ISD::SETUGT:
2087 case ISD::SETUGE:
2088 std::swap(LHS, RHS);
2089 break;
2090 }
2091
2092 // On a floating point condition, the flags are set as follows:
2093 // ZF PF CF op
2094 // 0 | 0 | 0 | X > Y
2095 // 0 | 0 | 1 | X < Y
2096 // 1 | 0 | 0 | X == Y
2097 // 1 | 1 | 1 | unordered
2098 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002099 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002100 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002101 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002102 case ISD::SETOLT: // flipped
2103 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002104 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002105 case ISD::SETOLE: // flipped
2106 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002107 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002108 case ISD::SETUGT: // flipped
2109 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002110 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002111 case ISD::SETUGE: // flipped
2112 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002113 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002114 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002115 case ISD::SETNE: return X86::COND_NE;
2116 case ISD::SETUO: return X86::COND_P;
2117 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002118 }
Evan Chengd9558e02006-01-06 00:43:03 +00002119}
2120
Evan Cheng4a460802006-01-11 00:33:36 +00002121/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2122/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002123/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002124static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002125 switch (X86CC) {
2126 default:
2127 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002128 case X86::COND_B:
2129 case X86::COND_BE:
2130 case X86::COND_E:
2131 case X86::COND_P:
2132 case X86::COND_A:
2133 case X86::COND_AE:
2134 case X86::COND_NE:
2135 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002136 return true;
2137 }
2138}
2139
Nate Begeman9008ca62009-04-27 18:41:29 +00002140/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2141/// the specified range (L, H].
2142static bool isUndefOrInRange(int Val, int Low, int Hi) {
2143 return (Val < 0) || (Val >= Low && Val < Hi);
2144}
2145
2146/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2147/// specified value.
2148static bool isUndefOrEqual(int Val, int CmpVal) {
2149 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002150 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002151 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002152}
2153
Nate Begeman9008ca62009-04-27 18:41:29 +00002154/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2155/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2156/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002157static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002158 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2159 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2160 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2161 return (Mask[0] < 2 && Mask[1] < 2);
2162 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002163}
2164
Nate Begeman9008ca62009-04-27 18:41:29 +00002165bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2166 SmallVector<int, 8> M;
2167 N->getMask(M);
2168 return ::isPSHUFDMask(M, N->getValueType(0));
2169}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002170
Nate Begeman9008ca62009-04-27 18:41:29 +00002171/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2172/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002173static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002174 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002175 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002176
2177 // Lower quadword copied in order or undef.
2178 for (int i = 0; i != 4; ++i)
2179 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002180 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002181
Evan Cheng506d3df2006-03-29 23:07:14 +00002182 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002183 for (int i = 4; i != 8; ++i)
2184 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002185 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002186
Evan Cheng506d3df2006-03-29 23:07:14 +00002187 return true;
2188}
2189
Nate Begeman9008ca62009-04-27 18:41:29 +00002190bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2191 SmallVector<int, 8> M;
2192 N->getMask(M);
2193 return ::isPSHUFHWMask(M, N->getValueType(0));
2194}
Evan Cheng506d3df2006-03-29 23:07:14 +00002195
Nate Begeman9008ca62009-04-27 18:41:29 +00002196/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2197/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002198static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002199 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002200 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002201
Rafael Espindola15684b22009-04-24 12:40:33 +00002202 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002203 for (int i = 4; i != 8; ++i)
2204 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002205 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002206
Rafael Espindola15684b22009-04-24 12:40:33 +00002207 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002208 for (int i = 0; i != 4; ++i)
2209 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002210 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002211
Rafael Espindola15684b22009-04-24 12:40:33 +00002212 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002213}
2214
Nate Begeman9008ca62009-04-27 18:41:29 +00002215bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2216 SmallVector<int, 8> M;
2217 N->getMask(M);
2218 return ::isPSHUFLWMask(M, N->getValueType(0));
2219}
2220
Evan Cheng14aed5e2006-03-24 01:18:28 +00002221/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2222/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002223static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002224 int NumElems = VT.getVectorNumElements();
2225 if (NumElems != 2 && NumElems != 4)
2226 return false;
2227
2228 int Half = NumElems / 2;
2229 for (int i = 0; i < Half; ++i)
2230 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002231 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002232 for (int i = Half; i < NumElems; ++i)
2233 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002234 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002235
Evan Cheng14aed5e2006-03-24 01:18:28 +00002236 return true;
2237}
2238
Nate Begeman9008ca62009-04-27 18:41:29 +00002239bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2240 SmallVector<int, 8> M;
2241 N->getMask(M);
2242 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002243}
2244
Evan Cheng213d2cf2007-05-17 18:45:50 +00002245/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002246/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2247/// half elements to come from vector 1 (which would equal the dest.) and
2248/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002249static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002250 int NumElems = VT.getVectorNumElements();
2251
2252 if (NumElems != 2 && NumElems != 4)
2253 return false;
2254
2255 int Half = NumElems / 2;
2256 for (int i = 0; i < Half; ++i)
2257 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002258 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002259 for (int i = Half; i < NumElems; ++i)
2260 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002261 return false;
2262 return true;
2263}
2264
Nate Begeman9008ca62009-04-27 18:41:29 +00002265static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2266 SmallVector<int, 8> M;
2267 N->getMask(M);
2268 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002269}
2270
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002271/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2272/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002273bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2274 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002275 return false;
2276
Evan Cheng2064a2b2006-03-28 06:50:32 +00002277 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002278 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2279 isUndefOrEqual(N->getMaskElt(1), 7) &&
2280 isUndefOrEqual(N->getMaskElt(2), 2) &&
2281 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002282}
2283
Evan Cheng5ced1d82006-04-06 23:23:56 +00002284/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2285/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002286bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2287 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002288
Evan Cheng5ced1d82006-04-06 23:23:56 +00002289 if (NumElems != 2 && NumElems != 4)
2290 return false;
2291
Evan Chengc5cdff22006-04-07 21:53:05 +00002292 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002293 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002294 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002295
Evan Chengc5cdff22006-04-07 21:53:05 +00002296 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002297 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002298 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002299
2300 return true;
2301}
2302
2303/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002304/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2305/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002306bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2307 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002308
Evan Cheng5ced1d82006-04-06 23:23:56 +00002309 if (NumElems != 2 && NumElems != 4)
2310 return false;
2311
Evan Chengc5cdff22006-04-07 21:53:05 +00002312 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002313 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002314 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002315
Nate Begeman9008ca62009-04-27 18:41:29 +00002316 for (unsigned i = 0; i < NumElems/2; ++i)
2317 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002318 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002319
2320 return true;
2321}
2322
Nate Begeman9008ca62009-04-27 18:41:29 +00002323/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2324/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2325/// <2, 3, 2, 3>
2326bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2327 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2328
2329 if (NumElems != 4)
2330 return false;
2331
2332 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2333 isUndefOrEqual(N->getMaskElt(1), 3) &&
2334 isUndefOrEqual(N->getMaskElt(2), 2) &&
2335 isUndefOrEqual(N->getMaskElt(3), 3);
2336}
2337
Evan Cheng0038e592006-03-28 00:39:58 +00002338/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2339/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002340static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002341 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002342 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002343 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002344 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002345
2346 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2347 int BitI = Mask[i];
2348 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002349 if (!isUndefOrEqual(BitI, j))
2350 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002351 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002352 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002353 return false;
2354 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002355 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002356 return false;
2357 }
Evan Cheng0038e592006-03-28 00:39:58 +00002358 }
Evan Cheng0038e592006-03-28 00:39:58 +00002359 return true;
2360}
2361
Nate Begeman9008ca62009-04-27 18:41:29 +00002362bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2363 SmallVector<int, 8> M;
2364 N->getMask(M);
2365 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002366}
2367
Evan Cheng4fcb9222006-03-28 02:43:26 +00002368/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2369/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002370static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002371 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002372 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002373 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002374 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002375
2376 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2377 int BitI = Mask[i];
2378 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002379 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002380 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002381 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002382 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002383 return false;
2384 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002385 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002386 return false;
2387 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002388 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002389 return true;
2390}
2391
Nate Begeman9008ca62009-04-27 18:41:29 +00002392bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2393 SmallVector<int, 8> M;
2394 N->getMask(M);
2395 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002396}
2397
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002398/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2399/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2400/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002401static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002402 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002403 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002404 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002405
2406 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2407 int BitI = Mask[i];
2408 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002409 if (!isUndefOrEqual(BitI, j))
2410 return false;
2411 if (!isUndefOrEqual(BitI1, j))
2412 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002413 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002414 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002415}
2416
Nate Begeman9008ca62009-04-27 18:41:29 +00002417bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2418 SmallVector<int, 8> M;
2419 N->getMask(M);
2420 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2421}
2422
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002423/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2424/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2425/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002426static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002427 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002428 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2429 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002430
2431 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2432 int BitI = Mask[i];
2433 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002434 if (!isUndefOrEqual(BitI, j))
2435 return false;
2436 if (!isUndefOrEqual(BitI1, j))
2437 return false;
2438 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002439 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002440}
2441
Nate Begeman9008ca62009-04-27 18:41:29 +00002442bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2443 SmallVector<int, 8> M;
2444 N->getMask(M);
2445 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2446}
2447
Evan Cheng017dcc62006-04-21 01:05:10 +00002448/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2449/// specifies a shuffle of elements that is suitable for input to MOVSS,
2450/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002451static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002452 int NumElts = VT.getVectorNumElements();
Evan Cheng10762102007-12-06 22:14:22 +00002453 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002454 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002455
2456 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002457 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002458
2459 for (int i = 1; i < NumElts; ++i)
2460 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002461 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002462
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002463 return true;
2464}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002465
Nate Begeman9008ca62009-04-27 18:41:29 +00002466bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2467 SmallVector<int, 8> M;
2468 N->getMask(M);
2469 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002470}
2471
Evan Cheng017dcc62006-04-21 01:05:10 +00002472/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2473/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002474/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002475static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002476 bool V2IsSplat = false, bool V2IsUndef = false) {
2477 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002478 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002479 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002480
2481 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002482 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002483
2484 for (int i = 1; i < NumOps; ++i)
2485 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2486 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2487 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002488 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002489
Evan Cheng39623da2006-04-20 08:58:49 +00002490 return true;
2491}
2492
Nate Begeman9008ca62009-04-27 18:41:29 +00002493static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002494 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002495 SmallVector<int, 8> M;
2496 N->getMask(M);
2497 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002498}
2499
Evan Chengd9539472006-04-14 21:59:03 +00002500/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2501/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002502bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2503 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002504 return false;
2505
2506 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002507 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002508 int Elt = N->getMaskElt(i);
2509 if (Elt >= 0 && Elt != 1)
2510 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002511 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002512
2513 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002514 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 int Elt = N->getMaskElt(i);
2516 if (Elt >= 0 && Elt != 3)
2517 return false;
2518 if (Elt == 3)
2519 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002520 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002521 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002522 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002523 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002524}
2525
2526/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2527/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002528bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2529 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002530 return false;
2531
2532 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002533 for (unsigned i = 0; i < 2; ++i)
2534 if (N->getMaskElt(i) > 0)
2535 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002536
2537 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002538 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 int Elt = N->getMaskElt(i);
2540 if (Elt >= 0 && Elt != 2)
2541 return false;
2542 if (Elt == 2)
2543 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002544 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002545 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002546 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002547}
2548
Evan Cheng0b457f02008-09-25 20:50:48 +00002549/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2550/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002551bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2552 int e = N->getValueType(0).getVectorNumElements() / 2;
2553
2554 for (int i = 0; i < e; ++i)
2555 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002556 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002557 for (int i = 0; i < e; ++i)
2558 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002559 return false;
2560 return true;
2561}
2562
Evan Cheng63d33002006-03-22 08:01:21 +00002563/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2564/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2565/// instructions.
2566unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002567 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2568 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2569
Evan Chengb9df0ca2006-03-22 02:53:00 +00002570 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2571 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002572 for (int i = 0; i < NumOperands; ++i) {
2573 int Val = SVOp->getMaskElt(NumOperands-i-1);
2574 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002575 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002576 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002577 if (i != NumOperands - 1)
2578 Mask <<= Shift;
2579 }
Evan Cheng63d33002006-03-22 08:01:21 +00002580 return Mask;
2581}
2582
Evan Cheng506d3df2006-03-29 23:07:14 +00002583/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2584/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2585/// instructions.
2586unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002587 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002588 unsigned Mask = 0;
2589 // 8 nodes, but we only care about the last 4.
2590 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002591 int Val = SVOp->getMaskElt(i);
2592 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002593 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002594 if (i != 4)
2595 Mask <<= 2;
2596 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002597 return Mask;
2598}
2599
2600/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2601/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2602/// instructions.
2603unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002604 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002605 unsigned Mask = 0;
2606 // 8 nodes, but we only care about the first 4.
2607 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 int Val = SVOp->getMaskElt(i);
2609 if (Val >= 0)
2610 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002611 if (i != 0)
2612 Mask <<= 2;
2613 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002614 return Mask;
2615}
2616
Nate Begeman9008ca62009-04-27 18:41:29 +00002617/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2618/// their permute mask.
2619static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2620 SelectionDAG &DAG) {
2621 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002622 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002623 SmallVector<int, 8> MaskVec;
2624
Nate Begeman5a5ca152009-04-29 05:20:52 +00002625 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002626 int idx = SVOp->getMaskElt(i);
2627 if (idx < 0)
2628 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002629 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002631 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002633 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002634 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2635 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002636}
2637
Evan Cheng779ccea2007-12-07 21:30:01 +00002638/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2639/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002640static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002641 unsigned NumElems = VT.getVectorNumElements();
2642 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002643 int idx = Mask[i];
2644 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002645 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002646 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002647 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002648 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002649 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002650 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002651}
2652
Evan Cheng533a0aa2006-04-19 20:35:22 +00002653/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2654/// match movhlps. The lower half elements should come from upper half of
2655/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002656/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002657static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2658 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002659 return false;
2660 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002661 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002662 return false;
2663 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002664 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002665 return false;
2666 return true;
2667}
2668
Evan Cheng5ced1d82006-04-06 23:23:56 +00002669/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002670/// is promoted to a vector. It also returns the LoadSDNode by reference if
2671/// required.
2672static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002673 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2674 return false;
2675 N = N->getOperand(0).getNode();
2676 if (!ISD::isNON_EXTLoad(N))
2677 return false;
2678 if (LD)
2679 *LD = cast<LoadSDNode>(N);
2680 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002681}
2682
Evan Cheng533a0aa2006-04-19 20:35:22 +00002683/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2684/// match movlp{s|d}. The lower half elements should come from lower half of
2685/// V1 (and in order), and the upper half elements should come from the upper
2686/// half of V2 (and in order). And since V1 will become the source of the
2687/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002688static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2689 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002690 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002691 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002692 // Is V2 is a vector load, don't do this transformation. We will try to use
2693 // load folding shufps op.
2694 if (ISD::isNON_EXTLoad(V2))
2695 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002696
Nate Begeman5a5ca152009-04-29 05:20:52 +00002697 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002698
Evan Cheng533a0aa2006-04-19 20:35:22 +00002699 if (NumElems != 2 && NumElems != 4)
2700 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002701 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002702 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002703 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002704 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002706 return false;
2707 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002708}
2709
Evan Cheng39623da2006-04-20 08:58:49 +00002710/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2711/// all the same.
2712static bool isSplatVector(SDNode *N) {
2713 if (N->getOpcode() != ISD::BUILD_VECTOR)
2714 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002715
Dan Gohman475871a2008-07-27 21:46:04 +00002716 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002717 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2718 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002719 return false;
2720 return true;
2721}
2722
Evan Cheng213d2cf2007-05-17 18:45:50 +00002723/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2724/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002725static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002726 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002727 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002728 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002729 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002730}
2731
2732/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002733/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002734/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002735static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002736 SDValue V1 = N->getOperand(0);
2737 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002738 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2739 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002740 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002741 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002742 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002743 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2744 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2746 return false;
2747 } else if (Idx >= 0) {
2748 unsigned Opc = V1.getOpcode();
2749 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2750 continue;
2751 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002752 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002753 }
2754 }
2755 return true;
2756}
2757
2758/// getZeroVector - Returns a vector of specified type with all zero elements.
2759///
Dale Johannesenace16102009-02-03 19:33:06 +00002760static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2761 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002763
Chris Lattner8a594482007-11-25 00:24:49 +00002764 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2765 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002766 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002767 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002768 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002769 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002770 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002771 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002772 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002773 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002774 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002775 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002776 }
Dale Johannesenace16102009-02-03 19:33:06 +00002777 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002778}
2779
Chris Lattner8a594482007-11-25 00:24:49 +00002780/// getOnesVector - Returns a vector of specified type with all bits set.
2781///
Dale Johannesenace16102009-02-03 19:33:06 +00002782static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002783 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002784
Chris Lattner8a594482007-11-25 00:24:49 +00002785 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2786 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002787 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2788 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002789 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002790 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002791 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002792 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002793 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002794}
2795
2796
Evan Cheng39623da2006-04-20 08:58:49 +00002797/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2798/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002799static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2800 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002801 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002802
Evan Cheng39623da2006-04-20 08:58:49 +00002803 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002804 SmallVector<int, 8> MaskVec;
2805 SVOp->getMask(MaskVec);
2806
Nate Begeman5a5ca152009-04-29 05:20:52 +00002807 for (unsigned i = 0; i != NumElems; ++i) {
2808 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002809 MaskVec[i] = NumElems;
2810 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002811 }
Evan Cheng39623da2006-04-20 08:58:49 +00002812 }
Evan Cheng39623da2006-04-20 08:58:49 +00002813 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002814 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2815 SVOp->getOperand(1), &MaskVec[0]);
2816 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002817}
2818
Evan Cheng017dcc62006-04-21 01:05:10 +00002819/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2820/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002821static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2822 SDValue V2) {
2823 unsigned NumElems = VT.getVectorNumElements();
2824 SmallVector<int, 8> Mask;
2825 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002826 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 Mask.push_back(i);
2828 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002829}
2830
Nate Begeman9008ca62009-04-27 18:41:29 +00002831/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2832static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2833 SDValue V2) {
2834 unsigned NumElems = VT.getVectorNumElements();
2835 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002836 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 Mask.push_back(i);
2838 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002839 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002840 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002841}
2842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2844static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2845 SDValue V2) {
2846 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002847 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002848 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002849 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002850 Mask.push_back(i + Half);
2851 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002852 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002853 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002854}
2855
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002856/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002857static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2858 bool HasSSE2) {
2859 if (SV->getValueType(0).getVectorNumElements() <= 4)
2860 return SDValue(SV, 0);
2861
2862 MVT PVT = MVT::v4f32;
2863 MVT VT = SV->getValueType(0);
2864 DebugLoc dl = SV->getDebugLoc();
2865 SDValue V1 = SV->getOperand(0);
2866 int NumElems = VT.getVectorNumElements();
2867 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002868
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 // unpack elements to the correct location
2870 while (NumElems > 4) {
2871 if (EltNo < NumElems/2) {
2872 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2873 } else {
2874 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2875 EltNo -= NumElems/2;
2876 }
2877 NumElems >>= 1;
2878 }
2879
2880 // Perform the splat.
2881 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002882 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2884 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002885}
2886
Evan Chengba05f722006-04-21 23:03:30 +00002887/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002888/// vector of zero or undef vector. This produces a shuffle where the low
2889/// element of V2 is swizzled into the zero/undef vector, landing at element
2890/// 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 +00002891static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002892 bool isZero, bool HasSSE2,
2893 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002894 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002895 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002896 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2897 unsigned NumElems = VT.getVectorNumElements();
2898 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002899 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002900 // If this is the insertion idx, put the low elt of V2 here.
2901 MaskVec.push_back(i == Idx ? NumElems : i);
2902 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002903}
2904
Evan Chengf26ffe92008-05-29 08:22:04 +00002905/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2906/// a shuffle that is zero.
2907static
Nate Begeman9008ca62009-04-27 18:41:29 +00002908unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2909 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002910 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002911 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002912 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 int Idx = SVOp->getMaskElt(Index);
2914 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002915 ++NumZeros;
2916 continue;
2917 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002918 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002919 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002920 ++NumZeros;
2921 else
2922 break;
2923 }
2924 return NumZeros;
2925}
2926
2927/// isVectorShift - Returns true if the shuffle can be implemented as a
2928/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002929/// FIXME: split into pslldqi, psrldqi, palignr variants.
2930static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002931 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002932 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002933
2934 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002936 if (!NumZeros) {
2937 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002939 if (!NumZeros)
2940 return false;
2941 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002942 bool SeenV1 = false;
2943 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 for (int i = NumZeros; i < NumElems; ++i) {
2945 int Val = isLeft ? (i - NumZeros) : i;
2946 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2947 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002948 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002950 SeenV1 = true;
2951 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002953 SeenV2 = true;
2954 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002956 return false;
2957 }
2958 if (SeenV1 && SeenV2)
2959 return false;
2960
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002962 ShAmt = NumZeros;
2963 return true;
2964}
2965
2966
Evan Chengc78d3b42006-04-24 18:01:45 +00002967/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2968///
Dan Gohman475871a2008-07-27 21:46:04 +00002969static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002970 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002971 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002972 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002973 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002974
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002975 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002976 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002977 bool First = true;
2978 for (unsigned i = 0; i < 16; ++i) {
2979 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2980 if (ThisIsNonZero && First) {
2981 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002982 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002983 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002984 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002985 First = false;
2986 }
2987
2988 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002989 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002990 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2991 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002992 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002993 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002994 }
2995 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002996 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2997 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002998 ThisElt, DAG.getConstant(8, MVT::i8));
2999 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003000 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003001 } else
3002 ThisElt = LastElt;
3003
Gabor Greifba36cb52008-08-28 21:40:38 +00003004 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003005 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003006 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003007 }
3008 }
3009
Dale Johannesenace16102009-02-03 19:33:06 +00003010 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003011}
3012
Bill Wendlinga348c562007-03-22 18:42:45 +00003013/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003014///
Dan Gohman475871a2008-07-27 21:46:04 +00003015static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003016 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003017 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003018 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003019 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003020
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003021 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003022 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003023 bool First = true;
3024 for (unsigned i = 0; i < 8; ++i) {
3025 bool isNonZero = (NonZeros & (1 << i)) != 0;
3026 if (isNonZero) {
3027 if (First) {
3028 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003029 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003030 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003031 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003032 First = false;
3033 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003034 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003035 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003036 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003037 }
3038 }
3039
3040 return V;
3041}
3042
Evan Chengf26ffe92008-05-29 08:22:04 +00003043/// getVShift - Return a vector logical shift node.
3044///
Dan Gohman475871a2008-07-27 21:46:04 +00003045static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 unsigned NumBits, SelectionDAG &DAG,
3047 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003048 bool isMMX = VT.getSizeInBits() == 64;
3049 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003050 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003051 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3052 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3053 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003054 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003055}
3056
Dan Gohman475871a2008-07-27 21:46:04 +00003057SDValue
3058X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003059 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003060 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003061 if (ISD::isBuildVectorAllZeros(Op.getNode())
3062 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003063 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3064 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3065 // eliminated on x86-32 hosts.
3066 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3067 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003068
Gabor Greifba36cb52008-08-28 21:40:38 +00003069 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003070 return getOnesVector(Op.getValueType(), DAG, dl);
3071 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003072 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003073
Duncan Sands83ec4b62008-06-06 12:08:01 +00003074 MVT VT = Op.getValueType();
3075 MVT EVT = VT.getVectorElementType();
3076 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003077
3078 unsigned NumElems = Op.getNumOperands();
3079 unsigned NumZero = 0;
3080 unsigned NumNonZero = 0;
3081 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003082 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003083 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003084 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003085 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003086 if (Elt.getOpcode() == ISD::UNDEF)
3087 continue;
3088 Values.insert(Elt);
3089 if (Elt.getOpcode() != ISD::Constant &&
3090 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003091 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003092 if (isZeroNode(Elt))
3093 NumZero++;
3094 else {
3095 NonZeros |= (1 << i);
3096 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003097 }
3098 }
3099
Dan Gohman7f321562007-06-25 16:23:39 +00003100 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003101 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003102 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003103 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003104
Chris Lattner67f453a2008-03-09 05:42:06 +00003105 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003106 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003107 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003109
Chris Lattner62098042008-03-09 01:05:04 +00003110 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3111 // the value are obviously zero, truncate the value to i32 and do the
3112 // insertion that way. Only do this if the value is non-constant or if the
3113 // value is a constant being inserted into element 0. It is cheaper to do
3114 // a constant pool load than it is to do a movd + shuffle.
3115 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3116 (!IsAllConstants || Idx == 0)) {
3117 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3118 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003119 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3120 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003121
Chris Lattner62098042008-03-09 01:05:04 +00003122 // Truncate the value (which may itself be a constant) to i32, and
3123 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003124 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3125 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003126 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3127 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003128
Chris Lattner62098042008-03-09 01:05:04 +00003129 // Now we have our 32-bit value zero extended in the low element of
3130 // a vector. If Idx != 0, swizzle it into place.
3131 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 SmallVector<int, 4> Mask;
3133 Mask.push_back(Idx);
3134 for (unsigned i = 1; i != VecElts; ++i)
3135 Mask.push_back(i);
3136 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3137 DAG.getUNDEF(Item.getValueType()),
3138 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003139 }
Dale Johannesenace16102009-02-03 19:33:06 +00003140 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003141 }
3142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003143
Chris Lattner19f79692008-03-08 22:59:52 +00003144 // If we have a constant or non-constant insertion into the low element of
3145 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3146 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3147 // depending on what the source datatype is. Because we can only get here
3148 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3149 if (Idx == 0 &&
3150 // Don't do this for i64 values on x86-32.
3151 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003152 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003153 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003154 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3155 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003156 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003157
3158 // Is it a vector logical left shift?
3159 if (NumElems == 2 && Idx == 1 &&
3160 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003161 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003162 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003163 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003164 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003165 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003166 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003167
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003168 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003169 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003170
Chris Lattner19f79692008-03-08 22:59:52 +00003171 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3172 // is a non-constant being inserted into an element other than the low one,
3173 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3174 // movd/movss) to move this into the low element, then shuffle it into
3175 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003176 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003177 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003178
Evan Cheng0db9fe62006-04-25 20:13:52 +00003179 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003180 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3181 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003183 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 MaskVec.push_back(i == Idx ? 0 : 1);
3185 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003186 }
3187 }
3188
Chris Lattner67f453a2008-03-09 05:42:06 +00003189 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3190 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003191 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003192
Dan Gohmana3941172007-07-24 22:55:08 +00003193 // A vector full of immediates; various special cases are already
3194 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003195 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003196 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003197
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003198 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003199 if (EVTBits == 64) {
3200 if (NumNonZero == 1) {
3201 // One half is zero or undef.
3202 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003203 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003204 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003205 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3206 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003207 }
Dan Gohman475871a2008-07-27 21:46:04 +00003208 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003209 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003210
3211 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003212 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003213 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003214 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003215 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003216 }
3217
Bill Wendling826f36f2007-03-28 00:57:11 +00003218 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003219 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003220 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003221 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003222 }
3223
3224 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003225 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003226 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003227 if (NumElems == 4 && NumZero > 0) {
3228 for (unsigned i = 0; i < 4; ++i) {
3229 bool isZero = !(NonZeros & (1 << i));
3230 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003231 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003232 else
Dale Johannesenace16102009-02-03 19:33:06 +00003233 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003234 }
3235
3236 for (unsigned i = 0; i < 2; ++i) {
3237 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3238 default: break;
3239 case 0:
3240 V[i] = V[i*2]; // Must be a zero vector.
3241 break;
3242 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003244 break;
3245 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003247 break;
3248 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003250 break;
3251 }
3252 }
3253
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003255 bool Reverse = (NonZeros & 0x3) == 2;
3256 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003258 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3259 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3261 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003262 }
3263
3264 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003265 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3266 // values to be inserted is equal to the number of elements, in which case
3267 // use the unpack code below in the hopes of matching the consecutive elts
3268 // load merge pattern for shuffles.
3269 // FIXME: We could probably just check that here directly.
3270 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3271 getSubtarget()->hasSSE41()) {
3272 V[0] = DAG.getUNDEF(VT);
3273 for (unsigned i = 0; i < NumElems; ++i)
3274 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3275 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3276 Op.getOperand(i), DAG.getIntPtrConstant(i));
3277 return V[0];
3278 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003279 // Expand into a number of unpckl*.
3280 // e.g. for v4f32
3281 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3282 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3283 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003284 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003285 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003286 NumElems >>= 1;
3287 while (NumElems != 0) {
3288 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003290 NumElems >>= 1;
3291 }
3292 return V[0];
3293 }
3294
Dan Gohman475871a2008-07-27 21:46:04 +00003295 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003296}
3297
Nate Begemanb9a47b82009-02-23 08:49:38 +00003298// v8i16 shuffles - Prefer shuffles in the following order:
3299// 1. [all] pshuflw, pshufhw, optional move
3300// 2. [ssse3] 1 x pshufb
3301// 3. [ssse3] 2 x pshufb + 1 x por
3302// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003303static
Nate Begeman9008ca62009-04-27 18:41:29 +00003304SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3305 SelectionDAG &DAG, X86TargetLowering &TLI) {
3306 SDValue V1 = SVOp->getOperand(0);
3307 SDValue V2 = SVOp->getOperand(1);
3308 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003309 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003310
Nate Begemanb9a47b82009-02-23 08:49:38 +00003311 // Determine if more than 1 of the words in each of the low and high quadwords
3312 // of the result come from the same quadword of one of the two inputs. Undef
3313 // mask values count as coming from any quadword, for better codegen.
3314 SmallVector<unsigned, 4> LoQuad(4);
3315 SmallVector<unsigned, 4> HiQuad(4);
3316 BitVector InputQuads(4);
3317 for (unsigned i = 0; i < 8; ++i) {
3318 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003319 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003320 MaskVals.push_back(EltIdx);
3321 if (EltIdx < 0) {
3322 ++Quad[0];
3323 ++Quad[1];
3324 ++Quad[2];
3325 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003326 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003327 }
3328 ++Quad[EltIdx / 4];
3329 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003330 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003331
Nate Begemanb9a47b82009-02-23 08:49:38 +00003332 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003333 unsigned MaxQuad = 1;
3334 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003335 if (LoQuad[i] > MaxQuad) {
3336 BestLoQuad = i;
3337 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003338 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003339 }
3340
Nate Begemanb9a47b82009-02-23 08:49:38 +00003341 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003342 MaxQuad = 1;
3343 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003344 if (HiQuad[i] > MaxQuad) {
3345 BestHiQuad = i;
3346 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003347 }
3348 }
3349
Nate Begemanb9a47b82009-02-23 08:49:38 +00003350 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3351 // of the two input vectors, shuffle them into one input vector so only a
3352 // single pshufb instruction is necessary. If There are more than 2 input
3353 // quads, disable the next transformation since it does not help SSSE3.
3354 bool V1Used = InputQuads[0] || InputQuads[1];
3355 bool V2Used = InputQuads[2] || InputQuads[3];
3356 if (TLI.getSubtarget()->hasSSSE3()) {
3357 if (InputQuads.count() == 2 && V1Used && V2Used) {
3358 BestLoQuad = InputQuads.find_first();
3359 BestHiQuad = InputQuads.find_next(BestLoQuad);
3360 }
3361 if (InputQuads.count() > 2) {
3362 BestLoQuad = -1;
3363 BestHiQuad = -1;
3364 }
3365 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003366
Nate Begemanb9a47b82009-02-23 08:49:38 +00003367 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3368 // the shuffle mask. If a quad is scored as -1, that means that it contains
3369 // words from all 4 input quadwords.
3370 SDValue NewV;
3371 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 SmallVector<int, 8> MaskV;
3373 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3374 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3375 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3376 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3377 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003378 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003379
Nate Begemanb9a47b82009-02-23 08:49:38 +00003380 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3381 // source words for the shuffle, to aid later transformations.
3382 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003383 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003384 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003385 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003386 if (idx != (int)i)
3387 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003388 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003389 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003390 AllWordsInNewV = false;
3391 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003392 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003393
Nate Begemanb9a47b82009-02-23 08:49:38 +00003394 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3395 if (AllWordsInNewV) {
3396 for (int i = 0; i != 8; ++i) {
3397 int idx = MaskVals[i];
3398 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003399 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003400 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3401 if ((idx != i) && idx < 4)
3402 pshufhw = false;
3403 if ((idx != i) && idx > 3)
3404 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003405 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003406 V1 = NewV;
3407 V2Used = false;
3408 BestLoQuad = 0;
3409 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003410 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003411
Nate Begemanb9a47b82009-02-23 08:49:38 +00003412 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3413 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003414 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3416 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003417 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003418 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003419
3420 // If we have SSSE3, and all words of the result are from 1 input vector,
3421 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3422 // is present, fall back to case 4.
3423 if (TLI.getSubtarget()->hasSSSE3()) {
3424 SmallVector<SDValue,16> pshufbMask;
3425
3426 // If we have elements from both input vectors, set the high bit of the
3427 // shuffle mask element to zero out elements that come from V2 in the V1
3428 // mask, and elements that come from V1 in the V2 mask, so that the two
3429 // results can be OR'd together.
3430 bool TwoInputs = V1Used && V2Used;
3431 for (unsigned i = 0; i != 8; ++i) {
3432 int EltIdx = MaskVals[i] * 2;
3433 if (TwoInputs && (EltIdx >= 16)) {
3434 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3435 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3436 continue;
3437 }
3438 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3439 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3440 }
3441 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3442 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003443 DAG.getNode(ISD::BUILD_VECTOR, dl,
3444 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003445 if (!TwoInputs)
3446 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3447
3448 // Calculate the shuffle mask for the second input, shuffle it, and
3449 // OR it with the first shuffled input.
3450 pshufbMask.clear();
3451 for (unsigned i = 0; i != 8; ++i) {
3452 int EltIdx = MaskVals[i] * 2;
3453 if (EltIdx < 16) {
3454 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3455 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3456 continue;
3457 }
3458 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3459 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3460 }
3461 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3462 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003463 DAG.getNode(ISD::BUILD_VECTOR, dl,
3464 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003465 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3466 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3467 }
3468
3469 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3470 // and update MaskVals with new element order.
3471 BitVector InOrder(8);
3472 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003474 for (int i = 0; i != 4; ++i) {
3475 int idx = MaskVals[i];
3476 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003478 InOrder.set(i);
3479 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003481 InOrder.set(i);
3482 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003484 }
3485 }
3486 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003487 MaskV.push_back(i);
3488 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3489 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003490 }
3491
3492 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3493 // and update MaskVals with the new element order.
3494 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003496 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003498 for (unsigned i = 4; i != 8; ++i) {
3499 int idx = MaskVals[i];
3500 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003501 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003502 InOrder.set(i);
3503 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003504 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003505 InOrder.set(i);
3506 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003507 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003508 }
3509 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003510 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3511 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003512 }
3513
3514 // In case BestHi & BestLo were both -1, which means each quadword has a word
3515 // from each of the four input quadwords, calculate the InOrder bitvector now
3516 // before falling through to the insert/extract cleanup.
3517 if (BestLoQuad == -1 && BestHiQuad == -1) {
3518 NewV = V1;
3519 for (int i = 0; i != 8; ++i)
3520 if (MaskVals[i] < 0 || MaskVals[i] == i)
3521 InOrder.set(i);
3522 }
3523
3524 // The other elements are put in the right place using pextrw and pinsrw.
3525 for (unsigned i = 0; i != 8; ++i) {
3526 if (InOrder[i])
3527 continue;
3528 int EltIdx = MaskVals[i];
3529 if (EltIdx < 0)
3530 continue;
3531 SDValue ExtOp = (EltIdx < 8)
3532 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3533 DAG.getIntPtrConstant(EltIdx))
3534 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3535 DAG.getIntPtrConstant(EltIdx - 8));
3536 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3537 DAG.getIntPtrConstant(i));
3538 }
3539 return NewV;
3540}
3541
3542// v16i8 shuffles - Prefer shuffles in the following order:
3543// 1. [ssse3] 1 x pshufb
3544// 2. [ssse3] 2 x pshufb + 1 x por
3545// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3546static
Nate Begeman9008ca62009-04-27 18:41:29 +00003547SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3548 SelectionDAG &DAG, X86TargetLowering &TLI) {
3549 SDValue V1 = SVOp->getOperand(0);
3550 SDValue V2 = SVOp->getOperand(1);
3551 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003552 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003553 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003554
3555 // If we have SSSE3, case 1 is generated when all result bytes come from
3556 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3557 // present, fall back to case 3.
3558 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3559 bool V1Only = true;
3560 bool V2Only = true;
3561 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003563 if (EltIdx < 0)
3564 continue;
3565 if (EltIdx < 16)
3566 V2Only = false;
3567 else
3568 V1Only = false;
3569 }
3570
3571 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3572 if (TLI.getSubtarget()->hasSSSE3()) {
3573 SmallVector<SDValue,16> pshufbMask;
3574
3575 // If all result elements are from one input vector, then only translate
3576 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3577 //
3578 // Otherwise, we have elements from both input vectors, and must zero out
3579 // elements that come from V2 in the first mask, and V1 in the second mask
3580 // so that we can OR them together.
3581 bool TwoInputs = !(V1Only || V2Only);
3582 for (unsigned i = 0; i != 16; ++i) {
3583 int EltIdx = MaskVals[i];
3584 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3585 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3586 continue;
3587 }
3588 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3589 }
3590 // If all the elements are from V2, assign it to V1 and return after
3591 // building the first pshufb.
3592 if (V2Only)
3593 V1 = V2;
3594 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003595 DAG.getNode(ISD::BUILD_VECTOR, dl,
3596 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003597 if (!TwoInputs)
3598 return V1;
3599
3600 // Calculate the shuffle mask for the second input, shuffle it, and
3601 // OR it with the first shuffled input.
3602 pshufbMask.clear();
3603 for (unsigned i = 0; i != 16; ++i) {
3604 int EltIdx = MaskVals[i];
3605 if (EltIdx < 16) {
3606 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3607 continue;
3608 }
3609 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3610 }
3611 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003612 DAG.getNode(ISD::BUILD_VECTOR, dl,
3613 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003614 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3615 }
3616
3617 // No SSSE3 - Calculate in place words and then fix all out of place words
3618 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3619 // the 16 different words that comprise the two doublequadword input vectors.
3620 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3621 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3622 SDValue NewV = V2Only ? V2 : V1;
3623 for (int i = 0; i != 8; ++i) {
3624 int Elt0 = MaskVals[i*2];
3625 int Elt1 = MaskVals[i*2+1];
3626
3627 // This word of the result is all undef, skip it.
3628 if (Elt0 < 0 && Elt1 < 0)
3629 continue;
3630
3631 // This word of the result is already in the correct place, skip it.
3632 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3633 continue;
3634 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3635 continue;
3636
3637 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3638 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3639 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003640
3641 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3642 // using a single extract together, load it and store it.
3643 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3644 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3645 DAG.getIntPtrConstant(Elt1 / 2));
3646 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3647 DAG.getIntPtrConstant(i));
3648 continue;
3649 }
3650
Nate Begemanb9a47b82009-02-23 08:49:38 +00003651 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003652 // source byte is not also odd, shift the extracted word left 8 bits
3653 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003654 if (Elt1 >= 0) {
3655 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3656 DAG.getIntPtrConstant(Elt1 / 2));
3657 if ((Elt1 & 1) == 0)
3658 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3659 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003660 else if (Elt0 >= 0)
3661 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3662 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003663 }
3664 // If Elt0 is defined, extract it from the appropriate source. If the
3665 // source byte is not also even, shift the extracted word right 8 bits. If
3666 // Elt1 was also defined, OR the extracted values together before
3667 // inserting them in the result.
3668 if (Elt0 >= 0) {
3669 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3670 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3671 if ((Elt0 & 1) != 0)
3672 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3673 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003674 else if (Elt1 >= 0)
3675 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3676 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003677 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3678 : InsElt0;
3679 }
3680 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3681 DAG.getIntPtrConstant(i));
3682 }
3683 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003684}
3685
Evan Cheng7a831ce2007-12-15 03:00:47 +00003686/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3687/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3688/// done when every pair / quad of shuffle mask elements point to elements in
3689/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003690/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3691static
Nate Begeman9008ca62009-04-27 18:41:29 +00003692SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3693 SelectionDAG &DAG,
3694 TargetLowering &TLI, DebugLoc dl) {
3695 MVT VT = SVOp->getValueType(0);
3696 SDValue V1 = SVOp->getOperand(0);
3697 SDValue V2 = SVOp->getOperand(1);
3698 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003699 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003700 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003701 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003702 MVT NewVT = MaskVT;
3703 switch (VT.getSimpleVT()) {
3704 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003705 case MVT::v4f32: NewVT = MVT::v2f64; break;
3706 case MVT::v4i32: NewVT = MVT::v2i64; break;
3707 case MVT::v8i16: NewVT = MVT::v4i32; break;
3708 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003709 }
3710
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003711 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003713 NewVT = MVT::v2i64;
3714 else
3715 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003716 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 int Scale = NumElems / NewWidth;
3718 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003719 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003720 int StartIdx = -1;
3721 for (int j = 0; j < Scale; ++j) {
3722 int EltIdx = SVOp->getMaskElt(i+j);
3723 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003724 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003726 StartIdx = EltIdx - (EltIdx % Scale);
3727 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003728 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003729 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003730 if (StartIdx == -1)
3731 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003732 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003733 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003734 }
3735
Dale Johannesenace16102009-02-03 19:33:06 +00003736 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3737 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003738 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003739}
3740
Evan Chengd880b972008-05-09 21:53:03 +00003741/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003742///
Dan Gohman475871a2008-07-27 21:46:04 +00003743static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003744 SDValue SrcOp, SelectionDAG &DAG,
3745 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003746 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3747 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003748 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003749 LD = dyn_cast<LoadSDNode>(SrcOp);
3750 if (!LD) {
3751 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3752 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003753 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003754 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3755 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3756 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3757 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3758 // PR2108
3759 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003760 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3761 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3762 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3763 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003764 SrcOp.getOperand(0)
3765 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003766 }
3767 }
3768 }
3769
Dale Johannesenace16102009-02-03 19:33:06 +00003770 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3771 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003772 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003773 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003774}
3775
Evan Chengace3c172008-07-22 21:13:36 +00003776/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3777/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003778static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003779LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3780 SDValue V1 = SVOp->getOperand(0);
3781 SDValue V2 = SVOp->getOperand(1);
3782 DebugLoc dl = SVOp->getDebugLoc();
3783 MVT VT = SVOp->getValueType(0);
3784
Evan Chengace3c172008-07-22 21:13:36 +00003785 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003786 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003787 SmallVector<int, 8> Mask1(4U, -1);
3788 SmallVector<int, 8> PermMask;
3789 SVOp->getMask(PermMask);
3790
Evan Chengace3c172008-07-22 21:13:36 +00003791 unsigned NumHi = 0;
3792 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003793 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003794 int Idx = PermMask[i];
3795 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003796 Locs[i] = std::make_pair(-1, -1);
3797 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003798 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3799 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003800 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003801 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003802 NumLo++;
3803 } else {
3804 Locs[i] = std::make_pair(1, NumHi);
3805 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003807 NumHi++;
3808 }
3809 }
3810 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003811
Evan Chengace3c172008-07-22 21:13:36 +00003812 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003813 // If no more than two elements come from either vector. This can be
3814 // implemented with two shuffles. First shuffle gather the elements.
3815 // The second shuffle, which takes the first shuffle as both of its
3816 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003818
Nate Begeman9008ca62009-04-27 18:41:29 +00003819 SmallVector<int, 8> Mask2(4U, -1);
3820
Evan Chengace3c172008-07-22 21:13:36 +00003821 for (unsigned i = 0; i != 4; ++i) {
3822 if (Locs[i].first == -1)
3823 continue;
3824 else {
3825 unsigned Idx = (i < 2) ? 0 : 4;
3826 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003828 }
3829 }
3830
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003832 } else if (NumLo == 3 || NumHi == 3) {
3833 // Otherwise, we must have three elements from one vector, call it X, and
3834 // one element from the other, call it Y. First, use a shufps to build an
3835 // intermediate vector with the one element from Y and the element from X
3836 // that will be in the same half in the final destination (the indexes don't
3837 // matter). Then, use a shufps to build the final vector, taking the half
3838 // containing the element from Y from the intermediate, and the other half
3839 // from X.
3840 if (NumHi == 3) {
3841 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003842 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003843 std::swap(V1, V2);
3844 }
3845
3846 // Find the element from V2.
3847 unsigned HiIndex;
3848 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 int Val = PermMask[HiIndex];
3850 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003851 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003852 if (Val >= 4)
3853 break;
3854 }
3855
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 Mask1[0] = PermMask[HiIndex];
3857 Mask1[1] = -1;
3858 Mask1[2] = PermMask[HiIndex^1];
3859 Mask1[3] = -1;
3860 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003861
3862 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003863 Mask1[0] = PermMask[0];
3864 Mask1[1] = PermMask[1];
3865 Mask1[2] = HiIndex & 1 ? 6 : 4;
3866 Mask1[3] = HiIndex & 1 ? 4 : 6;
3867 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003868 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 Mask1[0] = HiIndex & 1 ? 2 : 0;
3870 Mask1[1] = HiIndex & 1 ? 0 : 2;
3871 Mask1[2] = PermMask[2];
3872 Mask1[3] = PermMask[3];
3873 if (Mask1[2] >= 0)
3874 Mask1[2] += 4;
3875 if (Mask1[3] >= 0)
3876 Mask1[3] += 4;
3877 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003878 }
Evan Chengace3c172008-07-22 21:13:36 +00003879 }
3880
3881 // Break it into (shuffle shuffle_hi, shuffle_lo).
3882 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003883 SmallVector<int,8> LoMask(4U, -1);
3884 SmallVector<int,8> HiMask(4U, -1);
3885
3886 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003887 unsigned MaskIdx = 0;
3888 unsigned LoIdx = 0;
3889 unsigned HiIdx = 2;
3890 for (unsigned i = 0; i != 4; ++i) {
3891 if (i == 2) {
3892 MaskPtr = &HiMask;
3893 MaskIdx = 1;
3894 LoIdx = 0;
3895 HiIdx = 2;
3896 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 int Idx = PermMask[i];
3898 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003899 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003901 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003903 LoIdx++;
3904 } else {
3905 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003907 HiIdx++;
3908 }
3909 }
3910
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3912 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3913 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003914 for (unsigned i = 0; i != 4; ++i) {
3915 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003917 } else {
3918 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003920 }
3921 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003923}
3924
Dan Gohman475871a2008-07-27 21:46:04 +00003925SDValue
3926X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003928 SDValue V1 = Op.getOperand(0);
3929 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003930 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003931 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003933 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3935 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003936 bool V1IsSplat = false;
3937 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003938
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003940 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003941
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 // Promote splats to v4f32.
3943 if (SVOp->isSplat()) {
3944 if (isMMX || NumElems < 4)
3945 return Op;
3946 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 }
3948
Evan Cheng7a831ce2007-12-15 03:00:47 +00003949 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3950 // do it!
3951 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003953 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003954 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003955 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003956 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3957 // FIXME: Figure out a cleaner way to do this.
3958 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003959 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003961 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3963 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3964 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003965 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003966 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003967 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3968 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003969 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003970 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003971 }
3972 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003973
3974 if (X86::isPSHUFDMask(SVOp))
3975 return Op;
3976
Evan Chengf26ffe92008-05-29 08:22:04 +00003977 // Check if this can be converted into a logical shift.
3978 bool isLeft = false;
3979 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003980 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 bool isShift = getSubtarget()->hasSSE2() &&
3982 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003983 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003984 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003985 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003986 MVT EVT = VT.getVectorElementType();
3987 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003988 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003989 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003990
3991 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003992 if (V1IsUndef)
3993 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003994 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003995 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003996 if (!isMMX)
3997 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003998 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003999
4000 // FIXME: fold these into legal mask.
4001 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4002 X86::isMOVSLDUPMask(SVOp) ||
4003 X86::isMOVHLPSMask(SVOp) ||
4004 X86::isMOVHPMask(SVOp) ||
4005 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004006 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004007
Nate Begeman9008ca62009-04-27 18:41:29 +00004008 if (ShouldXformToMOVHLPS(SVOp) ||
4009 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4010 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004011
Evan Chengf26ffe92008-05-29 08:22:04 +00004012 if (isShift) {
4013 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004014 MVT EVT = VT.getVectorElementType();
4015 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004016 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004017 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004018
Evan Cheng9eca5e82006-10-25 21:49:50 +00004019 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004020 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4021 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004022 V1IsSplat = isSplatVector(V1.getNode());
4023 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004024
Chris Lattner8a594482007-11-25 00:24:49 +00004025 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004026 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004027 Op = CommuteVectorShuffle(SVOp, DAG);
4028 SVOp = cast<ShuffleVectorSDNode>(Op);
4029 V1 = SVOp->getOperand(0);
4030 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004031 std::swap(V1IsSplat, V2IsSplat);
4032 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004033 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004034 }
4035
Nate Begeman9008ca62009-04-27 18:41:29 +00004036 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4037 // Shuffling low element of v1 into undef, just return v1.
4038 if (V2IsUndef)
4039 return V1;
4040 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4041 // the instruction selector will not match, so get a canonical MOVL with
4042 // swapped operands to undo the commute.
4043 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004044 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004045
Nate Begeman9008ca62009-04-27 18:41:29 +00004046 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4047 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4048 X86::isUNPCKLMask(SVOp) ||
4049 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004050 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004051
Evan Cheng9bbbb982006-10-25 20:48:19 +00004052 if (V2IsSplat) {
4053 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004054 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004055 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 SDValue NewMask = NormalizeMask(SVOp, DAG);
4057 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4058 if (NSVOp != SVOp) {
4059 if (X86::isUNPCKLMask(NSVOp, true)) {
4060 return NewMask;
4061 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4062 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063 }
4064 }
4065 }
4066
Evan Cheng9eca5e82006-10-25 21:49:50 +00004067 if (Commuted) {
4068 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004069 // FIXME: this seems wrong.
4070 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4071 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4072 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4073 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4074 X86::isUNPCKLMask(NewSVOp) ||
4075 X86::isUNPCKHMask(NewSVOp))
4076 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004077 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004078
Nate Begemanb9a47b82009-02-23 08:49:38 +00004079 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004080
4081 // Normalize the node to match x86 shuffle ops if needed
4082 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4083 return CommuteVectorShuffle(SVOp, DAG);
4084
4085 // Check for legal shuffle and return?
4086 SmallVector<int, 16> PermMask;
4087 SVOp->getMask(PermMask);
4088 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004089 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004090
Evan Cheng14b32e12007-12-11 01:46:18 +00004091 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4092 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004093 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004094 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004095 return NewOp;
4096 }
4097
Nate Begemanb9a47b82009-02-23 08:49:38 +00004098 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004099 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004100 if (NewOp.getNode())
4101 return NewOp;
4102 }
4103
Evan Chengace3c172008-07-22 21:13:36 +00004104 // Handle all 4 wide cases with a number of shuffles except for MMX.
4105 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004107
Dan Gohman475871a2008-07-27 21:46:04 +00004108 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004109}
4110
Dan Gohman475871a2008-07-27 21:46:04 +00004111SDValue
4112X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004113 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004114 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004115 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004116 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004117 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004118 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004119 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004120 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004121 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004122 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004123 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4124 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4125 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004126 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4127 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4128 DAG.getNode(ISD::BIT_CONVERT, dl,
4129 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004130 Op.getOperand(0)),
4131 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004132 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004133 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004134 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004135 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004136 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004137 } else if (VT == MVT::f32) {
4138 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4139 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004140 // result has a single use which is a store or a bitcast to i32. And in
4141 // the case of a store, it's not worth it if the index is a constant 0,
4142 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004143 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004144 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004145 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004146 if ((User->getOpcode() != ISD::STORE ||
4147 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4148 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004149 (User->getOpcode() != ISD::BIT_CONVERT ||
4150 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004151 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004152 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004153 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004154 Op.getOperand(0)),
4155 Op.getOperand(1));
4156 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004157 } else if (VT == MVT::i32) {
4158 // ExtractPS works with constant index.
4159 if (isa<ConstantSDNode>(Op.getOperand(1)))
4160 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004161 }
Dan Gohman475871a2008-07-27 21:46:04 +00004162 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004163}
4164
4165
Dan Gohman475871a2008-07-27 21:46:04 +00004166SDValue
4167X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004168 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004169 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170
Evan Cheng62a3f152008-03-24 21:52:23 +00004171 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004172 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004173 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004174 return Res;
4175 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004176
Duncan Sands83ec4b62008-06-06 12:08:01 +00004177 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004178 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004179 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004180 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004181 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004182 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004183 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004184 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4185 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004186 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004187 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004188 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004189 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004190 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004191 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004192 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004193 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004194 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004195 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004196 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004197 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004198 if (Idx == 0)
4199 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004200
Evan Cheng0db9fe62006-04-25 20:13:52 +00004201 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 int Mask[4] = { Idx, -1, -1, -1 };
4203 MVT VVT = Op.getOperand(0).getValueType();
4204 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4205 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004206 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004207 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004208 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004209 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4210 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4211 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004212 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004213 if (Idx == 0)
4214 return Op;
4215
4216 // UNPCKHPD the element to the lowest double word, then movsd.
4217 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4218 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004219 int Mask[2] = { 1, -1 };
4220 MVT VVT = Op.getOperand(0).getValueType();
4221 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4222 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004223 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004224 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004225 }
4226
Dan Gohman475871a2008-07-27 21:46:04 +00004227 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004228}
4229
Dan Gohman475871a2008-07-27 21:46:04 +00004230SDValue
4231X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004232 MVT VT = Op.getValueType();
4233 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004234 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004235
Dan Gohman475871a2008-07-27 21:46:04 +00004236 SDValue N0 = Op.getOperand(0);
4237 SDValue N1 = Op.getOperand(1);
4238 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004239
Dan Gohmanef521f12008-08-14 22:53:18 +00004240 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4241 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004242 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004244 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4245 // argument.
4246 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004247 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004248 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004249 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004250 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004251 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004252 // Bits [7:6] of the constant are the source select. This will always be
4253 // zero here. The DAG Combiner may combine an extract_elt index into these
4254 // bits. For example (insert (extract, 3), 2) could be matched by putting
4255 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004256 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004257 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004258 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004259 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004260 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004261 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004262 } else if (EVT == MVT::i32) {
4263 // InsertPS works with constant index.
4264 if (isa<ConstantSDNode>(N2))
4265 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004266 }
Dan Gohman475871a2008-07-27 21:46:04 +00004267 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004268}
4269
Dan Gohman475871a2008-07-27 21:46:04 +00004270SDValue
4271X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004272 MVT VT = Op.getValueType();
4273 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004274
4275 if (Subtarget->hasSSE41())
4276 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4277
Evan Cheng794405e2007-12-12 07:55:34 +00004278 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004279 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004280
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004281 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004282 SDValue N0 = Op.getOperand(0);
4283 SDValue N1 = Op.getOperand(1);
4284 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004285
Duncan Sands83ec4b62008-06-06 12:08:01 +00004286 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004287 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4288 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004289 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004290 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004291 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004292 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004293 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004294 }
Dan Gohman475871a2008-07-27 21:46:04 +00004295 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296}
4297
Dan Gohman475871a2008-07-27 21:46:04 +00004298SDValue
4299X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004300 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004301 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004302 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4303 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4304 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004305 Op.getOperand(0))));
4306
Dale Johannesenace16102009-02-03 19:33:06 +00004307 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004308 MVT VT = MVT::v2i32;
4309 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004310 default: break;
4311 case MVT::v16i8:
4312 case MVT::v8i16:
4313 VT = MVT::v4i32;
4314 break;
4315 }
Dale Johannesenace16102009-02-03 19:33:06 +00004316 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4317 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004318}
4319
Bill Wendling056292f2008-09-16 21:48:12 +00004320// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4321// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4322// one of the above mentioned nodes. It has to be wrapped because otherwise
4323// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4324// be used to form addressing mode. These wrapped nodes will be selected
4325// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004326SDValue
4327X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004328 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004329 // FIXME there isn't really any debug info here, should come from the parent
4330 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004331 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4332 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004333 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004334 // With PIC, the address is actually $g + Offset.
4335 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4336 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004337 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004338 DAG.getNode(X86ISD::GlobalBaseReg,
4339 DebugLoc::getUnknownLoc(),
4340 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004341 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 }
4343
4344 return Result;
4345}
4346
Dan Gohman475871a2008-07-27 21:46:04 +00004347SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004348X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004349 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004350 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004351 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4352 bool ExtraLoadRequired =
4353 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4354
4355 // Create the TargetGlobalAddress node, folding in the constant
4356 // offset if it is legal.
4357 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004358 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004359 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4360 Offset = 0;
4361 } else
4362 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004363 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004364
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004365 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004366 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004367 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4368 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004369 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004371
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004372 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4373 // load the value at address GV, not the value of GV itself. This means that
4374 // the GlobalAddress must be in the base or index register of the address, not
4375 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004376 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004377 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004378 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004379 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004380
Dan Gohman6520e202008-10-18 02:06:02 +00004381 // If there was a non-zero offset that we didn't fold, create an explicit
4382 // addition for it.
4383 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004384 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004385 DAG.getConstant(Offset, getPointerTy()));
4386
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 return Result;
4388}
4389
Evan Chengda43bcf2008-09-24 00:05:32 +00004390SDValue
4391X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4392 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004393 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004394 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004395}
4396
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004397static SDValue
4398GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004399 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004400 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4401 DebugLoc dl = GA->getDebugLoc();
4402 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4403 GA->getValueType(0),
4404 GA->getOffset());
4405 if (InFlag) {
4406 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004407 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004408 } else {
4409 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004410 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004411 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004412 SDValue Flag = Chain.getValue(1);
4413 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004414}
4415
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004416// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004417static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004418LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004419 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004420 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004421 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4422 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004423 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004424 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004425 PtrVT), InFlag);
4426 InFlag = Chain.getValue(1);
4427
Rafael Espindola15f1b662009-04-24 12:59:40 +00004428 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004429}
4430
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004431// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004432static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004433LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004434 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004435 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004436}
4437
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004438// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4439// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004440static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004441 const MVT PtrVT, TLSModel::Model model,
4442 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004443 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004444 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004445 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4446 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004447 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4448 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004449
4450 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4451 NULL, 0);
4452
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004453 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4454 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004455 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004456 GA->getValueType(0),
4457 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004458 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004459
Rafael Espindola9a580232009-02-27 13:37:18 +00004460 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004461 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004462 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004463
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004464 // The address of the thread local variable is the add of the thread
4465 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004466 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004467}
4468
Dan Gohman475871a2008-07-27 21:46:04 +00004469SDValue
4470X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004471 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004472 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004473 assert(Subtarget->isTargetELF() &&
4474 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004475 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004476 GlobalValue *GV = GA->getGlobal();
4477 TLSModel::Model model =
4478 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004479 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004480 switch (model) {
4481 case TLSModel::GeneralDynamic:
4482 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004483 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004484
4485 case TLSModel::InitialExec:
4486 case TLSModel::LocalExec:
4487 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004488 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004489 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004490 switch (model) {
4491 case TLSModel::GeneralDynamic:
4492 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004493 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004494
4495 case TLSModel::InitialExec:
4496 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004497 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004498 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004499 }
Chris Lattner5867de12009-04-01 22:14:45 +00004500 assert(0 && "Unreachable");
4501 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004502}
4503
Dan Gohman475871a2008-07-27 21:46:04 +00004504SDValue
4505X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004506 // FIXME there isn't really any debug info here
4507 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004508 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4509 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004510 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004511 // With PIC, the address is actually $g + Offset.
4512 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4513 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004514 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004515 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004516 DebugLoc::getUnknownLoc(),
4517 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004518 Result);
4519 }
4520
4521 return Result;
4522}
4523
Dan Gohman475871a2008-07-27 21:46:04 +00004524SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004525 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004526 // FIXME there isn't really any debug into here
4527 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004528 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004529 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004530 // With PIC, the address is actually $g + Offset.
4531 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4532 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004533 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004534 DAG.getNode(X86ISD::GlobalBaseReg,
4535 DebugLoc::getUnknownLoc(),
4536 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004537 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004538 }
4539
4540 return Result;
4541}
4542
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004543/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004544/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004545SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004546 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004547 MVT VT = Op.getValueType();
4548 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004549 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004550 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue ShOpLo = Op.getOperand(0);
4552 SDValue ShOpHi = Op.getOperand(1);
4553 SDValue ShAmt = Op.getOperand(2);
4554 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004555 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004556 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004557 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004558
Dan Gohman475871a2008-07-27 21:46:04 +00004559 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004560 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004561 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4562 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004563 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004564 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4565 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004566 }
Evan Chenge3413162006-01-09 18:33:28 +00004567
Dale Johannesenace16102009-02-03 19:33:06 +00004568 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004569 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004570 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004571 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004572
Dan Gohman475871a2008-07-27 21:46:04 +00004573 SDValue Hi, Lo;
4574 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4575 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4576 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004577
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004578 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004579 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4580 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004581 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004582 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4583 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004584 }
4585
Dan Gohman475871a2008-07-27 21:46:04 +00004586 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004587 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004588}
Evan Chenga3195e82006-01-12 22:54:21 +00004589
Dan Gohman475871a2008-07-27 21:46:04 +00004590SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004591 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004592 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004593 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004594
Chris Lattnerb09916b2008-02-27 05:57:41 +00004595 // These are really Legal; caller falls through into that case.
4596 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004597 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00004598 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004599 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004600 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00004601
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004602 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004603 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004604 MachineFunction &MF = DAG.getMachineFunction();
4605 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004606 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004607 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004608 StackSlot,
4609 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004610 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4611}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004612
Eli Friedman948e95a2009-05-23 09:59:16 +00004613SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4614 SDValue StackSlot,
4615 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004616 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004617 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004618 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004619 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004620 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004621 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4622 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004623 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004624 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004625 Ops.push_back(Chain);
4626 Ops.push_back(StackSlot);
4627 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004628 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004629 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004630
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004631 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004632 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004633 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004634
4635 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4636 // shouldn't be necessary except that RFP cannot be live across
4637 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004638 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004640 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004641 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004643 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004644 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004645 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004646 Ops.push_back(DAG.getValueType(Op.getValueType()));
4647 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004648 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4649 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004650 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004651 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004652
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653 return Result;
4654}
4655
Bill Wendling8b8a6362009-01-17 03:56:04 +00004656// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4657SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4658 // This algorithm is not obvious. Here it is in C code, more or less:
4659 /*
4660 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4661 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4662 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004663
Bill Wendling8b8a6362009-01-17 03:56:04 +00004664 // Copy ints to xmm registers.
4665 __m128i xh = _mm_cvtsi32_si128( hi );
4666 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004667
Bill Wendling8b8a6362009-01-17 03:56:04 +00004668 // Combine into low half of a single xmm register.
4669 __m128i x = _mm_unpacklo_epi32( xh, xl );
4670 __m128d d;
4671 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004672
Bill Wendling8b8a6362009-01-17 03:56:04 +00004673 // Merge in appropriate exponents to give the integer bits the right
4674 // magnitude.
4675 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004676
Bill Wendling8b8a6362009-01-17 03:56:04 +00004677 // Subtract away the biases to deal with the IEEE-754 double precision
4678 // implicit 1.
4679 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004680
Bill Wendling8b8a6362009-01-17 03:56:04 +00004681 // All conversions up to here are exact. The correctly rounded result is
4682 // calculated using the current rounding mode using the following
4683 // horizontal add.
4684 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4685 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4686 // store doesn't really need to be here (except
4687 // maybe to zero the other double)
4688 return sd;
4689 }
4690 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004691
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004692 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004693
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004694 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004695 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004696 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4697 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4698 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4699 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4700 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004701 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004702
Bill Wendling8b8a6362009-01-17 03:56:04 +00004703 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004704 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4705 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4706 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004707 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004708
Dale Johannesenace16102009-02-03 19:33:06 +00004709 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4710 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004711 Op.getOperand(0),
4712 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004713 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4714 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004715 Op.getOperand(0),
4716 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004717 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004718 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004719 PseudoSourceValue::getConstantPool(), 0,
4720 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004721 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004722 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4723 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004724 PseudoSourceValue::getConstantPool(), 0,
4725 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004726 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004727
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004728 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004729 int ShufMask[2] = { 1, -1 };
4730 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4731 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004732 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4733 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004734 DAG.getIntPtrConstant(0));
4735}
4736
Bill Wendling8b8a6362009-01-17 03:56:04 +00004737// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4738SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004739 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004740 // FP constant to bias correct the final result.
4741 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4742 MVT::f64);
4743
4744 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004745 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4746 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004747 Op.getOperand(0),
4748 DAG.getIntPtrConstant(0)));
4749
Dale Johannesenace16102009-02-03 19:33:06 +00004750 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4751 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004752 DAG.getIntPtrConstant(0));
4753
4754 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004755 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4756 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4757 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004758 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004759 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4760 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004761 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004762 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4763 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004764 DAG.getIntPtrConstant(0));
4765
4766 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004767 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004768
4769 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004770 MVT DestVT = Op.getValueType();
4771
4772 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004773 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004774 DAG.getIntPtrConstant(0));
4775 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004776 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004777 }
4778
4779 // Handle final rounding.
4780 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004781}
4782
4783SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004784 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004785 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004786
Evan Chenga06ec9e2009-01-19 08:08:22 +00004787 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4788 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4789 // the optimization here.
4790 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004791 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004792
4793 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004794 if (SrcVT == MVT::i64) {
4795 // We only handle SSE2 f64 target here; caller can handle the rest.
4796 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4797 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004798
Bill Wendling8b8a6362009-01-17 03:56:04 +00004799 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004800 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004801 return LowerUINT_TO_FP_i32(Op, DAG);
4802 }
4803
Eli Friedman948e95a2009-05-23 09:59:16 +00004804 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4805
4806 // Make a 64-bit buffer, and use it to build an FILD.
4807 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4808 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4809 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4810 getPointerTy(), StackSlot, WordOff);
4811 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4812 StackSlot, NULL, 0);
4813 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4814 OffsetSlot, NULL, 0);
4815 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004816}
4817
Dan Gohman475871a2008-07-27 21:46:04 +00004818std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004819FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004820 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004821
4822 MVT DstTy = Op.getValueType();
4823
4824 if (!IsSigned) {
4825 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4826 DstTy = MVT::i64;
4827 }
4828
4829 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4830 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004832
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004833 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004834 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004835 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004836 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004837 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004838 DstTy == MVT::i64 &&
Dale Johannesen73328d12007-09-19 23:55:34 +00004839 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004841
Evan Cheng87c89352007-10-15 20:11:21 +00004842 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4843 // stack slot.
4844 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004845 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004846 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004847 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004848
Evan Cheng0db9fe62006-04-25 20:13:52 +00004849 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004850 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004851 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4852 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4853 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4854 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004855 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004856
Dan Gohman475871a2008-07-27 21:46:04 +00004857 SDValue Chain = DAG.getEntryNode();
4858 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004859 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004860 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004861 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004862 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004863 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004864 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004865 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4866 };
Dale Johannesenace16102009-02-03 19:33:06 +00004867 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004868 Chain = Value.getValue(1);
4869 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4870 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4871 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004872
Evan Cheng0db9fe62006-04-25 20:13:52 +00004873 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004874 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004875 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004876
Chris Lattner27a6c732007-11-24 07:07:01 +00004877 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004878}
4879
Dan Gohman475871a2008-07-27 21:46:04 +00004880SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004881 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004882 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00004883 if (FIST.getNode() == 0) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00004884
Chris Lattner27a6c732007-11-24 07:07:01 +00004885 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004886 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004887 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004888}
4889
Eli Friedman948e95a2009-05-23 09:59:16 +00004890SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4891 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4892 SDValue FIST = Vals.first, StackSlot = Vals.second;
4893 assert(FIST.getNode() && "Unexpected failure");
4894
4895 // Load the result.
4896 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4897 FIST, StackSlot, NULL, 0);
4898}
4899
Dan Gohman475871a2008-07-27 21:46:04 +00004900SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004901 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004902 MVT VT = Op.getValueType();
4903 MVT EltVT = VT;
4904 if (VT.isVector())
4905 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004906 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004907 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004908 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004909 CV.push_back(C);
4910 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004911 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004912 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004913 CV.push_back(C);
4914 CV.push_back(C);
4915 CV.push_back(C);
4916 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004917 }
Dan Gohmand3006222007-07-27 17:16:43 +00004918 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004919 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004920 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004921 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004922 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004923 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004924}
4925
Dan Gohman475871a2008-07-27 21:46:04 +00004926SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004927 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004928 MVT VT = Op.getValueType();
4929 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004930 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004931 if (VT.isVector()) {
4932 EltVT = VT.getVectorElementType();
4933 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004934 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004935 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004936 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004937 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004938 CV.push_back(C);
4939 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004940 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004941 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004942 CV.push_back(C);
4943 CV.push_back(C);
4944 CV.push_back(C);
4945 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004946 }
Dan Gohmand3006222007-07-27 17:16:43 +00004947 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004948 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004949 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004950 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004951 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004952 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004953 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4954 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004955 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004956 Op.getOperand(0)),
4957 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004958 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004959 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004960 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004961}
4962
Dan Gohman475871a2008-07-27 21:46:04 +00004963SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4964 SDValue Op0 = Op.getOperand(0);
4965 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004966 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004967 MVT VT = Op.getValueType();
4968 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004969
4970 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004971 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004972 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004973 SrcVT = VT;
4974 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004975 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004976 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004977 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004978 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004979 }
4980
4981 // At this point the operands and the result should have the same
4982 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004983
Evan Cheng68c47cb2007-01-05 07:55:56 +00004984 // First get the sign bit of second operand.
4985 std::vector<Constant*> CV;
4986 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004987 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4988 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004989 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4993 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004994 }
Dan Gohmand3006222007-07-27 17:16:43 +00004995 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004996 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004997 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004998 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004999 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005000 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005001
5002 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005003 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005004 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005005 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5006 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005007 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005008 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5009 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005010 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005011 }
5012
Evan Cheng73d6cf12007-01-05 21:37:56 +00005013 // Clear first operand sign bit.
5014 CV.clear();
5015 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005016 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5017 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005018 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005019 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5020 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5021 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5022 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005023 }
Dan Gohmand3006222007-07-27 17:16:43 +00005024 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005025 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005026 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005027 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005028 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005029 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005030
5031 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005032 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005033}
5034
Dan Gohman076aee32009-03-04 19:44:21 +00005035/// Emit nodes that will be selected as "test Op0,Op0", or something
5036/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005037SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5038 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005039 DebugLoc dl = Op.getDebugLoc();
5040
Dan Gohman31125812009-03-07 01:58:32 +00005041 // CF and OF aren't always set the way we want. Determine which
5042 // of these we need.
5043 bool NeedCF = false;
5044 bool NeedOF = false;
5045 switch (X86CC) {
5046 case X86::COND_A: case X86::COND_AE:
5047 case X86::COND_B: case X86::COND_BE:
5048 NeedCF = true;
5049 break;
5050 case X86::COND_G: case X86::COND_GE:
5051 case X86::COND_L: case X86::COND_LE:
5052 case X86::COND_O: case X86::COND_NO:
5053 NeedOF = true;
5054 break;
5055 default: break;
5056 }
5057
Dan Gohman076aee32009-03-04 19:44:21 +00005058 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005059 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5060 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5061 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005062 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005063 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005064 switch (Op.getNode()->getOpcode()) {
5065 case ISD::ADD:
5066 // Due to an isel shortcoming, be conservative if this add is likely to
5067 // be selected as part of a load-modify-store instruction. When the root
5068 // node in a match is a store, isel doesn't know how to remap non-chain
5069 // non-flag uses of other nodes in the match, such as the ADD in this
5070 // case. This leads to the ADD being left around and reselected, with
5071 // the result being two adds in the output.
5072 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5073 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5074 if (UI->getOpcode() == ISD::STORE)
5075 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005076 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005077 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5078 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005079 if (C->getAPIntValue() == 1) {
5080 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005081 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005082 break;
5083 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005084 // An add of negative one (subtract of one) will be selected as a DEC.
5085 if (C->getAPIntValue().isAllOnesValue()) {
5086 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005087 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005088 break;
5089 }
5090 }
Dan Gohman076aee32009-03-04 19:44:21 +00005091 // Otherwise use a regular EFLAGS-setting add.
5092 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005093 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005094 break;
5095 case ISD::SUB:
5096 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5097 // likely to be selected as part of a load-modify-store instruction.
5098 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5099 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5100 if (UI->getOpcode() == ISD::STORE)
5101 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005102 // Otherwise use a regular EFLAGS-setting sub.
5103 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005104 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005105 break;
5106 case X86ISD::ADD:
5107 case X86ISD::SUB:
5108 case X86ISD::INC:
5109 case X86ISD::DEC:
5110 return SDValue(Op.getNode(), 1);
5111 default:
5112 default_case:
5113 break;
5114 }
5115 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005116 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005117 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005118 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005119 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005120 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005121 DAG.ReplaceAllUsesWith(Op, New);
5122 return SDValue(New.getNode(), 1);
5123 }
5124 }
5125
5126 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5127 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5128 DAG.getConstant(0, Op.getValueType()));
5129}
5130
5131/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5132/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005133SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5134 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005135 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5136 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005137 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005138
5139 DebugLoc dl = Op0.getDebugLoc();
5140 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5141}
5142
Dan Gohman475871a2008-07-27 21:46:04 +00005143SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005144 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005145 SDValue Op0 = Op.getOperand(0);
5146 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005147 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005148 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005149
Dan Gohmane5af2d32009-01-29 01:59:02 +00005150 // Lower (X & (1 << N)) == 0 to BT(X, N).
5151 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5152 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005153 if (Op0.getOpcode() == ISD::AND &&
5154 Op0.hasOneUse() &&
5155 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005156 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005157 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005158 SDValue LHS, RHS;
5159 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5160 if (ConstantSDNode *Op010C =
5161 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5162 if (Op010C->getZExtValue() == 1) {
5163 LHS = Op0.getOperand(0);
5164 RHS = Op0.getOperand(1).getOperand(1);
5165 }
5166 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5167 if (ConstantSDNode *Op000C =
5168 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5169 if (Op000C->getZExtValue() == 1) {
5170 LHS = Op0.getOperand(1);
5171 RHS = Op0.getOperand(0).getOperand(1);
5172 }
5173 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5174 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5175 SDValue AndLHS = Op0.getOperand(0);
5176 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5177 LHS = AndLHS.getOperand(0);
5178 RHS = AndLHS.getOperand(1);
5179 }
5180 }
Evan Cheng0488db92007-09-25 01:57:46 +00005181
Dan Gohmane5af2d32009-01-29 01:59:02 +00005182 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005183 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5184 // instruction. Since the shift amount is in-range-or-undefined, we know
5185 // that doing a bittest on the i16 value is ok. We extend to i32 because
5186 // the encoding for the i16 version is larger than the i32 version.
5187 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005188 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005189
5190 // If the operand types disagree, extend the shift amount to match. Since
5191 // BT ignores high bits (like shifts) we can use anyextend.
5192 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005193 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005194
Dale Johannesenace16102009-02-03 19:33:06 +00005195 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005196 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005197 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005198 DAG.getConstant(Cond, MVT::i8), BT);
5199 }
5200 }
5201
5202 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5203 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005204
Dan Gohman31125812009-03-07 01:58:32 +00005205 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005206 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005207 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005208}
5209
Dan Gohman475871a2008-07-27 21:46:04 +00005210SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5211 SDValue Cond;
5212 SDValue Op0 = Op.getOperand(0);
5213 SDValue Op1 = Op.getOperand(1);
5214 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005215 MVT VT = Op.getValueType();
5216 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5217 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005218 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005219
5220 if (isFP) {
5221 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005222 MVT VT0 = Op0.getValueType();
5223 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5224 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005225 bool Swap = false;
5226
5227 switch (SetCCOpcode) {
5228 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005229 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005230 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005231 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005232 case ISD::SETGT: Swap = true; // Fallthrough
5233 case ISD::SETLT:
5234 case ISD::SETOLT: SSECC = 1; break;
5235 case ISD::SETOGE:
5236 case ISD::SETGE: Swap = true; // Fallthrough
5237 case ISD::SETLE:
5238 case ISD::SETOLE: SSECC = 2; break;
5239 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005240 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005241 case ISD::SETNE: SSECC = 4; break;
5242 case ISD::SETULE: Swap = true;
5243 case ISD::SETUGE: SSECC = 5; break;
5244 case ISD::SETULT: Swap = true;
5245 case ISD::SETUGT: SSECC = 6; break;
5246 case ISD::SETO: SSECC = 7; break;
5247 }
5248 if (Swap)
5249 std::swap(Op0, Op1);
5250
Nate Begemanfb8ead02008-07-25 19:05:58 +00005251 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005252 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005253 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005254 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005255 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5256 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5257 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005258 }
5259 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005260 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005261 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5262 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5263 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005264 }
5265 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005266 }
5267 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005268 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005269 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005270
Nate Begeman30a0de92008-07-17 16:51:19 +00005271 // We are handling one of the integer comparisons here. Since SSE only has
5272 // GT and EQ comparisons for integer, swapping operands and multiple
5273 // operations may be required for some comparisons.
5274 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5275 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005276
Nate Begeman30a0de92008-07-17 16:51:19 +00005277 switch (VT.getSimpleVT()) {
5278 default: break;
5279 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5280 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5281 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5282 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005284
Nate Begeman30a0de92008-07-17 16:51:19 +00005285 switch (SetCCOpcode) {
5286 default: break;
5287 case ISD::SETNE: Invert = true;
5288 case ISD::SETEQ: Opc = EQOpc; break;
5289 case ISD::SETLT: Swap = true;
5290 case ISD::SETGT: Opc = GTOpc; break;
5291 case ISD::SETGE: Swap = true;
5292 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5293 case ISD::SETULT: Swap = true;
5294 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5295 case ISD::SETUGE: Swap = true;
5296 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5297 }
5298 if (Swap)
5299 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005300
Nate Begeman30a0de92008-07-17 16:51:19 +00005301 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5302 // bits of the inputs before performing those operations.
5303 if (FlipSigns) {
5304 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005305 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5306 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005307 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005308 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5309 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005310 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5311 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005312 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005313
Dale Johannesenace16102009-02-03 19:33:06 +00005314 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005315
5316 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005317 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005318 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005319
Nate Begeman30a0de92008-07-17 16:51:19 +00005320 return Result;
5321}
Evan Cheng0488db92007-09-25 01:57:46 +00005322
Evan Cheng370e5342008-12-03 08:38:43 +00005323// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005324static bool isX86LogicalCmp(SDValue Op) {
5325 unsigned Opc = Op.getNode()->getOpcode();
5326 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5327 return true;
5328 if (Op.getResNo() == 1 &&
5329 (Opc == X86ISD::ADD ||
5330 Opc == X86ISD::SUB ||
5331 Opc == X86ISD::SMUL ||
5332 Opc == X86ISD::UMUL ||
5333 Opc == X86ISD::INC ||
5334 Opc == X86ISD::DEC))
5335 return true;
5336
5337 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005338}
5339
Dan Gohman475871a2008-07-27 21:46:04 +00005340SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005341 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005342 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005343 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005344 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005345
Evan Cheng734503b2006-09-11 02:19:56 +00005346 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005347 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005348
Evan Cheng3f41d662007-10-08 22:16:29 +00005349 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5350 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005351 if (Cond.getOpcode() == X86ISD::SETCC) {
5352 CC = Cond.getOperand(0);
5353
Dan Gohman475871a2008-07-27 21:46:04 +00005354 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005355 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005356 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005357
Evan Cheng3f41d662007-10-08 22:16:29 +00005358 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005359 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005360 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005361 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005362
Chris Lattnerd1980a52009-03-12 06:52:53 +00005363 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5364 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005365 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005366 addTest = false;
5367 }
5368 }
5369
5370 if (addTest) {
5371 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005372 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005373 }
5374
Dan Gohmanfc166572009-04-09 23:54:40 +00005375 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005376 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005377 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5378 // condition is true.
5379 Ops.push_back(Op.getOperand(2));
5380 Ops.push_back(Op.getOperand(1));
5381 Ops.push_back(CC);
5382 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005383 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005384}
5385
Evan Cheng370e5342008-12-03 08:38:43 +00005386// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5387// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5388// from the AND / OR.
5389static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5390 Opc = Op.getOpcode();
5391 if (Opc != ISD::OR && Opc != ISD::AND)
5392 return false;
5393 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5394 Op.getOperand(0).hasOneUse() &&
5395 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5396 Op.getOperand(1).hasOneUse());
5397}
5398
Evan Cheng961d6d42009-02-02 08:19:07 +00005399// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5400// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005401static bool isXor1OfSetCC(SDValue Op) {
5402 if (Op.getOpcode() != ISD::XOR)
5403 return false;
5404 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5405 if (N1C && N1C->getAPIntValue() == 1) {
5406 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5407 Op.getOperand(0).hasOneUse();
5408 }
5409 return false;
5410}
5411
Dan Gohman475871a2008-07-27 21:46:04 +00005412SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005413 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue Chain = Op.getOperand(0);
5415 SDValue Cond = Op.getOperand(1);
5416 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005417 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005419
Evan Cheng0db9fe62006-04-25 20:13:52 +00005420 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005421 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005422#if 0
5423 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005424 else if (Cond.getOpcode() == X86ISD::ADD ||
5425 Cond.getOpcode() == X86ISD::SUB ||
5426 Cond.getOpcode() == X86ISD::SMUL ||
5427 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005428 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005429#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005430
Evan Cheng3f41d662007-10-08 22:16:29 +00005431 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5432 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005434 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005435
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005437 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005438 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005439 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005440 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005441 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005442 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005443 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005444 default: break;
5445 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005446 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005447 // These can only come from an arithmetic instruction with overflow,
5448 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005449 Cond = Cond.getNode()->getOperand(1);
5450 addTest = false;
5451 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005452 }
Evan Cheng0488db92007-09-25 01:57:46 +00005453 }
Evan Cheng370e5342008-12-03 08:38:43 +00005454 } else {
5455 unsigned CondOpc;
5456 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5457 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005458 if (CondOpc == ISD::OR) {
5459 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5460 // two branches instead of an explicit OR instruction with a
5461 // separate test.
5462 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005463 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005464 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005465 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005466 Chain, Dest, CC, Cmp);
5467 CC = Cond.getOperand(1).getOperand(0);
5468 Cond = Cmp;
5469 addTest = false;
5470 }
5471 } else { // ISD::AND
5472 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5473 // two branches instead of an explicit AND instruction with a
5474 // separate test. However, we only do this if this block doesn't
5475 // have a fall-through edge, because this requires an explicit
5476 // jmp when the condition is false.
5477 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005478 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005479 Op.getNode()->hasOneUse()) {
5480 X86::CondCode CCode =
5481 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5482 CCode = X86::GetOppositeBranchCondition(CCode);
5483 CC = DAG.getConstant(CCode, MVT::i8);
5484 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5485 // Look for an unconditional branch following this conditional branch.
5486 // We need this because we need to reverse the successors in order
5487 // to implement FCMP_OEQ.
5488 if (User.getOpcode() == ISD::BR) {
5489 SDValue FalseBB = User.getOperand(1);
5490 SDValue NewBR =
5491 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5492 assert(NewBR == User);
5493 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005494
Dale Johannesene4d209d2009-02-03 20:21:25 +00005495 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005496 Chain, Dest, CC, Cmp);
5497 X86::CondCode CCode =
5498 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5499 CCode = X86::GetOppositeBranchCondition(CCode);
5500 CC = DAG.getConstant(CCode, MVT::i8);
5501 Cond = Cmp;
5502 addTest = false;
5503 }
5504 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005505 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005506 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5507 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5508 // It should be transformed during dag combiner except when the condition
5509 // is set by a arithmetics with overflow node.
5510 X86::CondCode CCode =
5511 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5512 CCode = X86::GetOppositeBranchCondition(CCode);
5513 CC = DAG.getConstant(CCode, MVT::i8);
5514 Cond = Cond.getOperand(0).getOperand(1);
5515 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005516 }
Evan Cheng0488db92007-09-25 01:57:46 +00005517 }
5518
5519 if (addTest) {
5520 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005521 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005522 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005523 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005524 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005525}
5526
Anton Korobeynikove060b532007-04-17 19:34:00 +00005527
5528// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5529// Calls to _alloca is needed to probe the stack when allocating more than 4k
5530// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5531// that the guard pages used by the OS virtual memory manager are allocated in
5532// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005533SDValue
5534X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005535 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005536 assert(Subtarget->isTargetCygMing() &&
5537 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005538 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005539
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005540 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005541 SDValue Chain = Op.getOperand(0);
5542 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005543 // FIXME: Ensure alignment here
5544
Dan Gohman475871a2008-07-27 21:46:04 +00005545 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005546
Duncan Sands83ec4b62008-06-06 12:08:01 +00005547 MVT IntPtr = getPointerTy();
5548 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005549
Chris Lattnere563bbc2008-10-11 22:08:30 +00005550 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005551
Dale Johannesendd64c412009-02-04 00:33:20 +00005552 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005553 Flag = Chain.getValue(1);
5554
5555 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005556 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005557 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005558 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005559 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005560 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005561 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005562 Flag = Chain.getValue(1);
5563
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005564 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005565 DAG.getIntPtrConstant(0, true),
5566 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005567 Flag);
5568
Dale Johannesendd64c412009-02-04 00:33:20 +00005569 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005570
Dan Gohman475871a2008-07-27 21:46:04 +00005571 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005572 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005573}
5574
Dan Gohman475871a2008-07-27 21:46:04 +00005575SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005576X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005577 SDValue Chain,
5578 SDValue Dst, SDValue Src,
5579 SDValue Size, unsigned Align,
5580 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005581 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005582 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005583
Bill Wendling6f287b22008-09-30 21:22:07 +00005584 // If not DWORD aligned or size is more than the threshold, call the library.
5585 // The libc version is likely to be faster for these cases. It can use the
5586 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005587 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005588 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005589 ConstantSize->getZExtValue() >
5590 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005591 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005592
5593 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005594 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005595
Bill Wendling6158d842008-10-01 00:59:58 +00005596 if (const char *bzeroEntry = V &&
5597 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5598 MVT IntPtr = getPointerTy();
5599 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005600 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005601 TargetLowering::ArgListEntry Entry;
5602 Entry.Node = Dst;
5603 Entry.Ty = IntPtrTy;
5604 Args.push_back(Entry);
5605 Entry.Node = Size;
5606 Args.push_back(Entry);
5607 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005608 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5609 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005610 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005611 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005612 }
5613
Dan Gohman707e0182008-04-12 04:36:06 +00005614 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005615 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005616 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005617
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005618 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005619 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005620 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005621 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005622 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005623 unsigned BytesLeft = 0;
5624 bool TwoRepStos = false;
5625 if (ValC) {
5626 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005627 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005628
Evan Cheng0db9fe62006-04-25 20:13:52 +00005629 // If the value is a constant, then we can potentially use larger sets.
5630 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005631 case 2: // WORD aligned
5632 AVT = MVT::i16;
5633 ValReg = X86::AX;
5634 Val = (Val << 8) | Val;
5635 break;
5636 case 0: // DWORD aligned
5637 AVT = MVT::i32;
5638 ValReg = X86::EAX;
5639 Val = (Val << 8) | Val;
5640 Val = (Val << 16) | Val;
5641 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5642 AVT = MVT::i64;
5643 ValReg = X86::RAX;
5644 Val = (Val << 32) | Val;
5645 }
5646 break;
5647 default: // Byte aligned
5648 AVT = MVT::i8;
5649 ValReg = X86::AL;
5650 Count = DAG.getIntPtrConstant(SizeVal);
5651 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005652 }
5653
Duncan Sands8e4eb092008-06-08 20:54:56 +00005654 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005655 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005656 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5657 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005658 }
5659
Dale Johannesen0f502f62009-02-03 22:26:09 +00005660 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005661 InFlag);
5662 InFlag = Chain.getValue(1);
5663 } else {
5664 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005665 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005666 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005667 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005668 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005669
Scott Michelfdc40a02009-02-17 22:15:04 +00005670 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005671 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005672 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005673 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005674 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005675 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005676 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005677 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005678
Chris Lattnerd96d0722007-02-25 06:40:16 +00005679 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005680 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005681 Ops.push_back(Chain);
5682 Ops.push_back(DAG.getValueType(AVT));
5683 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005684 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005685
Evan Cheng0db9fe62006-04-25 20:13:52 +00005686 if (TwoRepStos) {
5687 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005688 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005689 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005690 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005691 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005692 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005693 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005694 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005695 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005696 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005697 Ops.clear();
5698 Ops.push_back(Chain);
5699 Ops.push_back(DAG.getValueType(MVT::i8));
5700 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005701 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005702 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005703 // Handle the last 1 - 7 bytes.
5704 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005705 MVT AddrVT = Dst.getValueType();
5706 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005707
Dale Johannesen0f502f62009-02-03 22:26:09 +00005708 Chain = DAG.getMemset(Chain, dl,
5709 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005710 DAG.getConstant(Offset, AddrVT)),
5711 Src,
5712 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005713 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005714 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005715
Dan Gohman707e0182008-04-12 04:36:06 +00005716 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 return Chain;
5718}
Evan Cheng11e15b32006-04-03 20:53:28 +00005719
Dan Gohman475871a2008-07-27 21:46:04 +00005720SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005721X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005722 SDValue Chain, SDValue Dst, SDValue Src,
5723 SDValue Size, unsigned Align,
5724 bool AlwaysInline,
5725 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005726 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005727 // This requires the copy size to be a constant, preferrably
5728 // within a subtarget-specific limit.
5729 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5730 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005731 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005732 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005733 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005734 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005735
Evan Cheng1887c1c2008-08-21 21:00:15 +00005736 /// If not DWORD aligned, call the library.
5737 if ((Align & 3) != 0)
5738 return SDValue();
5739
5740 // DWORD aligned
5741 MVT AVT = MVT::i32;
5742 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005743 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744
Duncan Sands83ec4b62008-06-06 12:08:01 +00005745 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005746 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005747 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005748 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005749
Dan Gohman475871a2008-07-27 21:46:04 +00005750 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005751 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005752 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005753 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005754 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005755 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005756 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005757 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005758 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005759 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005760 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005761 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005762 InFlag = Chain.getValue(1);
5763
Chris Lattnerd96d0722007-02-25 06:40:16 +00005764 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005765 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005766 Ops.push_back(Chain);
5767 Ops.push_back(DAG.getValueType(AVT));
5768 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005769 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005770
Dan Gohman475871a2008-07-27 21:46:04 +00005771 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005772 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005773 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005774 // Handle the last 1 - 7 bytes.
5775 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005776 MVT DstVT = Dst.getValueType();
5777 MVT SrcVT = Src.getValueType();
5778 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005779 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005780 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005781 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005782 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005783 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005784 DAG.getConstant(BytesLeft, SizeVT),
5785 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005786 DstSV, DstSVOff + Offset,
5787 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005788 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005789
Scott Michelfdc40a02009-02-17 22:15:04 +00005790 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005791 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005792}
5793
Dan Gohman475871a2008-07-27 21:46:04 +00005794SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005795 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005796 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005797
Evan Cheng25ab6902006-09-08 06:48:29 +00005798 if (!Subtarget->is64Bit()) {
5799 // vastart just stores the address of the VarArgsFrameIndex slot into the
5800 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005801 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005802 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005803 }
5804
5805 // __va_list_tag:
5806 // gp_offset (0 - 6 * 8)
5807 // fp_offset (48 - 48 + 8 * 16)
5808 // overflow_arg_area (point to parameters coming in memory).
5809 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005810 SmallVector<SDValue, 8> MemOps;
5811 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005812 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005813 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005814 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005815 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005816 MemOps.push_back(Store);
5817
5818 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005819 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005820 FIN, DAG.getIntPtrConstant(4));
5821 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005822 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005823 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005824 MemOps.push_back(Store);
5825
5826 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005827 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005828 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005830 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005831 MemOps.push_back(Store);
5832
5833 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005834 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005835 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005836 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005837 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005838 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005839 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005840 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005841}
5842
Dan Gohman475871a2008-07-27 21:46:04 +00005843SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005844 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5845 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005846 SDValue Chain = Op.getOperand(0);
5847 SDValue SrcPtr = Op.getOperand(1);
5848 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005849
5850 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5851 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005852 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005853}
5854
Dan Gohman475871a2008-07-27 21:46:04 +00005855SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005856 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005857 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005858 SDValue Chain = Op.getOperand(0);
5859 SDValue DstPtr = Op.getOperand(1);
5860 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005861 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5862 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005863 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005864
Dale Johannesendd64c412009-02-04 00:33:20 +00005865 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005866 DAG.getIntPtrConstant(24), 8, false,
5867 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005868}
5869
Dan Gohman475871a2008-07-27 21:46:04 +00005870SDValue
5871X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005872 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005873 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005874 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005875 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005876 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005877 case Intrinsic::x86_sse_comieq_ss:
5878 case Intrinsic::x86_sse_comilt_ss:
5879 case Intrinsic::x86_sse_comile_ss:
5880 case Intrinsic::x86_sse_comigt_ss:
5881 case Intrinsic::x86_sse_comige_ss:
5882 case Intrinsic::x86_sse_comineq_ss:
5883 case Intrinsic::x86_sse_ucomieq_ss:
5884 case Intrinsic::x86_sse_ucomilt_ss:
5885 case Intrinsic::x86_sse_ucomile_ss:
5886 case Intrinsic::x86_sse_ucomigt_ss:
5887 case Intrinsic::x86_sse_ucomige_ss:
5888 case Intrinsic::x86_sse_ucomineq_ss:
5889 case Intrinsic::x86_sse2_comieq_sd:
5890 case Intrinsic::x86_sse2_comilt_sd:
5891 case Intrinsic::x86_sse2_comile_sd:
5892 case Intrinsic::x86_sse2_comigt_sd:
5893 case Intrinsic::x86_sse2_comige_sd:
5894 case Intrinsic::x86_sse2_comineq_sd:
5895 case Intrinsic::x86_sse2_ucomieq_sd:
5896 case Intrinsic::x86_sse2_ucomilt_sd:
5897 case Intrinsic::x86_sse2_ucomile_sd:
5898 case Intrinsic::x86_sse2_ucomigt_sd:
5899 case Intrinsic::x86_sse2_ucomige_sd:
5900 case Intrinsic::x86_sse2_ucomineq_sd: {
5901 unsigned Opc = 0;
5902 ISD::CondCode CC = ISD::SETCC_INVALID;
5903 switch (IntNo) {
5904 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005905 case Intrinsic::x86_sse_comieq_ss:
5906 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005907 Opc = X86ISD::COMI;
5908 CC = ISD::SETEQ;
5909 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005910 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005911 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005912 Opc = X86ISD::COMI;
5913 CC = ISD::SETLT;
5914 break;
5915 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005916 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005917 Opc = X86ISD::COMI;
5918 CC = ISD::SETLE;
5919 break;
5920 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005921 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005922 Opc = X86ISD::COMI;
5923 CC = ISD::SETGT;
5924 break;
5925 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005926 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005927 Opc = X86ISD::COMI;
5928 CC = ISD::SETGE;
5929 break;
5930 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005931 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005932 Opc = X86ISD::COMI;
5933 CC = ISD::SETNE;
5934 break;
5935 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005936 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005937 Opc = X86ISD::UCOMI;
5938 CC = ISD::SETEQ;
5939 break;
5940 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005941 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005942 Opc = X86ISD::UCOMI;
5943 CC = ISD::SETLT;
5944 break;
5945 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005946 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005947 Opc = X86ISD::UCOMI;
5948 CC = ISD::SETLE;
5949 break;
5950 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005951 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005952 Opc = X86ISD::UCOMI;
5953 CC = ISD::SETGT;
5954 break;
5955 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005956 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005957 Opc = X86ISD::UCOMI;
5958 CC = ISD::SETGE;
5959 break;
5960 case Intrinsic::x86_sse_ucomineq_ss:
5961 case Intrinsic::x86_sse2_ucomineq_sd:
5962 Opc = X86ISD::UCOMI;
5963 CC = ISD::SETNE;
5964 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005965 }
Evan Cheng734503b2006-09-11 02:19:56 +00005966
Dan Gohman475871a2008-07-27 21:46:04 +00005967 SDValue LHS = Op.getOperand(1);
5968 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005969 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005970 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5971 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005972 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005973 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005974 }
Evan Cheng5759f972008-05-04 09:15:50 +00005975
5976 // Fix vector shift instructions where the last operand is a non-immediate
5977 // i32 value.
5978 case Intrinsic::x86_sse2_pslli_w:
5979 case Intrinsic::x86_sse2_pslli_d:
5980 case Intrinsic::x86_sse2_pslli_q:
5981 case Intrinsic::x86_sse2_psrli_w:
5982 case Intrinsic::x86_sse2_psrli_d:
5983 case Intrinsic::x86_sse2_psrli_q:
5984 case Intrinsic::x86_sse2_psrai_w:
5985 case Intrinsic::x86_sse2_psrai_d:
5986 case Intrinsic::x86_mmx_pslli_w:
5987 case Intrinsic::x86_mmx_pslli_d:
5988 case Intrinsic::x86_mmx_pslli_q:
5989 case Intrinsic::x86_mmx_psrli_w:
5990 case Intrinsic::x86_mmx_psrli_d:
5991 case Intrinsic::x86_mmx_psrli_q:
5992 case Intrinsic::x86_mmx_psrai_w:
5993 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005994 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005995 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005996 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005997
5998 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005999 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006000 switch (IntNo) {
6001 case Intrinsic::x86_sse2_pslli_w:
6002 NewIntNo = Intrinsic::x86_sse2_psll_w;
6003 break;
6004 case Intrinsic::x86_sse2_pslli_d:
6005 NewIntNo = Intrinsic::x86_sse2_psll_d;
6006 break;
6007 case Intrinsic::x86_sse2_pslli_q:
6008 NewIntNo = Intrinsic::x86_sse2_psll_q;
6009 break;
6010 case Intrinsic::x86_sse2_psrli_w:
6011 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6012 break;
6013 case Intrinsic::x86_sse2_psrli_d:
6014 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6015 break;
6016 case Intrinsic::x86_sse2_psrli_q:
6017 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6018 break;
6019 case Intrinsic::x86_sse2_psrai_w:
6020 NewIntNo = Intrinsic::x86_sse2_psra_w;
6021 break;
6022 case Intrinsic::x86_sse2_psrai_d:
6023 NewIntNo = Intrinsic::x86_sse2_psra_d;
6024 break;
6025 default: {
6026 ShAmtVT = MVT::v2i32;
6027 switch (IntNo) {
6028 case Intrinsic::x86_mmx_pslli_w:
6029 NewIntNo = Intrinsic::x86_mmx_psll_w;
6030 break;
6031 case Intrinsic::x86_mmx_pslli_d:
6032 NewIntNo = Intrinsic::x86_mmx_psll_d;
6033 break;
6034 case Intrinsic::x86_mmx_pslli_q:
6035 NewIntNo = Intrinsic::x86_mmx_psll_q;
6036 break;
6037 case Intrinsic::x86_mmx_psrli_w:
6038 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6039 break;
6040 case Intrinsic::x86_mmx_psrli_d:
6041 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6042 break;
6043 case Intrinsic::x86_mmx_psrli_q:
6044 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6045 break;
6046 case Intrinsic::x86_mmx_psrai_w:
6047 NewIntNo = Intrinsic::x86_mmx_psra_w;
6048 break;
6049 case Intrinsic::x86_mmx_psrai_d:
6050 NewIntNo = Intrinsic::x86_mmx_psra_d;
6051 break;
6052 default: abort(); // Can't reach here.
6053 }
6054 break;
6055 }
6056 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006057 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006058 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6059 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6060 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006061 DAG.getConstant(NewIntNo, MVT::i32),
6062 Op.getOperand(1), ShAmt);
6063 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006064 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006065}
Evan Cheng72261582005-12-20 06:22:03 +00006066
Dan Gohman475871a2008-07-27 21:46:04 +00006067SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006068 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006069 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006070
6071 if (Depth > 0) {
6072 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6073 SDValue Offset =
6074 DAG.getConstant(TD->getPointerSize(),
6075 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006076 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006077 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006078 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006079 NULL, 0);
6080 }
6081
6082 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006083 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006084 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006085 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006086}
6087
Dan Gohman475871a2008-07-27 21:46:04 +00006088SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006089 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6090 MFI->setFrameAddressIsTaken(true);
6091 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006092 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006093 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6094 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006095 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006096 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006097 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006098 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006099}
6100
Dan Gohman475871a2008-07-27 21:46:04 +00006101SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006102 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006103 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006104}
6105
Dan Gohman475871a2008-07-27 21:46:04 +00006106SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006107{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006108 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006109 SDValue Chain = Op.getOperand(0);
6110 SDValue Offset = Op.getOperand(1);
6111 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006112 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006113
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006114 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6115 getPointerTy());
6116 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006117
Dale Johannesene4d209d2009-02-03 20:21:25 +00006118 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006119 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006120 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6121 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006122 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006123 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006124
Dale Johannesene4d209d2009-02-03 20:21:25 +00006125 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006126 MVT::Other,
6127 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006128}
6129
Dan Gohman475871a2008-07-27 21:46:04 +00006130SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006131 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006132 SDValue Root = Op.getOperand(0);
6133 SDValue Trmp = Op.getOperand(1); // trampoline
6134 SDValue FPtr = Op.getOperand(2); // nested function
6135 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006136 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006137
Dan Gohman69de1932008-02-06 22:27:42 +00006138 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006139
Duncan Sands339e14f2008-01-16 22:55:25 +00006140 const X86InstrInfo *TII =
6141 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6142
Duncan Sandsb116fac2007-07-27 20:02:49 +00006143 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006144 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006145
6146 // Large code-model.
6147
6148 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6149 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6150
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006151 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6152 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006153
6154 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6155
6156 // Load the pointer to the nested function into R11.
6157 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006158 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006159 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6160 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006161
Scott Michelfdc40a02009-02-17 22:15:04 +00006162 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006163 DAG.getConstant(2, MVT::i64));
6164 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006165
6166 // Load the 'nest' parameter value into R10.
6167 // R10 is specified in X86CallingConv.td
6168 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006169 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006170 DAG.getConstant(10, MVT::i64));
6171 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6172 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006173
Scott Michelfdc40a02009-02-17 22:15:04 +00006174 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006175 DAG.getConstant(12, MVT::i64));
6176 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006177
6178 // Jump to the nested function.
6179 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006180 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006181 DAG.getConstant(20, MVT::i64));
6182 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6183 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006184
6185 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006186 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006187 DAG.getConstant(22, MVT::i64));
6188 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006189 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006190
Dan Gohman475871a2008-07-27 21:46:04 +00006191 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006192 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6193 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006194 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006195 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006196 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6197 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006198 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006199
6200 switch (CC) {
6201 default:
6202 assert(0 && "Unsupported calling convention");
6203 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006204 case CallingConv::X86_StdCall: {
6205 // Pass 'nest' parameter in ECX.
6206 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006207 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006208
6209 // Check that ECX wasn't needed by an 'inreg' parameter.
6210 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006211 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006212
Chris Lattner58d74912008-03-12 17:45:29 +00006213 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006214 unsigned InRegCount = 0;
6215 unsigned Idx = 1;
6216
6217 for (FunctionType::param_iterator I = FTy->param_begin(),
6218 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006219 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006220 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006221 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006222
6223 if (InRegCount > 2) {
6224 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6225 abort();
6226 }
6227 }
6228 break;
6229 }
6230 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006231 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006232 // Pass 'nest' parameter in EAX.
6233 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006234 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006235 break;
6236 }
6237
Dan Gohman475871a2008-07-27 21:46:04 +00006238 SDValue OutChains[4];
6239 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006240
Scott Michelfdc40a02009-02-17 22:15:04 +00006241 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006242 DAG.getConstant(10, MVT::i32));
6243 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006244
Duncan Sands339e14f2008-01-16 22:55:25 +00006245 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006246 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006247 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006248 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006249 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006250
Scott Michelfdc40a02009-02-17 22:15:04 +00006251 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006252 DAG.getConstant(1, MVT::i32));
6253 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006254
Duncan Sands339e14f2008-01-16 22:55:25 +00006255 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006256 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006257 DAG.getConstant(5, MVT::i32));
6258 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006259 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006260
Scott Michelfdc40a02009-02-17 22:15:04 +00006261 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006262 DAG.getConstant(6, MVT::i32));
6263 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006264
Dan Gohman475871a2008-07-27 21:46:04 +00006265 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006266 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6267 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006268 }
6269}
6270
Dan Gohman475871a2008-07-27 21:46:04 +00006271SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006272 /*
6273 The rounding mode is in bits 11:10 of FPSR, and has the following
6274 settings:
6275 00 Round to nearest
6276 01 Round to -inf
6277 10 Round to +inf
6278 11 Round to 0
6279
6280 FLT_ROUNDS, on the other hand, expects the following:
6281 -1 Undefined
6282 0 Round to 0
6283 1 Round to nearest
6284 2 Round to +inf
6285 3 Round to -inf
6286
6287 To perform the conversion, we do:
6288 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6289 */
6290
6291 MachineFunction &MF = DAG.getMachineFunction();
6292 const TargetMachine &TM = MF.getTarget();
6293 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6294 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006295 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006296 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006297
6298 // Save FP Control Word to stack slot
6299 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006300 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006301
Dale Johannesene4d209d2009-02-03 20:21:25 +00006302 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006303 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006304
6305 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006306 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006307
6308 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006309 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006310 DAG.getNode(ISD::SRL, dl, MVT::i16,
6311 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006312 CWD, DAG.getConstant(0x800, MVT::i16)),
6313 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006314 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006315 DAG.getNode(ISD::SRL, dl, MVT::i16,
6316 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006317 CWD, DAG.getConstant(0x400, MVT::i16)),
6318 DAG.getConstant(9, MVT::i8));
6319
Dan Gohman475871a2008-07-27 21:46:04 +00006320 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006321 DAG.getNode(ISD::AND, dl, MVT::i16,
6322 DAG.getNode(ISD::ADD, dl, MVT::i16,
6323 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006324 DAG.getConstant(1, MVT::i16)),
6325 DAG.getConstant(3, MVT::i16));
6326
6327
Duncan Sands83ec4b62008-06-06 12:08:01 +00006328 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006329 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006330}
6331
Dan Gohman475871a2008-07-27 21:46:04 +00006332SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006333 MVT VT = Op.getValueType();
6334 MVT OpVT = VT;
6335 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006336 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006337
6338 Op = Op.getOperand(0);
6339 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006340 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006341 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006342 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006343 }
Evan Cheng18efe262007-12-14 02:13:44 +00006344
Evan Cheng152804e2007-12-14 08:30:15 +00006345 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6346 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006347 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006348
6349 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006350 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006351 Ops.push_back(Op);
6352 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6353 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6354 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006355 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006356
6357 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006358 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006359
Evan Cheng18efe262007-12-14 02:13:44 +00006360 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006361 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006362 return Op;
6363}
6364
Dan Gohman475871a2008-07-27 21:46:04 +00006365SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006366 MVT VT = Op.getValueType();
6367 MVT OpVT = VT;
6368 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006369 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006370
6371 Op = Op.getOperand(0);
6372 if (VT == MVT::i8) {
6373 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006374 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006375 }
Evan Cheng152804e2007-12-14 08:30:15 +00006376
6377 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6378 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006379 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006380
6381 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006382 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006383 Ops.push_back(Op);
6384 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6385 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6386 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006387 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006388
Evan Cheng18efe262007-12-14 02:13:44 +00006389 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006390 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006391 return Op;
6392}
6393
Mon P Wangaf9b9522008-12-18 21:42:19 +00006394SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6395 MVT VT = Op.getValueType();
6396 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006397 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006398
Mon P Wangaf9b9522008-12-18 21:42:19 +00006399 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6400 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6401 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6402 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6403 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6404 //
6405 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6406 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6407 // return AloBlo + AloBhi + AhiBlo;
6408
6409 SDValue A = Op.getOperand(0);
6410 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006411
Dale Johannesene4d209d2009-02-03 20:21:25 +00006412 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006413 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6414 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006415 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006416 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6417 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006418 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006419 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6420 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006421 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006422 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6423 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006424 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006425 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6426 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006427 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006428 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6429 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006430 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006431 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6432 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006433 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6434 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006435 return Res;
6436}
6437
6438
Bill Wendling74c37652008-12-09 22:08:41 +00006439SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6440 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6441 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006442 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6443 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006444 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006445 SDValue LHS = N->getOperand(0);
6446 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006447 unsigned BaseOp = 0;
6448 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006449 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006450
6451 switch (Op.getOpcode()) {
6452 default: assert(0 && "Unknown ovf instruction!");
6453 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006454 // A subtract of one will be selected as a INC. Note that INC doesn't
6455 // set CF, so we can't do this for UADDO.
6456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6457 if (C->getAPIntValue() == 1) {
6458 BaseOp = X86ISD::INC;
6459 Cond = X86::COND_O;
6460 break;
6461 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006462 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006463 Cond = X86::COND_O;
6464 break;
6465 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006466 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006467 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006468 break;
6469 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006470 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6471 // set CF, so we can't do this for USUBO.
6472 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6473 if (C->getAPIntValue() == 1) {
6474 BaseOp = X86ISD::DEC;
6475 Cond = X86::COND_O;
6476 break;
6477 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006478 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006479 Cond = X86::COND_O;
6480 break;
6481 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006482 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006483 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006484 break;
6485 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006486 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006487 Cond = X86::COND_O;
6488 break;
6489 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006490 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006491 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006492 break;
6493 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006494
Bill Wendling61edeb52008-12-02 01:06:39 +00006495 // Also sets EFLAGS.
6496 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006497 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006498
Bill Wendling61edeb52008-12-02 01:06:39 +00006499 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006500 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006501 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006502
Bill Wendling61edeb52008-12-02 01:06:39 +00006503 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6504 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006505}
6506
Dan Gohman475871a2008-07-27 21:46:04 +00006507SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006508 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006509 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006510 unsigned Reg = 0;
6511 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006512 switch(T.getSimpleVT()) {
6513 default:
6514 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006515 case MVT::i8: Reg = X86::AL; size = 1; break;
6516 case MVT::i16: Reg = X86::AX; size = 2; break;
6517 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006518 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006519 assert(Subtarget->is64Bit() && "Node not type legal!");
6520 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006521 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006522 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006523 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006524 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006525 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006526 Op.getOperand(1),
6527 Op.getOperand(3),
6528 DAG.getTargetConstant(size, MVT::i8),
6529 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006530 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006531 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006532 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006533 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006534 return cpOut;
6535}
6536
Duncan Sands1607f052008-12-01 11:39:25 +00006537SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006538 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006539 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006540 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006541 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006542 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006543 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006544 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6545 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006546 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006547 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006548 DAG.getConstant(32, MVT::i8));
6549 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006550 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006551 rdx.getValue(1)
6552 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006553 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006554}
6555
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006556SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6557 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006558 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006559 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006560 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006561 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006562 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006563 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006564 Node->getOperand(0),
6565 Node->getOperand(1), negOp,
6566 cast<AtomicSDNode>(Node)->getSrcValue(),
6567 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006568}
6569
Evan Cheng0db9fe62006-04-25 20:13:52 +00006570/// LowerOperation - Provide custom lowering hooks for some operations.
6571///
Dan Gohman475871a2008-07-27 21:46:04 +00006572SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573 switch (Op.getOpcode()) {
6574 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006575 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6576 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6578 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6579 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6580 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6581 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6582 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6583 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006584 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006585 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006586 case ISD::SHL_PARTS:
6587 case ISD::SRA_PARTS:
6588 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6589 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006590 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006591 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006592 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006593 case ISD::FABS: return LowerFABS(Op, DAG);
6594 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006595 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006596 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006597 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006598 case ISD::SELECT: return LowerSELECT(Op, DAG);
6599 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006601 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006603 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006604 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006605 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006606 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006608 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6609 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006610 case ISD::FRAME_TO_ARGS_OFFSET:
6611 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006612 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006613 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006614 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006615 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006616 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6617 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006618 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006619 case ISD::SADDO:
6620 case ISD::UADDO:
6621 case ISD::SSUBO:
6622 case ISD::USUBO:
6623 case ISD::SMULO:
6624 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006625 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006626 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006627}
6628
Duncan Sands1607f052008-12-01 11:39:25 +00006629void X86TargetLowering::
6630ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6631 SelectionDAG &DAG, unsigned NewOp) {
6632 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006633 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006634 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6635
6636 SDValue Chain = Node->getOperand(0);
6637 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006638 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006639 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006640 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006641 Node->getOperand(2), DAG.getIntPtrConstant(1));
6642 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6643 // have a MemOperand. Pass the info through as a normal operand.
6644 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6645 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6646 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006647 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006648 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006649 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006650 Results.push_back(Result.getValue(2));
6651}
6652
Duncan Sands126d9072008-07-04 11:47:58 +00006653/// ReplaceNodeResults - Replace a node with an illegal result type
6654/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006655void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6656 SmallVectorImpl<SDValue>&Results,
6657 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006658 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006659 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006660 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006661 assert(false && "Do not know how to custom type legalize this operation!");
6662 return;
6663 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006664 std::pair<SDValue,SDValue> Vals =
6665 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006666 SDValue FIST = Vals.first, StackSlot = Vals.second;
6667 if (FIST.getNode() != 0) {
6668 MVT VT = N->getValueType(0);
6669 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006670 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006671 }
6672 return;
6673 }
6674 case ISD::READCYCLECOUNTER: {
6675 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6676 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006677 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006678 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006679 rd.getValue(1));
6680 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006681 eax.getValue(2));
6682 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6683 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006684 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006685 Results.push_back(edx.getValue(1));
6686 return;
6687 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006688 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006689 MVT T = N->getValueType(0);
6690 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6691 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006692 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006693 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006694 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006695 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006696 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6697 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006698 cpInL.getValue(1));
6699 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006700 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006701 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006702 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006703 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006704 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006705 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006706 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006707 swapInL.getValue(1));
6708 SDValue Ops[] = { swapInH.getValue(0),
6709 N->getOperand(1),
6710 swapInH.getValue(1) };
6711 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006712 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006713 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6714 MVT::i32, Result.getValue(1));
6715 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6716 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006717 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006718 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006719 Results.push_back(cpOutH.getValue(1));
6720 return;
6721 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006722 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006723 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6724 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006725 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006726 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6727 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006728 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006729 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6730 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006731 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006732 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6733 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006734 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006735 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6736 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006737 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006738 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6739 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006740 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006741 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6742 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006743 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744}
6745
Evan Cheng72261582005-12-20 06:22:03 +00006746const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6747 switch (Opcode) {
6748 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006749 case X86ISD::BSF: return "X86ISD::BSF";
6750 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006751 case X86ISD::SHLD: return "X86ISD::SHLD";
6752 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006753 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006754 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006755 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006756 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006757 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006758 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006759 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6760 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6761 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006762 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006763 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006764 case X86ISD::CALL: return "X86ISD::CALL";
6765 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6766 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006767 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006768 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006769 case X86ISD::COMI: return "X86ISD::COMI";
6770 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006771 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006772 case X86ISD::CMOV: return "X86ISD::CMOV";
6773 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006774 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006775 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6776 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006777 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006778 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006779 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006780 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006781 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6782 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006783 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006784 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006785 case X86ISD::FMAX: return "X86ISD::FMAX";
6786 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006787 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6788 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006789 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006790 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006791 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006792 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006793 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006794 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6795 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006796 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6797 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6798 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6799 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6800 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6801 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006802 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6803 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006804 case X86ISD::VSHL: return "X86ISD::VSHL";
6805 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006806 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6807 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6808 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6809 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6810 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6811 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6812 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6813 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6814 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6815 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006816 case X86ISD::ADD: return "X86ISD::ADD";
6817 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006818 case X86ISD::SMUL: return "X86ISD::SMUL";
6819 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006820 case X86ISD::INC: return "X86ISD::INC";
6821 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006822 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006823 }
6824}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006825
Chris Lattnerc9addb72007-03-30 23:15:24 +00006826// isLegalAddressingMode - Return true if the addressing mode represented
6827// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006828bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006829 const Type *Ty) const {
6830 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006831
Chris Lattnerc9addb72007-03-30 23:15:24 +00006832 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6833 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6834 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006835
Chris Lattnerc9addb72007-03-30 23:15:24 +00006836 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006837 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006838 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6839 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006840 // If BaseGV requires a register, we cannot also have a BaseReg.
6841 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6842 AM.HasBaseReg)
6843 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006844
6845 // X86-64 only supports addr of globals in small code model.
6846 if (Subtarget->is64Bit()) {
6847 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6848 return false;
6849 // If lower 4G is not available, then we must use rip-relative addressing.
6850 if (AM.BaseOffs || AM.Scale > 1)
6851 return false;
6852 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006853 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006854
Chris Lattnerc9addb72007-03-30 23:15:24 +00006855 switch (AM.Scale) {
6856 case 0:
6857 case 1:
6858 case 2:
6859 case 4:
6860 case 8:
6861 // These scales always work.
6862 break;
6863 case 3:
6864 case 5:
6865 case 9:
6866 // These scales are formed with basereg+scalereg. Only accept if there is
6867 // no basereg yet.
6868 if (AM.HasBaseReg)
6869 return false;
6870 break;
6871 default: // Other stuff never works.
6872 return false;
6873 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006874
Chris Lattnerc9addb72007-03-30 23:15:24 +00006875 return true;
6876}
6877
6878
Evan Cheng2bd122c2007-10-26 01:56:11 +00006879bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6880 if (!Ty1->isInteger() || !Ty2->isInteger())
6881 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006882 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6883 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006884 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006885 return false;
6886 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006887}
6888
Duncan Sands83ec4b62008-06-06 12:08:01 +00006889bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6890 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006891 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006892 unsigned NumBits1 = VT1.getSizeInBits();
6893 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006894 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006895 return false;
6896 return Subtarget->is64Bit() || NumBits1 < 64;
6897}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006898
Dan Gohman97121ba2009-04-08 00:15:30 +00006899bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006900 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006901 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6902}
6903
6904bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006905 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006906 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6907}
6908
Evan Cheng60c07e12006-07-05 22:17:51 +00006909/// isShuffleMaskLegal - Targets can use this to indicate that they only
6910/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6911/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6912/// are assumed to be legal.
6913bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006914X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6915 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006916 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006917 if (VT.getSizeInBits() == 64)
6918 return false;
6919
6920 // FIXME: pshufb, blends, palignr, shifts.
6921 return (VT.getVectorNumElements() == 2 ||
6922 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6923 isMOVLMask(M, VT) ||
6924 isSHUFPMask(M, VT) ||
6925 isPSHUFDMask(M, VT) ||
6926 isPSHUFHWMask(M, VT) ||
6927 isPSHUFLWMask(M, VT) ||
6928 isUNPCKLMask(M, VT) ||
6929 isUNPCKHMask(M, VT) ||
6930 isUNPCKL_v_undef_Mask(M, VT) ||
6931 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006932}
6933
Dan Gohman7d8143f2008-04-09 20:09:42 +00006934bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006935X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006936 MVT VT) const {
6937 unsigned NumElts = VT.getVectorNumElements();
6938 // FIXME: This collection of masks seems suspect.
6939 if (NumElts == 2)
6940 return true;
6941 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6942 return (isMOVLMask(Mask, VT) ||
6943 isCommutedMOVLMask(Mask, VT, true) ||
6944 isSHUFPMask(Mask, VT) ||
6945 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006946 }
6947 return false;
6948}
6949
6950//===----------------------------------------------------------------------===//
6951// X86 Scheduler Hooks
6952//===----------------------------------------------------------------------===//
6953
Mon P Wang63307c32008-05-05 19:05:59 +00006954// private utility function
6955MachineBasicBlock *
6956X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6957 MachineBasicBlock *MBB,
6958 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006959 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006960 unsigned LoadOpc,
6961 unsigned CXchgOpc,
6962 unsigned copyOpc,
6963 unsigned notOpc,
6964 unsigned EAXreg,
6965 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006966 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006967 // For the atomic bitwise operator, we generate
6968 // thisMBB:
6969 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006970 // ld t1 = [bitinstr.addr]
6971 // op t2 = t1, [bitinstr.val]
6972 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006973 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6974 // bz newMBB
6975 // fallthrough -->nextMBB
6976 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6977 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006978 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006979 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006980
Mon P Wang63307c32008-05-05 19:05:59 +00006981 /// First build the CFG
6982 MachineFunction *F = MBB->getParent();
6983 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006984 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6985 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6986 F->insert(MBBIter, newMBB);
6987 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006988
Mon P Wang63307c32008-05-05 19:05:59 +00006989 // Move all successors to thisMBB to nextMBB
6990 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006991
Mon P Wang63307c32008-05-05 19:05:59 +00006992 // Update thisMBB to fall through to newMBB
6993 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006994
Mon P Wang63307c32008-05-05 19:05:59 +00006995 // newMBB jumps to itself and fall through to nextMBB
6996 newMBB->addSuccessor(nextMBB);
6997 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006998
Mon P Wang63307c32008-05-05 19:05:59 +00006999 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007000 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
7001 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007002 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007003 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007004 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007005 int numArgs = bInstr->getNumOperands() - 1;
7006 for (int i=0; i < numArgs; ++i)
7007 argOpers[i] = &bInstr->getOperand(i+1);
7008
7009 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007010 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7011 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007012
Dale Johannesen140be2d2008-08-19 18:47:28 +00007013 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007014 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007015 for (int i=0; i <= lastAddrIndx; ++i)
7016 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007017
Dale Johannesen140be2d2008-08-19 18:47:28 +00007018 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007019 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007020 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007021 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007022 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007023 tt = t1;
7024
Dale Johannesen140be2d2008-08-19 18:47:28 +00007025 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007026 assert((argOpers[valArgIndx]->isReg() ||
7027 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007028 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007029 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007030 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007031 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007032 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007033 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007034 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007035
Dale Johannesene4d209d2009-02-03 20:21:25 +00007036 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007037 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007038
Dale Johannesene4d209d2009-02-03 20:21:25 +00007039 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007040 for (int i=0; i <= lastAddrIndx; ++i)
7041 (*MIB).addOperand(*argOpers[i]);
7042 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007043 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7044 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7045
Dale Johannesene4d209d2009-02-03 20:21:25 +00007046 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007047 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007048
Mon P Wang63307c32008-05-05 19:05:59 +00007049 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007050 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007051
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007052 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007053 return nextMBB;
7054}
7055
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007056// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007057MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007058X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7059 MachineBasicBlock *MBB,
7060 unsigned regOpcL,
7061 unsigned regOpcH,
7062 unsigned immOpcL,
7063 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007064 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007065 // For the atomic bitwise operator, we generate
7066 // thisMBB (instructions are in pairs, except cmpxchg8b)
7067 // ld t1,t2 = [bitinstr.addr]
7068 // newMBB:
7069 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7070 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007071 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007072 // mov ECX, EBX <- t5, t6
7073 // mov EAX, EDX <- t1, t2
7074 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7075 // mov t3, t4 <- EAX, EDX
7076 // bz newMBB
7077 // result in out1, out2
7078 // fallthrough -->nextMBB
7079
7080 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7081 const unsigned LoadOpc = X86::MOV32rm;
7082 const unsigned copyOpc = X86::MOV32rr;
7083 const unsigned NotOpc = X86::NOT32r;
7084 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7085 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7086 MachineFunction::iterator MBBIter = MBB;
7087 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007088
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007089 /// First build the CFG
7090 MachineFunction *F = MBB->getParent();
7091 MachineBasicBlock *thisMBB = MBB;
7092 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7093 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7094 F->insert(MBBIter, newMBB);
7095 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007096
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007097 // Move all successors to thisMBB to nextMBB
7098 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007099
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007100 // Update thisMBB to fall through to newMBB
7101 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007102
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007103 // newMBB jumps to itself and fall through to nextMBB
7104 newMBB->addSuccessor(nextMBB);
7105 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007106
Dale Johannesene4d209d2009-02-03 20:21:25 +00007107 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007108 // Insert instructions into newMBB based on incoming instruction
7109 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007110 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
7111 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007112 MachineOperand& dest1Oper = bInstr->getOperand(0);
7113 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007114 MachineOperand* argOpers[2 + X86AddrNumOperands];
7115 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007116 argOpers[i] = &bInstr->getOperand(i+2);
7117
7118 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007119 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007120
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007121 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007122 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007123 for (int i=0; i <= lastAddrIndx; ++i)
7124 (*MIB).addOperand(*argOpers[i]);
7125 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007126 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007127 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007128 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007129 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007130 MachineOperand newOp3 = *(argOpers[3]);
7131 if (newOp3.isImm())
7132 newOp3.setImm(newOp3.getImm()+4);
7133 else
7134 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007135 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007136 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007137
7138 // t3/4 are defined later, at the bottom of the loop
7139 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7140 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007141 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007142 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007143 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007144 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7145
7146 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7147 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007148 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007149 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7150 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007151 } else {
7152 tt1 = t1;
7153 tt2 = t2;
7154 }
7155
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007156 int valArgIndx = lastAddrIndx + 1;
7157 assert((argOpers[valArgIndx]->isReg() ||
7158 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007159 "invalid operand");
7160 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7161 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007162 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007163 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007164 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007165 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007166 if (regOpcL != X86::MOV32rr)
7167 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007168 (*MIB).addOperand(*argOpers[valArgIndx]);
7169 assert(argOpers[valArgIndx + 1]->isReg() ==
7170 argOpers[valArgIndx]->isReg());
7171 assert(argOpers[valArgIndx + 1]->isImm() ==
7172 argOpers[valArgIndx]->isImm());
7173 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007174 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007175 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007176 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007177 if (regOpcH != X86::MOV32rr)
7178 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007179 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007180
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007182 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007183 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007184 MIB.addReg(t2);
7185
Dale Johannesene4d209d2009-02-03 20:21:25 +00007186 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007187 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007188 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007189 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007190
Dale Johannesene4d209d2009-02-03 20:21:25 +00007191 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007192 for (int i=0; i <= lastAddrIndx; ++i)
7193 (*MIB).addOperand(*argOpers[i]);
7194
7195 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7196 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7197
Dale Johannesene4d209d2009-02-03 20:21:25 +00007198 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007199 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007200 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007201 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007202
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007203 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007204 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007205
7206 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7207 return nextMBB;
7208}
7209
7210// private utility function
7211MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007212X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7213 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007214 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007215 // For the atomic min/max operator, we generate
7216 // thisMBB:
7217 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007218 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007219 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007220 // cmp t1, t2
7221 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007222 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007223 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7224 // bz newMBB
7225 // fallthrough -->nextMBB
7226 //
7227 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7228 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007229 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007230 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007231
Mon P Wang63307c32008-05-05 19:05:59 +00007232 /// First build the CFG
7233 MachineFunction *F = MBB->getParent();
7234 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007235 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7236 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7237 F->insert(MBBIter, newMBB);
7238 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007239
Mon P Wang63307c32008-05-05 19:05:59 +00007240 // Move all successors to thisMBB to nextMBB
7241 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Mon P Wang63307c32008-05-05 19:05:59 +00007243 // Update thisMBB to fall through to newMBB
7244 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007245
Mon P Wang63307c32008-05-05 19:05:59 +00007246 // newMBB jumps to newMBB and fall through to nextMBB
7247 newMBB->addSuccessor(nextMBB);
7248 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007249
Dale Johannesene4d209d2009-02-03 20:21:25 +00007250 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007251 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007252 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
7253 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007254 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007255 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007256 int numArgs = mInstr->getNumOperands() - 1;
7257 for (int i=0; i < numArgs; ++i)
7258 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007259
Mon P Wang63307c32008-05-05 19:05:59 +00007260 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007261 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7262 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007263
Mon P Wangab3e7472008-05-05 22:56:23 +00007264 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007265 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007266 for (int i=0; i <= lastAddrIndx; ++i)
7267 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007268
Mon P Wang63307c32008-05-05 19:05:59 +00007269 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007270 assert((argOpers[valArgIndx]->isReg() ||
7271 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007272 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007273
7274 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007275 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007276 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007277 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007278 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007279 (*MIB).addOperand(*argOpers[valArgIndx]);
7280
Dale Johannesene4d209d2009-02-03 20:21:25 +00007281 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007282 MIB.addReg(t1);
7283
Dale Johannesene4d209d2009-02-03 20:21:25 +00007284 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007285 MIB.addReg(t1);
7286 MIB.addReg(t2);
7287
7288 // Generate movc
7289 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007290 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007291 MIB.addReg(t2);
7292 MIB.addReg(t1);
7293
7294 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007295 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007296 for (int i=0; i <= lastAddrIndx; ++i)
7297 (*MIB).addOperand(*argOpers[i]);
7298 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007299 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7300 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007301
Dale Johannesene4d209d2009-02-03 20:21:25 +00007302 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007303 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007304
Mon P Wang63307c32008-05-05 19:05:59 +00007305 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007306 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007307
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007308 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007309 return nextMBB;
7310}
7311
7312
Evan Cheng60c07e12006-07-05 22:17:51 +00007313MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007314X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007315 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007316 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007317 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007318 switch (MI->getOpcode()) {
7319 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007320 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007321 case X86::CMOV_FR32:
7322 case X86::CMOV_FR64:
7323 case X86::CMOV_V4F32:
7324 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007325 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007326 // To "insert" a SELECT_CC instruction, we actually have to insert the
7327 // diamond control-flow pattern. The incoming instruction knows the
7328 // destination vreg to set, the condition code register to branch on, the
7329 // true/false values to select between, and a branch opcode to use.
7330 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007331 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007332 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007333
Evan Cheng60c07e12006-07-05 22:17:51 +00007334 // thisMBB:
7335 // ...
7336 // TrueVal = ...
7337 // cmpTY ccX, r1, r2
7338 // bCC copy1MBB
7339 // fallthrough --> copy0MBB
7340 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007341 MachineFunction *F = BB->getParent();
7342 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7343 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007344 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007345 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007346 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007347 F->insert(It, copy0MBB);
7348 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007349 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007350 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007351 sinkMBB->transferSuccessors(BB);
7352
7353 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007354 BB->addSuccessor(copy0MBB);
7355 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007356
Evan Cheng60c07e12006-07-05 22:17:51 +00007357 // copy0MBB:
7358 // %FalseValue = ...
7359 // # fallthrough to sinkMBB
7360 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007361
Evan Cheng60c07e12006-07-05 22:17:51 +00007362 // Update machine-CFG edges
7363 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007364
Evan Cheng60c07e12006-07-05 22:17:51 +00007365 // sinkMBB:
7366 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7367 // ...
7368 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007369 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007370 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7371 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7372
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007373 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007374 return BB;
7375 }
7376
Dale Johannesen849f2142007-07-03 00:53:03 +00007377 case X86::FP32_TO_INT16_IN_MEM:
7378 case X86::FP32_TO_INT32_IN_MEM:
7379 case X86::FP32_TO_INT64_IN_MEM:
7380 case X86::FP64_TO_INT16_IN_MEM:
7381 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007382 case X86::FP64_TO_INT64_IN_MEM:
7383 case X86::FP80_TO_INT16_IN_MEM:
7384 case X86::FP80_TO_INT32_IN_MEM:
7385 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007386 // Change the floating point control register to use "round towards zero"
7387 // mode when truncating to an integer value.
7388 MachineFunction *F = BB->getParent();
7389 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007390 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007391
7392 // Load the old value of the high byte of the control word...
7393 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007394 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007395 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007396 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007397
7398 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007399 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007400 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007401
7402 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007403 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007404
7405 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007406 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007407 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007408
7409 // Get the X86 opcode to use.
7410 unsigned Opc;
7411 switch (MI->getOpcode()) {
7412 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007413 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7414 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7415 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7416 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7417 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7418 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007419 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7420 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7421 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007422 }
7423
7424 X86AddressMode AM;
7425 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007426 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007427 AM.BaseType = X86AddressMode::RegBase;
7428 AM.Base.Reg = Op.getReg();
7429 } else {
7430 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007431 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007432 }
7433 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007434 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007435 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007436 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007437 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007438 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007439 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007440 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007441 AM.GV = Op.getGlobal();
7442 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007443 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007444 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007445 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007446 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007447
7448 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007449 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007450
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007451 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007452 return BB;
7453 }
Mon P Wang63307c32008-05-05 19:05:59 +00007454 case X86::ATOMAND32:
7455 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007456 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007457 X86::LCMPXCHG32, X86::MOV32rr,
7458 X86::NOT32r, X86::EAX,
7459 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007460 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007461 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7462 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007463 X86::LCMPXCHG32, X86::MOV32rr,
7464 X86::NOT32r, X86::EAX,
7465 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007466 case X86::ATOMXOR32:
7467 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007468 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007469 X86::LCMPXCHG32, X86::MOV32rr,
7470 X86::NOT32r, X86::EAX,
7471 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007472 case X86::ATOMNAND32:
7473 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007474 X86::AND32ri, X86::MOV32rm,
7475 X86::LCMPXCHG32, X86::MOV32rr,
7476 X86::NOT32r, X86::EAX,
7477 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007478 case X86::ATOMMIN32:
7479 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7480 case X86::ATOMMAX32:
7481 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7482 case X86::ATOMUMIN32:
7483 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7484 case X86::ATOMUMAX32:
7485 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007486
7487 case X86::ATOMAND16:
7488 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7489 X86::AND16ri, X86::MOV16rm,
7490 X86::LCMPXCHG16, X86::MOV16rr,
7491 X86::NOT16r, X86::AX,
7492 X86::GR16RegisterClass);
7493 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007494 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007495 X86::OR16ri, X86::MOV16rm,
7496 X86::LCMPXCHG16, X86::MOV16rr,
7497 X86::NOT16r, X86::AX,
7498 X86::GR16RegisterClass);
7499 case X86::ATOMXOR16:
7500 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7501 X86::XOR16ri, X86::MOV16rm,
7502 X86::LCMPXCHG16, X86::MOV16rr,
7503 X86::NOT16r, X86::AX,
7504 X86::GR16RegisterClass);
7505 case X86::ATOMNAND16:
7506 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7507 X86::AND16ri, X86::MOV16rm,
7508 X86::LCMPXCHG16, X86::MOV16rr,
7509 X86::NOT16r, X86::AX,
7510 X86::GR16RegisterClass, true);
7511 case X86::ATOMMIN16:
7512 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7513 case X86::ATOMMAX16:
7514 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7515 case X86::ATOMUMIN16:
7516 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7517 case X86::ATOMUMAX16:
7518 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7519
7520 case X86::ATOMAND8:
7521 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7522 X86::AND8ri, X86::MOV8rm,
7523 X86::LCMPXCHG8, X86::MOV8rr,
7524 X86::NOT8r, X86::AL,
7525 X86::GR8RegisterClass);
7526 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007527 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007528 X86::OR8ri, X86::MOV8rm,
7529 X86::LCMPXCHG8, X86::MOV8rr,
7530 X86::NOT8r, X86::AL,
7531 X86::GR8RegisterClass);
7532 case X86::ATOMXOR8:
7533 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7534 X86::XOR8ri, X86::MOV8rm,
7535 X86::LCMPXCHG8, X86::MOV8rr,
7536 X86::NOT8r, X86::AL,
7537 X86::GR8RegisterClass);
7538 case X86::ATOMNAND8:
7539 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7540 X86::AND8ri, X86::MOV8rm,
7541 X86::LCMPXCHG8, X86::MOV8rr,
7542 X86::NOT8r, X86::AL,
7543 X86::GR8RegisterClass, true);
7544 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007545 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007546 case X86::ATOMAND64:
7547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007548 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007549 X86::LCMPXCHG64, X86::MOV64rr,
7550 X86::NOT64r, X86::RAX,
7551 X86::GR64RegisterClass);
7552 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7554 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007555 X86::LCMPXCHG64, X86::MOV64rr,
7556 X86::NOT64r, X86::RAX,
7557 X86::GR64RegisterClass);
7558 case X86::ATOMXOR64:
7559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007560 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007561 X86::LCMPXCHG64, X86::MOV64rr,
7562 X86::NOT64r, X86::RAX,
7563 X86::GR64RegisterClass);
7564 case X86::ATOMNAND64:
7565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7566 X86::AND64ri32, X86::MOV64rm,
7567 X86::LCMPXCHG64, X86::MOV64rr,
7568 X86::NOT64r, X86::RAX,
7569 X86::GR64RegisterClass, true);
7570 case X86::ATOMMIN64:
7571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7572 case X86::ATOMMAX64:
7573 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7574 case X86::ATOMUMIN64:
7575 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7576 case X86::ATOMUMAX64:
7577 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007578
7579 // This group does 64-bit operations on a 32-bit host.
7580 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007581 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007582 X86::AND32rr, X86::AND32rr,
7583 X86::AND32ri, X86::AND32ri,
7584 false);
7585 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007586 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007587 X86::OR32rr, X86::OR32rr,
7588 X86::OR32ri, X86::OR32ri,
7589 false);
7590 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007591 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007592 X86::XOR32rr, X86::XOR32rr,
7593 X86::XOR32ri, X86::XOR32ri,
7594 false);
7595 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007596 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007597 X86::AND32rr, X86::AND32rr,
7598 X86::AND32ri, X86::AND32ri,
7599 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007600 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007601 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007602 X86::ADD32rr, X86::ADC32rr,
7603 X86::ADD32ri, X86::ADC32ri,
7604 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007605 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007606 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007607 X86::SUB32rr, X86::SBB32rr,
7608 X86::SUB32ri, X86::SBB32ri,
7609 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007610 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007611 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007612 X86::MOV32rr, X86::MOV32rr,
7613 X86::MOV32ri, X86::MOV32ri,
7614 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007615 }
7616}
7617
7618//===----------------------------------------------------------------------===//
7619// X86 Optimization Hooks
7620//===----------------------------------------------------------------------===//
7621
Dan Gohman475871a2008-07-27 21:46:04 +00007622void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007623 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007624 APInt &KnownZero,
7625 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007626 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007627 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007628 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007629 assert((Opc >= ISD::BUILTIN_OP_END ||
7630 Opc == ISD::INTRINSIC_WO_CHAIN ||
7631 Opc == ISD::INTRINSIC_W_CHAIN ||
7632 Opc == ISD::INTRINSIC_VOID) &&
7633 "Should use MaskedValueIsZero if you don't know whether Op"
7634 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007635
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007636 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007637 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007638 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007639 case X86ISD::ADD:
7640 case X86ISD::SUB:
7641 case X86ISD::SMUL:
7642 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007643 case X86ISD::INC:
7644 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007645 // These nodes' second result is a boolean.
7646 if (Op.getResNo() == 0)
7647 break;
7648 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007649 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007650 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7651 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007652 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007653 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007654}
Chris Lattner259e97c2006-01-31 19:43:35 +00007655
Evan Cheng206ee9d2006-07-07 08:33:52 +00007656/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007657/// node is a GlobalAddress + offset.
7658bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7659 GlobalValue* &GA, int64_t &Offset) const{
7660 if (N->getOpcode() == X86ISD::Wrapper) {
7661 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007662 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007663 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007664 return true;
7665 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007666 }
Evan Chengad4196b2008-05-12 19:56:52 +00007667 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007668}
7669
Evan Chengad4196b2008-05-12 19:56:52 +00007670static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7671 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007672 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007673 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007674 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007675 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007676 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007677 return false;
7678}
7679
Nate Begeman9008ca62009-04-27 18:41:29 +00007680static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7681 MVT EVT, SDNode *&Base,
Evan Chengad4196b2008-05-12 19:56:52 +00007682 SelectionDAG &DAG, MachineFrameInfo *MFI,
7683 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007684 Base = NULL;
7685 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007686 if (N->getMaskElt(i) < 0) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007687 if (!Base)
7688 return false;
7689 continue;
7690 }
7691
Dan Gohman475871a2008-07-27 21:46:04 +00007692 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007693 if (!Elt.getNode() ||
7694 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007695 return false;
7696 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007697 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007698 if (Base->getOpcode() == ISD::UNDEF)
7699 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007700 continue;
7701 }
7702 if (Elt.getOpcode() == ISD::UNDEF)
7703 continue;
7704
Gabor Greifba36cb52008-08-28 21:40:38 +00007705 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007706 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007707 return false;
7708 }
7709 return true;
7710}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007711
7712/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7713/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7714/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007715/// order. In the case of v2i64, it will see if it can rewrite the
7716/// shuffle to be an appropriate build vector so it can take advantage of
7717// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007718static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007719 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007720 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007721 MVT VT = N->getValueType(0);
7722 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007723 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7724 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007725
7726 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7727 // where the upper half is 0, it is advantageous to rewrite it as a build
7728 // vector of (0, val) so it can use movq.
7729 if (VT == MVT::v2i64) {
7730 SDValue In[2];
7731 In[0] = N->getOperand(0);
7732 In[1] = N->getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00007733 int Idx0 = SVN->getMaskElt(0);
7734 int Idx1 = SVN->getMaskElt(1);
7735 // FIXME: can we take advantage of undef index?
7736 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang1e955802009-04-03 02:43:30 +00007737 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7738 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7739 ConstantSDNode* InsertVecIdx =
7740 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7741 if (InsertVecIdx &&
Nate Begeman9008ca62009-04-27 18:41:29 +00007742 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang1e955802009-04-03 02:43:30 +00007743 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7744 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7745 In[Idx0/2].getOperand(1),
7746 In[Idx1/2].getOperand(Idx1 % 2));
7747 }
7748 }
7749 }
7750
7751 // Try to combine a vector_shuffle into a 128-bit load.
7752 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007753 SDNode *Base = NULL;
Nate Begeman9008ca62009-04-27 18:41:29 +00007754 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007755 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007756
Dan Gohmand3006222007-07-27 17:16:43 +00007757 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007758 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007759 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007760 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007761 LD->isVolatile());
7762 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7763 LD->getSrcValue(), LD->getSrcValueOffset(),
7764 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007765}
7766
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007767/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007768static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007769 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007770 const X86Subtarget *Subtarget,
7771 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007772 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007773 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007774
Evan Chengd880b972008-05-09 21:53:03 +00007775 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007776 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007777 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007778
Duncan Sands83ec4b62008-06-06 12:08:01 +00007779 MVT VT = N->getValueType(0);
7780 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00007781 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7782 // We are looking for load i64 and zero extend. We want to transform
7783 // it before legalizer has a chance to expand it. Also look for i64
7784 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00007785 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007786 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00007787 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00007788 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007789 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007790
7791 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00007792 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00007793 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007794 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00007795 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007796 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007797 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00007798 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007799 }
Evan Chengd880b972008-05-09 21:53:03 +00007800
7801 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007802 LoadSDNode *LD = cast<LoadSDNode>(Base);
Scott Michelfdc40a02009-02-17 22:15:04 +00007803
Nate Begemanf7333bf2008-05-28 00:24:25 +00007804 // Load must not be an extload.
7805 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00007806 return SDValue();
Mon P Wang7ad9b512009-01-30 07:07:40 +00007807
7808 // Load type should legal type so we don't have to legalize it.
7809 if (!TLI.isTypeLegal(VT))
7810 return SDValue();
7811
Evan Cheng8a186ae2008-09-24 23:26:36 +00007812 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7813 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007814 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007815 TargetLowering::TargetLoweringOpt TLO(DAG);
7816 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7817 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng8a186ae2008-09-24 23:26:36 +00007818 return ResNode;
Scott Michelfdc40a02009-02-17 22:15:04 +00007819}
Evan Chengd880b972008-05-09 21:53:03 +00007820
Chris Lattner83e6c992006-10-04 06:57:07 +00007821/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007822static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007823 const X86Subtarget *Subtarget) {
7824 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007825 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007826 // Get the LHS/RHS of the select.
7827 SDValue LHS = N->getOperand(1);
7828 SDValue RHS = N->getOperand(2);
7829
Chris Lattner83e6c992006-10-04 06:57:07 +00007830 // If we have SSE[12] support, try to form min/max nodes.
7831 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007832 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7833 Cond.getOpcode() == ISD::SETCC) {
7834 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007835
Chris Lattner47b4ce82009-03-11 05:48:52 +00007836 unsigned Opcode = 0;
7837 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7838 switch (CC) {
7839 default: break;
7840 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7841 case ISD::SETULE:
7842 case ISD::SETLE:
7843 if (!UnsafeFPMath) break;
7844 // FALL THROUGH.
7845 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7846 case ISD::SETLT:
7847 Opcode = X86ISD::FMIN;
7848 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007849
Chris Lattner47b4ce82009-03-11 05:48:52 +00007850 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7851 case ISD::SETUGT:
7852 case ISD::SETGT:
7853 if (!UnsafeFPMath) break;
7854 // FALL THROUGH.
7855 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7856 case ISD::SETGE:
7857 Opcode = X86ISD::FMAX;
7858 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007859 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007860 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7861 switch (CC) {
7862 default: break;
7863 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7864 case ISD::SETUGT:
7865 case ISD::SETGT:
7866 if (!UnsafeFPMath) break;
7867 // FALL THROUGH.
7868 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7869 case ISD::SETGE:
7870 Opcode = X86ISD::FMIN;
7871 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007872
Chris Lattner47b4ce82009-03-11 05:48:52 +00007873 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7874 case ISD::SETULE:
7875 case ISD::SETLE:
7876 if (!UnsafeFPMath) break;
7877 // FALL THROUGH.
7878 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7879 case ISD::SETLT:
7880 Opcode = X86ISD::FMAX;
7881 break;
7882 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007883 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007884
Chris Lattner47b4ce82009-03-11 05:48:52 +00007885 if (Opcode)
7886 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007887 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007888
Chris Lattnerd1980a52009-03-12 06:52:53 +00007889 // If this is a select between two integer constants, try to do some
7890 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007891 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7892 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007893 // Don't do this for crazy integer types.
7894 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7895 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007896 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007897 bool NeedsCondInvert = false;
7898
Chris Lattnercee56e72009-03-13 05:53:31 +00007899 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007900 // Efficiently invertible.
7901 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7902 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7903 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7904 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007905 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007906 }
7907
7908 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007909 if (FalseC->getAPIntValue() == 0 &&
7910 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007911 if (NeedsCondInvert) // Invert the condition if needed.
7912 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7913 DAG.getConstant(1, Cond.getValueType()));
7914
7915 // Zero extend the condition if needed.
7916 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7917
Chris Lattnercee56e72009-03-13 05:53:31 +00007918 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007919 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7920 DAG.getConstant(ShAmt, MVT::i8));
7921 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007922
7923 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007924 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007925 if (NeedsCondInvert) // Invert the condition if needed.
7926 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7927 DAG.getConstant(1, Cond.getValueType()));
7928
7929 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007930 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7931 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007932 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007933 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007934 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007935
7936 // Optimize cases that will turn into an LEA instruction. This requires
7937 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7938 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7939 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7940 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7941
7942 bool isFastMultiplier = false;
7943 if (Diff < 10) {
7944 switch ((unsigned char)Diff) {
7945 default: break;
7946 case 1: // result = add base, cond
7947 case 2: // result = lea base( , cond*2)
7948 case 3: // result = lea base(cond, cond*2)
7949 case 4: // result = lea base( , cond*4)
7950 case 5: // result = lea base(cond, cond*4)
7951 case 8: // result = lea base( , cond*8)
7952 case 9: // result = lea base(cond, cond*8)
7953 isFastMultiplier = true;
7954 break;
7955 }
7956 }
7957
7958 if (isFastMultiplier) {
7959 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7960 if (NeedsCondInvert) // Invert the condition if needed.
7961 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7962 DAG.getConstant(1, Cond.getValueType()));
7963
7964 // Zero extend the condition if needed.
7965 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7966 Cond);
7967 // Scale the condition by the difference.
7968 if (Diff != 1)
7969 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7970 DAG.getConstant(Diff, Cond.getValueType()));
7971
7972 // Add the base if non-zero.
7973 if (FalseC->getAPIntValue() != 0)
7974 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7975 SDValue(FalseC, 0));
7976 return Cond;
7977 }
7978 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007979 }
7980 }
7981
Dan Gohman475871a2008-07-27 21:46:04 +00007982 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007983}
7984
Chris Lattnerd1980a52009-03-12 06:52:53 +00007985/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7986static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7987 TargetLowering::DAGCombinerInfo &DCI) {
7988 DebugLoc DL = N->getDebugLoc();
7989
7990 // If the flag operand isn't dead, don't touch this CMOV.
7991 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
7992 return SDValue();
7993
7994 // If this is a select between two integer constants, try to do some
7995 // optimizations. Note that the operands are ordered the opposite of SELECT
7996 // operands.
7997 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
7998 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
7999 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8000 // larger than FalseC (the false value).
8001 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8002
8003 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8004 CC = X86::GetOppositeBranchCondition(CC);
8005 std::swap(TrueC, FalseC);
8006 }
8007
8008 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008009 // This is efficient for any integer data type (including i8/i16) and
8010 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008011 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8012 SDValue Cond = N->getOperand(3);
8013 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8014 DAG.getConstant(CC, MVT::i8), Cond);
8015
8016 // Zero extend the condition if needed.
8017 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8018
8019 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8020 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8021 DAG.getConstant(ShAmt, MVT::i8));
8022 if (N->getNumValues() == 2) // Dead flag value?
8023 return DCI.CombineTo(N, Cond, SDValue());
8024 return Cond;
8025 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008026
8027 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8028 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008029 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8030 SDValue Cond = N->getOperand(3);
8031 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8032 DAG.getConstant(CC, MVT::i8), Cond);
8033
8034 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008035 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8036 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008037 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8038 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008039
Chris Lattner97a29a52009-03-13 05:22:11 +00008040 if (N->getNumValues() == 2) // Dead flag value?
8041 return DCI.CombineTo(N, Cond, SDValue());
8042 return Cond;
8043 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008044
8045 // Optimize cases that will turn into an LEA instruction. This requires
8046 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8047 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8048 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8049 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8050
8051 bool isFastMultiplier = false;
8052 if (Diff < 10) {
8053 switch ((unsigned char)Diff) {
8054 default: break;
8055 case 1: // result = add base, cond
8056 case 2: // result = lea base( , cond*2)
8057 case 3: // result = lea base(cond, cond*2)
8058 case 4: // result = lea base( , cond*4)
8059 case 5: // result = lea base(cond, cond*4)
8060 case 8: // result = lea base( , cond*8)
8061 case 9: // result = lea base(cond, cond*8)
8062 isFastMultiplier = true;
8063 break;
8064 }
8065 }
8066
8067 if (isFastMultiplier) {
8068 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8069 SDValue Cond = N->getOperand(3);
8070 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8071 DAG.getConstant(CC, MVT::i8), Cond);
8072 // Zero extend the condition if needed.
8073 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8074 Cond);
8075 // Scale the condition by the difference.
8076 if (Diff != 1)
8077 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8078 DAG.getConstant(Diff, Cond.getValueType()));
8079
8080 // Add the base if non-zero.
8081 if (FalseC->getAPIntValue() != 0)
8082 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8083 SDValue(FalseC, 0));
8084 if (N->getNumValues() == 2) // Dead flag value?
8085 return DCI.CombineTo(N, Cond, SDValue());
8086 return Cond;
8087 }
8088 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008089 }
8090 }
8091 return SDValue();
8092}
8093
8094
Evan Cheng0b0cd912009-03-28 05:57:29 +00008095/// PerformMulCombine - Optimize a single multiply with constant into two
8096/// in order to implement it with two cheaper instructions, e.g.
8097/// LEA + SHL, LEA + LEA.
8098static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8099 TargetLowering::DAGCombinerInfo &DCI) {
8100 if (DAG.getMachineFunction().
8101 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8102 return SDValue();
8103
8104 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8105 return SDValue();
8106
8107 MVT VT = N->getValueType(0);
8108 if (VT != MVT::i64)
8109 return SDValue();
8110
8111 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8112 if (!C)
8113 return SDValue();
8114 uint64_t MulAmt = C->getZExtValue();
8115 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8116 return SDValue();
8117
8118 uint64_t MulAmt1 = 0;
8119 uint64_t MulAmt2 = 0;
8120 if ((MulAmt % 9) == 0) {
8121 MulAmt1 = 9;
8122 MulAmt2 = MulAmt / 9;
8123 } else if ((MulAmt % 5) == 0) {
8124 MulAmt1 = 5;
8125 MulAmt2 = MulAmt / 5;
8126 } else if ((MulAmt % 3) == 0) {
8127 MulAmt1 = 3;
8128 MulAmt2 = MulAmt / 3;
8129 }
8130 if (MulAmt2 &&
8131 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8132 DebugLoc DL = N->getDebugLoc();
8133
8134 if (isPowerOf2_64(MulAmt2) &&
8135 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8136 // If second multiplifer is pow2, issue it first. We want the multiply by
8137 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8138 // is an add.
8139 std::swap(MulAmt1, MulAmt2);
8140
8141 SDValue NewMul;
8142 if (isPowerOf2_64(MulAmt1))
8143 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8144 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8145 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008146 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008147 DAG.getConstant(MulAmt1, VT));
8148
8149 if (isPowerOf2_64(MulAmt2))
8150 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8151 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8152 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008153 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008154 DAG.getConstant(MulAmt2, VT));
8155
8156 // Do not add new nodes to DAG combiner worklist.
8157 DCI.CombineTo(N, NewMul, false);
8158 }
8159 return SDValue();
8160}
8161
8162
Nate Begeman740ab032009-01-26 00:52:55 +00008163/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8164/// when possible.
8165static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8166 const X86Subtarget *Subtarget) {
8167 // On X86 with SSE2 support, we can transform this to a vector shift if
8168 // all elements are shifted by the same amount. We can't do this in legalize
8169 // because the a constant vector is typically transformed to a constant pool
8170 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008171 if (!Subtarget->hasSSE2())
8172 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008173
Nate Begeman740ab032009-01-26 00:52:55 +00008174 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008175 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8176 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008177
Mon P Wang3becd092009-01-28 08:12:05 +00008178 SDValue ShAmtOp = N->getOperand(1);
8179 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008180 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008181 SDValue BaseShAmt;
8182 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8183 unsigned NumElts = VT.getVectorNumElements();
8184 unsigned i = 0;
8185 for (; i != NumElts; ++i) {
8186 SDValue Arg = ShAmtOp.getOperand(i);
8187 if (Arg.getOpcode() == ISD::UNDEF) continue;
8188 BaseShAmt = Arg;
8189 break;
8190 }
8191 for (; i != NumElts; ++i) {
8192 SDValue Arg = ShAmtOp.getOperand(i);
8193 if (Arg.getOpcode() == ISD::UNDEF) continue;
8194 if (Arg != BaseShAmt) {
8195 return SDValue();
8196 }
8197 }
8198 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008199 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8200 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8201 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008202 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008203 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008204
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008205 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008206 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008207 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008208 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008209
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008210 // The shift amount is identical so we can do a vector shift.
8211 SDValue ValOp = N->getOperand(0);
8212 switch (N->getOpcode()) {
8213 default:
8214 assert(0 && "Unknown shift opcode!");
8215 break;
8216 case ISD::SHL:
8217 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008218 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008219 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8220 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008221 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008222 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008223 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8224 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008225 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008226 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008227 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8228 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008229 break;
8230 case ISD::SRA:
8231 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008232 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008233 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8234 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008235 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008236 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008237 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8238 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008239 break;
8240 case ISD::SRL:
8241 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008242 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008243 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8244 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008245 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008246 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008247 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8248 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008249 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008250 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008251 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8252 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008253 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008254 }
8255 return SDValue();
8256}
8257
Chris Lattner149a4e52008-02-22 02:09:43 +00008258/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008259static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008260 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008261 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8262 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008263 // A preferable solution to the general problem is to figure out the right
8264 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008265
8266 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008267 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008268 MVT VT = St->getValue().getValueType();
8269 if (VT.getSizeInBits() != 64)
8270 return SDValue();
8271
8272 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloat && Subtarget->hasSSE2();
8273 if ((VT.isVector() ||
8274 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008275 isa<LoadSDNode>(St->getValue()) &&
8276 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8277 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008278 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008279 LoadSDNode *Ld = 0;
8280 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008281 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008282 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008283 // Must be a store of a load. We currently handle two cases: the load
8284 // is a direct child, and it's under an intervening TokenFactor. It is
8285 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008286 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008287 Ld = cast<LoadSDNode>(St->getChain());
8288 else if (St->getValue().hasOneUse() &&
8289 ChainVal->getOpcode() == ISD::TokenFactor) {
8290 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008291 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008292 TokenFactorIndex = i;
8293 Ld = cast<LoadSDNode>(St->getValue());
8294 } else
8295 Ops.push_back(ChainVal->getOperand(i));
8296 }
8297 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008298
Evan Cheng536e6672009-03-12 05:59:15 +00008299 if (!Ld || !ISD::isNormalLoad(Ld))
8300 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008301
Evan Cheng536e6672009-03-12 05:59:15 +00008302 // If this is not the MMX case, i.e. we are just turning i64 load/store
8303 // into f64 load/store, avoid the transformation if there are multiple
8304 // uses of the loaded value.
8305 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8306 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008307
Evan Cheng536e6672009-03-12 05:59:15 +00008308 DebugLoc LdDL = Ld->getDebugLoc();
8309 DebugLoc StDL = N->getDebugLoc();
8310 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8311 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8312 // pair instead.
8313 if (Subtarget->is64Bit() || F64IsLegal) {
8314 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8315 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8316 Ld->getBasePtr(), Ld->getSrcValue(),
8317 Ld->getSrcValueOffset(), Ld->isVolatile(),
8318 Ld->getAlignment());
8319 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008320 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008321 Ops.push_back(NewChain);
8322 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008323 Ops.size());
8324 }
Evan Cheng536e6672009-03-12 05:59:15 +00008325 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008326 St->getSrcValue(), St->getSrcValueOffset(),
8327 St->isVolatile(), St->getAlignment());
8328 }
Evan Cheng536e6672009-03-12 05:59:15 +00008329
8330 // Otherwise, lower to two pairs of 32-bit loads / stores.
8331 SDValue LoAddr = Ld->getBasePtr();
8332 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8333 DAG.getConstant(4, MVT::i32));
8334
8335 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8336 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8337 Ld->isVolatile(), Ld->getAlignment());
8338 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8339 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8340 Ld->isVolatile(),
8341 MinAlign(Ld->getAlignment(), 4));
8342
8343 SDValue NewChain = LoLd.getValue(1);
8344 if (TokenFactorIndex != -1) {
8345 Ops.push_back(LoLd);
8346 Ops.push_back(HiLd);
8347 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8348 Ops.size());
8349 }
8350
8351 LoAddr = St->getBasePtr();
8352 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8353 DAG.getConstant(4, MVT::i32));
8354
8355 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8356 St->getSrcValue(), St->getSrcValueOffset(),
8357 St->isVolatile(), St->getAlignment());
8358 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8359 St->getSrcValue(),
8360 St->getSrcValueOffset() + 4,
8361 St->isVolatile(),
8362 MinAlign(St->getAlignment(), 4));
8363 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008364 }
Dan Gohman475871a2008-07-27 21:46:04 +00008365 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008366}
8367
Chris Lattner6cf73262008-01-25 06:14:17 +00008368/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8369/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008370static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008371 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8372 // F[X]OR(0.0, x) -> x
8373 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008374 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8375 if (C->getValueAPF().isPosZero())
8376 return N->getOperand(1);
8377 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8378 if (C->getValueAPF().isPosZero())
8379 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008380 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008381}
8382
8383/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008384static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008385 // FAND(0.0, x) -> 0.0
8386 // FAND(x, 0.0) -> 0.0
8387 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8388 if (C->getValueAPF().isPosZero())
8389 return N->getOperand(0);
8390 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8391 if (C->getValueAPF().isPosZero())
8392 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008393 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008394}
8395
Dan Gohmane5af2d32009-01-29 01:59:02 +00008396static SDValue PerformBTCombine(SDNode *N,
8397 SelectionDAG &DAG,
8398 TargetLowering::DAGCombinerInfo &DCI) {
8399 // BT ignores high bits in the bit index operand.
8400 SDValue Op1 = N->getOperand(1);
8401 if (Op1.hasOneUse()) {
8402 unsigned BitWidth = Op1.getValueSizeInBits();
8403 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8404 APInt KnownZero, KnownOne;
8405 TargetLowering::TargetLoweringOpt TLO(DAG);
8406 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8407 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8408 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8409 DCI.CommitTargetLoweringOpt(TLO);
8410 }
8411 return SDValue();
8412}
Chris Lattner83e6c992006-10-04 06:57:07 +00008413
Dan Gohman475871a2008-07-27 21:46:04 +00008414SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008415 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008416 SelectionDAG &DAG = DCI.DAG;
8417 switch (N->getOpcode()) {
8418 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008419 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8420 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008421 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008422 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008423 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008424 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008425 case ISD::SHL:
8426 case ISD::SRA:
8427 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008428 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008429 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008430 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8431 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008432 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008433 }
8434
Dan Gohman475871a2008-07-27 21:46:04 +00008435 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008436}
8437
Evan Cheng60c07e12006-07-05 22:17:51 +00008438//===----------------------------------------------------------------------===//
8439// X86 Inline Assembly Support
8440//===----------------------------------------------------------------------===//
8441
Chris Lattnerf4dff842006-07-11 02:54:03 +00008442/// getConstraintType - Given a constraint letter, return the type of
8443/// constraint it is for this target.
8444X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008445X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8446 if (Constraint.size() == 1) {
8447 switch (Constraint[0]) {
8448 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008449 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008450 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008451 case 'r':
8452 case 'R':
8453 case 'l':
8454 case 'q':
8455 case 'Q':
8456 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008457 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008458 case 'Y':
8459 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008460 case 'e':
8461 case 'Z':
8462 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008463 default:
8464 break;
8465 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008466 }
Chris Lattner4234f572007-03-25 02:14:49 +00008467 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008468}
8469
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008470/// LowerXConstraint - try to replace an X constraint, which matches anything,
8471/// with another that has more specific requirements based on the type of the
8472/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008473const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008474LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008475 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8476 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008477 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008478 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008479 return "Y";
8480 if (Subtarget->hasSSE1())
8481 return "x";
8482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008483
Chris Lattner5e764232008-04-26 23:02:14 +00008484 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008485}
8486
Chris Lattner48884cd2007-08-25 00:47:38 +00008487/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8488/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008489void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008490 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008491 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008492 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008493 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008494 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008495
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008496 switch (Constraint) {
8497 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008498 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008499 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008500 if (C->getZExtValue() <= 31) {
8501 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008502 break;
8503 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008504 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008505 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008506 case 'J':
8507 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8508 if (C->getZExtValue() <= 63) {
8509 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8510 break;
8511 }
8512 }
8513 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008514 case 'N':
8515 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008516 if (C->getZExtValue() <= 255) {
8517 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008518 break;
8519 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008520 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008521 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008522 case 'e': {
8523 // 32-bit signed value
8524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8525 const ConstantInt *CI = C->getConstantIntValue();
8526 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8527 // Widen to 64 bits here to get it sign extended.
8528 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8529 break;
8530 }
8531 // FIXME gcc accepts some relocatable values here too, but only in certain
8532 // memory models; it's complicated.
8533 }
8534 return;
8535 }
8536 case 'Z': {
8537 // 32-bit unsigned value
8538 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8539 const ConstantInt *CI = C->getConstantIntValue();
8540 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8541 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8542 break;
8543 }
8544 }
8545 // FIXME gcc accepts some relocatable values here too, but only in certain
8546 // memory models; it's complicated.
8547 return;
8548 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008549 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008550 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008551 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008552 // Widen to 64 bits here to get it sign extended.
8553 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008554 break;
8555 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008556
Chris Lattnerdc43a882007-05-03 16:52:29 +00008557 // If we are in non-pic codegen mode, we allow the address of a global (with
8558 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008559 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008560 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008561
Chris Lattner49921962009-05-08 18:23:14 +00008562 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8563 while (1) {
8564 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8565 Offset += GA->getOffset();
8566 break;
8567 } else if (Op.getOpcode() == ISD::ADD) {
8568 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8569 Offset += C->getZExtValue();
8570 Op = Op.getOperand(0);
8571 continue;
8572 }
8573 } else if (Op.getOpcode() == ISD::SUB) {
8574 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8575 Offset += -C->getZExtValue();
8576 Op = Op.getOperand(0);
8577 continue;
8578 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008579 }
Chris Lattner49921962009-05-08 18:23:14 +00008580
8581 // Otherwise, this isn't something we can handle, reject it.
8582 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008584
Chris Lattner49921962009-05-08 18:23:14 +00008585 if (hasMemory)
8586 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8587 else
8588 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8589 Offset);
8590 Result = Op;
8591 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008592 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008593 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008594
Gabor Greifba36cb52008-08-28 21:40:38 +00008595 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008596 Ops.push_back(Result);
8597 return;
8598 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008599 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8600 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008601}
8602
Chris Lattner259e97c2006-01-31 19:43:35 +00008603std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008604getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008605 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008606 if (Constraint.size() == 1) {
8607 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008608 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008609 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008610 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8611 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008612 if (VT == MVT::i32)
8613 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8614 else if (VT == MVT::i16)
8615 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8616 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008617 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008618 else if (VT == MVT::i64)
8619 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8620 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008621 }
8622 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008623
Chris Lattner1efa40f2006-02-22 00:56:39 +00008624 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008625}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008626
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008627std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008628X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008629 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008630 // First, see if this is a constraint that directly corresponds to an LLVM
8631 // register class.
8632 if (Constraint.size() == 1) {
8633 // GCC Constraint Letters
8634 switch (Constraint[0]) {
8635 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008636 case 'r': // GENERAL_REGS
8637 case 'R': // LEGACY_REGS
8638 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008639 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008640 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008641 if (VT == MVT::i16)
8642 return std::make_pair(0U, X86::GR16RegisterClass);
8643 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008644 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008645 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008646 case 'f': // FP Stack registers.
8647 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8648 // value to the correct fpstack register class.
8649 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8650 return std::make_pair(0U, X86::RFP32RegisterClass);
8651 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8652 return std::make_pair(0U, X86::RFP64RegisterClass);
8653 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008654 case 'y': // MMX_REGS if MMX allowed.
8655 if (!Subtarget->hasMMX()) break;
8656 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008657 case 'Y': // SSE_REGS if SSE2 allowed
8658 if (!Subtarget->hasSSE2()) break;
8659 // FALL THROUGH.
8660 case 'x': // SSE_REGS if SSE1 allowed
8661 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008662
8663 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008664 default: break;
8665 // Scalar SSE types.
8666 case MVT::f32:
8667 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008668 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008669 case MVT::f64:
8670 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008671 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008672 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008673 case MVT::v16i8:
8674 case MVT::v8i16:
8675 case MVT::v4i32:
8676 case MVT::v2i64:
8677 case MVT::v4f32:
8678 case MVT::v2f64:
8679 return std::make_pair(0U, X86::VR128RegisterClass);
8680 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008681 break;
8682 }
8683 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008684
Chris Lattnerf76d1802006-07-31 23:26:50 +00008685 // Use the default implementation in TargetLowering to convert the register
8686 // constraint into a member of a register class.
8687 std::pair<unsigned, const TargetRegisterClass*> Res;
8688 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008689
8690 // Not found as a standard register?
8691 if (Res.second == 0) {
8692 // GCC calls "st(0)" just plain "st".
8693 if (StringsEqualNoCase("{st}", Constraint)) {
8694 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008695 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008696 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008697 // 'A' means EAX + EDX.
8698 if (Constraint == "A") {
8699 Res.first = X86::EAX;
8700 Res.second = X86::GRADRegisterClass;
8701 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008702 return Res;
8703 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008704
Chris Lattnerf76d1802006-07-31 23:26:50 +00008705 // Otherwise, check to see if this is a register class of the wrong value
8706 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8707 // turn into {ax},{dx}.
8708 if (Res.second->hasType(VT))
8709 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008710
Chris Lattnerf76d1802006-07-31 23:26:50 +00008711 // All of the single-register GCC register classes map their values onto
8712 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8713 // really want an 8-bit or 32-bit register, map to the appropriate register
8714 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008715 if (Res.second == X86::GR16RegisterClass) {
8716 if (VT == MVT::i8) {
8717 unsigned DestReg = 0;
8718 switch (Res.first) {
8719 default: break;
8720 case X86::AX: DestReg = X86::AL; break;
8721 case X86::DX: DestReg = X86::DL; break;
8722 case X86::CX: DestReg = X86::CL; break;
8723 case X86::BX: DestReg = X86::BL; break;
8724 }
8725 if (DestReg) {
8726 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008727 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008728 }
8729 } else if (VT == MVT::i32) {
8730 unsigned DestReg = 0;
8731 switch (Res.first) {
8732 default: break;
8733 case X86::AX: DestReg = X86::EAX; break;
8734 case X86::DX: DestReg = X86::EDX; break;
8735 case X86::CX: DestReg = X86::ECX; break;
8736 case X86::BX: DestReg = X86::EBX; break;
8737 case X86::SI: DestReg = X86::ESI; break;
8738 case X86::DI: DestReg = X86::EDI; break;
8739 case X86::BP: DestReg = X86::EBP; break;
8740 case X86::SP: DestReg = X86::ESP; break;
8741 }
8742 if (DestReg) {
8743 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008744 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008745 }
8746 } else if (VT == MVT::i64) {
8747 unsigned DestReg = 0;
8748 switch (Res.first) {
8749 default: break;
8750 case X86::AX: DestReg = X86::RAX; break;
8751 case X86::DX: DestReg = X86::RDX; break;
8752 case X86::CX: DestReg = X86::RCX; break;
8753 case X86::BX: DestReg = X86::RBX; break;
8754 case X86::SI: DestReg = X86::RSI; break;
8755 case X86::DI: DestReg = X86::RDI; break;
8756 case X86::BP: DestReg = X86::RBP; break;
8757 case X86::SP: DestReg = X86::RSP; break;
8758 }
8759 if (DestReg) {
8760 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008761 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008762 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008763 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008764 } else if (Res.second == X86::FR32RegisterClass ||
8765 Res.second == X86::FR64RegisterClass ||
8766 Res.second == X86::VR128RegisterClass) {
8767 // Handle references to XMM physical registers that got mapped into the
8768 // wrong class. This can happen with constraints like {xmm0} where the
8769 // target independent register mapper will just pick the first match it can
8770 // find, ignoring the required type.
8771 if (VT == MVT::f32)
8772 Res.second = X86::FR32RegisterClass;
8773 else if (VT == MVT::f64)
8774 Res.second = X86::FR64RegisterClass;
8775 else if (X86::VR128RegisterClass->hasType(VT))
8776 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008777 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008778
Chris Lattnerf76d1802006-07-31 23:26:50 +00008779 return Res;
8780}
Mon P Wang0c397192008-10-30 08:01:45 +00008781
8782//===----------------------------------------------------------------------===//
8783// X86 Widen vector type
8784//===----------------------------------------------------------------------===//
8785
8786/// getWidenVectorType: given a vector type, returns the type to widen
8787/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8788/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008789/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008790/// scalarizing vs using the wider vector type.
8791
Dan Gohmanc13cf132009-01-15 17:34:08 +00008792MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008793 assert(VT.isVector());
8794 if (isTypeLegal(VT))
8795 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008796
Mon P Wang0c397192008-10-30 08:01:45 +00008797 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8798 // type based on element type. This would speed up our search (though
8799 // it may not be worth it since the size of the list is relatively
8800 // small).
8801 MVT EltVT = VT.getVectorElementType();
8802 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008803
Mon P Wang0c397192008-10-30 08:01:45 +00008804 // On X86, it make sense to widen any vector wider than 1
8805 if (NElts <= 1)
8806 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008807
8808 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008809 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8810 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008811
8812 if (isTypeLegal(SVT) &&
8813 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008814 SVT.getVectorNumElements() > NElts)
8815 return SVT;
8816 }
8817 return MVT::Other;
8818}