blob: cb0b30c857ebf2af2bd02e92f84c713992392a52 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000022#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000025#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000026#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000032#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000033#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000034#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000037#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039using namespace llvm;
40
Mon P Wang3c81d352008-11-23 04:37:22 +000041static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000042DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000043
Evan Cheng10e86422008-04-25 19:11:04 +000044// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000045static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
46 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000047
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000048X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000050 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000051 X86ScalarSSEf64 = Subtarget->hasSSE2();
52 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000053 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000056 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000057
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000058 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000062 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000063 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000065 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000066
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000068 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000071 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Evan Cheng03294662008-10-14 21:26:46 +000087 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000088
Scott Michelfdc40a02009-02-17 22:15:04 +000089 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +000090 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000095 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
97 // SETOEQ and SETUNE require checking two conditions.
98 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
99 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
100 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
101 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
102 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
103 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000104
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
106 // operation.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
108 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
109 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000110
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000112 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000113 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000114 } else if (!UseSoftFloat) {
115 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000116 // We have an impenetrably clever algorithm for ui64->double only.
117 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000118 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000119 // We have an algorithm for SSE2, and we turn this into a 64-bit
120 // FILD for other targets.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000122 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000123
124 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
125 // this operation.
126 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
127 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000128
Devang Patel6a784892009-06-05 18:48:29 +0000129 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000130 // SSE has no i16 to fp conversion, only i32
131 if (X86ScalarSSEf32) {
132 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
133 // f32 and f64 cases are Legal, f80 case is not
134 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
135 } else {
136 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000139 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000140 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
141 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000142 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143
Dale Johannesen73328d12007-09-19 23:55:34 +0000144 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
145 // are Legal, f80 is custom lowered.
146 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
147 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000148
Evan Cheng02568ff2006-01-30 22:13:22 +0000149 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
150 // this operation.
151 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
152 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
153
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000154 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000155 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000156 // f32 and f64 cases are Legal, f80 case is not
157 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000158 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000159 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000160 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 }
162
163 // Handle FP_TO_UINT by promoting the destination to a larger signed
164 // conversion.
165 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
166 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
167 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
168
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 if (Subtarget->is64Bit()) {
170 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000172 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000173 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 // Expand FP_TO_UINT into a select.
175 // FIXME: We would like to use a Custom expander here eventually to do
176 // the optimal thing for SSE vs. the default expansion in the legalizer.
177 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
178 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000179 // With SSE3 we can use fisttpll to convert to a signed i64; without
180 // SSE, we're stuck with a fistpll.
181 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000182 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000183
Chris Lattner399610a2006-12-05 18:22:22 +0000184 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000185 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000186 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
187 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
188 }
Chris Lattner21f66852005-12-23 05:15:23 +0000189
Dan Gohmanb00ee212008-02-18 19:34:53 +0000190 // Scalar integer divide and remainder are lowered to use operations that
191 // produce two results, to match the available instructions. This exposes
192 // the two-result form to trivial CSE, which is able to combine x/y and x%y
193 // into a single instruction.
194 //
195 // Scalar integer multiply-high is also lowered to use two-result
196 // operations, to match the available instructions. However, plain multiply
197 // (low) operations are left as Legal, as there are single-result
198 // instructions for this in x86. Using the two-result multiply instructions
199 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
204 setOperationAction(ISD::SREM , MVT::i8 , Expand);
205 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000206 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
207 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
208 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
209 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
210 setOperationAction(ISD::SREM , MVT::i16 , Expand);
211 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000212 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
213 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
214 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
215 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
216 setOperationAction(ISD::SREM , MVT::i32 , Expand);
217 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000218 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
219 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
220 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
221 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
222 setOperationAction(ISD::SREM , MVT::i64 , Expand);
223 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000224
Evan Chengc35497f2006-10-30 08:02:39 +0000225 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000226 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000227 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
228 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
232 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
234 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000235 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000237 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000238 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000241 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
242 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000243 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000247 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
248 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit()) {
250 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000251 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
252 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 }
254
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000255 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000256 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000257
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258 // These should be promoted to a larger select which is supported.
259 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
260 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000261 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000262 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
263 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
264 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
265 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000266 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
268 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
269 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
270 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
271 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000272 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (Subtarget->is64Bit()) {
274 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
275 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
276 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000277 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000278 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000279 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000280
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000281 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000282 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000283 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000285 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000286 if (Subtarget->is64Bit())
287 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000288 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
290 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
291 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
292 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000293 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000294 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000295 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000296 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
297 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
298 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000299 if (Subtarget->is64Bit()) {
300 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
301 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
302 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
303 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000304
Evan Chengd2cde682008-03-10 19:38:10 +0000305 if (Subtarget->hasSSE1())
306 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000307
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000308 if (!Subtarget->hasSSE2())
309 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
310
Mon P Wang63307c32008-05-05 19:05:59 +0000311 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
313 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
314 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
315 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000316
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
319 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000321
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000322 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000323 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
328 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000330 }
331
Dan Gohman7f460202008-06-30 20:59:49 +0000332 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
333 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000334 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000335 if (!Subtarget->isTargetDarwin() &&
336 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000337 !Subtarget->isTargetCygMing()) {
338 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
339 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
340 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000341
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000342 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
343 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
344 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
345 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
346 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000347 setExceptionPointerRegister(X86::RAX);
348 setExceptionSelectorRegister(X86::RDX);
349 } else {
350 setExceptionPointerRegister(X86::EAX);
351 setExceptionSelectorRegister(X86::EDX);
352 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000353 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000354 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
355
Duncan Sandsf7331b32007-09-11 14:10:23 +0000356 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000357
Chris Lattnerda68d302008-01-15 21:58:22 +0000358 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000359
Nate Begemanacc398c2006-01-25 18:21:52 +0000360 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
361 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000362 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000363 if (Subtarget->is64Bit()) {
364 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000365 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000366 } else {
367 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000368 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000369 }
Evan Chengae642192007-03-02 23:16:35 +0000370
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000371 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000372 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
374 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000375 if (Subtarget->isTargetCygMing())
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
377 else
378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000379
Evan Chengc7ce29b2009-02-13 22:36:38 +0000380 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000381 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000382 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000383 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
384 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000385
Evan Cheng223547a2006-01-31 22:28:30 +0000386 // Use ANDPD to simulate FABS.
387 setOperationAction(ISD::FABS , MVT::f64, Custom);
388 setOperationAction(ISD::FABS , MVT::f32, Custom);
389
390 // Use XORP to simulate FNEG.
391 setOperationAction(ISD::FNEG , MVT::f64, Custom);
392 setOperationAction(ISD::FNEG , MVT::f32, Custom);
393
Evan Cheng68c47cb2007-01-05 07:55:56 +0000394 // Use ANDPD and ORPD to simulate FCOPYSIGN.
395 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
396 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
397
Evan Chengd25e9e82006-02-02 00:28:23 +0000398 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000399 setOperationAction(ISD::FSIN , MVT::f64, Expand);
400 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000401 setOperationAction(ISD::FSIN , MVT::f32, Expand);
402 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403
Chris Lattnera54aa942006-01-29 06:26:08 +0000404 // Expand FP immediates into loads from the stack, except for the special
405 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000406 addLegalFPImmediate(APFloat(+0.0)); // xorpd
407 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000408 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000409 // Use SSE for f32, x87 for f64.
410 // Set up the FP register classes.
411 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
412 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
413
414 // Use ANDPS to simulate FABS.
415 setOperationAction(ISD::FABS , MVT::f32, Custom);
416
417 // Use XORP to simulate FNEG.
418 setOperationAction(ISD::FNEG , MVT::f32, Custom);
419
420 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
421
422 // Use ANDPS and ORPS to simulate FCOPYSIGN.
423 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
424 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
425
426 // We don't support sin/cos/fmod
427 setOperationAction(ISD::FSIN , MVT::f32, Expand);
428 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429
Nate Begemane1795842008-02-14 08:57:00 +0000430 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000431 addLegalFPImmediate(APFloat(+0.0f)); // xorps
432 addLegalFPImmediate(APFloat(+0.0)); // FLD0
433 addLegalFPImmediate(APFloat(+1.0)); // FLD1
434 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
435 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
436
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000441 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000442 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000444 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
445 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000446
Evan Cheng68c47cb2007-01-05 07:55:56 +0000447 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000448 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
450 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000451
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000456 addLegalFPImmediate(APFloat(+0.0)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000464 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000465
Dale Johannesen59a58732007-08-05 18:49:15 +0000466 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000467 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000468 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
469 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
471 {
472 bool ignored;
473 APFloat TmpFlt(+0.0);
474 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
475 &ignored);
476 addLegalFPImmediate(TmpFlt); // FLD0
477 TmpFlt.changeSign();
478 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
479 APFloat TmpFlt2(+1.0);
480 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
481 &ignored);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000486
Evan Chengc7ce29b2009-02-13 22:36:38 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000491 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000492
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000493 // Always use a library call for pow.
494 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
496 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
497
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000498 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000499 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000500 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000502 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
503
Mon P Wangf007a8b2008-11-06 05:31:54 +0000504 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000505 // (for widening) or expand (for scalarization). Then we will selectively
506 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000507 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
508 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000509 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000522 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000524 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000525 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000526 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000548 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Eli Friedman23ef1052009-06-06 03:57:58 +0000553 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000557 }
558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
560 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000561 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000562 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
563 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
564 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000565 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000566 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000567
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000568 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
569 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
570 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000571 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000572
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000573 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
574 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
575 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000576 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000577
Bill Wendling74027e92007-03-15 21:24:36 +0000578 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
579 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
580
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000581 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000582 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000583 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000584 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
585 setOperationAction(ISD::AND, MVT::v2i32, Promote);
586 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
587 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000588
589 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000590 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000591 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000592 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
593 setOperationAction(ISD::OR, MVT::v2i32, Promote);
594 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
595 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000596
597 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000598 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000599 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000600 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
601 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
602 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
603 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000604
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000605 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000606 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000607 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000608 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
609 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
610 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000611 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
612 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000613 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000614
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000615 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
616 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
617 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000618 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000619 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000620
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
623 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000624 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000625
Evan Cheng52672b82008-07-22 18:39:19 +0000626 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000627 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000630
631 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000632
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000633 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000634 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
635 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
636 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
637 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
638 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000639 }
640
Evan Cheng92722532009-03-26 23:06:32 +0000641 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000642 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
643
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000644 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
645 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
646 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
647 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000648 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
649 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000650 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
651 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
652 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000653 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000654 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000655 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000656 }
657
Evan Cheng92722532009-03-26 23:06:32 +0000658 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000659 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000660
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000661 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
662 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000663 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
664 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
665 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
666 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
667
Evan Chengf7c378e2006-04-10 07:23:14 +0000668 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
669 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
670 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000671 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000672 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000673 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
674 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
675 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000676 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000677 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000678 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
679 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
680 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
681 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000682 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
683 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000684
Nate Begeman30a0de92008-07-17 16:51:19 +0000685 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
686 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
687 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
688 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000689
Evan Chengf7c378e2006-04-10 07:23:14 +0000690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
691 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000694 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000695
Evan Cheng2c3ae372006-04-12 21:21:57 +0000696 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000697 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
698 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000699 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000700 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000701 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000702 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
703 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
704 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000705 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000706
Evan Cheng2c3ae372006-04-12 21:21:57 +0000707 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
708 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
709 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
710 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000713
Nate Begemancdd1eec2008-02-12 22:51:28 +0000714 if (Subtarget->is64Bit()) {
715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000716 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000717 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000718
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000719 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000720 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
722 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
723 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
724 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
725 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
726 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
727 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
728 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
729 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
730 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000731 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000732
Chris Lattnerddf89562008-01-17 19:59:44 +0000733 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000734
Evan Cheng2c3ae372006-04-12 21:21:57 +0000735 // Custom lower v2i64 and v2f64 selects.
736 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000737 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000738 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000740
Eli Friedman23ef1052009-06-06 03:57:58 +0000741 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
742 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
743 if (!DisableMMX && Subtarget->hasMMX()) {
744 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
745 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
746 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000747 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000748
Nate Begeman14d12ca2008-02-11 04:19:36 +0000749 if (Subtarget->hasSSE41()) {
750 // FIXME: Do we need to handle scalar-to-vector here?
751 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
752
753 // i8 and i16 vectors are custom , because the source register and source
754 // source memory operand types are not the same width. f32 vectors are
755 // custom since the immediate controlling the insert encodes additional
756 // information.
757 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000760 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
761
762 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
763 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000764 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000765 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000766
767 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
769 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000770 }
771 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000772
Nate Begeman30a0de92008-07-17 16:51:19 +0000773 if (Subtarget->hasSSE42()) {
774 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
775 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Evan Cheng6be2c582006-04-05 23:38:46 +0000777 // We want to custom lower some of our intrinsics.
778 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
779
Bill Wendling74c37652008-12-09 22:08:41 +0000780 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000781 setOperationAction(ISD::SADDO, MVT::i32, Custom);
782 setOperationAction(ISD::SADDO, MVT::i64, Custom);
783 setOperationAction(ISD::UADDO, MVT::i32, Custom);
784 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000785 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
786 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
787 setOperationAction(ISD::USUBO, MVT::i32, Custom);
788 setOperationAction(ISD::USUBO, MVT::i64, Custom);
789 setOperationAction(ISD::SMULO, MVT::i32, Custom);
790 setOperationAction(ISD::SMULO, MVT::i64, Custom);
791 setOperationAction(ISD::UMULO, MVT::i32, Custom);
792 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000793
Evan Chengd54f2d52009-03-31 19:38:51 +0000794 if (!Subtarget->is64Bit()) {
795 // These libcalls are not available in 32-bit.
796 setLibcallName(RTLIB::SHL_I128, 0);
797 setLibcallName(RTLIB::SRL_I128, 0);
798 setLibcallName(RTLIB::SRA_I128, 0);
799 }
800
Evan Cheng206ee9d2006-07-07 08:33:52 +0000801 // We have target-specific dag combine patterns for the following nodes:
802 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000803 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000804 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000805 setTargetDAGCombine(ISD::SHL);
806 setTargetDAGCombine(ISD::SRA);
807 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000808 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000809 if (Subtarget->is64Bit())
810 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000811
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000812 computeRegisterProperties();
813
Evan Cheng87ed7162006-02-14 08:25:08 +0000814 // FIXME: These should be based on subtarget info. Plus, the values should
815 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000816 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
817 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
818 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000819 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000820 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000821 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000822}
823
Scott Michel5b8f82e2008-03-10 15:42:14 +0000824
Duncan Sands5480c042009-01-01 15:52:00 +0000825MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000826 return MVT::i8;
827}
828
829
Evan Cheng29286502008-01-23 23:17:41 +0000830/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
831/// the desired ByVal argument alignment.
832static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
833 if (MaxAlign == 16)
834 return;
835 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
836 if (VTy->getBitWidth() == 128)
837 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000838 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
839 unsigned EltAlign = 0;
840 getMaxByValAlign(ATy->getElementType(), EltAlign);
841 if (EltAlign > MaxAlign)
842 MaxAlign = EltAlign;
843 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
844 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
845 unsigned EltAlign = 0;
846 getMaxByValAlign(STy->getElementType(i), EltAlign);
847 if (EltAlign > MaxAlign)
848 MaxAlign = EltAlign;
849 if (MaxAlign == 16)
850 break;
851 }
852 }
853 return;
854}
855
856/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
857/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000858/// that contain SSE vectors are placed at 16-byte boundaries while the rest
859/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000860unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000861 if (Subtarget->is64Bit()) {
862 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000863 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000864 if (TyAlign > 8)
865 return TyAlign;
866 return 8;
867 }
868
Evan Cheng29286502008-01-23 23:17:41 +0000869 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000870 if (Subtarget->hasSSE1())
871 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000872 return Align;
873}
Chris Lattner2b02a442007-02-25 08:29:00 +0000874
Evan Chengf0df0312008-05-15 08:39:06 +0000875/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000876/// and store operations as a result of memset, memcpy, and memmove
877/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000878/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000879MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000880X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +0000881 bool isSrcConst, bool isSrcStr,
882 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000883 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
884 // linux. This is because the stack realignment code can't handle certain
885 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +0000886 const Function *F = DAG.getMachineFunction().getFunction();
887 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
888 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000889 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
890 return MVT::v4i32;
891 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
892 return MVT::v4f32;
893 }
Evan Chengf0df0312008-05-15 08:39:06 +0000894 if (Subtarget->is64Bit() && Size >= 8)
895 return MVT::i64;
896 return MVT::i32;
897}
898
Evan Chengcc415862007-11-09 01:32:10 +0000899/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
900/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000901SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000902 SelectionDAG &DAG) const {
903 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000904 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000905 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000906 // This doesn't have DebugLoc associated with it, but is not really the
907 // same as a Register.
908 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
909 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000910 return Table;
911}
912
Chris Lattner2b02a442007-02-25 08:29:00 +0000913//===----------------------------------------------------------------------===//
914// Return Value Calling Convention Implementation
915//===----------------------------------------------------------------------===//
916
Chris Lattner59ed56b2007-02-28 04:55:35 +0000917#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000918
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000919/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000920SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000921 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000922 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000923
Chris Lattner9774c912007-02-27 05:28:59 +0000924 SmallVector<CCValAssign, 16> RVLocs;
925 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000926 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
927 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000928 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000929
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000930 // If this is the first return lowered for this function, add the regs to the
931 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000932 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000933 for (unsigned i = 0; i != RVLocs.size(); ++i)
934 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000935 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000936 }
Dan Gohman475871a2008-07-27 21:46:04 +0000937 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000938
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000939 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000940 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000941 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SDValue TailCall = Chain;
943 SDValue TargetAddress = TailCall.getOperand(1);
944 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000945 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000946 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +0000947 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R11)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000948 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000949 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000950 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000951 assert(StackAdjustment.getOpcode() == ISD::Constant &&
952 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000953
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000955 Operands.push_back(Chain.getOperand(0));
956 Operands.push_back(TargetAddress);
957 Operands.push_back(StackAdjustment);
958 // Copy registers used by the call. Last operand is a flag so it is not
959 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000960 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000961 Operands.push_back(Chain.getOperand(i));
962 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000963 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000964 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000965 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000966
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000967 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000968 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000969
Dan Gohman475871a2008-07-27 21:46:04 +0000970 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000971 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
972 // Operand #1 = Bytes To Pop
973 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000974
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000975 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000976 for (unsigned i = 0; i != RVLocs.size(); ++i) {
977 CCValAssign &VA = RVLocs[i];
978 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000980
Chris Lattner447ff682008-03-11 03:23:40 +0000981 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
982 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000983 if (VA.getLocReg() == X86::ST0 ||
984 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000985 // If this is a copy from an xmm register to ST(0), use an FPExtend to
986 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000987 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000988 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000989 RetOps.push_back(ValToCopy);
990 // Don't emit a copytoreg.
991 continue;
992 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000993
Evan Cheng242b38b2009-02-23 09:03:22 +0000994 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
995 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +0000996 if (Subtarget->is64Bit()) {
997 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +0000998 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +0000999 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001000 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1001 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1002 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001003 }
1004
Dale Johannesendd64c412009-02-04 00:33:20 +00001005 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001006 Flag = Chain.getValue(1);
1007 }
Dan Gohman61a92132008-04-21 23:59:07 +00001008
1009 // The x86-64 ABI for returning structs by value requires that we copy
1010 // the sret argument into %rax for the return. We saved the argument into
1011 // a virtual register in the entry block, so now we copy the value out
1012 // and into %rax.
1013 if (Subtarget->is64Bit() &&
1014 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1015 MachineFunction &MF = DAG.getMachineFunction();
1016 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1017 unsigned Reg = FuncInfo->getSRetReturnReg();
1018 if (!Reg) {
1019 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1020 FuncInfo->setSRetReturnReg(Reg);
1021 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001022 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001023
Dale Johannesendd64c412009-02-04 00:33:20 +00001024 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001025 Flag = Chain.getValue(1);
1026 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Chris Lattner447ff682008-03-11 03:23:40 +00001028 RetOps[0] = Chain; // Update chain.
1029
1030 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001031 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001032 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001033
1034 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001035 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001036}
1037
1038
Chris Lattner3085e152007-02-25 08:59:22 +00001039/// LowerCallResult - Lower the result values of an ISD::CALL into the
1040/// appropriate copies out of appropriate physical registers. This assumes that
1041/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1042/// being lowered. The returns a SDNode with the same number of values as the
1043/// ISD::CALL.
1044SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001045LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001046 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001047
Scott Michelfdc40a02009-02-17 22:15:04 +00001048 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001049 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001050 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001051 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001052 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001053 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001054 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1055
Dan Gohman475871a2008-07-27 21:46:04 +00001056 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001057
Chris Lattner3085e152007-02-25 08:59:22 +00001058 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001059 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001060 CCValAssign &VA = RVLocs[i];
1061 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001062
Torok Edwin3f142c32009-02-01 18:15:56 +00001063 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001064 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001065 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1066 cerr << "SSE register return with SSE disabled\n";
1067 exit(1);
1068 }
1069
Chris Lattner8e6da152008-03-10 21:08:41 +00001070 // If this is a call to a function that returns an fp value on the floating
1071 // point stack, but where we prefer to use the value in xmm registers, copy
1072 // 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 +00001073 if ((VA.getLocReg() == X86::ST0 ||
1074 VA.getLocReg() == X86::ST1) &&
1075 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001076 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001077 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Evan Cheng79fb3b42009-02-20 20:43:02 +00001079 SDValue Val;
1080 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001081 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1082 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1083 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1084 MVT::v2i64, InFlag).getValue(1);
1085 Val = Chain.getValue(0);
1086 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1087 Val, DAG.getConstant(0, MVT::i64));
1088 } else {
1089 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1090 MVT::i64, InFlag).getValue(1);
1091 Val = Chain.getValue(0);
1092 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001093 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1094 } else {
1095 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1096 CopyVT, InFlag).getValue(1);
1097 Val = Chain.getValue(0);
1098 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001099 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001100
Dan Gohman37eed792009-02-04 17:28:58 +00001101 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001102 // Round the F80 the right size, which also moves to the appropriate xmm
1103 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001104 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001105 // This truncation won't change the value.
1106 DAG.getIntPtrConstant(1));
1107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001108
Chris Lattner8e6da152008-03-10 21:08:41 +00001109 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001110 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001111
Chris Lattner3085e152007-02-25 08:59:22 +00001112 // Merge everything together with a MERGE_VALUES node.
1113 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001114 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1115 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001116}
1117
1118
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001119//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001120// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001121//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001122// StdCall calling convention seems to be standard for many Windows' API
1123// routines and around. It differs from C calling convention just a little:
1124// callee should clean up the stack, not caller. Symbols should be also
1125// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001126// For info on fast calling convention see Fast Calling Convention (tail call)
1127// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001128
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001129/// CallIsStructReturn - Determines whether a CALL node uses struct return
1130/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001131static bool CallIsStructReturn(CallSDNode *TheCall) {
1132 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001133 if (!NumOps)
1134 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001135
Dan Gohman095cc292008-09-13 01:54:27 +00001136 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001137}
1138
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001139/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1140/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001141static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001142 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001143 if (!NumArgs)
1144 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001145
1146 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001147}
1148
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001149/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1150/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001151/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001152bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001153 if (IsVarArg)
1154 return false;
1155
Dan Gohman095cc292008-09-13 01:54:27 +00001156 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001157 default:
1158 return false;
1159 case CallingConv::X86_StdCall:
1160 return !Subtarget->is64Bit();
1161 case CallingConv::X86_FastCall:
1162 return !Subtarget->is64Bit();
1163 case CallingConv::Fast:
1164 return PerformTailCallOpt;
1165 }
1166}
1167
Dan Gohman095cc292008-09-13 01:54:27 +00001168/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1169/// given CallingConvention value.
1170CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001171 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001172 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001173 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001174 else
1175 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001176 }
1177
Gordon Henriksen86737662008-01-05 16:56:59 +00001178 if (CC == CallingConv::X86_FastCall)
1179 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001180 else if (CC == CallingConv::Fast)
1181 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001182 else
1183 return CC_X86_32_C;
1184}
1185
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001186/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1187/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001188NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001189X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001190 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001191 if (CC == CallingConv::X86_FastCall)
1192 return FastCall;
1193 else if (CC == CallingConv::X86_StdCall)
1194 return StdCall;
1195 return None;
1196}
1197
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001198
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001199/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1200/// in a register before calling.
1201bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1202 return !IsTailCall && !Is64Bit &&
1203 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1204 Subtarget->isPICStyleGOT();
1205}
1206
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001207/// CallRequiresFnAddressInReg - Check whether the call requires the function
1208/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001209bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001210X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001211 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001212 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1213 Subtarget->isPICStyleGOT();
1214}
1215
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001216/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1217/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001218/// the specific parameter attribute. The copy will be passed as a byval
1219/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001220static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001221CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001222 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1223 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001224 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001225 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001226 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001227}
1228
Dan Gohman475871a2008-07-27 21:46:04 +00001229SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001230 const CCValAssign &VA,
1231 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001232 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001234 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001235 ISD::ArgFlagsTy Flags =
1236 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001237 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001238 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001239
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001240 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001241 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001242 // In case of tail call optimization mark all arguments mutable. Since they
1243 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001245 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001246 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001247 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001248 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001249 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001250 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001251}
1252
Dan Gohman475871a2008-07-27 21:46:04 +00001253SDValue
1254X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001255 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001256 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001257 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001258
Gordon Henriksen86737662008-01-05 16:56:59 +00001259 const Function* Fn = MF.getFunction();
1260 if (Fn->hasExternalLinkage() &&
1261 Subtarget->isTargetCygMing() &&
1262 Fn->getName() == "main")
1263 FuncInfo->setForceFramePointer(true);
1264
1265 // Decorate the function name.
1266 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001267
Evan Cheng1bc78042006-04-26 01:20:17 +00001268 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001269 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001270 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001271 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001272 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001273 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001274
1275 assert(!(isVarArg && CC == CallingConv::Fast) &&
1276 "Var args not supported with calling convention fastcc");
1277
Chris Lattner638402b2007-02-28 07:00:42 +00001278 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001279 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001280 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001281 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001282
Dan Gohman475871a2008-07-27 21:46:04 +00001283 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001284 unsigned LastVal = ~0U;
1285 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1286 CCValAssign &VA = ArgLocs[i];
1287 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1288 // places.
1289 assert(VA.getValNo() != LastVal &&
1290 "Don't support value assigned to multiple locs yet");
1291 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Chris Lattnerf39f7712007-02-28 05:46:49 +00001293 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001294 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001295 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001296 if (RegVT == MVT::i32)
1297 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001298 else if (Is64Bit && RegVT == MVT::i64)
1299 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001300 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001301 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001302 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001303 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001304 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001305 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001306 else if (RegVT.isVector()) {
1307 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001308 if (!Is64Bit)
1309 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1310 else {
1311 // Darwin calling convention passes MMX values in either GPRs or
1312 // XMMs in x86-64. Other targets pass them in memory.
1313 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1314 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1315 RegVT = MVT::v2i64;
1316 } else {
1317 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1318 RegVT = MVT::i64;
1319 }
1320 }
1321 } else {
1322 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001323 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001324
Bob Wilson998e1252009-04-20 18:36:57 +00001325 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001326 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001327
Chris Lattnerf39f7712007-02-28 05:46:49 +00001328 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1329 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1330 // right size.
1331 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001332 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001333 DAG.getValueType(VA.getValVT()));
1334 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001335 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001336 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001337
Chris Lattnerf39f7712007-02-28 05:46:49 +00001338 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001339 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Gordon Henriksen86737662008-01-05 16:56:59 +00001341 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001342 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001343 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001344 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001345 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001346 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1347 ArgValue, DAG.getConstant(0, MVT::i64));
1348 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001349 }
1350 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001351
Chris Lattnerf39f7712007-02-28 05:46:49 +00001352 ArgValues.push_back(ArgValue);
1353 } else {
1354 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001355 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001356 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001357 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001358
Dan Gohman61a92132008-04-21 23:59:07 +00001359 // The x86-64 ABI for returning structs by value requires that we copy
1360 // the sret argument into %rax for the return. Save the argument into
1361 // a virtual register so that we can access it from the return points.
1362 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1363 MachineFunction &MF = DAG.getMachineFunction();
1364 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1365 unsigned Reg = FuncInfo->getSRetReturnReg();
1366 if (!Reg) {
1367 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1368 FuncInfo->setSRetReturnReg(Reg);
1369 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001370 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001371 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001372 }
1373
Chris Lattnerf39f7712007-02-28 05:46:49 +00001374 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001375 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001376 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001377 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001378
Evan Cheng1bc78042006-04-26 01:20:17 +00001379 // If the function takes variable number of arguments, make a frame index for
1380 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001381 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001382 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1383 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1384 }
1385 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001386 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1387
1388 // FIXME: We should really autogenerate these arrays
1389 static const unsigned GPR64ArgRegsWin64[] = {
1390 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001391 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001392 static const unsigned XMMArgRegsWin64[] = {
1393 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1394 };
1395 static const unsigned GPR64ArgRegs64Bit[] = {
1396 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1397 };
1398 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001399 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1400 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1401 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001402 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1403
1404 if (IsWin64) {
1405 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1406 GPR64ArgRegs = GPR64ArgRegsWin64;
1407 XMMArgRegs = XMMArgRegsWin64;
1408 } else {
1409 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1410 GPR64ArgRegs = GPR64ArgRegs64Bit;
1411 XMMArgRegs = XMMArgRegs64Bit;
1412 }
1413 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1414 TotalNumIntRegs);
1415 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1416 TotalNumXMMRegs);
1417
Devang Patel578efa92009-06-05 21:57:13 +00001418 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001419 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001420 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001421 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001422 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001423 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001424 // Kernel mode asks for SSE to be disabled, so don't push them
1425 // on the stack.
1426 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001427
Gordon Henriksen86737662008-01-05 16:56:59 +00001428 // For X86-64, if there are vararg parameters that are passed via
1429 // registers, then we must store them to their spots on the stack so they
1430 // may be loaded by deferencing the result of va_next.
1431 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001432 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1433 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1434 TotalNumXMMRegs * 16, 16);
1435
Gordon Henriksen86737662008-01-05 16:56:59 +00001436 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001437 SmallVector<SDValue, 8> MemOps;
1438 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001439 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001440 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001441 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001442 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1443 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001444 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001445 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001446 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001447 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001448 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001449 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001450 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001452
Gordon Henriksen86737662008-01-05 16:56:59 +00001453 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001454 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001455 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001456 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001457 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1458 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001459 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001460 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001461 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001462 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001463 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001464 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001465 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001466 }
1467 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001468 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001469 &MemOps[0], MemOps.size());
1470 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001471 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001472
Gordon Henriksenae636f82008-01-03 16:47:34 +00001473 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001474
Gordon Henriksen86737662008-01-05 16:56:59 +00001475 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001476 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001477 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001478 BytesCallerReserves = 0;
1479 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001480 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001481 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001482 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001483 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001484 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001485 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001486
Gordon Henriksen86737662008-01-05 16:56:59 +00001487 if (!Is64Bit) {
1488 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1489 if (CC == CallingConv::X86_FastCall)
1490 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1491 }
Evan Cheng25caf632006-05-23 21:06:34 +00001492
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001493 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001494
Evan Cheng25caf632006-05-23 21:06:34 +00001495 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001496 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001497 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001498}
1499
Dan Gohman475871a2008-07-27 21:46:04 +00001500SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001501X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001502 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001503 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001504 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001505 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001506 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001507 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001509 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001510 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001511 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001512 }
Dale Johannesenace16102009-02-03 19:33:06 +00001513 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001514 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001515}
1516
Bill Wendling64e87322009-01-16 19:25:27 +00001517/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001518/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001519SDValue
1520X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001521 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001522 SDValue Chain,
1523 bool IsTailCall,
1524 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001525 int FPDiff,
1526 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001527 if (!IsTailCall || FPDiff==0) return Chain;
1528
1529 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001530 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001531 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001532
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001533 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001534 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001535 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001536}
1537
1538/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1539/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001540static SDValue
1541EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001543 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001544 // Store the return address to the appropriate stack slot.
1545 if (!FPDiff) return Chain;
1546 // Calculate the new stack slot for the return address.
1547 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001548 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001549 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001550 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001551 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001552 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001553 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001554 return Chain;
1555}
1556
Dan Gohman475871a2008-07-27 21:46:04 +00001557SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001558 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001559 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1560 SDValue Chain = TheCall->getChain();
1561 unsigned CC = TheCall->getCallingConv();
1562 bool isVarArg = TheCall->isVarArg();
1563 bool IsTailCall = TheCall->isTailCall() &&
1564 CC == CallingConv::Fast && PerformTailCallOpt;
1565 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001566 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001567 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001568 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001569
1570 assert(!(isVarArg && CC == CallingConv::Fast) &&
1571 "Var args not supported with calling convention fastcc");
1572
Chris Lattner638402b2007-02-28 07:00:42 +00001573 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001574 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001575 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001576 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001577
Chris Lattner423c5f42007-02-28 05:31:48 +00001578 // Get a count of how many bytes are to be pushed on the stack.
1579 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001580 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001581 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001582
Gordon Henriksen86737662008-01-05 16:56:59 +00001583 int FPDiff = 0;
1584 if (IsTailCall) {
1585 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001586 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001587 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1588 FPDiff = NumBytesCallerPushed - NumBytes;
1589
1590 // Set the delta of movement of the returnaddr stackslot.
1591 // But only set if delta is greater than previous delta.
1592 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1593 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1594 }
1595
Chris Lattnere563bbc2008-10-11 22:08:30 +00001596 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001597
Dan Gohman475871a2008-07-27 21:46:04 +00001598 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001599 // Load return adress for tail calls.
1600 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001601 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001602
Dan Gohman475871a2008-07-27 21:46:04 +00001603 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1604 SmallVector<SDValue, 8> MemOpChains;
1605 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001606
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001607 // Walk the register/memloc assignments, inserting copies/loads. In the case
1608 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001609 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1610 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001611 SDValue Arg = TheCall->getArg(i);
1612 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1613 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001614
Chris Lattner423c5f42007-02-28 05:31:48 +00001615 // Promote the value if needed.
1616 switch (VA.getLocInfo()) {
1617 default: assert(0 && "Unknown loc info!");
1618 case CCValAssign::Full: break;
1619 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001620 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001621 break;
1622 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001623 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001624 break;
1625 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001626 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001627 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001629
Chris Lattner423c5f42007-02-28 05:31:48 +00001630 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001631 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001632 MVT RegVT = VA.getLocVT();
1633 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001634 switch (VA.getLocReg()) {
1635 default:
1636 break;
1637 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1638 case X86::R8: {
1639 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001640 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001641 break;
1642 }
1643 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1644 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1645 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001646 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1647 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001648 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001649 break;
1650 }
1651 }
1652 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001653 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1654 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001655 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001656 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001657 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001658 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001659
Dan Gohman095cc292008-09-13 01:54:27 +00001660 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1661 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001662 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001663 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001664 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001665
Evan Cheng32fe1032006-05-25 00:59:30 +00001666 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001667 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001668 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001669
Evan Cheng347d5f72006-04-28 21:29:37 +00001670 // Build a sequence of copy-to-reg nodes chained together with token chain
1671 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001672 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001673 // Tail call byval lowering might overwrite argument registers so in case of
1674 // tail call optimization the copies to registers are lowered later.
1675 if (!IsTailCall)
1676 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001677 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001678 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001679 InFlag = Chain.getValue(1);
1680 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001681
Evan Chengf4684712007-02-21 21:18:14 +00001682 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001683 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001684 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001685 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001686 DAG.getNode(X86ISD::GlobalBaseReg,
1687 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001688 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001689 InFlag);
1690 InFlag = Chain.getValue(1);
1691 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001692 // If we are tail calling and generating PIC/GOT style code load the address
1693 // of the callee into ecx. The value in ecx is used as target of the tail
1694 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1695 // calls on PIC/GOT architectures. Normally we would just put the address of
1696 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1697 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001698 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001699 // Note: The actual moving to ecx is done further down.
1700 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001701 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001702 !G->getGlobal()->hasProtectedVisibility())
1703 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001704 else if (isa<ExternalSymbolSDNode>(Callee))
1705 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001706 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001707
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 if (Is64Bit && isVarArg) {
1709 // From AMD64 ABI document:
1710 // For calls that may call functions that use varargs or stdargs
1711 // (prototype-less calls or calls to functions containing ellipsis (...) in
1712 // the declaration) %al is used as hidden argument to specify the number
1713 // of SSE registers used. The contents of %al do not need to match exactly
1714 // the number of registers, but must be an ubound on the number of SSE
1715 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001716
1717 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 // Count the number of XMM registers allocated.
1719 static const unsigned XMMArgRegs[] = {
1720 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1721 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1722 };
1723 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001724 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001725 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Dale Johannesendd64c412009-02-04 00:33:20 +00001727 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001728 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1729 InFlag = Chain.getValue(1);
1730 }
1731
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001732
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001733 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001734 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001735 SmallVector<SDValue, 8> MemOpChains2;
1736 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001737 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001738 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001739 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001740 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1741 CCValAssign &VA = ArgLocs[i];
1742 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001743 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001744 SDValue Arg = TheCall->getArg(i);
1745 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001746 // Create frame index.
1747 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001748 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001749 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001750 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001751
Duncan Sands276dcbd2008-03-21 09:14:45 +00001752 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001753 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001754 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001755 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001756 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001757 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001758 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001759
1760 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001761 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001762 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001763 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001764 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001765 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001766 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001767 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 }
1769 }
1770
1771 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001772 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001773 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001774
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001775 // Copy arguments to their registers.
1776 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001777 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001778 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001779 InFlag = Chain.getValue(1);
1780 }
Dan Gohman475871a2008-07-27 21:46:04 +00001781 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001782
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001784 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001785 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001786 }
1787
Evan Cheng32fe1032006-05-25 00:59:30 +00001788 // If the callee is a GlobalAddress node (quite common, every direct call is)
1789 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001790 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001791 // We should use extra load for direct calls to dllimported functions in
1792 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001793 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1794 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001795 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1796 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001797 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1798 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001799 } else if (IsTailCall) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001800 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001801
Dale Johannesendd64c412009-02-04 00:33:20 +00001802 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001803 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001804 Callee,InFlag);
1805 Callee = DAG.getRegister(Opc, getPointerTy());
1806 // Add register as live out.
1807 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001808 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001809
Chris Lattnerd96d0722007-02-25 06:40:16 +00001810 // Returns a chain & a flag for retval copy to use.
1811 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001812 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001813
1814 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001815 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1816 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001817 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001818
Gordon Henriksen86737662008-01-05 16:56:59 +00001819 // Returns a chain & a flag for retval copy to use.
1820 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1821 Ops.clear();
1822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001823
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001824 Ops.push_back(Chain);
1825 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001826
Gordon Henriksen86737662008-01-05 16:56:59 +00001827 if (IsTailCall)
1828 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001829
Gordon Henriksen86737662008-01-05 16:56:59 +00001830 // Add argument registers to the end of the list so that they are known live
1831 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001832 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1833 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1834 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001835
Evan Cheng586ccac2008-03-18 23:36:35 +00001836 // Add an implicit use GOT pointer in EBX.
1837 if (!IsTailCall && !Is64Bit &&
1838 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1839 Subtarget->isPICStyleGOT())
1840 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1841
1842 // Add an implicit use of AL for x86 vararg functions.
1843 if (Is64Bit && isVarArg)
1844 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1845
Gabor Greifba36cb52008-08-28 21:40:38 +00001846 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001847 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001848
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001850 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001852 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001853 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001854
Gabor Greifba36cb52008-08-28 21:40:38 +00001855 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 }
1857
Dale Johannesenace16102009-02-03 19:33:06 +00001858 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001859 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001860
Chris Lattner2d297092006-05-23 18:50:38 +00001861 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001863 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001864 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001865 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001866 // If this is is a call to a struct-return function, the callee
1867 // pops the hidden struct pointer, so we have to push it back.
1868 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001869 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001870 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001871 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Gordon Henriksenae636f82008-01-03 16:47:34 +00001873 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001874 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001875 DAG.getIntPtrConstant(NumBytes, true),
1876 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1877 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001878 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001879 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001880
Chris Lattner3085e152007-02-25 08:59:22 +00001881 // Handle result values, copying them out of physregs into vregs that we
1882 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001883 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001884 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001885}
1886
Evan Cheng25ab6902006-09-08 06:48:29 +00001887
1888//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001889// Fast Calling Convention (tail call) implementation
1890//===----------------------------------------------------------------------===//
1891
1892// Like std call, callee cleans arguments, convention except that ECX is
1893// reserved for storing the tail called function address. Only 2 registers are
1894// free for argument passing (inreg). Tail call optimization is performed
1895// provided:
1896// * tailcallopt is enabled
1897// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001898// On X86_64 architecture with GOT-style position independent code only local
1899// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001900// To keep the stack aligned according to platform abi the function
1901// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1902// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001903// If a tail called function callee has more arguments than the caller the
1904// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001905// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001906// original REtADDR, but before the saved framepointer or the spilled registers
1907// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1908// stack layout:
1909// arg1
1910// arg2
1911// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001912// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001913// move area ]
1914// (possible EBP)
1915// ESI
1916// EDI
1917// local1 ..
1918
1919/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1920/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001921unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001922 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001923 MachineFunction &MF = DAG.getMachineFunction();
1924 const TargetMachine &TM = MF.getTarget();
1925 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1926 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001927 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001928 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001929 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001930 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1931 // Number smaller than 12 so just add the difference.
1932 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1933 } else {
1934 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001936 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001937 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001938 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001939}
1940
1941/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001942/// following the call is a return. A function is eligible if caller/callee
1943/// calling conventions match, currently only fastcc supports tail calls, and
1944/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001945bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001947 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001948 if (!PerformTailCallOpt)
1949 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001950
Dan Gohman095cc292008-09-13 01:54:27 +00001951 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001952 MachineFunction &MF = DAG.getMachineFunction();
1953 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001954 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001955 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001956 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001957 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001958 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001959 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001960 return true;
1961
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001962 // Can only do local tail calls (in same module, hidden or protected) on
1963 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001964 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1965 return G->getGlobal()->hasHiddenVisibility()
1966 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001967 }
1968 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001969
1970 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001971}
1972
Dan Gohman3df24e62008-09-03 23:12:08 +00001973FastISel *
1974X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001975 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001976 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001977 DenseMap<const Value *, unsigned> &vm,
1978 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001979 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001980 DenseMap<const AllocaInst *, int> &am
1981#ifndef NDEBUG
1982 , SmallSet<Instruction*, 8> &cil
1983#endif
1984 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001985 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001986#ifndef NDEBUG
1987 , cil
1988#endif
1989 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001990}
1991
1992
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001993//===----------------------------------------------------------------------===//
1994// Other Lowering Hooks
1995//===----------------------------------------------------------------------===//
1996
1997
Dan Gohman475871a2008-07-27 21:46:04 +00001998SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001999 MachineFunction &MF = DAG.getMachineFunction();
2000 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2001 int ReturnAddrIndex = FuncInfo->getRAIndex();
2002
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002003 if (ReturnAddrIndex == 0) {
2004 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002005 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002006 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002007 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002008 }
2009
Evan Cheng25ab6902006-09-08 06:48:29 +00002010 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002011}
2012
2013
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002014/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2015/// specific condition code, returning the condition code and the LHS/RHS of the
2016/// comparison to make.
2017static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2018 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002019 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002020 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2021 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2022 // X > -1 -> X == 0, jump !sign.
2023 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002024 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002025 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2026 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002027 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002028 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002029 // X < 1 -> X <= 0
2030 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002031 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002032 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002033 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002034
Evan Chengd9558e02006-01-06 00:43:03 +00002035 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002036 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002037 case ISD::SETEQ: return X86::COND_E;
2038 case ISD::SETGT: return X86::COND_G;
2039 case ISD::SETGE: return X86::COND_GE;
2040 case ISD::SETLT: return X86::COND_L;
2041 case ISD::SETLE: return X86::COND_LE;
2042 case ISD::SETNE: return X86::COND_NE;
2043 case ISD::SETULT: return X86::COND_B;
2044 case ISD::SETUGT: return X86::COND_A;
2045 case ISD::SETULE: return X86::COND_BE;
2046 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002047 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002049
Chris Lattner4c78e022008-12-23 23:42:27 +00002050 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002051
Chris Lattner4c78e022008-12-23 23:42:27 +00002052 // If LHS is a foldable load, but RHS is not, flip the condition.
2053 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2054 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2055 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2056 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002057 }
2058
Chris Lattner4c78e022008-12-23 23:42:27 +00002059 switch (SetCCOpcode) {
2060 default: break;
2061 case ISD::SETOLT:
2062 case ISD::SETOLE:
2063 case ISD::SETUGT:
2064 case ISD::SETUGE:
2065 std::swap(LHS, RHS);
2066 break;
2067 }
2068
2069 // On a floating point condition, the flags are set as follows:
2070 // ZF PF CF op
2071 // 0 | 0 | 0 | X > Y
2072 // 0 | 0 | 1 | X < Y
2073 // 1 | 0 | 0 | X == Y
2074 // 1 | 1 | 1 | unordered
2075 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002076 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002077 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002078 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002079 case ISD::SETOLT: // flipped
2080 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002081 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002082 case ISD::SETOLE: // flipped
2083 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002084 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002085 case ISD::SETUGT: // flipped
2086 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002087 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002088 case ISD::SETUGE: // flipped
2089 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002090 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002091 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002092 case ISD::SETNE: return X86::COND_NE;
2093 case ISD::SETUO: return X86::COND_P;
2094 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002095 }
Evan Chengd9558e02006-01-06 00:43:03 +00002096}
2097
Evan Cheng4a460802006-01-11 00:33:36 +00002098/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2099/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002100/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002101static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002102 switch (X86CC) {
2103 default:
2104 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002105 case X86::COND_B:
2106 case X86::COND_BE:
2107 case X86::COND_E:
2108 case X86::COND_P:
2109 case X86::COND_A:
2110 case X86::COND_AE:
2111 case X86::COND_NE:
2112 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002113 return true;
2114 }
2115}
2116
Nate Begeman9008ca62009-04-27 18:41:29 +00002117/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2118/// the specified range (L, H].
2119static bool isUndefOrInRange(int Val, int Low, int Hi) {
2120 return (Val < 0) || (Val >= Low && Val < Hi);
2121}
2122
2123/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2124/// specified value.
2125static bool isUndefOrEqual(int Val, int CmpVal) {
2126 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002127 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002128 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002129}
2130
Nate Begeman9008ca62009-04-27 18:41:29 +00002131/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2132/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2133/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002134static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002135 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2136 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2137 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2138 return (Mask[0] < 2 && Mask[1] < 2);
2139 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002140}
2141
Nate Begeman9008ca62009-04-27 18:41:29 +00002142bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2143 SmallVector<int, 8> M;
2144 N->getMask(M);
2145 return ::isPSHUFDMask(M, N->getValueType(0));
2146}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002147
Nate Begeman9008ca62009-04-27 18:41:29 +00002148/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2149/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002150static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002151 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002152 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002153
2154 // Lower quadword copied in order or undef.
2155 for (int i = 0; i != 4; ++i)
2156 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002157 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002158
Evan Cheng506d3df2006-03-29 23:07:14 +00002159 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002160 for (int i = 4; i != 8; ++i)
2161 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002162 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002163
Evan Cheng506d3df2006-03-29 23:07:14 +00002164 return true;
2165}
2166
Nate Begeman9008ca62009-04-27 18:41:29 +00002167bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2168 SmallVector<int, 8> M;
2169 N->getMask(M);
2170 return ::isPSHUFHWMask(M, N->getValueType(0));
2171}
Evan Cheng506d3df2006-03-29 23:07:14 +00002172
Nate Begeman9008ca62009-04-27 18:41:29 +00002173/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2174/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002175static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002176 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002177 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002178
Rafael Espindola15684b22009-04-24 12:40:33 +00002179 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002180 for (int i = 4; i != 8; ++i)
2181 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002182 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002183
Rafael Espindola15684b22009-04-24 12:40:33 +00002184 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002185 for (int i = 0; i != 4; ++i)
2186 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002187 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002188
Rafael Espindola15684b22009-04-24 12:40:33 +00002189 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002190}
2191
Nate Begeman9008ca62009-04-27 18:41:29 +00002192bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2193 SmallVector<int, 8> M;
2194 N->getMask(M);
2195 return ::isPSHUFLWMask(M, N->getValueType(0));
2196}
2197
Evan Cheng14aed5e2006-03-24 01:18:28 +00002198/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2199/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002200static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002201 int NumElems = VT.getVectorNumElements();
2202 if (NumElems != 2 && NumElems != 4)
2203 return false;
2204
2205 int Half = NumElems / 2;
2206 for (int i = 0; i < Half; ++i)
2207 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002208 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002209 for (int i = Half; i < NumElems; ++i)
2210 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002211 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002212
Evan Cheng14aed5e2006-03-24 01:18:28 +00002213 return true;
2214}
2215
Nate Begeman9008ca62009-04-27 18:41:29 +00002216bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2217 SmallVector<int, 8> M;
2218 N->getMask(M);
2219 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002220}
2221
Evan Cheng213d2cf2007-05-17 18:45:50 +00002222/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002223/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2224/// half elements to come from vector 1 (which would equal the dest.) and
2225/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002226static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002227 int NumElems = VT.getVectorNumElements();
2228
2229 if (NumElems != 2 && NumElems != 4)
2230 return false;
2231
2232 int Half = NumElems / 2;
2233 for (int i = 0; i < Half; ++i)
2234 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002235 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002236 for (int i = Half; i < NumElems; ++i)
2237 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002238 return false;
2239 return true;
2240}
2241
Nate Begeman9008ca62009-04-27 18:41:29 +00002242static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2243 SmallVector<int, 8> M;
2244 N->getMask(M);
2245 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002246}
2247
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002248/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2249/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002250bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2251 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002252 return false;
2253
Evan Cheng2064a2b2006-03-28 06:50:32 +00002254 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002255 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2256 isUndefOrEqual(N->getMaskElt(1), 7) &&
2257 isUndefOrEqual(N->getMaskElt(2), 2) &&
2258 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002259}
2260
Evan Cheng5ced1d82006-04-06 23:23:56 +00002261/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2262/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002263bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2264 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002265
Evan Cheng5ced1d82006-04-06 23:23:56 +00002266 if (NumElems != 2 && NumElems != 4)
2267 return false;
2268
Evan Chengc5cdff22006-04-07 21:53:05 +00002269 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002270 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002271 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002272
Evan Chengc5cdff22006-04-07 21:53:05 +00002273 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002274 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002275 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002276
2277 return true;
2278}
2279
2280/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002281/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2282/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002283bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2284 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002285
Evan Cheng5ced1d82006-04-06 23:23:56 +00002286 if (NumElems != 2 && NumElems != 4)
2287 return false;
2288
Evan Chengc5cdff22006-04-07 21:53:05 +00002289 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002290 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002291 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002292
Nate Begeman9008ca62009-04-27 18:41:29 +00002293 for (unsigned i = 0; i < NumElems/2; ++i)
2294 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002295 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002296
2297 return true;
2298}
2299
Nate Begeman9008ca62009-04-27 18:41:29 +00002300/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2301/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2302/// <2, 3, 2, 3>
2303bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2304 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2305
2306 if (NumElems != 4)
2307 return false;
2308
2309 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2310 isUndefOrEqual(N->getMaskElt(1), 3) &&
2311 isUndefOrEqual(N->getMaskElt(2), 2) &&
2312 isUndefOrEqual(N->getMaskElt(3), 3);
2313}
2314
Evan Cheng0038e592006-03-28 00:39:58 +00002315/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2316/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002317static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002318 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002319 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002320 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002321 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002322
2323 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2324 int BitI = Mask[i];
2325 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002326 if (!isUndefOrEqual(BitI, j))
2327 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002328 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002329 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002330 return false;
2331 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002332 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002333 return false;
2334 }
Evan Cheng0038e592006-03-28 00:39:58 +00002335 }
Evan Cheng0038e592006-03-28 00:39:58 +00002336 return true;
2337}
2338
Nate Begeman9008ca62009-04-27 18:41:29 +00002339bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2340 SmallVector<int, 8> M;
2341 N->getMask(M);
2342 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002343}
2344
Evan Cheng4fcb9222006-03-28 02:43:26 +00002345/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2346/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002347static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002348 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002349 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002350 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002351 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002352
2353 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2354 int BitI = Mask[i];
2355 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002356 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002357 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002358 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002359 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002360 return false;
2361 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002362 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002363 return false;
2364 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002365 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002366 return true;
2367}
2368
Nate Begeman9008ca62009-04-27 18:41:29 +00002369bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2370 SmallVector<int, 8> M;
2371 N->getMask(M);
2372 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002373}
2374
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002375/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2376/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2377/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002378static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002379 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002380 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002381 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002382
2383 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2384 int BitI = Mask[i];
2385 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002386 if (!isUndefOrEqual(BitI, j))
2387 return false;
2388 if (!isUndefOrEqual(BitI1, j))
2389 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002390 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002391 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002392}
2393
Nate Begeman9008ca62009-04-27 18:41:29 +00002394bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2395 SmallVector<int, 8> M;
2396 N->getMask(M);
2397 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2398}
2399
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002400/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2401/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2402/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002403static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002404 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002405 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2406 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002407
2408 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2409 int BitI = Mask[i];
2410 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002411 if (!isUndefOrEqual(BitI, j))
2412 return false;
2413 if (!isUndefOrEqual(BitI1, j))
2414 return false;
2415 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002416 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002417}
2418
Nate Begeman9008ca62009-04-27 18:41:29 +00002419bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2420 SmallVector<int, 8> M;
2421 N->getMask(M);
2422 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2423}
2424
Evan Cheng017dcc62006-04-21 01:05:10 +00002425/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2426/// specifies a shuffle of elements that is suitable for input to MOVSS,
2427/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002428static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002429 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002430 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002431
2432 int NumElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002433
2434 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002435 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002436
2437 for (int i = 1; i < NumElts; ++i)
2438 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002439 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002440
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002441 return true;
2442}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002443
Nate Begeman9008ca62009-04-27 18:41:29 +00002444bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2445 SmallVector<int, 8> M;
2446 N->getMask(M);
2447 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002448}
2449
Evan Cheng017dcc62006-04-21 01:05:10 +00002450/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2451/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002452/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002453static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002454 bool V2IsSplat = false, bool V2IsUndef = false) {
2455 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002456 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002457 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002458
2459 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002460 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002461
2462 for (int i = 1; i < NumOps; ++i)
2463 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2464 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2465 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002466 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002467
Evan Cheng39623da2006-04-20 08:58:49 +00002468 return true;
2469}
2470
Nate Begeman9008ca62009-04-27 18:41:29 +00002471static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002472 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002473 SmallVector<int, 8> M;
2474 N->getMask(M);
2475 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002476}
2477
Evan Chengd9539472006-04-14 21:59:03 +00002478/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2479/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002480bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2481 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002482 return false;
2483
2484 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002485 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002486 int Elt = N->getMaskElt(i);
2487 if (Elt >= 0 && Elt != 1)
2488 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002489 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002490
2491 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002492 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002493 int Elt = N->getMaskElt(i);
2494 if (Elt >= 0 && Elt != 3)
2495 return false;
2496 if (Elt == 3)
2497 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002498 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002499 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002500 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002501 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002502}
2503
2504/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2505/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002506bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2507 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002508 return false;
2509
2510 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002511 for (unsigned i = 0; i < 2; ++i)
2512 if (N->getMaskElt(i) > 0)
2513 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002514
2515 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002516 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002517 int Elt = N->getMaskElt(i);
2518 if (Elt >= 0 && Elt != 2)
2519 return false;
2520 if (Elt == 2)
2521 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002522 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002523 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002524 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002525}
2526
Evan Cheng0b457f02008-09-25 20:50:48 +00002527/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2528/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002529bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2530 int e = N->getValueType(0).getVectorNumElements() / 2;
2531
2532 for (int i = 0; i < e; ++i)
2533 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002534 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002535 for (int i = 0; i < e; ++i)
2536 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002537 return false;
2538 return true;
2539}
2540
Evan Cheng63d33002006-03-22 08:01:21 +00002541/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2542/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2543/// instructions.
2544unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002545 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2546 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2547
Evan Chengb9df0ca2006-03-22 02:53:00 +00002548 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2549 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002550 for (int i = 0; i < NumOperands; ++i) {
2551 int Val = SVOp->getMaskElt(NumOperands-i-1);
2552 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002553 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002554 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002555 if (i != NumOperands - 1)
2556 Mask <<= Shift;
2557 }
Evan Cheng63d33002006-03-22 08:01:21 +00002558 return Mask;
2559}
2560
Evan Cheng506d3df2006-03-29 23:07:14 +00002561/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2562/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2563/// instructions.
2564unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002565 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002566 unsigned Mask = 0;
2567 // 8 nodes, but we only care about the last 4.
2568 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002569 int Val = SVOp->getMaskElt(i);
2570 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002571 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002572 if (i != 4)
2573 Mask <<= 2;
2574 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002575 return Mask;
2576}
2577
2578/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2579/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2580/// instructions.
2581unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002582 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002583 unsigned Mask = 0;
2584 // 8 nodes, but we only care about the first 4.
2585 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002586 int Val = SVOp->getMaskElt(i);
2587 if (Val >= 0)
2588 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002589 if (i != 0)
2590 Mask <<= 2;
2591 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002592 return Mask;
2593}
2594
Nate Begeman9008ca62009-04-27 18:41:29 +00002595/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2596/// their permute mask.
2597static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2598 SelectionDAG &DAG) {
2599 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002600 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 SmallVector<int, 8> MaskVec;
2602
Nate Begeman5a5ca152009-04-29 05:20:52 +00002603 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002604 int idx = SVOp->getMaskElt(i);
2605 if (idx < 0)
2606 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002607 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002609 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002610 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002611 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2613 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002614}
2615
Evan Cheng779ccea2007-12-07 21:30:01 +00002616/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2617/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002618static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002619 unsigned NumElems = VT.getVectorNumElements();
2620 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002621 int idx = Mask[i];
2622 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002623 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002624 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002626 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002627 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002628 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002629}
2630
Evan Cheng533a0aa2006-04-19 20:35:22 +00002631/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2632/// match movhlps. The lower half elements should come from upper half of
2633/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002634/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002635static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2636 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002637 return false;
2638 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002640 return false;
2641 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002642 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002643 return false;
2644 return true;
2645}
2646
Evan Cheng5ced1d82006-04-06 23:23:56 +00002647/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002648/// is promoted to a vector. It also returns the LoadSDNode by reference if
2649/// required.
2650static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002651 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2652 return false;
2653 N = N->getOperand(0).getNode();
2654 if (!ISD::isNON_EXTLoad(N))
2655 return false;
2656 if (LD)
2657 *LD = cast<LoadSDNode>(N);
2658 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002659}
2660
Evan Cheng533a0aa2006-04-19 20:35:22 +00002661/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2662/// match movlp{s|d}. The lower half elements should come from lower half of
2663/// V1 (and in order), and the upper half elements should come from the upper
2664/// half of V2 (and in order). And since V1 will become the source of the
2665/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002666static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2667 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002668 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002669 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002670 // Is V2 is a vector load, don't do this transformation. We will try to use
2671 // load folding shufps op.
2672 if (ISD::isNON_EXTLoad(V2))
2673 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002674
Nate Begeman5a5ca152009-04-29 05:20:52 +00002675 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002676
Evan Cheng533a0aa2006-04-19 20:35:22 +00002677 if (NumElems != 2 && NumElems != 4)
2678 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002679 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002680 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002681 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002682 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002683 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002684 return false;
2685 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002686}
2687
Evan Cheng39623da2006-04-20 08:58:49 +00002688/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2689/// all the same.
2690static bool isSplatVector(SDNode *N) {
2691 if (N->getOpcode() != ISD::BUILD_VECTOR)
2692 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002693
Dan Gohman475871a2008-07-27 21:46:04 +00002694 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002695 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2696 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002697 return false;
2698 return true;
2699}
2700
Evan Cheng213d2cf2007-05-17 18:45:50 +00002701/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2702/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002703static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002704 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002705 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002706 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002707 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002708}
2709
2710/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002711/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002712/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002713static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002714 SDValue V1 = N->getOperand(0);
2715 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002716 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2717 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002718 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002719 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002720 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002721 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2722 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002723 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2724 return false;
2725 } else if (Idx >= 0) {
2726 unsigned Opc = V1.getOpcode();
2727 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2728 continue;
2729 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002730 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002731 }
2732 }
2733 return true;
2734}
2735
2736/// getZeroVector - Returns a vector of specified type with all zero elements.
2737///
Dale Johannesenace16102009-02-03 19:33:06 +00002738static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2739 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002740 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002741
Chris Lattner8a594482007-11-25 00:24:49 +00002742 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2743 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002744 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002745 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002747 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002748 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002749 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002750 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002751 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002753 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002754 }
Dale Johannesenace16102009-02-03 19:33:06 +00002755 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002756}
2757
Chris Lattner8a594482007-11-25 00:24:49 +00002758/// getOnesVector - Returns a vector of specified type with all bits set.
2759///
Dale Johannesenace16102009-02-03 19:33:06 +00002760static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002762
Chris Lattner8a594482007-11-25 00:24:49 +00002763 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2764 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002765 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2766 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002767 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002768 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002769 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002770 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002771 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002772}
2773
2774
Evan Cheng39623da2006-04-20 08:58:49 +00002775/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2776/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002777static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2778 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002779 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002780
Evan Cheng39623da2006-04-20 08:58:49 +00002781 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002782 SmallVector<int, 8> MaskVec;
2783 SVOp->getMask(MaskVec);
2784
Nate Begeman5a5ca152009-04-29 05:20:52 +00002785 for (unsigned i = 0; i != NumElems; ++i) {
2786 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002787 MaskVec[i] = NumElems;
2788 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002789 }
Evan Cheng39623da2006-04-20 08:58:49 +00002790 }
Evan Cheng39623da2006-04-20 08:58:49 +00002791 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002792 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2793 SVOp->getOperand(1), &MaskVec[0]);
2794 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002795}
2796
Evan Cheng017dcc62006-04-21 01:05:10 +00002797/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2798/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002799static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2800 SDValue V2) {
2801 unsigned NumElems = VT.getVectorNumElements();
2802 SmallVector<int, 8> Mask;
2803 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002804 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 Mask.push_back(i);
2806 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002807}
2808
Nate Begeman9008ca62009-04-27 18:41:29 +00002809/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2810static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2811 SDValue V2) {
2812 unsigned NumElems = VT.getVectorNumElements();
2813 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002814 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002815 Mask.push_back(i);
2816 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002817 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002818 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002819}
2820
Nate Begeman9008ca62009-04-27 18:41:29 +00002821/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2822static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2823 SDValue V2) {
2824 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002825 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002826 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002827 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 Mask.push_back(i + Half);
2829 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002830 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002831 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002832}
2833
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002834/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002835static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2836 bool HasSSE2) {
2837 if (SV->getValueType(0).getVectorNumElements() <= 4)
2838 return SDValue(SV, 0);
2839
2840 MVT PVT = MVT::v4f32;
2841 MVT VT = SV->getValueType(0);
2842 DebugLoc dl = SV->getDebugLoc();
2843 SDValue V1 = SV->getOperand(0);
2844 int NumElems = VT.getVectorNumElements();
2845 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002846
Nate Begeman9008ca62009-04-27 18:41:29 +00002847 // unpack elements to the correct location
2848 while (NumElems > 4) {
2849 if (EltNo < NumElems/2) {
2850 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2851 } else {
2852 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2853 EltNo -= NumElems/2;
2854 }
2855 NumElems >>= 1;
2856 }
2857
2858 // Perform the splat.
2859 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002860 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2862 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002863}
2864
Evan Chengba05f722006-04-21 23:03:30 +00002865/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002866/// vector of zero or undef vector. This produces a shuffle where the low
2867/// element of V2 is swizzled into the zero/undef vector, landing at element
2868/// 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 +00002869static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002870 bool isZero, bool HasSSE2,
2871 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002872 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002873 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2875 unsigned NumElems = VT.getVectorNumElements();
2876 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002877 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 // If this is the insertion idx, put the low elt of V2 here.
2879 MaskVec.push_back(i == Idx ? NumElems : i);
2880 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002881}
2882
Evan Chengf26ffe92008-05-29 08:22:04 +00002883/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2884/// a shuffle that is zero.
2885static
Nate Begeman9008ca62009-04-27 18:41:29 +00002886unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2887 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002888 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002890 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002891 int Idx = SVOp->getMaskElt(Index);
2892 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002893 ++NumZeros;
2894 continue;
2895 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002896 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002897 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002898 ++NumZeros;
2899 else
2900 break;
2901 }
2902 return NumZeros;
2903}
2904
2905/// isVectorShift - Returns true if the shuffle can be implemented as a
2906/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002907/// FIXME: split into pslldqi, psrldqi, palignr variants.
2908static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002909 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002910 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002911
2912 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002914 if (!NumZeros) {
2915 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002916 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002917 if (!NumZeros)
2918 return false;
2919 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002920 bool SeenV1 = false;
2921 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002922 for (int i = NumZeros; i < NumElems; ++i) {
2923 int Val = isLeft ? (i - NumZeros) : i;
2924 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2925 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002926 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002927 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002928 SeenV1 = true;
2929 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002931 SeenV2 = true;
2932 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002933 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002934 return false;
2935 }
2936 if (SeenV1 && SeenV2)
2937 return false;
2938
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002940 ShAmt = NumZeros;
2941 return true;
2942}
2943
2944
Evan Chengc78d3b42006-04-24 18:01:45 +00002945/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2946///
Dan Gohman475871a2008-07-27 21:46:04 +00002947static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002948 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002949 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002950 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002951 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002952
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002953 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002954 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002955 bool First = true;
2956 for (unsigned i = 0; i < 16; ++i) {
2957 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2958 if (ThisIsNonZero && First) {
2959 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002960 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002961 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002962 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002963 First = false;
2964 }
2965
2966 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002967 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002968 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2969 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002970 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002971 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002972 }
2973 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002974 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2975 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002976 ThisElt, DAG.getConstant(8, MVT::i8));
2977 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002978 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00002979 } else
2980 ThisElt = LastElt;
2981
Gabor Greifba36cb52008-08-28 21:40:38 +00002982 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00002983 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002984 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002985 }
2986 }
2987
Dale Johannesenace16102009-02-03 19:33:06 +00002988 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00002989}
2990
Bill Wendlinga348c562007-03-22 18:42:45 +00002991/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002992///
Dan Gohman475871a2008-07-27 21:46:04 +00002993static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002994 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002995 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002996 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00002997 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002998
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002999 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003000 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003001 bool First = true;
3002 for (unsigned i = 0; i < 8; ++i) {
3003 bool isNonZero = (NonZeros & (1 << i)) != 0;
3004 if (isNonZero) {
3005 if (First) {
3006 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003007 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003008 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003009 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003010 First = false;
3011 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003012 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003013 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003014 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003015 }
3016 }
3017
3018 return V;
3019}
3020
Evan Chengf26ffe92008-05-29 08:22:04 +00003021/// getVShift - Return a vector logical shift node.
3022///
Dan Gohman475871a2008-07-27 21:46:04 +00003023static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 unsigned NumBits, SelectionDAG &DAG,
3025 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003026 bool isMMX = VT.getSizeInBits() == 64;
3027 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003028 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003029 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3030 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3031 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003032 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003033}
3034
Dan Gohman475871a2008-07-27 21:46:04 +00003035SDValue
3036X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003037 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003038 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003039 if (ISD::isBuildVectorAllZeros(Op.getNode())
3040 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003041 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3042 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3043 // eliminated on x86-32 hosts.
3044 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3045 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003046
Gabor Greifba36cb52008-08-28 21:40:38 +00003047 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003048 return getOnesVector(Op.getValueType(), DAG, dl);
3049 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003050 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003051
Duncan Sands83ec4b62008-06-06 12:08:01 +00003052 MVT VT = Op.getValueType();
3053 MVT EVT = VT.getVectorElementType();
3054 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003055
3056 unsigned NumElems = Op.getNumOperands();
3057 unsigned NumZero = 0;
3058 unsigned NumNonZero = 0;
3059 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003060 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003061 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003062 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003063 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003064 if (Elt.getOpcode() == ISD::UNDEF)
3065 continue;
3066 Values.insert(Elt);
3067 if (Elt.getOpcode() != ISD::Constant &&
3068 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003069 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003070 if (isZeroNode(Elt))
3071 NumZero++;
3072 else {
3073 NonZeros |= (1 << i);
3074 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003075 }
3076 }
3077
Dan Gohman7f321562007-06-25 16:23:39 +00003078 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003079 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003080 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003081 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003082
Chris Lattner67f453a2008-03-09 05:42:06 +00003083 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003084 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003085 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003086 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003087
Chris Lattner62098042008-03-09 01:05:04 +00003088 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3089 // the value are obviously zero, truncate the value to i32 and do the
3090 // insertion that way. Only do this if the value is non-constant or if the
3091 // value is a constant being inserted into element 0. It is cheaper to do
3092 // a constant pool load than it is to do a movd + shuffle.
3093 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3094 (!IsAllConstants || Idx == 0)) {
3095 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3096 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003097 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3098 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003099
Chris Lattner62098042008-03-09 01:05:04 +00003100 // Truncate the value (which may itself be a constant) to i32, and
3101 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003102 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3103 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003104 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3105 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003106
Chris Lattner62098042008-03-09 01:05:04 +00003107 // Now we have our 32-bit value zero extended in the low element of
3108 // a vector. If Idx != 0, swizzle it into place.
3109 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003110 SmallVector<int, 4> Mask;
3111 Mask.push_back(Idx);
3112 for (unsigned i = 1; i != VecElts; ++i)
3113 Mask.push_back(i);
3114 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3115 DAG.getUNDEF(Item.getValueType()),
3116 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003117 }
Dale Johannesenace16102009-02-03 19:33:06 +00003118 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003119 }
3120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003121
Chris Lattner19f79692008-03-08 22:59:52 +00003122 // If we have a constant or non-constant insertion into the low element of
3123 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3124 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003125 // depending on what the source datatype is.
3126 if (Idx == 0) {
3127 if (NumZero == 0) {
3128 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3129 } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 ||
3130 (EVT == MVT::i64 && Subtarget->is64Bit())) {
3131 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3132 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3133 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3134 DAG);
3135 } else if (EVT == MVT::i16 || EVT == MVT::i8) {
3136 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3137 MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
3138 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3139 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3140 Subtarget->hasSSE2(), DAG);
3141 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3142 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003143 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003144
3145 // Is it a vector logical left shift?
3146 if (NumElems == 2 && Idx == 1 &&
3147 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003148 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003149 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003150 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003151 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003152 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003153 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003154
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003155 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003156 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003157
Chris Lattner19f79692008-03-08 22:59:52 +00003158 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3159 // is a non-constant being inserted into an element other than the low one,
3160 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3161 // movd/movss) to move this into the low element, then shuffle it into
3162 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003163 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003164 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003165
Evan Cheng0db9fe62006-04-25 20:13:52 +00003166 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003167 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3168 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003170 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 MaskVec.push_back(i == Idx ? 0 : 1);
3172 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003173 }
3174 }
3175
Chris Lattner67f453a2008-03-09 05:42:06 +00003176 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3177 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003178 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003179
Dan Gohmana3941172007-07-24 22:55:08 +00003180 // A vector full of immediates; various special cases are already
3181 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003182 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003183 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003184
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003185 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003186 if (EVTBits == 64) {
3187 if (NumNonZero == 1) {
3188 // One half is zero or undef.
3189 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003190 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003191 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003192 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3193 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003194 }
Dan Gohman475871a2008-07-27 21:46:04 +00003195 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003196 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003197
3198 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003199 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003200 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003201 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003202 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 }
3204
Bill Wendling826f36f2007-03-28 00:57:11 +00003205 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003206 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003207 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003208 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003209 }
3210
3211 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003212 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003213 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003214 if (NumElems == 4 && NumZero > 0) {
3215 for (unsigned i = 0; i < 4; ++i) {
3216 bool isZero = !(NonZeros & (1 << i));
3217 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003218 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003219 else
Dale Johannesenace16102009-02-03 19:33:06 +00003220 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003221 }
3222
3223 for (unsigned i = 0; i < 2; ++i) {
3224 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3225 default: break;
3226 case 0:
3227 V[i] = V[i*2]; // Must be a zero vector.
3228 break;
3229 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003231 break;
3232 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003234 break;
3235 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003237 break;
3238 }
3239 }
3240
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003242 bool Reverse = (NonZeros & 0x3) == 2;
3243 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003245 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3246 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3248 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003249 }
3250
3251 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003252 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3253 // values to be inserted is equal to the number of elements, in which case
3254 // use the unpack code below in the hopes of matching the consecutive elts
3255 // load merge pattern for shuffles.
3256 // FIXME: We could probably just check that here directly.
3257 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3258 getSubtarget()->hasSSE41()) {
3259 V[0] = DAG.getUNDEF(VT);
3260 for (unsigned i = 0; i < NumElems; ++i)
3261 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3262 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3263 Op.getOperand(i), DAG.getIntPtrConstant(i));
3264 return V[0];
3265 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003266 // Expand into a number of unpckl*.
3267 // e.g. for v4f32
3268 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3269 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3270 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003271 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003272 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003273 NumElems >>= 1;
3274 while (NumElems != 0) {
3275 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003276 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003277 NumElems >>= 1;
3278 }
3279 return V[0];
3280 }
3281
Dan Gohman475871a2008-07-27 21:46:04 +00003282 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003283}
3284
Nate Begemanb9a47b82009-02-23 08:49:38 +00003285// v8i16 shuffles - Prefer shuffles in the following order:
3286// 1. [all] pshuflw, pshufhw, optional move
3287// 2. [ssse3] 1 x pshufb
3288// 3. [ssse3] 2 x pshufb + 1 x por
3289// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003290static
Nate Begeman9008ca62009-04-27 18:41:29 +00003291SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3292 SelectionDAG &DAG, X86TargetLowering &TLI) {
3293 SDValue V1 = SVOp->getOperand(0);
3294 SDValue V2 = SVOp->getOperand(1);
3295 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003296 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003297
Nate Begemanb9a47b82009-02-23 08:49:38 +00003298 // Determine if more than 1 of the words in each of the low and high quadwords
3299 // of the result come from the same quadword of one of the two inputs. Undef
3300 // mask values count as coming from any quadword, for better codegen.
3301 SmallVector<unsigned, 4> LoQuad(4);
3302 SmallVector<unsigned, 4> HiQuad(4);
3303 BitVector InputQuads(4);
3304 for (unsigned i = 0; i < 8; ++i) {
3305 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003307 MaskVals.push_back(EltIdx);
3308 if (EltIdx < 0) {
3309 ++Quad[0];
3310 ++Quad[1];
3311 ++Quad[2];
3312 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003313 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003314 }
3315 ++Quad[EltIdx / 4];
3316 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003317 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003318
Nate Begemanb9a47b82009-02-23 08:49:38 +00003319 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003320 unsigned MaxQuad = 1;
3321 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003322 if (LoQuad[i] > MaxQuad) {
3323 BestLoQuad = i;
3324 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003325 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003326 }
3327
Nate Begemanb9a47b82009-02-23 08:49:38 +00003328 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003329 MaxQuad = 1;
3330 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003331 if (HiQuad[i] > MaxQuad) {
3332 BestHiQuad = i;
3333 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003334 }
3335 }
3336
Nate Begemanb9a47b82009-02-23 08:49:38 +00003337 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3338 // of the two input vectors, shuffle them into one input vector so only a
3339 // single pshufb instruction is necessary. If There are more than 2 input
3340 // quads, disable the next transformation since it does not help SSSE3.
3341 bool V1Used = InputQuads[0] || InputQuads[1];
3342 bool V2Used = InputQuads[2] || InputQuads[3];
3343 if (TLI.getSubtarget()->hasSSSE3()) {
3344 if (InputQuads.count() == 2 && V1Used && V2Used) {
3345 BestLoQuad = InputQuads.find_first();
3346 BestHiQuad = InputQuads.find_next(BestLoQuad);
3347 }
3348 if (InputQuads.count() > 2) {
3349 BestLoQuad = -1;
3350 BestHiQuad = -1;
3351 }
3352 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003353
Nate Begemanb9a47b82009-02-23 08:49:38 +00003354 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3355 // the shuffle mask. If a quad is scored as -1, that means that it contains
3356 // words from all 4 input quadwords.
3357 SDValue NewV;
3358 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 SmallVector<int, 8> MaskV;
3360 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3361 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3362 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3363 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3364 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003365 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003366
Nate Begemanb9a47b82009-02-23 08:49:38 +00003367 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3368 // source words for the shuffle, to aid later transformations.
3369 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003370 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003371 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003372 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003373 if (idx != (int)i)
3374 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003375 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003376 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003377 AllWordsInNewV = false;
3378 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003379 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003380
Nate Begemanb9a47b82009-02-23 08:49:38 +00003381 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3382 if (AllWordsInNewV) {
3383 for (int i = 0; i != 8; ++i) {
3384 int idx = MaskVals[i];
3385 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003386 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003387 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3388 if ((idx != i) && idx < 4)
3389 pshufhw = false;
3390 if ((idx != i) && idx > 3)
3391 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003392 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003393 V1 = NewV;
3394 V2Used = false;
3395 BestLoQuad = 0;
3396 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003397 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003398
Nate Begemanb9a47b82009-02-23 08:49:38 +00003399 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3400 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003401 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3403 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003404 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003405 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003406
3407 // If we have SSSE3, and all words of the result are from 1 input vector,
3408 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3409 // is present, fall back to case 4.
3410 if (TLI.getSubtarget()->hasSSSE3()) {
3411 SmallVector<SDValue,16> pshufbMask;
3412
3413 // If we have elements from both input vectors, set the high bit of the
3414 // shuffle mask element to zero out elements that come from V2 in the V1
3415 // mask, and elements that come from V1 in the V2 mask, so that the two
3416 // results can be OR'd together.
3417 bool TwoInputs = V1Used && V2Used;
3418 for (unsigned i = 0; i != 8; ++i) {
3419 int EltIdx = MaskVals[i] * 2;
3420 if (TwoInputs && (EltIdx >= 16)) {
3421 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3422 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3423 continue;
3424 }
3425 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3426 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3427 }
3428 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3429 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003430 DAG.getNode(ISD::BUILD_VECTOR, dl,
3431 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003432 if (!TwoInputs)
3433 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3434
3435 // Calculate the shuffle mask for the second input, shuffle it, and
3436 // OR it with the first shuffled input.
3437 pshufbMask.clear();
3438 for (unsigned i = 0; i != 8; ++i) {
3439 int EltIdx = MaskVals[i] * 2;
3440 if (EltIdx < 16) {
3441 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3442 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3443 continue;
3444 }
3445 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3446 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3447 }
3448 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3449 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003450 DAG.getNode(ISD::BUILD_VECTOR, dl,
3451 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003452 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3453 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3454 }
3455
3456 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3457 // and update MaskVals with new element order.
3458 BitVector InOrder(8);
3459 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003461 for (int i = 0; i != 4; ++i) {
3462 int idx = MaskVals[i];
3463 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003465 InOrder.set(i);
3466 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003468 InOrder.set(i);
3469 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003471 }
3472 }
3473 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 MaskV.push_back(i);
3475 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3476 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003477 }
3478
3479 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3480 // and update MaskVals with the new element order.
3481 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003483 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003484 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003485 for (unsigned i = 4; i != 8; ++i) {
3486 int idx = MaskVals[i];
3487 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003488 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003489 InOrder.set(i);
3490 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003492 InOrder.set(i);
3493 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003494 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003495 }
3496 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3498 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003499 }
3500
3501 // In case BestHi & BestLo were both -1, which means each quadword has a word
3502 // from each of the four input quadwords, calculate the InOrder bitvector now
3503 // before falling through to the insert/extract cleanup.
3504 if (BestLoQuad == -1 && BestHiQuad == -1) {
3505 NewV = V1;
3506 for (int i = 0; i != 8; ++i)
3507 if (MaskVals[i] < 0 || MaskVals[i] == i)
3508 InOrder.set(i);
3509 }
3510
3511 // The other elements are put in the right place using pextrw and pinsrw.
3512 for (unsigned i = 0; i != 8; ++i) {
3513 if (InOrder[i])
3514 continue;
3515 int EltIdx = MaskVals[i];
3516 if (EltIdx < 0)
3517 continue;
3518 SDValue ExtOp = (EltIdx < 8)
3519 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3520 DAG.getIntPtrConstant(EltIdx))
3521 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3522 DAG.getIntPtrConstant(EltIdx - 8));
3523 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3524 DAG.getIntPtrConstant(i));
3525 }
3526 return NewV;
3527}
3528
3529// v16i8 shuffles - Prefer shuffles in the following order:
3530// 1. [ssse3] 1 x pshufb
3531// 2. [ssse3] 2 x pshufb + 1 x por
3532// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3533static
Nate Begeman9008ca62009-04-27 18:41:29 +00003534SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3535 SelectionDAG &DAG, X86TargetLowering &TLI) {
3536 SDValue V1 = SVOp->getOperand(0);
3537 SDValue V2 = SVOp->getOperand(1);
3538 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003539 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003540 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003541
3542 // If we have SSSE3, case 1 is generated when all result bytes come from
3543 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3544 // present, fall back to case 3.
3545 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3546 bool V1Only = true;
3547 bool V2Only = true;
3548 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003549 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003550 if (EltIdx < 0)
3551 continue;
3552 if (EltIdx < 16)
3553 V2Only = false;
3554 else
3555 V1Only = false;
3556 }
3557
3558 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3559 if (TLI.getSubtarget()->hasSSSE3()) {
3560 SmallVector<SDValue,16> pshufbMask;
3561
3562 // If all result elements are from one input vector, then only translate
3563 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3564 //
3565 // Otherwise, we have elements from both input vectors, and must zero out
3566 // elements that come from V2 in the first mask, and V1 in the second mask
3567 // so that we can OR them together.
3568 bool TwoInputs = !(V1Only || V2Only);
3569 for (unsigned i = 0; i != 16; ++i) {
3570 int EltIdx = MaskVals[i];
3571 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3572 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3573 continue;
3574 }
3575 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3576 }
3577 // If all the elements are from V2, assign it to V1 and return after
3578 // building the first pshufb.
3579 if (V2Only)
3580 V1 = V2;
3581 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003582 DAG.getNode(ISD::BUILD_VECTOR, dl,
3583 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003584 if (!TwoInputs)
3585 return V1;
3586
3587 // Calculate the shuffle mask for the second input, shuffle it, and
3588 // OR it with the first shuffled input.
3589 pshufbMask.clear();
3590 for (unsigned i = 0; i != 16; ++i) {
3591 int EltIdx = MaskVals[i];
3592 if (EltIdx < 16) {
3593 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3594 continue;
3595 }
3596 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3597 }
3598 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003599 DAG.getNode(ISD::BUILD_VECTOR, dl,
3600 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003601 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3602 }
3603
3604 // No SSSE3 - Calculate in place words and then fix all out of place words
3605 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3606 // the 16 different words that comprise the two doublequadword input vectors.
3607 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3608 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3609 SDValue NewV = V2Only ? V2 : V1;
3610 for (int i = 0; i != 8; ++i) {
3611 int Elt0 = MaskVals[i*2];
3612 int Elt1 = MaskVals[i*2+1];
3613
3614 // This word of the result is all undef, skip it.
3615 if (Elt0 < 0 && Elt1 < 0)
3616 continue;
3617
3618 // This word of the result is already in the correct place, skip it.
3619 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3620 continue;
3621 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3622 continue;
3623
3624 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3625 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3626 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003627
3628 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3629 // using a single extract together, load it and store it.
3630 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3631 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3632 DAG.getIntPtrConstant(Elt1 / 2));
3633 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3634 DAG.getIntPtrConstant(i));
3635 continue;
3636 }
3637
Nate Begemanb9a47b82009-02-23 08:49:38 +00003638 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003639 // source byte is not also odd, shift the extracted word left 8 bits
3640 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003641 if (Elt1 >= 0) {
3642 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3643 DAG.getIntPtrConstant(Elt1 / 2));
3644 if ((Elt1 & 1) == 0)
3645 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3646 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003647 else if (Elt0 >= 0)
3648 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3649 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003650 }
3651 // If Elt0 is defined, extract it from the appropriate source. If the
3652 // source byte is not also even, shift the extracted word right 8 bits. If
3653 // Elt1 was also defined, OR the extracted values together before
3654 // inserting them in the result.
3655 if (Elt0 >= 0) {
3656 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3657 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3658 if ((Elt0 & 1) != 0)
3659 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3660 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003661 else if (Elt1 >= 0)
3662 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3663 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003664 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3665 : InsElt0;
3666 }
3667 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3668 DAG.getIntPtrConstant(i));
3669 }
3670 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003671}
3672
Evan Cheng7a831ce2007-12-15 03:00:47 +00003673/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3674/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3675/// done when every pair / quad of shuffle mask elements point to elements in
3676/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003677/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3678static
Nate Begeman9008ca62009-04-27 18:41:29 +00003679SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3680 SelectionDAG &DAG,
3681 TargetLowering &TLI, DebugLoc dl) {
3682 MVT VT = SVOp->getValueType(0);
3683 SDValue V1 = SVOp->getOperand(0);
3684 SDValue V2 = SVOp->getOperand(1);
3685 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003686 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003687 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003688 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003689 MVT NewVT = MaskVT;
3690 switch (VT.getSimpleVT()) {
3691 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003692 case MVT::v4f32: NewVT = MVT::v2f64; break;
3693 case MVT::v4i32: NewVT = MVT::v2i64; break;
3694 case MVT::v8i16: NewVT = MVT::v4i32; break;
3695 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003696 }
3697
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003698 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003699 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003700 NewVT = MVT::v2i64;
3701 else
3702 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003703 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003704 int Scale = NumElems / NewWidth;
3705 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003706 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 int StartIdx = -1;
3708 for (int j = 0; j < Scale; ++j) {
3709 int EltIdx = SVOp->getMaskElt(i+j);
3710 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003711 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003713 StartIdx = EltIdx - (EltIdx % Scale);
3714 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003715 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003716 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 if (StartIdx == -1)
3718 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003719 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003720 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003721 }
3722
Dale Johannesenace16102009-02-03 19:33:06 +00003723 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3724 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003725 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003726}
3727
Evan Chengd880b972008-05-09 21:53:03 +00003728/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003729///
Dan Gohman475871a2008-07-27 21:46:04 +00003730static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003731 SDValue SrcOp, SelectionDAG &DAG,
3732 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003733 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3734 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003735 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003736 LD = dyn_cast<LoadSDNode>(SrcOp);
3737 if (!LD) {
3738 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3739 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003740 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003741 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3742 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3743 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3744 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3745 // PR2108
3746 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003747 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3748 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3749 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3750 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003751 SrcOp.getOperand(0)
3752 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003753 }
3754 }
3755 }
3756
Dale Johannesenace16102009-02-03 19:33:06 +00003757 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3758 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003759 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003760 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003761}
3762
Evan Chengace3c172008-07-22 21:13:36 +00003763/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3764/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003765static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003766LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3767 SDValue V1 = SVOp->getOperand(0);
3768 SDValue V2 = SVOp->getOperand(1);
3769 DebugLoc dl = SVOp->getDebugLoc();
3770 MVT VT = SVOp->getValueType(0);
3771
Evan Chengace3c172008-07-22 21:13:36 +00003772 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003773 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003774 SmallVector<int, 8> Mask1(4U, -1);
3775 SmallVector<int, 8> PermMask;
3776 SVOp->getMask(PermMask);
3777
Evan Chengace3c172008-07-22 21:13:36 +00003778 unsigned NumHi = 0;
3779 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003780 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003781 int Idx = PermMask[i];
3782 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003783 Locs[i] = std::make_pair(-1, -1);
3784 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3786 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003787 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003789 NumLo++;
3790 } else {
3791 Locs[i] = std::make_pair(1, NumHi);
3792 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003794 NumHi++;
3795 }
3796 }
3797 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003798
Evan Chengace3c172008-07-22 21:13:36 +00003799 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003800 // If no more than two elements come from either vector. This can be
3801 // implemented with two shuffles. First shuffle gather the elements.
3802 // The second shuffle, which takes the first shuffle as both of its
3803 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003805
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 SmallVector<int, 8> Mask2(4U, -1);
3807
Evan Chengace3c172008-07-22 21:13:36 +00003808 for (unsigned i = 0; i != 4; ++i) {
3809 if (Locs[i].first == -1)
3810 continue;
3811 else {
3812 unsigned Idx = (i < 2) ? 0 : 4;
3813 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003815 }
3816 }
3817
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003819 } else if (NumLo == 3 || NumHi == 3) {
3820 // Otherwise, we must have three elements from one vector, call it X, and
3821 // one element from the other, call it Y. First, use a shufps to build an
3822 // intermediate vector with the one element from Y and the element from X
3823 // that will be in the same half in the final destination (the indexes don't
3824 // matter). Then, use a shufps to build the final vector, taking the half
3825 // containing the element from Y from the intermediate, and the other half
3826 // from X.
3827 if (NumHi == 3) {
3828 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003829 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003830 std::swap(V1, V2);
3831 }
3832
3833 // Find the element from V2.
3834 unsigned HiIndex;
3835 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003836 int Val = PermMask[HiIndex];
3837 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003838 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003839 if (Val >= 4)
3840 break;
3841 }
3842
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 Mask1[0] = PermMask[HiIndex];
3844 Mask1[1] = -1;
3845 Mask1[2] = PermMask[HiIndex^1];
3846 Mask1[3] = -1;
3847 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003848
3849 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003850 Mask1[0] = PermMask[0];
3851 Mask1[1] = PermMask[1];
3852 Mask1[2] = HiIndex & 1 ? 6 : 4;
3853 Mask1[3] = HiIndex & 1 ? 4 : 6;
3854 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003855 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 Mask1[0] = HiIndex & 1 ? 2 : 0;
3857 Mask1[1] = HiIndex & 1 ? 0 : 2;
3858 Mask1[2] = PermMask[2];
3859 Mask1[3] = PermMask[3];
3860 if (Mask1[2] >= 0)
3861 Mask1[2] += 4;
3862 if (Mask1[3] >= 0)
3863 Mask1[3] += 4;
3864 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003865 }
Evan Chengace3c172008-07-22 21:13:36 +00003866 }
3867
3868 // Break it into (shuffle shuffle_hi, shuffle_lo).
3869 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 SmallVector<int,8> LoMask(4U, -1);
3871 SmallVector<int,8> HiMask(4U, -1);
3872
3873 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003874 unsigned MaskIdx = 0;
3875 unsigned LoIdx = 0;
3876 unsigned HiIdx = 2;
3877 for (unsigned i = 0; i != 4; ++i) {
3878 if (i == 2) {
3879 MaskPtr = &HiMask;
3880 MaskIdx = 1;
3881 LoIdx = 0;
3882 HiIdx = 2;
3883 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 int Idx = PermMask[i];
3885 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003886 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003888 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003889 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003890 LoIdx++;
3891 } else {
3892 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003894 HiIdx++;
3895 }
3896 }
3897
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3899 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3900 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003901 for (unsigned i = 0; i != 4; ++i) {
3902 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003904 } else {
3905 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003907 }
3908 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003910}
3911
Dan Gohman475871a2008-07-27 21:46:04 +00003912SDValue
3913X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003914 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue V1 = Op.getOperand(0);
3916 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003917 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003918 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003920 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003921 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3922 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003923 bool V1IsSplat = false;
3924 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003925
Nate Begeman9008ca62009-04-27 18:41:29 +00003926 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003927 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003928
Nate Begeman9008ca62009-04-27 18:41:29 +00003929 // Promote splats to v4f32.
3930 if (SVOp->isSplat()) {
3931 if (isMMX || NumElems < 4)
3932 return Op;
3933 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 }
3935
Evan Cheng7a831ce2007-12-15 03:00:47 +00003936 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3937 // do it!
3938 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003940 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003941 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003942 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003943 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3944 // FIXME: Figure out a cleaner way to do this.
3945 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003946 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003948 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3950 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3951 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003952 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003953 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3955 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003956 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003957 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003958 }
3959 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003960
3961 if (X86::isPSHUFDMask(SVOp))
3962 return Op;
3963
Evan Chengf26ffe92008-05-29 08:22:04 +00003964 // Check if this can be converted into a logical shift.
3965 bool isLeft = false;
3966 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003967 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 bool isShift = getSubtarget()->hasSSE2() &&
3969 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003970 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003971 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003972 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003973 MVT EVT = VT.getVectorElementType();
3974 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003975 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003976 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003977
3978 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003979 if (V1IsUndef)
3980 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003981 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003982 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003983 if (!isMMX)
3984 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003985 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003986
3987 // FIXME: fold these into legal mask.
3988 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
3989 X86::isMOVSLDUPMask(SVOp) ||
3990 X86::isMOVHLPSMask(SVOp) ||
3991 X86::isMOVHPMask(SVOp) ||
3992 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003993 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003994
Nate Begeman9008ca62009-04-27 18:41:29 +00003995 if (ShouldXformToMOVHLPS(SVOp) ||
3996 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
3997 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003998
Evan Chengf26ffe92008-05-29 08:22:04 +00003999 if (isShift) {
4000 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004001 MVT EVT = VT.getVectorElementType();
4002 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004003 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004004 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004005
Evan Cheng9eca5e82006-10-25 21:49:50 +00004006 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004007 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4008 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004009 V1IsSplat = isSplatVector(V1.getNode());
4010 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004011
Chris Lattner8a594482007-11-25 00:24:49 +00004012 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004013 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004014 Op = CommuteVectorShuffle(SVOp, DAG);
4015 SVOp = cast<ShuffleVectorSDNode>(Op);
4016 V1 = SVOp->getOperand(0);
4017 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004018 std::swap(V1IsSplat, V2IsSplat);
4019 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004020 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004021 }
4022
Nate Begeman9008ca62009-04-27 18:41:29 +00004023 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4024 // Shuffling low element of v1 into undef, just return v1.
4025 if (V2IsUndef)
4026 return V1;
4027 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4028 // the instruction selector will not match, so get a canonical MOVL with
4029 // swapped operands to undo the commute.
4030 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004031 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004032
Nate Begeman9008ca62009-04-27 18:41:29 +00004033 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4034 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4035 X86::isUNPCKLMask(SVOp) ||
4036 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004037 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004038
Evan Cheng9bbbb982006-10-25 20:48:19 +00004039 if (V2IsSplat) {
4040 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004041 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004042 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004043 SDValue NewMask = NormalizeMask(SVOp, DAG);
4044 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4045 if (NSVOp != SVOp) {
4046 if (X86::isUNPCKLMask(NSVOp, true)) {
4047 return NewMask;
4048 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4049 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004050 }
4051 }
4052 }
4053
Evan Cheng9eca5e82006-10-25 21:49:50 +00004054 if (Commuted) {
4055 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004056 // FIXME: this seems wrong.
4057 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4058 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4059 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4060 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4061 X86::isUNPCKLMask(NewSVOp) ||
4062 X86::isUNPCKHMask(NewSVOp))
4063 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004064 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004065
Nate Begemanb9a47b82009-02-23 08:49:38 +00004066 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004067
4068 // Normalize the node to match x86 shuffle ops if needed
4069 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4070 return CommuteVectorShuffle(SVOp, DAG);
4071
4072 // Check for legal shuffle and return?
4073 SmallVector<int, 16> PermMask;
4074 SVOp->getMask(PermMask);
4075 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004076 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004077
Evan Cheng14b32e12007-12-11 01:46:18 +00004078 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4079 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004080 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004081 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004082 return NewOp;
4083 }
4084
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004086 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 if (NewOp.getNode())
4088 return NewOp;
4089 }
4090
Evan Chengace3c172008-07-22 21:13:36 +00004091 // Handle all 4 wide cases with a number of shuffles except for MMX.
4092 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004093 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004094
Dan Gohman475871a2008-07-27 21:46:04 +00004095 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004096}
4097
Dan Gohman475871a2008-07-27 21:46:04 +00004098SDValue
4099X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004100 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004102 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004103 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004104 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004105 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004106 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004107 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004108 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004109 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004110 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4111 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4112 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004113 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4114 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4115 DAG.getNode(ISD::BIT_CONVERT, dl,
4116 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004117 Op.getOperand(0)),
4118 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004119 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004120 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004121 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004122 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004123 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004124 } else if (VT == MVT::f32) {
4125 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4126 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004127 // result has a single use which is a store or a bitcast to i32. And in
4128 // the case of a store, it's not worth it if the index is a constant 0,
4129 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004130 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004131 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004133 if ((User->getOpcode() != ISD::STORE ||
4134 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4135 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004136 (User->getOpcode() != ISD::BIT_CONVERT ||
4137 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004138 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004139 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004140 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004141 Op.getOperand(0)),
4142 Op.getOperand(1));
4143 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004144 } else if (VT == MVT::i32) {
4145 // ExtractPS works with constant index.
4146 if (isa<ConstantSDNode>(Op.getOperand(1)))
4147 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004148 }
Dan Gohman475871a2008-07-27 21:46:04 +00004149 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004150}
4151
4152
Dan Gohman475871a2008-07-27 21:46:04 +00004153SDValue
4154X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004156 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004157
Evan Cheng62a3f152008-03-24 21:52:23 +00004158 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004159 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004160 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004161 return Res;
4162 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004163
Duncan Sands83ec4b62008-06-06 12:08:01 +00004164 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004165 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004166 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004167 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004168 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004169 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004170 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004171 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4172 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004173 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004174 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004175 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004176 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004177 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004178 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004179 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004180 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004181 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004182 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004183 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004184 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004185 if (Idx == 0)
4186 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004187
Evan Cheng0db9fe62006-04-25 20:13:52 +00004188 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004189 int Mask[4] = { Idx, -1, -1, -1 };
4190 MVT VVT = Op.getOperand(0).getValueType();
4191 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4192 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004193 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004194 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004195 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004196 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4197 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4198 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004199 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004200 if (Idx == 0)
4201 return Op;
4202
4203 // UNPCKHPD the element to the lowest double word, then movsd.
4204 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4205 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004206 int Mask[2] = { 1, -1 };
4207 MVT VVT = Op.getOperand(0).getValueType();
4208 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4209 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004210 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004211 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004212 }
4213
Dan Gohman475871a2008-07-27 21:46:04 +00004214 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004215}
4216
Dan Gohman475871a2008-07-27 21:46:04 +00004217SDValue
4218X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004219 MVT VT = Op.getValueType();
4220 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004221 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004222
Dan Gohman475871a2008-07-27 21:46:04 +00004223 SDValue N0 = Op.getOperand(0);
4224 SDValue N1 = Op.getOperand(1);
4225 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004226
Dan Gohmanef521f12008-08-14 22:53:18 +00004227 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4228 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004229 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004230 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004231 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4232 // argument.
4233 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004234 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004235 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004236 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004237 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004238 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004239 // Bits [7:6] of the constant are the source select. This will always be
4240 // zero here. The DAG Combiner may combine an extract_elt index into these
4241 // bits. For example (insert (extract, 3), 2) could be matched by putting
4242 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004243 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004244 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004245 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004246 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004247 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004248 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004249 } else if (EVT == MVT::i32) {
4250 // InsertPS works with constant index.
4251 if (isa<ConstantSDNode>(N2))
4252 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004253 }
Dan Gohman475871a2008-07-27 21:46:04 +00004254 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004255}
4256
Dan Gohman475871a2008-07-27 21:46:04 +00004257SDValue
4258X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004259 MVT VT = Op.getValueType();
4260 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004261
4262 if (Subtarget->hasSSE41())
4263 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4264
Evan Cheng794405e2007-12-12 07:55:34 +00004265 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004266 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004267
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004268 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004269 SDValue N0 = Op.getOperand(0);
4270 SDValue N1 = Op.getOperand(1);
4271 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004272
Eli Friedman30e71eb2009-06-06 06:32:50 +00004273 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004274 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4275 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004276 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004277 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004278 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004279 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004280 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004281 }
Dan Gohman475871a2008-07-27 21:46:04 +00004282 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004283}
4284
Dan Gohman475871a2008-07-27 21:46:04 +00004285SDValue
4286X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004287 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004288 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004289 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4290 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4291 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004292 Op.getOperand(0))));
4293
Dale Johannesenace16102009-02-03 19:33:06 +00004294 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004295 MVT VT = MVT::v2i32;
4296 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004297 default: break;
4298 case MVT::v16i8:
4299 case MVT::v8i16:
4300 VT = MVT::v4i32;
4301 break;
4302 }
Dale Johannesenace16102009-02-03 19:33:06 +00004303 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4304 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004305}
4306
Bill Wendling056292f2008-09-16 21:48:12 +00004307// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4308// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4309// one of the above mentioned nodes. It has to be wrapped because otherwise
4310// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4311// be used to form addressing mode. These wrapped nodes will be selected
4312// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004313SDValue
4314X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004316 // FIXME there isn't really any debug info here, should come from the parent
4317 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004318 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4319 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004320 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004321 // With PIC, the address is actually $g + Offset.
4322 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4323 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004324 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004325 DAG.getNode(X86ISD::GlobalBaseReg,
4326 DebugLoc::getUnknownLoc(),
4327 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004328 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004329 }
4330
4331 return Result;
4332}
4333
Dan Gohman475871a2008-07-27 21:46:04 +00004334SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004335X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004336 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004337 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004338 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4339 bool ExtraLoadRequired =
4340 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4341
4342 // Create the TargetGlobalAddress node, folding in the constant
4343 // offset if it is legal.
4344 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004345 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004346 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4347 Offset = 0;
4348 } else
4349 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004350 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004351
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004352 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004353 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004354 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4355 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004356 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004358
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004359 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4360 // load the value at address GV, not the value of GV itself. This means that
4361 // the GlobalAddress must be in the base or index register of the address, not
4362 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004363 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004364 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004365 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004366 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367
Dan Gohman6520e202008-10-18 02:06:02 +00004368 // If there was a non-zero offset that we didn't fold, create an explicit
4369 // addition for it.
4370 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004371 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004372 DAG.getConstant(Offset, getPointerTy()));
4373
Evan Cheng0db9fe62006-04-25 20:13:52 +00004374 return Result;
4375}
4376
Evan Chengda43bcf2008-09-24 00:05:32 +00004377SDValue
4378X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4379 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004380 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004381 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004382}
4383
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004384static SDValue
4385GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004386 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004387 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4388 DebugLoc dl = GA->getDebugLoc();
4389 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4390 GA->getValueType(0),
4391 GA->getOffset());
4392 if (InFlag) {
4393 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004394 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004395 } else {
4396 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004397 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004398 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004399 SDValue Flag = Chain.getValue(1);
4400 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004401}
4402
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004403// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004404static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004405LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004406 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004407 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004408 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4409 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004410 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004411 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004412 PtrVT), InFlag);
4413 InFlag = Chain.getValue(1);
4414
Rafael Espindola15f1b662009-04-24 12:59:40 +00004415 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004416}
4417
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004418// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004419static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004420LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004421 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004422 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004423}
4424
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004425// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4426// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004427static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004428 const MVT PtrVT, TLSModel::Model model,
4429 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004430 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004431 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004432 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4433 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004434 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4435 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004436
4437 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4438 NULL, 0);
4439
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004440 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4441 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004442 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004443 GA->getValueType(0),
4444 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004445 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004446
Rafael Espindola9a580232009-02-27 13:37:18 +00004447 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004448 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004449 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004450
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004451 // The address of the thread local variable is the add of the thread
4452 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004453 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004454}
4455
Dan Gohman475871a2008-07-27 21:46:04 +00004456SDValue
4457X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004458 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004459 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004460 assert(Subtarget->isTargetELF() &&
4461 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004462 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004463 GlobalValue *GV = GA->getGlobal();
4464 TLSModel::Model model =
4465 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004466 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004467 switch (model) {
4468 case TLSModel::GeneralDynamic:
4469 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004470 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004471
4472 case TLSModel::InitialExec:
4473 case TLSModel::LocalExec:
4474 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004475 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004476 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004477 switch (model) {
4478 case TLSModel::GeneralDynamic:
4479 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004480 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004481
4482 case TLSModel::InitialExec:
4483 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004484 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004485 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004486 }
Chris Lattner5867de12009-04-01 22:14:45 +00004487 assert(0 && "Unreachable");
4488 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004489}
4490
Dan Gohman475871a2008-07-27 21:46:04 +00004491SDValue
4492X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004493 // FIXME there isn't really any debug info here
4494 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004495 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4496 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004497 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004498 // With PIC, the address is actually $g + Offset.
4499 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4500 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004501 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004502 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004503 DebugLoc::getUnknownLoc(),
4504 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004505 Result);
4506 }
4507
4508 return Result;
4509}
4510
Dan Gohman475871a2008-07-27 21:46:04 +00004511SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004512 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004513 // FIXME there isn't really any debug into here
4514 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004515 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004516 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004517 // With PIC, the address is actually $g + Offset.
4518 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4519 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004520 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004521 DAG.getNode(X86ISD::GlobalBaseReg,
4522 DebugLoc::getUnknownLoc(),
4523 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004524 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004525 }
4526
4527 return Result;
4528}
4529
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004530/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004531/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004532SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004533 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004534 MVT VT = Op.getValueType();
4535 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004536 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004537 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004538 SDValue ShOpLo = Op.getOperand(0);
4539 SDValue ShOpHi = Op.getOperand(1);
4540 SDValue ShAmt = Op.getOperand(2);
4541 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004542 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004543 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004544 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004545
Dan Gohman475871a2008-07-27 21:46:04 +00004546 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004547 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004548 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4549 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004550 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004551 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4552 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004553 }
Evan Chenge3413162006-01-09 18:33:28 +00004554
Dale Johannesenace16102009-02-03 19:33:06 +00004555 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004556 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004557 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004558 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004559
Dan Gohman475871a2008-07-27 21:46:04 +00004560 SDValue Hi, Lo;
4561 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4562 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4563 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004564
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004565 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004566 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4567 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004568 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004569 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4570 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004571 }
4572
Dan Gohman475871a2008-07-27 21:46:04 +00004573 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004574 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575}
Evan Chenga3195e82006-01-12 22:54:21 +00004576
Dan Gohman475871a2008-07-27 21:46:04 +00004577SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004578 MVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004579
4580 if (SrcVT.isVector()) {
4581 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
4582 return Op;
4583 }
4584 return SDValue();
4585 }
4586
Duncan Sands8e4eb092008-06-08 20:54:56 +00004587 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004588 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004589
Eli Friedman36df4992009-05-27 00:47:34 +00004590 // These are really Legal; return the operand so the caller accepts it as
4591 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004592 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004593 return Op;
4594 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4595 Subtarget->is64Bit()) {
4596 return Op;
4597 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004598
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004599 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004600 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 MachineFunction &MF = DAG.getMachineFunction();
4602 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004604 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004605 StackSlot,
4606 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004607 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4608}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609
Eli Friedman948e95a2009-05-23 09:59:16 +00004610SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4611 SDValue StackSlot,
4612 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004614 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004615 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004616 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004617 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004618 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4619 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004620 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004622 Ops.push_back(Chain);
4623 Ops.push_back(StackSlot);
4624 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004625 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004626 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004627
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004628 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004629 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004630 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004631
4632 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4633 // shouldn't be necessary except that RFP cannot be live across
4634 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004635 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004636 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004637 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004638 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004639 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004640 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004641 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004642 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004643 Ops.push_back(DAG.getValueType(Op.getValueType()));
4644 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004645 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4646 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004647 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004648 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004649
Evan Cheng0db9fe62006-04-25 20:13:52 +00004650 return Result;
4651}
4652
Bill Wendling8b8a6362009-01-17 03:56:04 +00004653// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4654SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4655 // This algorithm is not obvious. Here it is in C code, more or less:
4656 /*
4657 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4658 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4659 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004660
Bill Wendling8b8a6362009-01-17 03:56:04 +00004661 // Copy ints to xmm registers.
4662 __m128i xh = _mm_cvtsi32_si128( hi );
4663 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004664
Bill Wendling8b8a6362009-01-17 03:56:04 +00004665 // Combine into low half of a single xmm register.
4666 __m128i x = _mm_unpacklo_epi32( xh, xl );
4667 __m128d d;
4668 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004669
Bill Wendling8b8a6362009-01-17 03:56:04 +00004670 // Merge in appropriate exponents to give the integer bits the right
4671 // magnitude.
4672 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004673
Bill Wendling8b8a6362009-01-17 03:56:04 +00004674 // Subtract away the biases to deal with the IEEE-754 double precision
4675 // implicit 1.
4676 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004677
Bill Wendling8b8a6362009-01-17 03:56:04 +00004678 // All conversions up to here are exact. The correctly rounded result is
4679 // calculated using the current rounding mode using the following
4680 // horizontal add.
4681 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4682 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4683 // store doesn't really need to be here (except
4684 // maybe to zero the other double)
4685 return sd;
4686 }
4687 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004688
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004689 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004690
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004691 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004692 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004693 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4694 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4695 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4696 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4697 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004698 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004699
Bill Wendling8b8a6362009-01-17 03:56:04 +00004700 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004701 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4702 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4703 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004704 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004705
Dale Johannesenace16102009-02-03 19:33:06 +00004706 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4707 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004708 Op.getOperand(0),
4709 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004710 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4711 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004712 Op.getOperand(0),
4713 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004714 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004715 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004716 PseudoSourceValue::getConstantPool(), 0,
4717 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004719 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4720 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004721 PseudoSourceValue::getConstantPool(), 0,
4722 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004723 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004724
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004725 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 int ShufMask[2] = { 1, -1 };
4727 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4728 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004729 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4730 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004731 DAG.getIntPtrConstant(0));
4732}
4733
Bill Wendling8b8a6362009-01-17 03:56:04 +00004734// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4735SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004736 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004737 // FP constant to bias correct the final result.
4738 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4739 MVT::f64);
4740
4741 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004742 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4743 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004744 Op.getOperand(0),
4745 DAG.getIntPtrConstant(0)));
4746
Dale Johannesenace16102009-02-03 19:33:06 +00004747 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4748 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004749 DAG.getIntPtrConstant(0));
4750
4751 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004752 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4753 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4754 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004755 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004756 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, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004759 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4760 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004761 DAG.getIntPtrConstant(0));
4762
4763 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004764 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004765
4766 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004767 MVT DestVT = Op.getValueType();
4768
4769 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004770 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004771 DAG.getIntPtrConstant(0));
4772 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004773 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004774 }
4775
4776 // Handle final rounding.
4777 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004778}
4779
4780SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004781 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004782 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004783
Evan Chenga06ec9e2009-01-19 08:08:22 +00004784 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4785 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4786 // the optimization here.
4787 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004788 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004789
4790 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004791 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004792 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004793 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004794 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004795
Bill Wendling8b8a6362009-01-17 03:56:04 +00004796 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004797 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004798 return LowerUINT_TO_FP_i32(Op, DAG);
4799 }
4800
Eli Friedman948e95a2009-05-23 09:59:16 +00004801 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4802
4803 // Make a 64-bit buffer, and use it to build an FILD.
4804 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4805 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4806 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4807 getPointerTy(), StackSlot, WordOff);
4808 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4809 StackSlot, NULL, 0);
4810 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4811 OffsetSlot, NULL, 0);
4812 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004813}
4814
Dan Gohman475871a2008-07-27 21:46:04 +00004815std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004816FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004817 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004818
4819 MVT DstTy = Op.getValueType();
4820
4821 if (!IsSigned) {
4822 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4823 DstTy = MVT::i64;
4824 }
4825
4826 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4827 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004828 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004830 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004831 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004832 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004833 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004834 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004835 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00004836 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004837 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004838
Evan Cheng87c89352007-10-15 20:11:21 +00004839 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4840 // stack slot.
4841 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004842 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004843 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004844 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004845
Evan Cheng0db9fe62006-04-25 20:13:52 +00004846 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004847 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004848 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4849 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4850 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4851 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004852 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004853
Dan Gohman475871a2008-07-27 21:46:04 +00004854 SDValue Chain = DAG.getEntryNode();
4855 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004856 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004857 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004858 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004859 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004860 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004861 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004862 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4863 };
Dale Johannesenace16102009-02-03 19:33:06 +00004864 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004865 Chain = Value.getValue(1);
4866 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4867 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4868 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004869
Evan Cheng0db9fe62006-04-25 20:13:52 +00004870 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004871 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004872 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004873
Chris Lattner27a6c732007-11-24 07:07:01 +00004874 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004875}
4876
Dan Gohman475871a2008-07-27 21:46:04 +00004877SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00004878 if (Op.getValueType().isVector()) {
4879 if (Op.getValueType() == MVT::v2i32 &&
4880 Op.getOperand(0).getValueType() == MVT::v2f64) {
4881 return Op;
4882 }
4883 return SDValue();
4884 }
4885
Eli Friedman948e95a2009-05-23 09:59:16 +00004886 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004887 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00004888 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
4889 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004890
Chris Lattner27a6c732007-11-24 07:07:01 +00004891 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004892 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004893 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004894}
4895
Eli Friedman948e95a2009-05-23 09:59:16 +00004896SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4897 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4898 SDValue FIST = Vals.first, StackSlot = Vals.second;
4899 assert(FIST.getNode() && "Unexpected failure");
4900
4901 // Load the result.
4902 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4903 FIST, StackSlot, NULL, 0);
4904}
4905
Dan Gohman475871a2008-07-27 21:46:04 +00004906SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004907 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004908 MVT VT = Op.getValueType();
4909 MVT EltVT = VT;
4910 if (VT.isVector())
4911 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004912 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004913 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004914 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004915 CV.push_back(C);
4916 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004917 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004918 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004919 CV.push_back(C);
4920 CV.push_back(C);
4921 CV.push_back(C);
4922 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004923 }
Dan Gohmand3006222007-07-27 17:16:43 +00004924 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004925 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004926 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004927 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004928 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004929 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004930}
4931
Dan Gohman475871a2008-07-27 21:46:04 +00004932SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004933 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004934 MVT VT = Op.getValueType();
4935 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004936 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004937 if (VT.isVector()) {
4938 EltVT = VT.getVectorElementType();
4939 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004940 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004941 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004942 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004943 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004944 CV.push_back(C);
4945 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004946 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004947 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004948 CV.push_back(C);
4949 CV.push_back(C);
4950 CV.push_back(C);
4951 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004952 }
Dan Gohmand3006222007-07-27 17:16:43 +00004953 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004954 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004955 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004956 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004957 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004958 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004959 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4960 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004961 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004962 Op.getOperand(0)),
4963 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004964 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004965 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004966 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004967}
4968
Dan Gohman475871a2008-07-27 21:46:04 +00004969SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4970 SDValue Op0 = Op.getOperand(0);
4971 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004972 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004973 MVT VT = Op.getValueType();
4974 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004975
4976 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004977 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004978 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004979 SrcVT = VT;
4980 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004981 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004982 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004983 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004984 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004985 }
4986
4987 // At this point the operands and the result should have the same
4988 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004989
Evan Cheng68c47cb2007-01-05 07:55:56 +00004990 // First get the sign bit of second operand.
4991 std::vector<Constant*> CV;
4992 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004993 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4994 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004995 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004996 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4997 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4998 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4999 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005000 }
Dan Gohmand3006222007-07-27 17:16:43 +00005001 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005002 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005003 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005004 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005005 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005006 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005007
5008 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005009 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005010 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005011 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5012 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005013 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005014 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5015 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005016 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005017 }
5018
Evan Cheng73d6cf12007-01-05 21:37:56 +00005019 // Clear first operand sign bit.
5020 CV.clear();
5021 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005022 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5023 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005024 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005025 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5026 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5027 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5028 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005029 }
Dan Gohmand3006222007-07-27 17:16:43 +00005030 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005031 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005032 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005033 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005034 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005035 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005036
5037 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005038 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005039}
5040
Dan Gohman076aee32009-03-04 19:44:21 +00005041/// Emit nodes that will be selected as "test Op0,Op0", or something
5042/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005043SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5044 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005045 DebugLoc dl = Op.getDebugLoc();
5046
Dan Gohman31125812009-03-07 01:58:32 +00005047 // CF and OF aren't always set the way we want. Determine which
5048 // of these we need.
5049 bool NeedCF = false;
5050 bool NeedOF = false;
5051 switch (X86CC) {
5052 case X86::COND_A: case X86::COND_AE:
5053 case X86::COND_B: case X86::COND_BE:
5054 NeedCF = true;
5055 break;
5056 case X86::COND_G: case X86::COND_GE:
5057 case X86::COND_L: case X86::COND_LE:
5058 case X86::COND_O: case X86::COND_NO:
5059 NeedOF = true;
5060 break;
5061 default: break;
5062 }
5063
Dan Gohman076aee32009-03-04 19:44:21 +00005064 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005065 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5066 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5067 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005068 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005069 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005070 switch (Op.getNode()->getOpcode()) {
5071 case ISD::ADD:
5072 // Due to an isel shortcoming, be conservative if this add is likely to
5073 // be selected as part of a load-modify-store instruction. When the root
5074 // node in a match is a store, isel doesn't know how to remap non-chain
5075 // non-flag uses of other nodes in the match, such as the ADD in this
5076 // case. This leads to the ADD being left around and reselected, with
5077 // the result being two adds in the output.
5078 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5079 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5080 if (UI->getOpcode() == ISD::STORE)
5081 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005082 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005083 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5084 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005085 if (C->getAPIntValue() == 1) {
5086 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005087 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005088 break;
5089 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005090 // An add of negative one (subtract of one) will be selected as a DEC.
5091 if (C->getAPIntValue().isAllOnesValue()) {
5092 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005093 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005094 break;
5095 }
5096 }
Dan Gohman076aee32009-03-04 19:44:21 +00005097 // Otherwise use a regular EFLAGS-setting add.
5098 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005099 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005100 break;
5101 case ISD::SUB:
5102 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5103 // likely to be selected as part of a load-modify-store instruction.
5104 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5105 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5106 if (UI->getOpcode() == ISD::STORE)
5107 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005108 // Otherwise use a regular EFLAGS-setting sub.
5109 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005110 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005111 break;
5112 case X86ISD::ADD:
5113 case X86ISD::SUB:
5114 case X86ISD::INC:
5115 case X86ISD::DEC:
5116 return SDValue(Op.getNode(), 1);
5117 default:
5118 default_case:
5119 break;
5120 }
5121 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005122 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005123 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005124 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005125 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005126 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005127 DAG.ReplaceAllUsesWith(Op, New);
5128 return SDValue(New.getNode(), 1);
5129 }
5130 }
5131
5132 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5133 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5134 DAG.getConstant(0, Op.getValueType()));
5135}
5136
5137/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5138/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005139SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5140 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005141 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5142 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005143 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005144
5145 DebugLoc dl = Op0.getDebugLoc();
5146 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5147}
5148
Dan Gohman475871a2008-07-27 21:46:04 +00005149SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005150 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005151 SDValue Op0 = Op.getOperand(0);
5152 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005153 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005154 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005155
Dan Gohmane5af2d32009-01-29 01:59:02 +00005156 // Lower (X & (1 << N)) == 0 to BT(X, N).
5157 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5158 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005159 if (Op0.getOpcode() == ISD::AND &&
5160 Op0.hasOneUse() &&
5161 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005162 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005163 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005164 SDValue LHS, RHS;
5165 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5166 if (ConstantSDNode *Op010C =
5167 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5168 if (Op010C->getZExtValue() == 1) {
5169 LHS = Op0.getOperand(0);
5170 RHS = Op0.getOperand(1).getOperand(1);
5171 }
5172 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5173 if (ConstantSDNode *Op000C =
5174 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5175 if (Op000C->getZExtValue() == 1) {
5176 LHS = Op0.getOperand(1);
5177 RHS = Op0.getOperand(0).getOperand(1);
5178 }
5179 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5180 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5181 SDValue AndLHS = Op0.getOperand(0);
5182 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5183 LHS = AndLHS.getOperand(0);
5184 RHS = AndLHS.getOperand(1);
5185 }
5186 }
Evan Cheng0488db92007-09-25 01:57:46 +00005187
Dan Gohmane5af2d32009-01-29 01:59:02 +00005188 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005189 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5190 // instruction. Since the shift amount is in-range-or-undefined, we know
5191 // that doing a bittest on the i16 value is ok. We extend to i32 because
5192 // the encoding for the i16 version is larger than the i32 version.
5193 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005194 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005195
5196 // If the operand types disagree, extend the shift amount to match. Since
5197 // BT ignores high bits (like shifts) we can use anyextend.
5198 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005199 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005200
Dale Johannesenace16102009-02-03 19:33:06 +00005201 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005202 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005203 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005204 DAG.getConstant(Cond, MVT::i8), BT);
5205 }
5206 }
5207
5208 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5209 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005210
Dan Gohman31125812009-03-07 01:58:32 +00005211 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005212 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005213 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005214}
5215
Dan Gohman475871a2008-07-27 21:46:04 +00005216SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5217 SDValue Cond;
5218 SDValue Op0 = Op.getOperand(0);
5219 SDValue Op1 = Op.getOperand(1);
5220 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005221 MVT VT = Op.getValueType();
5222 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5223 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005224 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005225
5226 if (isFP) {
5227 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005228 MVT VT0 = Op0.getValueType();
5229 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5230 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005231 bool Swap = false;
5232
5233 switch (SetCCOpcode) {
5234 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005235 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005236 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005237 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005238 case ISD::SETGT: Swap = true; // Fallthrough
5239 case ISD::SETLT:
5240 case ISD::SETOLT: SSECC = 1; break;
5241 case ISD::SETOGE:
5242 case ISD::SETGE: Swap = true; // Fallthrough
5243 case ISD::SETLE:
5244 case ISD::SETOLE: SSECC = 2; break;
5245 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005246 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005247 case ISD::SETNE: SSECC = 4; break;
5248 case ISD::SETULE: Swap = true;
5249 case ISD::SETUGE: SSECC = 5; break;
5250 case ISD::SETULT: Swap = true;
5251 case ISD::SETUGT: SSECC = 6; break;
5252 case ISD::SETO: SSECC = 7; break;
5253 }
5254 if (Swap)
5255 std::swap(Op0, Op1);
5256
Nate Begemanfb8ead02008-07-25 19:05:58 +00005257 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005258 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005259 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005260 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005261 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5262 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5263 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005264 }
5265 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005266 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005267 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5268 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5269 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005270 }
5271 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005272 }
5273 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005274 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005275 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005276
Nate Begeman30a0de92008-07-17 16:51:19 +00005277 // We are handling one of the integer comparisons here. Since SSE only has
5278 // GT and EQ comparisons for integer, swapping operands and multiple
5279 // operations may be required for some comparisons.
5280 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5281 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Nate Begeman30a0de92008-07-17 16:51:19 +00005283 switch (VT.getSimpleVT()) {
5284 default: break;
5285 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5286 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5287 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5288 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005290
Nate Begeman30a0de92008-07-17 16:51:19 +00005291 switch (SetCCOpcode) {
5292 default: break;
5293 case ISD::SETNE: Invert = true;
5294 case ISD::SETEQ: Opc = EQOpc; break;
5295 case ISD::SETLT: Swap = true;
5296 case ISD::SETGT: Opc = GTOpc; break;
5297 case ISD::SETGE: Swap = true;
5298 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5299 case ISD::SETULT: Swap = true;
5300 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5301 case ISD::SETUGE: Swap = true;
5302 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5303 }
5304 if (Swap)
5305 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005306
Nate Begeman30a0de92008-07-17 16:51:19 +00005307 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5308 // bits of the inputs before performing those operations.
5309 if (FlipSigns) {
5310 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005311 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5312 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005313 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005314 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5315 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005316 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5317 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005318 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005319
Dale Johannesenace16102009-02-03 19:33:06 +00005320 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005321
5322 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005323 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005324 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005325
Nate Begeman30a0de92008-07-17 16:51:19 +00005326 return Result;
5327}
Evan Cheng0488db92007-09-25 01:57:46 +00005328
Evan Cheng370e5342008-12-03 08:38:43 +00005329// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005330static bool isX86LogicalCmp(SDValue Op) {
5331 unsigned Opc = Op.getNode()->getOpcode();
5332 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5333 return true;
5334 if (Op.getResNo() == 1 &&
5335 (Opc == X86ISD::ADD ||
5336 Opc == X86ISD::SUB ||
5337 Opc == X86ISD::SMUL ||
5338 Opc == X86ISD::UMUL ||
5339 Opc == X86ISD::INC ||
5340 Opc == X86ISD::DEC))
5341 return true;
5342
5343 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005344}
5345
Dan Gohman475871a2008-07-27 21:46:04 +00005346SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005347 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005348 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005349 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005350 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005351
Evan Cheng734503b2006-09-11 02:19:56 +00005352 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005353 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005354
Evan Cheng3f41d662007-10-08 22:16:29 +00005355 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5356 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005357 if (Cond.getOpcode() == X86ISD::SETCC) {
5358 CC = Cond.getOperand(0);
5359
Dan Gohman475871a2008-07-27 21:46:04 +00005360 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005361 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005362 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005363
Evan Cheng3f41d662007-10-08 22:16:29 +00005364 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005365 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005366 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005367 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005368
Chris Lattnerd1980a52009-03-12 06:52:53 +00005369 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5370 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005371 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005372 addTest = false;
5373 }
5374 }
5375
5376 if (addTest) {
5377 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005378 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005379 }
5380
Dan Gohmanfc166572009-04-09 23:54:40 +00005381 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005382 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005383 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5384 // condition is true.
5385 Ops.push_back(Op.getOperand(2));
5386 Ops.push_back(Op.getOperand(1));
5387 Ops.push_back(CC);
5388 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005389 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005390}
5391
Evan Cheng370e5342008-12-03 08:38:43 +00005392// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5393// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5394// from the AND / OR.
5395static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5396 Opc = Op.getOpcode();
5397 if (Opc != ISD::OR && Opc != ISD::AND)
5398 return false;
5399 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5400 Op.getOperand(0).hasOneUse() &&
5401 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5402 Op.getOperand(1).hasOneUse());
5403}
5404
Evan Cheng961d6d42009-02-02 08:19:07 +00005405// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5406// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005407static bool isXor1OfSetCC(SDValue Op) {
5408 if (Op.getOpcode() != ISD::XOR)
5409 return false;
5410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5411 if (N1C && N1C->getAPIntValue() == 1) {
5412 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5413 Op.getOperand(0).hasOneUse();
5414 }
5415 return false;
5416}
5417
Dan Gohman475871a2008-07-27 21:46:04 +00005418SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005419 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005420 SDValue Chain = Op.getOperand(0);
5421 SDValue Cond = Op.getOperand(1);
5422 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005423 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005424 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005425
Evan Cheng0db9fe62006-04-25 20:13:52 +00005426 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005427 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005428#if 0
5429 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005430 else if (Cond.getOpcode() == X86ISD::ADD ||
5431 Cond.getOpcode() == X86ISD::SUB ||
5432 Cond.getOpcode() == X86ISD::SMUL ||
5433 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005434 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005435#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005436
Evan Cheng3f41d662007-10-08 22:16:29 +00005437 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5438 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005440 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441
Dan Gohman475871a2008-07-27 21:46:04 +00005442 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005443 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005444 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005445 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005446 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005447 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005448 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005449 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005450 default: break;
5451 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005452 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005453 // These can only come from an arithmetic instruction with overflow,
5454 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005455 Cond = Cond.getNode()->getOperand(1);
5456 addTest = false;
5457 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005458 }
Evan Cheng0488db92007-09-25 01:57:46 +00005459 }
Evan Cheng370e5342008-12-03 08:38:43 +00005460 } else {
5461 unsigned CondOpc;
5462 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5463 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005464 if (CondOpc == ISD::OR) {
5465 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5466 // two branches instead of an explicit OR instruction with a
5467 // separate test.
5468 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005469 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005470 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005471 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005472 Chain, Dest, CC, Cmp);
5473 CC = Cond.getOperand(1).getOperand(0);
5474 Cond = Cmp;
5475 addTest = false;
5476 }
5477 } else { // ISD::AND
5478 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5479 // two branches instead of an explicit AND instruction with a
5480 // separate test. However, we only do this if this block doesn't
5481 // have a fall-through edge, because this requires an explicit
5482 // jmp when the condition is false.
5483 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005484 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005485 Op.getNode()->hasOneUse()) {
5486 X86::CondCode CCode =
5487 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5488 CCode = X86::GetOppositeBranchCondition(CCode);
5489 CC = DAG.getConstant(CCode, MVT::i8);
5490 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5491 // Look for an unconditional branch following this conditional branch.
5492 // We need this because we need to reverse the successors in order
5493 // to implement FCMP_OEQ.
5494 if (User.getOpcode() == ISD::BR) {
5495 SDValue FalseBB = User.getOperand(1);
5496 SDValue NewBR =
5497 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5498 assert(NewBR == User);
5499 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005500
Dale Johannesene4d209d2009-02-03 20:21:25 +00005501 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005502 Chain, Dest, CC, Cmp);
5503 X86::CondCode CCode =
5504 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5505 CCode = X86::GetOppositeBranchCondition(CCode);
5506 CC = DAG.getConstant(CCode, MVT::i8);
5507 Cond = Cmp;
5508 addTest = false;
5509 }
5510 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005511 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005512 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5513 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5514 // It should be transformed during dag combiner except when the condition
5515 // is set by a arithmetics with overflow node.
5516 X86::CondCode CCode =
5517 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5518 CCode = X86::GetOppositeBranchCondition(CCode);
5519 CC = DAG.getConstant(CCode, MVT::i8);
5520 Cond = Cond.getOperand(0).getOperand(1);
5521 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005522 }
Evan Cheng0488db92007-09-25 01:57:46 +00005523 }
5524
5525 if (addTest) {
5526 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005527 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005528 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005529 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005530 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005531}
5532
Anton Korobeynikove060b532007-04-17 19:34:00 +00005533
5534// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5535// Calls to _alloca is needed to probe the stack when allocating more than 4k
5536// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5537// that the guard pages used by the OS virtual memory manager are allocated in
5538// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005539SDValue
5540X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005541 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005542 assert(Subtarget->isTargetCygMing() &&
5543 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005544 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005545
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005546 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005547 SDValue Chain = Op.getOperand(0);
5548 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005549 // FIXME: Ensure alignment here
5550
Dan Gohman475871a2008-07-27 21:46:04 +00005551 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005552
Duncan Sands83ec4b62008-06-06 12:08:01 +00005553 MVT IntPtr = getPointerTy();
5554 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005555
Chris Lattnere563bbc2008-10-11 22:08:30 +00005556 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005557
Dale Johannesendd64c412009-02-04 00:33:20 +00005558 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005559 Flag = Chain.getValue(1);
5560
5561 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005562 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005563 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005564 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005565 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005566 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005567 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005568 Flag = Chain.getValue(1);
5569
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005570 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005571 DAG.getIntPtrConstant(0, true),
5572 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005573 Flag);
5574
Dale Johannesendd64c412009-02-04 00:33:20 +00005575 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005576
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005578 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005579}
5580
Dan Gohman475871a2008-07-27 21:46:04 +00005581SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005582X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005583 SDValue Chain,
5584 SDValue Dst, SDValue Src,
5585 SDValue Size, unsigned Align,
5586 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005587 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005588 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005589
Bill Wendling6f287b22008-09-30 21:22:07 +00005590 // If not DWORD aligned or size is more than the threshold, call the library.
5591 // The libc version is likely to be faster for these cases. It can use the
5592 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005593 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005594 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005595 ConstantSize->getZExtValue() >
5596 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005597 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005598
5599 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005600 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005601
Bill Wendling6158d842008-10-01 00:59:58 +00005602 if (const char *bzeroEntry = V &&
5603 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5604 MVT IntPtr = getPointerTy();
5605 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005606 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005607 TargetLowering::ArgListEntry Entry;
5608 Entry.Node = Dst;
5609 Entry.Ty = IntPtrTy;
5610 Args.push_back(Entry);
5611 Entry.Node = Size;
5612 Args.push_back(Entry);
5613 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005614 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5615 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005616 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005617 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005618 }
5619
Dan Gohman707e0182008-04-12 04:36:06 +00005620 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005621 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005622 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005623
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005624 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005626 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005627 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005628 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005629 unsigned BytesLeft = 0;
5630 bool TwoRepStos = false;
5631 if (ValC) {
5632 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005633 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005634
Evan Cheng0db9fe62006-04-25 20:13:52 +00005635 // If the value is a constant, then we can potentially use larger sets.
5636 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005637 case 2: // WORD aligned
5638 AVT = MVT::i16;
5639 ValReg = X86::AX;
5640 Val = (Val << 8) | Val;
5641 break;
5642 case 0: // DWORD aligned
5643 AVT = MVT::i32;
5644 ValReg = X86::EAX;
5645 Val = (Val << 8) | Val;
5646 Val = (Val << 16) | Val;
5647 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5648 AVT = MVT::i64;
5649 ValReg = X86::RAX;
5650 Val = (Val << 32) | Val;
5651 }
5652 break;
5653 default: // Byte aligned
5654 AVT = MVT::i8;
5655 ValReg = X86::AL;
5656 Count = DAG.getIntPtrConstant(SizeVal);
5657 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005658 }
5659
Duncan Sands8e4eb092008-06-08 20:54:56 +00005660 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005661 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005662 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5663 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005664 }
5665
Dale Johannesen0f502f62009-02-03 22:26:09 +00005666 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005667 InFlag);
5668 InFlag = Chain.getValue(1);
5669 } else {
5670 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005671 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005672 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005673 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005674 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005675
Scott Michelfdc40a02009-02-17 22:15:04 +00005676 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005677 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005678 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005679 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005680 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005681 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005682 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005683 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005684
Chris Lattnerd96d0722007-02-25 06:40:16 +00005685 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005686 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005687 Ops.push_back(Chain);
5688 Ops.push_back(DAG.getValueType(AVT));
5689 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005690 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005691
Evan Cheng0db9fe62006-04-25 20:13:52 +00005692 if (TwoRepStos) {
5693 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005694 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005695 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005696 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005697 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005698 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005699 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005700 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005701 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005702 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005703 Ops.clear();
5704 Ops.push_back(Chain);
5705 Ops.push_back(DAG.getValueType(MVT::i8));
5706 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005707 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005708 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005709 // Handle the last 1 - 7 bytes.
5710 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005711 MVT AddrVT = Dst.getValueType();
5712 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005713
Dale Johannesen0f502f62009-02-03 22:26:09 +00005714 Chain = DAG.getMemset(Chain, dl,
5715 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005716 DAG.getConstant(Offset, AddrVT)),
5717 Src,
5718 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005719 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005720 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005721
Dan Gohman707e0182008-04-12 04:36:06 +00005722 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005723 return Chain;
5724}
Evan Cheng11e15b32006-04-03 20:53:28 +00005725
Dan Gohman475871a2008-07-27 21:46:04 +00005726SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005727X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005728 SDValue Chain, SDValue Dst, SDValue Src,
5729 SDValue Size, unsigned Align,
5730 bool AlwaysInline,
5731 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005732 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005733 // This requires the copy size to be a constant, preferrably
5734 // within a subtarget-specific limit.
5735 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5736 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005737 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005738 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005739 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005740 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005741
Evan Cheng1887c1c2008-08-21 21:00:15 +00005742 /// If not DWORD aligned, call the library.
5743 if ((Align & 3) != 0)
5744 return SDValue();
5745
5746 // DWORD aligned
5747 MVT AVT = MVT::i32;
5748 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005749 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005750
Duncan Sands83ec4b62008-06-06 12:08:01 +00005751 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005752 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005754 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005755
Dan Gohman475871a2008-07-27 21:46:04 +00005756 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005757 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005758 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005759 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005760 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005761 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005762 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005763 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005764 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005765 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005766 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005767 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005768 InFlag = Chain.getValue(1);
5769
Chris Lattnerd96d0722007-02-25 06:40:16 +00005770 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005771 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005772 Ops.push_back(Chain);
5773 Ops.push_back(DAG.getValueType(AVT));
5774 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005775 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005776
Dan Gohman475871a2008-07-27 21:46:04 +00005777 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005778 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005779 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005780 // Handle the last 1 - 7 bytes.
5781 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005782 MVT DstVT = Dst.getValueType();
5783 MVT SrcVT = Src.getValueType();
5784 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005785 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005786 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005787 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005788 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005789 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005790 DAG.getConstant(BytesLeft, SizeVT),
5791 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005792 DstSV, DstSVOff + Offset,
5793 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005794 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005795
Scott Michelfdc40a02009-02-17 22:15:04 +00005796 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005797 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005798}
5799
Dan Gohman475871a2008-07-27 21:46:04 +00005800SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005801 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005802 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005803
Evan Cheng25ab6902006-09-08 06:48:29 +00005804 if (!Subtarget->is64Bit()) {
5805 // vastart just stores the address of the VarArgsFrameIndex slot into the
5806 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005807 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005808 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005809 }
5810
5811 // __va_list_tag:
5812 // gp_offset (0 - 6 * 8)
5813 // fp_offset (48 - 48 + 8 * 16)
5814 // overflow_arg_area (point to parameters coming in memory).
5815 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005816 SmallVector<SDValue, 8> MemOps;
5817 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005818 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005819 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005820 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005821 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005822 MemOps.push_back(Store);
5823
5824 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005825 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005826 FIN, DAG.getIntPtrConstant(4));
5827 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005828 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005829 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005830 MemOps.push_back(Store);
5831
5832 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005833 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005834 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005835 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005836 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005837 MemOps.push_back(Store);
5838
5839 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005840 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005841 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005842 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005843 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005844 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005845 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005846 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005847}
5848
Dan Gohman475871a2008-07-27 21:46:04 +00005849SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005850 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5851 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005852 SDValue Chain = Op.getOperand(0);
5853 SDValue SrcPtr = Op.getOperand(1);
5854 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005855
5856 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5857 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005858 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005859}
5860
Dan Gohman475871a2008-07-27 21:46:04 +00005861SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005862 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005863 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005864 SDValue Chain = Op.getOperand(0);
5865 SDValue DstPtr = Op.getOperand(1);
5866 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005867 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5868 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005869 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005870
Dale Johannesendd64c412009-02-04 00:33:20 +00005871 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005872 DAG.getIntPtrConstant(24), 8, false,
5873 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005874}
5875
Dan Gohman475871a2008-07-27 21:46:04 +00005876SDValue
5877X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005878 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005879 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005881 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005882 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005883 case Intrinsic::x86_sse_comieq_ss:
5884 case Intrinsic::x86_sse_comilt_ss:
5885 case Intrinsic::x86_sse_comile_ss:
5886 case Intrinsic::x86_sse_comigt_ss:
5887 case Intrinsic::x86_sse_comige_ss:
5888 case Intrinsic::x86_sse_comineq_ss:
5889 case Intrinsic::x86_sse_ucomieq_ss:
5890 case Intrinsic::x86_sse_ucomilt_ss:
5891 case Intrinsic::x86_sse_ucomile_ss:
5892 case Intrinsic::x86_sse_ucomigt_ss:
5893 case Intrinsic::x86_sse_ucomige_ss:
5894 case Intrinsic::x86_sse_ucomineq_ss:
5895 case Intrinsic::x86_sse2_comieq_sd:
5896 case Intrinsic::x86_sse2_comilt_sd:
5897 case Intrinsic::x86_sse2_comile_sd:
5898 case Intrinsic::x86_sse2_comigt_sd:
5899 case Intrinsic::x86_sse2_comige_sd:
5900 case Intrinsic::x86_sse2_comineq_sd:
5901 case Intrinsic::x86_sse2_ucomieq_sd:
5902 case Intrinsic::x86_sse2_ucomilt_sd:
5903 case Intrinsic::x86_sse2_ucomile_sd:
5904 case Intrinsic::x86_sse2_ucomigt_sd:
5905 case Intrinsic::x86_sse2_ucomige_sd:
5906 case Intrinsic::x86_sse2_ucomineq_sd: {
5907 unsigned Opc = 0;
5908 ISD::CondCode CC = ISD::SETCC_INVALID;
5909 switch (IntNo) {
5910 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005911 case Intrinsic::x86_sse_comieq_ss:
5912 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913 Opc = X86ISD::COMI;
5914 CC = ISD::SETEQ;
5915 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005916 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005917 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005918 Opc = X86ISD::COMI;
5919 CC = ISD::SETLT;
5920 break;
5921 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005922 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005923 Opc = X86ISD::COMI;
5924 CC = ISD::SETLE;
5925 break;
5926 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005927 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005928 Opc = X86ISD::COMI;
5929 CC = ISD::SETGT;
5930 break;
5931 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005932 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005933 Opc = X86ISD::COMI;
5934 CC = ISD::SETGE;
5935 break;
5936 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005937 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005938 Opc = X86ISD::COMI;
5939 CC = ISD::SETNE;
5940 break;
5941 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005942 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005943 Opc = X86ISD::UCOMI;
5944 CC = ISD::SETEQ;
5945 break;
5946 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005947 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005948 Opc = X86ISD::UCOMI;
5949 CC = ISD::SETLT;
5950 break;
5951 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005952 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005953 Opc = X86ISD::UCOMI;
5954 CC = ISD::SETLE;
5955 break;
5956 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005957 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005958 Opc = X86ISD::UCOMI;
5959 CC = ISD::SETGT;
5960 break;
5961 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005962 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005963 Opc = X86ISD::UCOMI;
5964 CC = ISD::SETGE;
5965 break;
5966 case Intrinsic::x86_sse_ucomineq_ss:
5967 case Intrinsic::x86_sse2_ucomineq_sd:
5968 Opc = X86ISD::UCOMI;
5969 CC = ISD::SETNE;
5970 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005971 }
Evan Cheng734503b2006-09-11 02:19:56 +00005972
Dan Gohman475871a2008-07-27 21:46:04 +00005973 SDValue LHS = Op.getOperand(1);
5974 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005975 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005976 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5977 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005978 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005979 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005980 }
Evan Cheng5759f972008-05-04 09:15:50 +00005981
5982 // Fix vector shift instructions where the last operand is a non-immediate
5983 // i32 value.
5984 case Intrinsic::x86_sse2_pslli_w:
5985 case Intrinsic::x86_sse2_pslli_d:
5986 case Intrinsic::x86_sse2_pslli_q:
5987 case Intrinsic::x86_sse2_psrli_w:
5988 case Intrinsic::x86_sse2_psrli_d:
5989 case Intrinsic::x86_sse2_psrli_q:
5990 case Intrinsic::x86_sse2_psrai_w:
5991 case Intrinsic::x86_sse2_psrai_d:
5992 case Intrinsic::x86_mmx_pslli_w:
5993 case Intrinsic::x86_mmx_pslli_d:
5994 case Intrinsic::x86_mmx_pslli_q:
5995 case Intrinsic::x86_mmx_psrli_w:
5996 case Intrinsic::x86_mmx_psrli_d:
5997 case Intrinsic::x86_mmx_psrli_q:
5998 case Intrinsic::x86_mmx_psrai_w:
5999 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006000 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006001 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006002 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006003
6004 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006005 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006006 switch (IntNo) {
6007 case Intrinsic::x86_sse2_pslli_w:
6008 NewIntNo = Intrinsic::x86_sse2_psll_w;
6009 break;
6010 case Intrinsic::x86_sse2_pslli_d:
6011 NewIntNo = Intrinsic::x86_sse2_psll_d;
6012 break;
6013 case Intrinsic::x86_sse2_pslli_q:
6014 NewIntNo = Intrinsic::x86_sse2_psll_q;
6015 break;
6016 case Intrinsic::x86_sse2_psrli_w:
6017 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6018 break;
6019 case Intrinsic::x86_sse2_psrli_d:
6020 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6021 break;
6022 case Intrinsic::x86_sse2_psrli_q:
6023 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6024 break;
6025 case Intrinsic::x86_sse2_psrai_w:
6026 NewIntNo = Intrinsic::x86_sse2_psra_w;
6027 break;
6028 case Intrinsic::x86_sse2_psrai_d:
6029 NewIntNo = Intrinsic::x86_sse2_psra_d;
6030 break;
6031 default: {
6032 ShAmtVT = MVT::v2i32;
6033 switch (IntNo) {
6034 case Intrinsic::x86_mmx_pslli_w:
6035 NewIntNo = Intrinsic::x86_mmx_psll_w;
6036 break;
6037 case Intrinsic::x86_mmx_pslli_d:
6038 NewIntNo = Intrinsic::x86_mmx_psll_d;
6039 break;
6040 case Intrinsic::x86_mmx_pslli_q:
6041 NewIntNo = Intrinsic::x86_mmx_psll_q;
6042 break;
6043 case Intrinsic::x86_mmx_psrli_w:
6044 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6045 break;
6046 case Intrinsic::x86_mmx_psrli_d:
6047 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6048 break;
6049 case Intrinsic::x86_mmx_psrli_q:
6050 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6051 break;
6052 case Intrinsic::x86_mmx_psrai_w:
6053 NewIntNo = Intrinsic::x86_mmx_psra_w;
6054 break;
6055 case Intrinsic::x86_mmx_psrai_d:
6056 NewIntNo = Intrinsic::x86_mmx_psra_d;
6057 break;
6058 default: abort(); // Can't reach here.
6059 }
6060 break;
6061 }
6062 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006063 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006064 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6065 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6066 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006067 DAG.getConstant(NewIntNo, MVT::i32),
6068 Op.getOperand(1), ShAmt);
6069 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006070 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006071}
Evan Cheng72261582005-12-20 06:22:03 +00006072
Dan Gohman475871a2008-07-27 21:46:04 +00006073SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006074 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006075 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006076
6077 if (Depth > 0) {
6078 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6079 SDValue Offset =
6080 DAG.getConstant(TD->getPointerSize(),
6081 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006082 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006083 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006084 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006085 NULL, 0);
6086 }
6087
6088 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006089 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006090 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006091 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006092}
6093
Dan Gohman475871a2008-07-27 21:46:04 +00006094SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006095 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6096 MFI->setFrameAddressIsTaken(true);
6097 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006098 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006099 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6100 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006101 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006102 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006103 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006104 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006105}
6106
Dan Gohman475871a2008-07-27 21:46:04 +00006107SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006108 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006109 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006110}
6111
Dan Gohman475871a2008-07-27 21:46:04 +00006112SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006113{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006114 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006115 SDValue Chain = Op.getOperand(0);
6116 SDValue Offset = Op.getOperand(1);
6117 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006118 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006119
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006120 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6121 getPointerTy());
6122 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006123
Dale Johannesene4d209d2009-02-03 20:21:25 +00006124 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006125 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006126 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6127 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006128 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006129 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006130
Dale Johannesene4d209d2009-02-03 20:21:25 +00006131 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006132 MVT::Other,
6133 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006134}
6135
Dan Gohman475871a2008-07-27 21:46:04 +00006136SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006137 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006138 SDValue Root = Op.getOperand(0);
6139 SDValue Trmp = Op.getOperand(1); // trampoline
6140 SDValue FPtr = Op.getOperand(2); // nested function
6141 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006142 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006143
Dan Gohman69de1932008-02-06 22:27:42 +00006144 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006145
Duncan Sands339e14f2008-01-16 22:55:25 +00006146 const X86InstrInfo *TII =
6147 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6148
Duncan Sandsb116fac2007-07-27 20:02:49 +00006149 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006150 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006151
6152 // Large code-model.
6153
6154 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6155 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6156
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006157 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6158 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006159
6160 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6161
6162 // Load the pointer to the nested function into R11.
6163 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006164 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006165 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6166 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006167
Scott Michelfdc40a02009-02-17 22:15:04 +00006168 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006169 DAG.getConstant(2, MVT::i64));
6170 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006171
6172 // Load the 'nest' parameter value into R10.
6173 // R10 is specified in X86CallingConv.td
6174 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006175 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006176 DAG.getConstant(10, MVT::i64));
6177 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6178 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006179
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(12, MVT::i64));
6182 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006183
6184 // Jump to the nested function.
6185 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
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(20, MVT::i64));
6188 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6189 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006190
6191 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006192 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006193 DAG.getConstant(22, MVT::i64));
6194 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006195 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006196
Dan Gohman475871a2008-07-27 21:46:04 +00006197 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006198 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6199 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006200 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006201 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006202 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6203 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006204 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006205
6206 switch (CC) {
6207 default:
6208 assert(0 && "Unsupported calling convention");
6209 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006210 case CallingConv::X86_StdCall: {
6211 // Pass 'nest' parameter in ECX.
6212 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006213 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006214
6215 // Check that ECX wasn't needed by an 'inreg' parameter.
6216 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006217 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006218
Chris Lattner58d74912008-03-12 17:45:29 +00006219 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006220 unsigned InRegCount = 0;
6221 unsigned Idx = 1;
6222
6223 for (FunctionType::param_iterator I = FTy->param_begin(),
6224 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006225 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006226 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006227 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006228
6229 if (InRegCount > 2) {
6230 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6231 abort();
6232 }
6233 }
6234 break;
6235 }
6236 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006237 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006238 // Pass 'nest' parameter in EAX.
6239 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006240 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006241 break;
6242 }
6243
Dan Gohman475871a2008-07-27 21:46:04 +00006244 SDValue OutChains[4];
6245 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006246
Scott Michelfdc40a02009-02-17 22:15:04 +00006247 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006248 DAG.getConstant(10, MVT::i32));
6249 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006250
Duncan Sands339e14f2008-01-16 22:55:25 +00006251 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006252 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006253 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006254 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006255 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006256
Scott Michelfdc40a02009-02-17 22:15:04 +00006257 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006258 DAG.getConstant(1, MVT::i32));
6259 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006260
Duncan Sands339e14f2008-01-16 22:55:25 +00006261 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006262 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006263 DAG.getConstant(5, MVT::i32));
6264 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006265 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006266
Scott Michelfdc40a02009-02-17 22:15:04 +00006267 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006268 DAG.getConstant(6, MVT::i32));
6269 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006270
Dan Gohman475871a2008-07-27 21:46:04 +00006271 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006272 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6273 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006274 }
6275}
6276
Dan Gohman475871a2008-07-27 21:46:04 +00006277SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006278 /*
6279 The rounding mode is in bits 11:10 of FPSR, and has the following
6280 settings:
6281 00 Round to nearest
6282 01 Round to -inf
6283 10 Round to +inf
6284 11 Round to 0
6285
6286 FLT_ROUNDS, on the other hand, expects the following:
6287 -1 Undefined
6288 0 Round to 0
6289 1 Round to nearest
6290 2 Round to +inf
6291 3 Round to -inf
6292
6293 To perform the conversion, we do:
6294 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6295 */
6296
6297 MachineFunction &MF = DAG.getMachineFunction();
6298 const TargetMachine &TM = MF.getTarget();
6299 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6300 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006301 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006302 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006303
6304 // Save FP Control Word to stack slot
6305 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006306 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006307
Dale Johannesene4d209d2009-02-03 20:21:25 +00006308 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006309 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006310
6311 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006312 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006313
6314 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006315 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006316 DAG.getNode(ISD::SRL, dl, MVT::i16,
6317 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006318 CWD, DAG.getConstant(0x800, MVT::i16)),
6319 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006320 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006321 DAG.getNode(ISD::SRL, dl, MVT::i16,
6322 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006323 CWD, DAG.getConstant(0x400, MVT::i16)),
6324 DAG.getConstant(9, MVT::i8));
6325
Dan Gohman475871a2008-07-27 21:46:04 +00006326 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006327 DAG.getNode(ISD::AND, dl, MVT::i16,
6328 DAG.getNode(ISD::ADD, dl, MVT::i16,
6329 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006330 DAG.getConstant(1, MVT::i16)),
6331 DAG.getConstant(3, MVT::i16));
6332
6333
Duncan Sands83ec4b62008-06-06 12:08:01 +00006334 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006335 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006336}
6337
Dan Gohman475871a2008-07-27 21:46:04 +00006338SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006339 MVT VT = Op.getValueType();
6340 MVT OpVT = VT;
6341 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006342 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006343
6344 Op = Op.getOperand(0);
6345 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006346 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006347 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006348 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006349 }
Evan Cheng18efe262007-12-14 02:13:44 +00006350
Evan Cheng152804e2007-12-14 08:30:15 +00006351 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6352 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006353 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006354
6355 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006356 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006357 Ops.push_back(Op);
6358 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6359 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6360 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006361 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006362
6363 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006364 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006365
Evan Cheng18efe262007-12-14 02:13:44 +00006366 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006367 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006368 return Op;
6369}
6370
Dan Gohman475871a2008-07-27 21:46:04 +00006371SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006372 MVT VT = Op.getValueType();
6373 MVT OpVT = VT;
6374 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006375 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006376
6377 Op = Op.getOperand(0);
6378 if (VT == MVT::i8) {
6379 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006380 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006381 }
Evan Cheng152804e2007-12-14 08:30:15 +00006382
6383 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6384 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006385 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006386
6387 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006388 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006389 Ops.push_back(Op);
6390 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6391 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6392 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006393 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006394
Evan Cheng18efe262007-12-14 02:13:44 +00006395 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006396 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006397 return Op;
6398}
6399
Mon P Wangaf9b9522008-12-18 21:42:19 +00006400SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6401 MVT VT = Op.getValueType();
6402 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006403 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006404
Mon P Wangaf9b9522008-12-18 21:42:19 +00006405 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6406 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6407 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6408 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6409 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6410 //
6411 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6412 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6413 // return AloBlo + AloBhi + AhiBlo;
6414
6415 SDValue A = Op.getOperand(0);
6416 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006417
Dale Johannesene4d209d2009-02-03 20:21:25 +00006418 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006419 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6420 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006421 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006422 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6423 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006424 SDValue AloBlo = 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 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006427 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006428 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6429 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006430 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006431 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6432 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006433 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006434 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6435 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006436 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006437 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6438 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006439 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6440 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006441 return Res;
6442}
6443
6444
Bill Wendling74c37652008-12-09 22:08:41 +00006445SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6446 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6447 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006448 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6449 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006450 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006451 SDValue LHS = N->getOperand(0);
6452 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006453 unsigned BaseOp = 0;
6454 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006455 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006456
6457 switch (Op.getOpcode()) {
6458 default: assert(0 && "Unknown ovf instruction!");
6459 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006460 // A subtract of one will be selected as a INC. Note that INC doesn't
6461 // set CF, so we can't do this for UADDO.
6462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6463 if (C->getAPIntValue() == 1) {
6464 BaseOp = X86ISD::INC;
6465 Cond = X86::COND_O;
6466 break;
6467 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006468 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006469 Cond = X86::COND_O;
6470 break;
6471 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006472 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006473 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006474 break;
6475 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006476 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6477 // set CF, so we can't do this for USUBO.
6478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6479 if (C->getAPIntValue() == 1) {
6480 BaseOp = X86ISD::DEC;
6481 Cond = X86::COND_O;
6482 break;
6483 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006484 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006485 Cond = X86::COND_O;
6486 break;
6487 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006488 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006489 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006490 break;
6491 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006492 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006493 Cond = X86::COND_O;
6494 break;
6495 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006496 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006497 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006498 break;
6499 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006500
Bill Wendling61edeb52008-12-02 01:06:39 +00006501 // Also sets EFLAGS.
6502 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006503 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006504
Bill Wendling61edeb52008-12-02 01:06:39 +00006505 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006506 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006507 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006508
Bill Wendling61edeb52008-12-02 01:06:39 +00006509 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6510 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006511}
6512
Dan Gohman475871a2008-07-27 21:46:04 +00006513SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006514 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006515 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006516 unsigned Reg = 0;
6517 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006518 switch(T.getSimpleVT()) {
6519 default:
6520 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006521 case MVT::i8: Reg = X86::AL; size = 1; break;
6522 case MVT::i16: Reg = X86::AX; size = 2; break;
6523 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006524 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006525 assert(Subtarget->is64Bit() && "Node not type legal!");
6526 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006527 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006528 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006529 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006530 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006531 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006532 Op.getOperand(1),
6533 Op.getOperand(3),
6534 DAG.getTargetConstant(size, MVT::i8),
6535 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006536 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006537 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006538 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006539 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006540 return cpOut;
6541}
6542
Duncan Sands1607f052008-12-01 11:39:25 +00006543SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006544 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006545 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006546 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006547 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006548 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006549 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006550 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6551 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006552 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006553 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006554 DAG.getConstant(32, MVT::i8));
6555 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006556 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006557 rdx.getValue(1)
6558 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006559 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006560}
6561
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006562SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6563 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006564 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006565 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006566 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006567 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006568 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006569 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006570 Node->getOperand(0),
6571 Node->getOperand(1), negOp,
6572 cast<AtomicSDNode>(Node)->getSrcValue(),
6573 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006574}
6575
Evan Cheng0db9fe62006-04-25 20:13:52 +00006576/// LowerOperation - Provide custom lowering hooks for some operations.
6577///
Dan Gohman475871a2008-07-27 21:46:04 +00006578SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006579 switch (Op.getOpcode()) {
6580 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006581 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6582 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006583 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6584 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6585 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6586 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6587 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6588 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6589 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006590 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006591 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006592 case ISD::SHL_PARTS:
6593 case ISD::SRA_PARTS:
6594 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6595 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006596 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006597 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006598 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006599 case ISD::FABS: return LowerFABS(Op, DAG);
6600 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006601 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006602 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006603 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006604 case ISD::SELECT: return LowerSELECT(Op, DAG);
6605 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006606 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006607 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006608 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006609 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006611 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006612 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006613 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006614 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6615 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006616 case ISD::FRAME_TO_ARGS_OFFSET:
6617 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006618 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006619 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006620 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006621 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006622 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6623 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006624 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006625 case ISD::SADDO:
6626 case ISD::UADDO:
6627 case ISD::SSUBO:
6628 case ISD::USUBO:
6629 case ISD::SMULO:
6630 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006631 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006632 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006633}
6634
Duncan Sands1607f052008-12-01 11:39:25 +00006635void X86TargetLowering::
6636ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6637 SelectionDAG &DAG, unsigned NewOp) {
6638 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006639 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006640 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6641
6642 SDValue Chain = Node->getOperand(0);
6643 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006644 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006645 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006646 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006647 Node->getOperand(2), DAG.getIntPtrConstant(1));
6648 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6649 // have a MemOperand. Pass the info through as a normal operand.
6650 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6651 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6652 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006653 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006654 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006655 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006656 Results.push_back(Result.getValue(2));
6657}
6658
Duncan Sands126d9072008-07-04 11:47:58 +00006659/// ReplaceNodeResults - Replace a node with an illegal result type
6660/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006661void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6662 SmallVectorImpl<SDValue>&Results,
6663 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006664 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006665 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006666 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006667 assert(false && "Do not know how to custom type legalize this operation!");
6668 return;
6669 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006670 std::pair<SDValue,SDValue> Vals =
6671 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006672 SDValue FIST = Vals.first, StackSlot = Vals.second;
6673 if (FIST.getNode() != 0) {
6674 MVT VT = N->getValueType(0);
6675 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006676 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006677 }
6678 return;
6679 }
6680 case ISD::READCYCLECOUNTER: {
6681 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6682 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006683 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006684 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006685 rd.getValue(1));
6686 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006687 eax.getValue(2));
6688 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6689 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006690 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006691 Results.push_back(edx.getValue(1));
6692 return;
6693 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006694 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006695 MVT T = N->getValueType(0);
6696 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6697 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006698 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006699 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006700 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006701 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006702 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6703 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006704 cpInL.getValue(1));
6705 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006706 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006707 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006708 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006709 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006710 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006711 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006712 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006713 swapInL.getValue(1));
6714 SDValue Ops[] = { swapInH.getValue(0),
6715 N->getOperand(1),
6716 swapInH.getValue(1) };
6717 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006718 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006719 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6720 MVT::i32, Result.getValue(1));
6721 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6722 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006723 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006724 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006725 Results.push_back(cpOutH.getValue(1));
6726 return;
6727 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006728 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006729 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6730 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006731 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006732 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6733 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006734 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006735 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6736 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006737 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006738 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6739 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006740 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006741 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6742 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006743 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006744 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6745 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006746 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006747 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6748 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006749 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006750}
6751
Evan Cheng72261582005-12-20 06:22:03 +00006752const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6753 switch (Opcode) {
6754 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006755 case X86ISD::BSF: return "X86ISD::BSF";
6756 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006757 case X86ISD::SHLD: return "X86ISD::SHLD";
6758 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006759 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006760 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006761 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006762 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006763 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006764 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006765 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6766 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6767 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006768 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006769 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006770 case X86ISD::CALL: return "X86ISD::CALL";
6771 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6772 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006773 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006774 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006775 case X86ISD::COMI: return "X86ISD::COMI";
6776 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006777 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006778 case X86ISD::CMOV: return "X86ISD::CMOV";
6779 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006780 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006781 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6782 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006783 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006784 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006785 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006786 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006787 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6788 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006789 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006790 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006791 case X86ISD::FMAX: return "X86ISD::FMAX";
6792 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006793 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6794 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006795 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006796 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006797 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006798 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006799 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006800 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6801 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006802 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6803 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6804 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6805 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6806 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6807 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006808 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6809 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006810 case X86ISD::VSHL: return "X86ISD::VSHL";
6811 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006812 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6813 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6814 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6815 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6816 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6817 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6818 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6819 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6820 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6821 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006822 case X86ISD::ADD: return "X86ISD::ADD";
6823 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006824 case X86ISD::SMUL: return "X86ISD::SMUL";
6825 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006826 case X86ISD::INC: return "X86ISD::INC";
6827 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006828 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006829 }
6830}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006831
Chris Lattnerc9addb72007-03-30 23:15:24 +00006832// isLegalAddressingMode - Return true if the addressing mode represented
6833// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006834bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006835 const Type *Ty) const {
6836 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006837
Chris Lattnerc9addb72007-03-30 23:15:24 +00006838 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6839 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6840 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006841
Chris Lattnerc9addb72007-03-30 23:15:24 +00006842 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006843 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006844 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6845 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006846 // If BaseGV requires a register, we cannot also have a BaseReg.
6847 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6848 AM.HasBaseReg)
6849 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006850
6851 // X86-64 only supports addr of globals in small code model.
6852 if (Subtarget->is64Bit()) {
6853 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6854 return false;
6855 // If lower 4G is not available, then we must use rip-relative addressing.
6856 if (AM.BaseOffs || AM.Scale > 1)
6857 return false;
6858 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006859 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006860
Chris Lattnerc9addb72007-03-30 23:15:24 +00006861 switch (AM.Scale) {
6862 case 0:
6863 case 1:
6864 case 2:
6865 case 4:
6866 case 8:
6867 // These scales always work.
6868 break;
6869 case 3:
6870 case 5:
6871 case 9:
6872 // These scales are formed with basereg+scalereg. Only accept if there is
6873 // no basereg yet.
6874 if (AM.HasBaseReg)
6875 return false;
6876 break;
6877 default: // Other stuff never works.
6878 return false;
6879 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006880
Chris Lattnerc9addb72007-03-30 23:15:24 +00006881 return true;
6882}
6883
6884
Evan Cheng2bd122c2007-10-26 01:56:11 +00006885bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6886 if (!Ty1->isInteger() || !Ty2->isInteger())
6887 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006888 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6889 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006890 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006891 return false;
6892 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006893}
6894
Duncan Sands83ec4b62008-06-06 12:08:01 +00006895bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6896 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006897 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006898 unsigned NumBits1 = VT1.getSizeInBits();
6899 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006900 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006901 return false;
6902 return Subtarget->is64Bit() || NumBits1 < 64;
6903}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006904
Dan Gohman97121ba2009-04-08 00:15:30 +00006905bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006906 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006907 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6908}
6909
6910bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006911 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006912 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6913}
6914
Evan Cheng8b944d32009-05-28 00:35:15 +00006915bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
6916 // i16 instructions are longer (0x66 prefix) and potentially slower.
6917 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
6918}
6919
Evan Cheng60c07e12006-07-05 22:17:51 +00006920/// isShuffleMaskLegal - Targets can use this to indicate that they only
6921/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6922/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6923/// are assumed to be legal.
6924bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006925X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6926 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006927 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006928 if (VT.getSizeInBits() == 64)
6929 return false;
6930
6931 // FIXME: pshufb, blends, palignr, shifts.
6932 return (VT.getVectorNumElements() == 2 ||
6933 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6934 isMOVLMask(M, VT) ||
6935 isSHUFPMask(M, VT) ||
6936 isPSHUFDMask(M, VT) ||
6937 isPSHUFHWMask(M, VT) ||
6938 isPSHUFLWMask(M, VT) ||
6939 isUNPCKLMask(M, VT) ||
6940 isUNPCKHMask(M, VT) ||
6941 isUNPCKL_v_undef_Mask(M, VT) ||
6942 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006943}
6944
Dan Gohman7d8143f2008-04-09 20:09:42 +00006945bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006946X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006947 MVT VT) const {
6948 unsigned NumElts = VT.getVectorNumElements();
6949 // FIXME: This collection of masks seems suspect.
6950 if (NumElts == 2)
6951 return true;
6952 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6953 return (isMOVLMask(Mask, VT) ||
6954 isCommutedMOVLMask(Mask, VT, true) ||
6955 isSHUFPMask(Mask, VT) ||
6956 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006957 }
6958 return false;
6959}
6960
6961//===----------------------------------------------------------------------===//
6962// X86 Scheduler Hooks
6963//===----------------------------------------------------------------------===//
6964
Mon P Wang63307c32008-05-05 19:05:59 +00006965// private utility function
6966MachineBasicBlock *
6967X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6968 MachineBasicBlock *MBB,
6969 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006970 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006971 unsigned LoadOpc,
6972 unsigned CXchgOpc,
6973 unsigned copyOpc,
6974 unsigned notOpc,
6975 unsigned EAXreg,
6976 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006977 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006978 // For the atomic bitwise operator, we generate
6979 // thisMBB:
6980 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006981 // ld t1 = [bitinstr.addr]
6982 // op t2 = t1, [bitinstr.val]
6983 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006984 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6985 // bz newMBB
6986 // fallthrough -->nextMBB
6987 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6988 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006989 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006990 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006991
Mon P Wang63307c32008-05-05 19:05:59 +00006992 /// First build the CFG
6993 MachineFunction *F = MBB->getParent();
6994 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006995 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6996 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6997 F->insert(MBBIter, newMBB);
6998 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006999
Mon P Wang63307c32008-05-05 19:05:59 +00007000 // Move all successors to thisMBB to nextMBB
7001 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007002
Mon P Wang63307c32008-05-05 19:05:59 +00007003 // Update thisMBB to fall through to newMBB
7004 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007005
Mon P Wang63307c32008-05-05 19:05:59 +00007006 // newMBB jumps to itself and fall through to nextMBB
7007 newMBB->addSuccessor(nextMBB);
7008 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007009
Mon P Wang63307c32008-05-05 19:05:59 +00007010 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007011 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007012 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007013 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007014 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007015 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007016 int numArgs = bInstr->getNumOperands() - 1;
7017 for (int i=0; i < numArgs; ++i)
7018 argOpers[i] = &bInstr->getOperand(i+1);
7019
7020 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007021 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7022 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007023
Dale Johannesen140be2d2008-08-19 18:47:28 +00007024 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007025 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007026 for (int i=0; i <= lastAddrIndx; ++i)
7027 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007028
Dale Johannesen140be2d2008-08-19 18:47:28 +00007029 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007030 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007031 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007032 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007033 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007034 tt = t1;
7035
Dale Johannesen140be2d2008-08-19 18:47:28 +00007036 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007037 assert((argOpers[valArgIndx]->isReg() ||
7038 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007039 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007040 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007041 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007042 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007043 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007044 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007045 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007046
Dale Johannesene4d209d2009-02-03 20:21:25 +00007047 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007048 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007049
Dale Johannesene4d209d2009-02-03 20:21:25 +00007050 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007051 for (int i=0; i <= lastAddrIndx; ++i)
7052 (*MIB).addOperand(*argOpers[i]);
7053 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007054 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7055 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7056
Dale Johannesene4d209d2009-02-03 20:21:25 +00007057 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007058 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007059
Mon P Wang63307c32008-05-05 19:05:59 +00007060 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007061 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007062
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007063 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007064 return nextMBB;
7065}
7066
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007067// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007068MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007069X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7070 MachineBasicBlock *MBB,
7071 unsigned regOpcL,
7072 unsigned regOpcH,
7073 unsigned immOpcL,
7074 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007075 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007076 // For the atomic bitwise operator, we generate
7077 // thisMBB (instructions are in pairs, except cmpxchg8b)
7078 // ld t1,t2 = [bitinstr.addr]
7079 // newMBB:
7080 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7081 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007082 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007083 // mov ECX, EBX <- t5, t6
7084 // mov EAX, EDX <- t1, t2
7085 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7086 // mov t3, t4 <- EAX, EDX
7087 // bz newMBB
7088 // result in out1, out2
7089 // fallthrough -->nextMBB
7090
7091 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7092 const unsigned LoadOpc = X86::MOV32rm;
7093 const unsigned copyOpc = X86::MOV32rr;
7094 const unsigned NotOpc = X86::NOT32r;
7095 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7096 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7097 MachineFunction::iterator MBBIter = MBB;
7098 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007099
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007100 /// First build the CFG
7101 MachineFunction *F = MBB->getParent();
7102 MachineBasicBlock *thisMBB = MBB;
7103 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7104 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7105 F->insert(MBBIter, newMBB);
7106 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007107
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007108 // Move all successors to thisMBB to nextMBB
7109 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007110
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007111 // Update thisMBB to fall through to newMBB
7112 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007113
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007114 // newMBB jumps to itself and fall through to nextMBB
7115 newMBB->addSuccessor(nextMBB);
7116 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007117
Dale Johannesene4d209d2009-02-03 20:21:25 +00007118 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007119 // Insert instructions into newMBB based on incoming instruction
7120 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007121 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007122 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007123 MachineOperand& dest1Oper = bInstr->getOperand(0);
7124 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007125 MachineOperand* argOpers[2 + X86AddrNumOperands];
7126 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007127 argOpers[i] = &bInstr->getOperand(i+2);
7128
7129 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007130 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007131
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007132 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007133 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007134 for (int i=0; i <= lastAddrIndx; ++i)
7135 (*MIB).addOperand(*argOpers[i]);
7136 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007137 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007138 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007139 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007140 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007141 MachineOperand newOp3 = *(argOpers[3]);
7142 if (newOp3.isImm())
7143 newOp3.setImm(newOp3.getImm()+4);
7144 else
7145 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007146 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007147 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007148
7149 // t3/4 are defined later, at the bottom of the loop
7150 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7151 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007152 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007153 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007154 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007155 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7156
7157 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7158 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007159 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007160 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7161 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007162 } else {
7163 tt1 = t1;
7164 tt2 = t2;
7165 }
7166
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007167 int valArgIndx = lastAddrIndx + 1;
7168 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007169 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007170 "invalid operand");
7171 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7172 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007173 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007174 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007175 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007176 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007177 if (regOpcL != X86::MOV32rr)
7178 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007179 (*MIB).addOperand(*argOpers[valArgIndx]);
7180 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007181 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007182 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007183 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007184 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007185 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007186 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007187 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007188 if (regOpcH != X86::MOV32rr)
7189 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007190 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007191
Dale Johannesene4d209d2009-02-03 20:21:25 +00007192 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007193 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007194 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007195 MIB.addReg(t2);
7196
Dale Johannesene4d209d2009-02-03 20:21:25 +00007197 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007198 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007199 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007200 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007201
Dale Johannesene4d209d2009-02-03 20:21:25 +00007202 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007203 for (int i=0; i <= lastAddrIndx; ++i)
7204 (*MIB).addOperand(*argOpers[i]);
7205
7206 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7207 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7208
Dale Johannesene4d209d2009-02-03 20:21:25 +00007209 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007210 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007211 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007212 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007213
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007214 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007215 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007216
7217 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7218 return nextMBB;
7219}
7220
7221// private utility function
7222MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007223X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7224 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007225 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007226 // For the atomic min/max operator, we generate
7227 // thisMBB:
7228 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007229 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007230 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007231 // cmp t1, t2
7232 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007233 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007234 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7235 // bz newMBB
7236 // fallthrough -->nextMBB
7237 //
7238 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7239 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007240 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007241 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Mon P Wang63307c32008-05-05 19:05:59 +00007243 /// First build the CFG
7244 MachineFunction *F = MBB->getParent();
7245 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007246 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7247 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7248 F->insert(MBBIter, newMBB);
7249 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007250
Mon P Wang63307c32008-05-05 19:05:59 +00007251 // Move all successors to thisMBB to nextMBB
7252 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007253
Mon P Wang63307c32008-05-05 19:05:59 +00007254 // Update thisMBB to fall through to newMBB
7255 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007256
Mon P Wang63307c32008-05-05 19:05:59 +00007257 // newMBB jumps to newMBB and fall through to nextMBB
7258 newMBB->addSuccessor(nextMBB);
7259 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007260
Dale Johannesene4d209d2009-02-03 20:21:25 +00007261 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007262 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007263 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007264 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007265 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007266 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007267 int numArgs = mInstr->getNumOperands() - 1;
7268 for (int i=0; i < numArgs; ++i)
7269 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007270
Mon P Wang63307c32008-05-05 19:05:59 +00007271 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007272 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7273 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007274
Mon P Wangab3e7472008-05-05 22:56:23 +00007275 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007276 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007277 for (int i=0; i <= lastAddrIndx; ++i)
7278 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007279
Mon P Wang63307c32008-05-05 19:05:59 +00007280 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007281 assert((argOpers[valArgIndx]->isReg() ||
7282 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007283 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007284
7285 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007286 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007287 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007288 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007289 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007290 (*MIB).addOperand(*argOpers[valArgIndx]);
7291
Dale Johannesene4d209d2009-02-03 20:21:25 +00007292 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007293 MIB.addReg(t1);
7294
Dale Johannesene4d209d2009-02-03 20:21:25 +00007295 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007296 MIB.addReg(t1);
7297 MIB.addReg(t2);
7298
7299 // Generate movc
7300 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007301 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007302 MIB.addReg(t2);
7303 MIB.addReg(t1);
7304
7305 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007306 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007307 for (int i=0; i <= lastAddrIndx; ++i)
7308 (*MIB).addOperand(*argOpers[i]);
7309 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007310 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7311 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007312
Dale Johannesene4d209d2009-02-03 20:21:25 +00007313 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007314 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007315
Mon P Wang63307c32008-05-05 19:05:59 +00007316 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007317 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007318
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007319 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007320 return nextMBB;
7321}
7322
7323
Evan Cheng60c07e12006-07-05 22:17:51 +00007324MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007325X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007326 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007327 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007328 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007329 switch (MI->getOpcode()) {
7330 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007331 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007332 case X86::CMOV_FR32:
7333 case X86::CMOV_FR64:
7334 case X86::CMOV_V4F32:
7335 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007336 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007337 // To "insert" a SELECT_CC instruction, we actually have to insert the
7338 // diamond control-flow pattern. The incoming instruction knows the
7339 // destination vreg to set, the condition code register to branch on, the
7340 // true/false values to select between, and a branch opcode to use.
7341 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007342 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007343 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007344
Evan Cheng60c07e12006-07-05 22:17:51 +00007345 // thisMBB:
7346 // ...
7347 // TrueVal = ...
7348 // cmpTY ccX, r1, r2
7349 // bCC copy1MBB
7350 // fallthrough --> copy0MBB
7351 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007352 MachineFunction *F = BB->getParent();
7353 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7354 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007355 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007356 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007357 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007358 F->insert(It, copy0MBB);
7359 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007360 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007361 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007362 sinkMBB->transferSuccessors(BB);
7363
7364 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007365 BB->addSuccessor(copy0MBB);
7366 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007367
Evan Cheng60c07e12006-07-05 22:17:51 +00007368 // copy0MBB:
7369 // %FalseValue = ...
7370 // # fallthrough to sinkMBB
7371 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007372
Evan Cheng60c07e12006-07-05 22:17:51 +00007373 // Update machine-CFG edges
7374 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007375
Evan Cheng60c07e12006-07-05 22:17:51 +00007376 // sinkMBB:
7377 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7378 // ...
7379 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007380 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007381 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7382 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7383
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007384 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007385 return BB;
7386 }
7387
Dale Johannesen849f2142007-07-03 00:53:03 +00007388 case X86::FP32_TO_INT16_IN_MEM:
7389 case X86::FP32_TO_INT32_IN_MEM:
7390 case X86::FP32_TO_INT64_IN_MEM:
7391 case X86::FP64_TO_INT16_IN_MEM:
7392 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007393 case X86::FP64_TO_INT64_IN_MEM:
7394 case X86::FP80_TO_INT16_IN_MEM:
7395 case X86::FP80_TO_INT32_IN_MEM:
7396 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007397 // Change the floating point control register to use "round towards zero"
7398 // mode when truncating to an integer value.
7399 MachineFunction *F = BB->getParent();
7400 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007401 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007402
7403 // Load the old value of the high byte of the control word...
7404 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007405 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007406 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007407 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007408
7409 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007410 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007411 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007412
7413 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007414 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007415
7416 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007417 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007418 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007419
7420 // Get the X86 opcode to use.
7421 unsigned Opc;
7422 switch (MI->getOpcode()) {
7423 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007424 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7425 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7426 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7427 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7428 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7429 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007430 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7431 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7432 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007433 }
7434
7435 X86AddressMode AM;
7436 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007437 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007438 AM.BaseType = X86AddressMode::RegBase;
7439 AM.Base.Reg = Op.getReg();
7440 } else {
7441 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007442 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007443 }
7444 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007445 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007446 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007447 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007448 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007449 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007450 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007451 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007452 AM.GV = Op.getGlobal();
7453 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007454 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007455 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007456 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007457 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007458
7459 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007460 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007461
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007462 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007463 return BB;
7464 }
Mon P Wang63307c32008-05-05 19:05:59 +00007465 case X86::ATOMAND32:
7466 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007467 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007468 X86::LCMPXCHG32, X86::MOV32rr,
7469 X86::NOT32r, X86::EAX,
7470 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007471 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007472 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7473 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007474 X86::LCMPXCHG32, X86::MOV32rr,
7475 X86::NOT32r, X86::EAX,
7476 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007477 case X86::ATOMXOR32:
7478 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007479 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007480 X86::LCMPXCHG32, X86::MOV32rr,
7481 X86::NOT32r, X86::EAX,
7482 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007483 case X86::ATOMNAND32:
7484 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007485 X86::AND32ri, X86::MOV32rm,
7486 X86::LCMPXCHG32, X86::MOV32rr,
7487 X86::NOT32r, X86::EAX,
7488 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007489 case X86::ATOMMIN32:
7490 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7491 case X86::ATOMMAX32:
7492 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7493 case X86::ATOMUMIN32:
7494 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7495 case X86::ATOMUMAX32:
7496 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007497
7498 case X86::ATOMAND16:
7499 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7500 X86::AND16ri, X86::MOV16rm,
7501 X86::LCMPXCHG16, X86::MOV16rr,
7502 X86::NOT16r, X86::AX,
7503 X86::GR16RegisterClass);
7504 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007505 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007506 X86::OR16ri, X86::MOV16rm,
7507 X86::LCMPXCHG16, X86::MOV16rr,
7508 X86::NOT16r, X86::AX,
7509 X86::GR16RegisterClass);
7510 case X86::ATOMXOR16:
7511 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7512 X86::XOR16ri, X86::MOV16rm,
7513 X86::LCMPXCHG16, X86::MOV16rr,
7514 X86::NOT16r, X86::AX,
7515 X86::GR16RegisterClass);
7516 case X86::ATOMNAND16:
7517 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7518 X86::AND16ri, X86::MOV16rm,
7519 X86::LCMPXCHG16, X86::MOV16rr,
7520 X86::NOT16r, X86::AX,
7521 X86::GR16RegisterClass, true);
7522 case X86::ATOMMIN16:
7523 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7524 case X86::ATOMMAX16:
7525 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7526 case X86::ATOMUMIN16:
7527 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7528 case X86::ATOMUMAX16:
7529 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7530
7531 case X86::ATOMAND8:
7532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7533 X86::AND8ri, X86::MOV8rm,
7534 X86::LCMPXCHG8, X86::MOV8rr,
7535 X86::NOT8r, X86::AL,
7536 X86::GR8RegisterClass);
7537 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007539 X86::OR8ri, X86::MOV8rm,
7540 X86::LCMPXCHG8, X86::MOV8rr,
7541 X86::NOT8r, X86::AL,
7542 X86::GR8RegisterClass);
7543 case X86::ATOMXOR8:
7544 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7545 X86::XOR8ri, X86::MOV8rm,
7546 X86::LCMPXCHG8, X86::MOV8rr,
7547 X86::NOT8r, X86::AL,
7548 X86::GR8RegisterClass);
7549 case X86::ATOMNAND8:
7550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7551 X86::AND8ri, X86::MOV8rm,
7552 X86::LCMPXCHG8, X86::MOV8rr,
7553 X86::NOT8r, X86::AL,
7554 X86::GR8RegisterClass, true);
7555 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007556 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007557 case X86::ATOMAND64:
7558 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007559 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007560 X86::LCMPXCHG64, X86::MOV64rr,
7561 X86::NOT64r, X86::RAX,
7562 X86::GR64RegisterClass);
7563 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007564 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7565 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007566 X86::LCMPXCHG64, X86::MOV64rr,
7567 X86::NOT64r, X86::RAX,
7568 X86::GR64RegisterClass);
7569 case X86::ATOMXOR64:
7570 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007571 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007572 X86::LCMPXCHG64, X86::MOV64rr,
7573 X86::NOT64r, X86::RAX,
7574 X86::GR64RegisterClass);
7575 case X86::ATOMNAND64:
7576 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7577 X86::AND64ri32, X86::MOV64rm,
7578 X86::LCMPXCHG64, X86::MOV64rr,
7579 X86::NOT64r, X86::RAX,
7580 X86::GR64RegisterClass, true);
7581 case X86::ATOMMIN64:
7582 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7583 case X86::ATOMMAX64:
7584 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7585 case X86::ATOMUMIN64:
7586 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7587 case X86::ATOMUMAX64:
7588 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007589
7590 // This group does 64-bit operations on a 32-bit host.
7591 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007592 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007593 X86::AND32rr, X86::AND32rr,
7594 X86::AND32ri, X86::AND32ri,
7595 false);
7596 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007597 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007598 X86::OR32rr, X86::OR32rr,
7599 X86::OR32ri, X86::OR32ri,
7600 false);
7601 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007602 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007603 X86::XOR32rr, X86::XOR32rr,
7604 X86::XOR32ri, X86::XOR32ri,
7605 false);
7606 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007607 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007608 X86::AND32rr, X86::AND32rr,
7609 X86::AND32ri, X86::AND32ri,
7610 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007611 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007612 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007613 X86::ADD32rr, X86::ADC32rr,
7614 X86::ADD32ri, X86::ADC32ri,
7615 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007616 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007617 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007618 X86::SUB32rr, X86::SBB32rr,
7619 X86::SUB32ri, X86::SBB32ri,
7620 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007621 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007622 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007623 X86::MOV32rr, X86::MOV32rr,
7624 X86::MOV32ri, X86::MOV32ri,
7625 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007626 }
7627}
7628
7629//===----------------------------------------------------------------------===//
7630// X86 Optimization Hooks
7631//===----------------------------------------------------------------------===//
7632
Dan Gohman475871a2008-07-27 21:46:04 +00007633void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007634 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007635 APInt &KnownZero,
7636 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007637 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007638 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007639 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007640 assert((Opc >= ISD::BUILTIN_OP_END ||
7641 Opc == ISD::INTRINSIC_WO_CHAIN ||
7642 Opc == ISD::INTRINSIC_W_CHAIN ||
7643 Opc == ISD::INTRINSIC_VOID) &&
7644 "Should use MaskedValueIsZero if you don't know whether Op"
7645 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007646
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007647 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007648 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007649 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007650 case X86ISD::ADD:
7651 case X86ISD::SUB:
7652 case X86ISD::SMUL:
7653 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007654 case X86ISD::INC:
7655 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007656 // These nodes' second result is a boolean.
7657 if (Op.getResNo() == 0)
7658 break;
7659 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007660 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007661 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7662 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007663 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007664 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007665}
Chris Lattner259e97c2006-01-31 19:43:35 +00007666
Evan Cheng206ee9d2006-07-07 08:33:52 +00007667/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007668/// node is a GlobalAddress + offset.
7669bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7670 GlobalValue* &GA, int64_t &Offset) const{
7671 if (N->getOpcode() == X86ISD::Wrapper) {
7672 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007673 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007674 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007675 return true;
7676 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007677 }
Evan Chengad4196b2008-05-12 19:56:52 +00007678 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007679}
7680
Evan Chengad4196b2008-05-12 19:56:52 +00007681static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7682 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007683 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007684 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007685 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007686 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007687 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007688 return false;
7689}
7690
Nate Begeman9008ca62009-04-27 18:41:29 +00007691static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Eli Friedman7a5e5552009-06-07 06:52:44 +00007692 MVT EVT, LoadSDNode *&LDBase,
7693 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00007694 SelectionDAG &DAG, MachineFrameInfo *MFI,
7695 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007696 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00007697 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007698 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007699 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007700 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00007701 return false;
7702 continue;
7703 }
7704
Dan Gohman475871a2008-07-27 21:46:04 +00007705 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007706 if (!Elt.getNode() ||
7707 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007708 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007709 if (!LDBase) {
7710 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00007711 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007712 LDBase = cast<LoadSDNode>(Elt.getNode());
7713 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007714 continue;
7715 }
7716 if (Elt.getOpcode() == ISD::UNDEF)
7717 continue;
7718
Nate Begemanabc01992009-06-05 21:37:30 +00007719 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Nate Begemanabc01992009-06-05 21:37:30 +00007720 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007721 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007722 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007723 }
7724 return true;
7725}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007726
7727/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7728/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7729/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007730/// order. In the case of v2i64, it will see if it can rewrite the
7731/// shuffle to be an appropriate build vector so it can take advantage of
7732// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007733static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007734 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007735 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007736 MVT VT = N->getValueType(0);
7737 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007738 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7739 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007740
Eli Friedman7a5e5552009-06-07 06:52:44 +00007741 if (VT.getSizeInBits() != 128)
7742 return SDValue();
7743
Mon P Wang1e955802009-04-03 02:43:30 +00007744 // Try to combine a vector_shuffle into a 128-bit load.
7745 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00007746 LoadSDNode *LD = NULL;
7747 unsigned LastLoadedElt;
7748 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG,
7749 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007750 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007751
Eli Friedman7a5e5552009-06-07 06:52:44 +00007752 if (LastLoadedElt == NumElems - 1) {
7753 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI))
7754 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7755 LD->getSrcValue(), LD->getSrcValueOffset(),
7756 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007757 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007758 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00007759 LD->isVolatile(), LD->getAlignment());
7760 } else if (NumElems == 4 && LastLoadedElt == 1) {
7761 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00007762 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7763 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00007764 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7765 }
7766 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007767}
Evan Chengd880b972008-05-09 21:53:03 +00007768
Chris Lattner83e6c992006-10-04 06:57:07 +00007769/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007770static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007771 const X86Subtarget *Subtarget) {
7772 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007773 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007774 // Get the LHS/RHS of the select.
7775 SDValue LHS = N->getOperand(1);
7776 SDValue RHS = N->getOperand(2);
7777
Chris Lattner83e6c992006-10-04 06:57:07 +00007778 // If we have SSE[12] support, try to form min/max nodes.
7779 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007780 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7781 Cond.getOpcode() == ISD::SETCC) {
7782 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007783
Chris Lattner47b4ce82009-03-11 05:48:52 +00007784 unsigned Opcode = 0;
7785 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7786 switch (CC) {
7787 default: break;
7788 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7789 case ISD::SETULE:
7790 case ISD::SETLE:
7791 if (!UnsafeFPMath) break;
7792 // FALL THROUGH.
7793 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7794 case ISD::SETLT:
7795 Opcode = X86ISD::FMIN;
7796 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007797
Chris Lattner47b4ce82009-03-11 05:48:52 +00007798 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7799 case ISD::SETUGT:
7800 case ISD::SETGT:
7801 if (!UnsafeFPMath) break;
7802 // FALL THROUGH.
7803 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7804 case ISD::SETGE:
7805 Opcode = X86ISD::FMAX;
7806 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007807 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007808 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7809 switch (CC) {
7810 default: break;
7811 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7812 case ISD::SETUGT:
7813 case ISD::SETGT:
7814 if (!UnsafeFPMath) break;
7815 // FALL THROUGH.
7816 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7817 case ISD::SETGE:
7818 Opcode = X86ISD::FMIN;
7819 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007820
Chris Lattner47b4ce82009-03-11 05:48:52 +00007821 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7822 case ISD::SETULE:
7823 case ISD::SETLE:
7824 if (!UnsafeFPMath) break;
7825 // FALL THROUGH.
7826 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7827 case ISD::SETLT:
7828 Opcode = X86ISD::FMAX;
7829 break;
7830 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007831 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007832
Chris Lattner47b4ce82009-03-11 05:48:52 +00007833 if (Opcode)
7834 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007835 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007836
Chris Lattnerd1980a52009-03-12 06:52:53 +00007837 // If this is a select between two integer constants, try to do some
7838 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007839 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7840 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007841 // Don't do this for crazy integer types.
7842 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7843 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007844 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007845 bool NeedsCondInvert = false;
7846
Chris Lattnercee56e72009-03-13 05:53:31 +00007847 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007848 // Efficiently invertible.
7849 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7850 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7851 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7852 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007853 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007854 }
7855
7856 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007857 if (FalseC->getAPIntValue() == 0 &&
7858 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007859 if (NeedsCondInvert) // Invert the condition if needed.
7860 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7861 DAG.getConstant(1, Cond.getValueType()));
7862
7863 // Zero extend the condition if needed.
7864 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7865
Chris Lattnercee56e72009-03-13 05:53:31 +00007866 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007867 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7868 DAG.getConstant(ShAmt, MVT::i8));
7869 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007870
7871 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007872 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007873 if (NeedsCondInvert) // Invert the condition if needed.
7874 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7875 DAG.getConstant(1, Cond.getValueType()));
7876
7877 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007878 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7879 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007880 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007881 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007882 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007883
7884 // Optimize cases that will turn into an LEA instruction. This requires
7885 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7886 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7887 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7888 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7889
7890 bool isFastMultiplier = false;
7891 if (Diff < 10) {
7892 switch ((unsigned char)Diff) {
7893 default: break;
7894 case 1: // result = add base, cond
7895 case 2: // result = lea base( , cond*2)
7896 case 3: // result = lea base(cond, cond*2)
7897 case 4: // result = lea base( , cond*4)
7898 case 5: // result = lea base(cond, cond*4)
7899 case 8: // result = lea base( , cond*8)
7900 case 9: // result = lea base(cond, cond*8)
7901 isFastMultiplier = true;
7902 break;
7903 }
7904 }
7905
7906 if (isFastMultiplier) {
7907 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7908 if (NeedsCondInvert) // Invert the condition if needed.
7909 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7910 DAG.getConstant(1, Cond.getValueType()));
7911
7912 // Zero extend the condition if needed.
7913 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7914 Cond);
7915 // Scale the condition by the difference.
7916 if (Diff != 1)
7917 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7918 DAG.getConstant(Diff, Cond.getValueType()));
7919
7920 // Add the base if non-zero.
7921 if (FalseC->getAPIntValue() != 0)
7922 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7923 SDValue(FalseC, 0));
7924 return Cond;
7925 }
7926 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007927 }
7928 }
7929
Dan Gohman475871a2008-07-27 21:46:04 +00007930 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007931}
7932
Chris Lattnerd1980a52009-03-12 06:52:53 +00007933/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7934static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7935 TargetLowering::DAGCombinerInfo &DCI) {
7936 DebugLoc DL = N->getDebugLoc();
7937
7938 // If the flag operand isn't dead, don't touch this CMOV.
7939 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
7940 return SDValue();
7941
7942 // If this is a select between two integer constants, try to do some
7943 // optimizations. Note that the operands are ordered the opposite of SELECT
7944 // operands.
7945 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
7946 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
7947 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
7948 // larger than FalseC (the false value).
7949 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
7950
7951 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
7952 CC = X86::GetOppositeBranchCondition(CC);
7953 std::swap(TrueC, FalseC);
7954 }
7955
7956 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007957 // This is efficient for any integer data type (including i8/i16) and
7958 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007959 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
7960 SDValue Cond = N->getOperand(3);
7961 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7962 DAG.getConstant(CC, MVT::i8), Cond);
7963
7964 // Zero extend the condition if needed.
7965 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
7966
7967 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
7968 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
7969 DAG.getConstant(ShAmt, MVT::i8));
7970 if (N->getNumValues() == 2) // Dead flag value?
7971 return DCI.CombineTo(N, Cond, SDValue());
7972 return Cond;
7973 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007974
7975 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
7976 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00007977 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
7978 SDValue Cond = N->getOperand(3);
7979 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7980 DAG.getConstant(CC, MVT::i8), Cond);
7981
7982 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007983 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7984 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007985 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7986 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00007987
Chris Lattner97a29a52009-03-13 05:22:11 +00007988 if (N->getNumValues() == 2) // Dead flag value?
7989 return DCI.CombineTo(N, Cond, SDValue());
7990 return Cond;
7991 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007992
7993 // Optimize cases that will turn into an LEA instruction. This requires
7994 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7995 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7996 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7997 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7998
7999 bool isFastMultiplier = false;
8000 if (Diff < 10) {
8001 switch ((unsigned char)Diff) {
8002 default: break;
8003 case 1: // result = add base, cond
8004 case 2: // result = lea base( , cond*2)
8005 case 3: // result = lea base(cond, cond*2)
8006 case 4: // result = lea base( , cond*4)
8007 case 5: // result = lea base(cond, cond*4)
8008 case 8: // result = lea base( , cond*8)
8009 case 9: // result = lea base(cond, cond*8)
8010 isFastMultiplier = true;
8011 break;
8012 }
8013 }
8014
8015 if (isFastMultiplier) {
8016 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8017 SDValue Cond = N->getOperand(3);
8018 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8019 DAG.getConstant(CC, MVT::i8), Cond);
8020 // Zero extend the condition if needed.
8021 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8022 Cond);
8023 // Scale the condition by the difference.
8024 if (Diff != 1)
8025 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8026 DAG.getConstant(Diff, Cond.getValueType()));
8027
8028 // Add the base if non-zero.
8029 if (FalseC->getAPIntValue() != 0)
8030 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8031 SDValue(FalseC, 0));
8032 if (N->getNumValues() == 2) // Dead flag value?
8033 return DCI.CombineTo(N, Cond, SDValue());
8034 return Cond;
8035 }
8036 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008037 }
8038 }
8039 return SDValue();
8040}
8041
8042
Evan Cheng0b0cd912009-03-28 05:57:29 +00008043/// PerformMulCombine - Optimize a single multiply with constant into two
8044/// in order to implement it with two cheaper instructions, e.g.
8045/// LEA + SHL, LEA + LEA.
8046static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8047 TargetLowering::DAGCombinerInfo &DCI) {
8048 if (DAG.getMachineFunction().
8049 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8050 return SDValue();
8051
8052 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8053 return SDValue();
8054
8055 MVT VT = N->getValueType(0);
8056 if (VT != MVT::i64)
8057 return SDValue();
8058
8059 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8060 if (!C)
8061 return SDValue();
8062 uint64_t MulAmt = C->getZExtValue();
8063 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8064 return SDValue();
8065
8066 uint64_t MulAmt1 = 0;
8067 uint64_t MulAmt2 = 0;
8068 if ((MulAmt % 9) == 0) {
8069 MulAmt1 = 9;
8070 MulAmt2 = MulAmt / 9;
8071 } else if ((MulAmt % 5) == 0) {
8072 MulAmt1 = 5;
8073 MulAmt2 = MulAmt / 5;
8074 } else if ((MulAmt % 3) == 0) {
8075 MulAmt1 = 3;
8076 MulAmt2 = MulAmt / 3;
8077 }
8078 if (MulAmt2 &&
8079 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8080 DebugLoc DL = N->getDebugLoc();
8081
8082 if (isPowerOf2_64(MulAmt2) &&
8083 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8084 // If second multiplifer is pow2, issue it first. We want the multiply by
8085 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8086 // is an add.
8087 std::swap(MulAmt1, MulAmt2);
8088
8089 SDValue NewMul;
8090 if (isPowerOf2_64(MulAmt1))
8091 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8092 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8093 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008094 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008095 DAG.getConstant(MulAmt1, VT));
8096
8097 if (isPowerOf2_64(MulAmt2))
8098 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8099 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8100 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008101 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008102 DAG.getConstant(MulAmt2, VT));
8103
8104 // Do not add new nodes to DAG combiner worklist.
8105 DCI.CombineTo(N, NewMul, false);
8106 }
8107 return SDValue();
8108}
8109
8110
Nate Begeman740ab032009-01-26 00:52:55 +00008111/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8112/// when possible.
8113static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8114 const X86Subtarget *Subtarget) {
8115 // On X86 with SSE2 support, we can transform this to a vector shift if
8116 // all elements are shifted by the same amount. We can't do this in legalize
8117 // because the a constant vector is typically transformed to a constant pool
8118 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008119 if (!Subtarget->hasSSE2())
8120 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008121
Nate Begeman740ab032009-01-26 00:52:55 +00008122 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008123 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8124 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008125
Mon P Wang3becd092009-01-28 08:12:05 +00008126 SDValue ShAmtOp = N->getOperand(1);
8127 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008128 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008129 SDValue BaseShAmt;
8130 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8131 unsigned NumElts = VT.getVectorNumElements();
8132 unsigned i = 0;
8133 for (; i != NumElts; ++i) {
8134 SDValue Arg = ShAmtOp.getOperand(i);
8135 if (Arg.getOpcode() == ISD::UNDEF) continue;
8136 BaseShAmt = Arg;
8137 break;
8138 }
8139 for (; i != NumElts; ++i) {
8140 SDValue Arg = ShAmtOp.getOperand(i);
8141 if (Arg.getOpcode() == ISD::UNDEF) continue;
8142 if (Arg != BaseShAmt) {
8143 return SDValue();
8144 }
8145 }
8146 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008147 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8148 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8149 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008150 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008151 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008152
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008153 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008154 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008155 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008156 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008157
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008158 // The shift amount is identical so we can do a vector shift.
8159 SDValue ValOp = N->getOperand(0);
8160 switch (N->getOpcode()) {
8161 default:
8162 assert(0 && "Unknown shift opcode!");
8163 break;
8164 case ISD::SHL:
8165 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008166 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008167 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8168 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008169 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008170 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008171 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8172 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008173 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008174 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008175 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8176 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008177 break;
8178 case ISD::SRA:
8179 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008180 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008181 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8182 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008183 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008184 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008185 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8186 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008187 break;
8188 case ISD::SRL:
8189 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008190 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008191 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8192 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008193 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008194 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008195 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8196 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008197 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008198 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008199 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8200 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008201 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008202 }
8203 return SDValue();
8204}
8205
Chris Lattner149a4e52008-02-22 02:09:43 +00008206/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008207static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008208 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008209 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8210 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008211 // A preferable solution to the general problem is to figure out the right
8212 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008213
8214 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008215 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008216 MVT VT = St->getValue().getValueType();
8217 if (VT.getSizeInBits() != 64)
8218 return SDValue();
8219
Devang Patel578efa92009-06-05 21:57:13 +00008220 const Function *F = DAG.getMachineFunction().getFunction();
8221 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8222 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8223 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008224 if ((VT.isVector() ||
8225 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008226 isa<LoadSDNode>(St->getValue()) &&
8227 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8228 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008229 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008230 LoadSDNode *Ld = 0;
8231 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008232 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008233 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008234 // Must be a store of a load. We currently handle two cases: the load
8235 // is a direct child, and it's under an intervening TokenFactor. It is
8236 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008237 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008238 Ld = cast<LoadSDNode>(St->getChain());
8239 else if (St->getValue().hasOneUse() &&
8240 ChainVal->getOpcode() == ISD::TokenFactor) {
8241 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008242 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008243 TokenFactorIndex = i;
8244 Ld = cast<LoadSDNode>(St->getValue());
8245 } else
8246 Ops.push_back(ChainVal->getOperand(i));
8247 }
8248 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008249
Evan Cheng536e6672009-03-12 05:59:15 +00008250 if (!Ld || !ISD::isNormalLoad(Ld))
8251 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008252
Evan Cheng536e6672009-03-12 05:59:15 +00008253 // If this is not the MMX case, i.e. we are just turning i64 load/store
8254 // into f64 load/store, avoid the transformation if there are multiple
8255 // uses of the loaded value.
8256 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8257 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008258
Evan Cheng536e6672009-03-12 05:59:15 +00008259 DebugLoc LdDL = Ld->getDebugLoc();
8260 DebugLoc StDL = N->getDebugLoc();
8261 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8262 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8263 // pair instead.
8264 if (Subtarget->is64Bit() || F64IsLegal) {
8265 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8266 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8267 Ld->getBasePtr(), Ld->getSrcValue(),
8268 Ld->getSrcValueOffset(), Ld->isVolatile(),
8269 Ld->getAlignment());
8270 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008271 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008272 Ops.push_back(NewChain);
8273 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008274 Ops.size());
8275 }
Evan Cheng536e6672009-03-12 05:59:15 +00008276 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008277 St->getSrcValue(), St->getSrcValueOffset(),
8278 St->isVolatile(), St->getAlignment());
8279 }
Evan Cheng536e6672009-03-12 05:59:15 +00008280
8281 // Otherwise, lower to two pairs of 32-bit loads / stores.
8282 SDValue LoAddr = Ld->getBasePtr();
8283 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8284 DAG.getConstant(4, MVT::i32));
8285
8286 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8287 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8288 Ld->isVolatile(), Ld->getAlignment());
8289 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8290 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8291 Ld->isVolatile(),
8292 MinAlign(Ld->getAlignment(), 4));
8293
8294 SDValue NewChain = LoLd.getValue(1);
8295 if (TokenFactorIndex != -1) {
8296 Ops.push_back(LoLd);
8297 Ops.push_back(HiLd);
8298 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8299 Ops.size());
8300 }
8301
8302 LoAddr = St->getBasePtr();
8303 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8304 DAG.getConstant(4, MVT::i32));
8305
8306 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8307 St->getSrcValue(), St->getSrcValueOffset(),
8308 St->isVolatile(), St->getAlignment());
8309 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8310 St->getSrcValue(),
8311 St->getSrcValueOffset() + 4,
8312 St->isVolatile(),
8313 MinAlign(St->getAlignment(), 4));
8314 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008315 }
Dan Gohman475871a2008-07-27 21:46:04 +00008316 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008317}
8318
Chris Lattner6cf73262008-01-25 06:14:17 +00008319/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8320/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008321static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008322 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8323 // F[X]OR(0.0, x) -> x
8324 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008325 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8326 if (C->getValueAPF().isPosZero())
8327 return N->getOperand(1);
8328 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8329 if (C->getValueAPF().isPosZero())
8330 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008331 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008332}
8333
8334/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008335static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008336 // FAND(0.0, x) -> 0.0
8337 // FAND(x, 0.0) -> 0.0
8338 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8339 if (C->getValueAPF().isPosZero())
8340 return N->getOperand(0);
8341 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8342 if (C->getValueAPF().isPosZero())
8343 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008344 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008345}
8346
Dan Gohmane5af2d32009-01-29 01:59:02 +00008347static SDValue PerformBTCombine(SDNode *N,
8348 SelectionDAG &DAG,
8349 TargetLowering::DAGCombinerInfo &DCI) {
8350 // BT ignores high bits in the bit index operand.
8351 SDValue Op1 = N->getOperand(1);
8352 if (Op1.hasOneUse()) {
8353 unsigned BitWidth = Op1.getValueSizeInBits();
8354 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8355 APInt KnownZero, KnownOne;
8356 TargetLowering::TargetLoweringOpt TLO(DAG);
8357 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8358 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8359 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8360 DCI.CommitTargetLoweringOpt(TLO);
8361 }
8362 return SDValue();
8363}
Chris Lattner83e6c992006-10-04 06:57:07 +00008364
Eli Friedman7a5e5552009-06-07 06:52:44 +00008365static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
8366 SDValue Op = N->getOperand(0);
8367 if (Op.getOpcode() == ISD::BIT_CONVERT)
8368 Op = Op.getOperand(0);
8369 MVT VT = N->getValueType(0), OpVT = Op.getValueType();
8370 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
8371 VT.getVectorElementType().getSizeInBits() ==
8372 OpVT.getVectorElementType().getSizeInBits()) {
8373 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
8374 }
8375 return SDValue();
8376}
8377
Dan Gohman475871a2008-07-27 21:46:04 +00008378SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008379 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008380 SelectionDAG &DAG = DCI.DAG;
8381 switch (N->getOpcode()) {
8382 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008383 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008384 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008385 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008386 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008387 case ISD::SHL:
8388 case ISD::SRA:
8389 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008390 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008391 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008392 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8393 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008394 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00008395 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008396 }
8397
Dan Gohman475871a2008-07-27 21:46:04 +00008398 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008399}
8400
Evan Cheng60c07e12006-07-05 22:17:51 +00008401//===----------------------------------------------------------------------===//
8402// X86 Inline Assembly Support
8403//===----------------------------------------------------------------------===//
8404
Chris Lattnerf4dff842006-07-11 02:54:03 +00008405/// getConstraintType - Given a constraint letter, return the type of
8406/// constraint it is for this target.
8407X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008408X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8409 if (Constraint.size() == 1) {
8410 switch (Constraint[0]) {
8411 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008412 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008413 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008414 case 'r':
8415 case 'R':
8416 case 'l':
8417 case 'q':
8418 case 'Q':
8419 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008420 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008421 case 'Y':
8422 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008423 case 'e':
8424 case 'Z':
8425 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008426 default:
8427 break;
8428 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008429 }
Chris Lattner4234f572007-03-25 02:14:49 +00008430 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008431}
8432
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008433/// LowerXConstraint - try to replace an X constraint, which matches anything,
8434/// with another that has more specific requirements based on the type of the
8435/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008436const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008437LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008438 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8439 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008440 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008441 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008442 return "Y";
8443 if (Subtarget->hasSSE1())
8444 return "x";
8445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008446
Chris Lattner5e764232008-04-26 23:02:14 +00008447 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008448}
8449
Chris Lattner48884cd2007-08-25 00:47:38 +00008450/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8451/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008452void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008453 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008454 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008455 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008456 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008457 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008458
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008459 switch (Constraint) {
8460 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008461 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008462 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008463 if (C->getZExtValue() <= 31) {
8464 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008465 break;
8466 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008467 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008468 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008469 case 'J':
8470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnere4935152009-06-15 04:01:39 +00008471 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
8472 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8473 break;
8474 }
8475 }
8476 return;
8477 case 'K':
8478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Evan Cheng364091e2008-09-22 23:57:37 +00008479 if (C->getZExtValue() <= 63) {
8480 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8481 break;
8482 }
8483 }
8484 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008485 case 'N':
8486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008487 if (C->getZExtValue() <= 255) {
8488 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008489 break;
8490 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008491 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008492 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008493 case 'e': {
8494 // 32-bit signed value
8495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8496 const ConstantInt *CI = C->getConstantIntValue();
8497 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8498 // Widen to 64 bits here to get it sign extended.
8499 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8500 break;
8501 }
8502 // FIXME gcc accepts some relocatable values here too, but only in certain
8503 // memory models; it's complicated.
8504 }
8505 return;
8506 }
8507 case 'Z': {
8508 // 32-bit unsigned value
8509 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8510 const ConstantInt *CI = C->getConstantIntValue();
8511 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8512 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8513 break;
8514 }
8515 }
8516 // FIXME gcc accepts some relocatable values here too, but only in certain
8517 // memory models; it's complicated.
8518 return;
8519 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008520 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008521 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008522 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008523 // Widen to 64 bits here to get it sign extended.
8524 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008525 break;
8526 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008527
Chris Lattnerdc43a882007-05-03 16:52:29 +00008528 // If we are in non-pic codegen mode, we allow the address of a global (with
8529 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008530 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008531 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008532
Chris Lattner49921962009-05-08 18:23:14 +00008533 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8534 while (1) {
8535 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8536 Offset += GA->getOffset();
8537 break;
8538 } else if (Op.getOpcode() == ISD::ADD) {
8539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8540 Offset += C->getZExtValue();
8541 Op = Op.getOperand(0);
8542 continue;
8543 }
8544 } else if (Op.getOpcode() == ISD::SUB) {
8545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8546 Offset += -C->getZExtValue();
8547 Op = Op.getOperand(0);
8548 continue;
8549 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008550 }
Chris Lattner49921962009-05-08 18:23:14 +00008551
8552 // Otherwise, this isn't something we can handle, reject it.
8553 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008554 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008555
Chris Lattner49921962009-05-08 18:23:14 +00008556 if (hasMemory)
8557 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8558 else
8559 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8560 Offset);
8561 Result = Op;
8562 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008563 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008564 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008565
Gabor Greifba36cb52008-08-28 21:40:38 +00008566 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008567 Ops.push_back(Result);
8568 return;
8569 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008570 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8571 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008572}
8573
Chris Lattner259e97c2006-01-31 19:43:35 +00008574std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008575getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008576 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008577 if (Constraint.size() == 1) {
8578 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008579 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008580 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008581 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8582 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008583 if (VT == MVT::i32)
8584 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8585 else if (VT == MVT::i16)
8586 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8587 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008588 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008589 else if (VT == MVT::i64)
8590 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8591 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008592 }
8593 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008594
Chris Lattner1efa40f2006-02-22 00:56:39 +00008595 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008596}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008597
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008598std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008599X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008600 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008601 // First, see if this is a constraint that directly corresponds to an LLVM
8602 // register class.
8603 if (Constraint.size() == 1) {
8604 // GCC Constraint Letters
8605 switch (Constraint[0]) {
8606 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008607 case 'r': // GENERAL_REGS
8608 case 'R': // LEGACY_REGS
8609 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008610 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008611 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008612 if (VT == MVT::i16)
8613 return std::make_pair(0U, X86::GR16RegisterClass);
8614 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008615 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008616 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008617 case 'f': // FP Stack registers.
8618 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8619 // value to the correct fpstack register class.
8620 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8621 return std::make_pair(0U, X86::RFP32RegisterClass);
8622 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8623 return std::make_pair(0U, X86::RFP64RegisterClass);
8624 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008625 case 'y': // MMX_REGS if MMX allowed.
8626 if (!Subtarget->hasMMX()) break;
8627 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008628 case 'Y': // SSE_REGS if SSE2 allowed
8629 if (!Subtarget->hasSSE2()) break;
8630 // FALL THROUGH.
8631 case 'x': // SSE_REGS if SSE1 allowed
8632 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008633
8634 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008635 default: break;
8636 // Scalar SSE types.
8637 case MVT::f32:
8638 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008639 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008640 case MVT::f64:
8641 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008642 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008643 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008644 case MVT::v16i8:
8645 case MVT::v8i16:
8646 case MVT::v4i32:
8647 case MVT::v2i64:
8648 case MVT::v4f32:
8649 case MVT::v2f64:
8650 return std::make_pair(0U, X86::VR128RegisterClass);
8651 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008652 break;
8653 }
8654 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008655
Chris Lattnerf76d1802006-07-31 23:26:50 +00008656 // Use the default implementation in TargetLowering to convert the register
8657 // constraint into a member of a register class.
8658 std::pair<unsigned, const TargetRegisterClass*> Res;
8659 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008660
8661 // Not found as a standard register?
8662 if (Res.second == 0) {
8663 // GCC calls "st(0)" just plain "st".
8664 if (StringsEqualNoCase("{st}", Constraint)) {
8665 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008666 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008667 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008668 // 'A' means EAX + EDX.
8669 if (Constraint == "A") {
8670 Res.first = X86::EAX;
8671 Res.second = X86::GRADRegisterClass;
8672 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008673 return Res;
8674 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008675
Chris Lattnerf76d1802006-07-31 23:26:50 +00008676 // Otherwise, check to see if this is a register class of the wrong value
8677 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8678 // turn into {ax},{dx}.
8679 if (Res.second->hasType(VT))
8680 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008681
Chris Lattnerf76d1802006-07-31 23:26:50 +00008682 // All of the single-register GCC register classes map their values onto
8683 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8684 // really want an 8-bit or 32-bit register, map to the appropriate register
8685 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008686 if (Res.second == X86::GR16RegisterClass) {
8687 if (VT == MVT::i8) {
8688 unsigned DestReg = 0;
8689 switch (Res.first) {
8690 default: break;
8691 case X86::AX: DestReg = X86::AL; break;
8692 case X86::DX: DestReg = X86::DL; break;
8693 case X86::CX: DestReg = X86::CL; break;
8694 case X86::BX: DestReg = X86::BL; break;
8695 }
8696 if (DestReg) {
8697 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008698 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008699 }
8700 } else if (VT == MVT::i32) {
8701 unsigned DestReg = 0;
8702 switch (Res.first) {
8703 default: break;
8704 case X86::AX: DestReg = X86::EAX; break;
8705 case X86::DX: DestReg = X86::EDX; break;
8706 case X86::CX: DestReg = X86::ECX; break;
8707 case X86::BX: DestReg = X86::EBX; break;
8708 case X86::SI: DestReg = X86::ESI; break;
8709 case X86::DI: DestReg = X86::EDI; break;
8710 case X86::BP: DestReg = X86::EBP; break;
8711 case X86::SP: DestReg = X86::ESP; break;
8712 }
8713 if (DestReg) {
8714 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008715 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008716 }
8717 } else if (VT == MVT::i64) {
8718 unsigned DestReg = 0;
8719 switch (Res.first) {
8720 default: break;
8721 case X86::AX: DestReg = X86::RAX; break;
8722 case X86::DX: DestReg = X86::RDX; break;
8723 case X86::CX: DestReg = X86::RCX; break;
8724 case X86::BX: DestReg = X86::RBX; break;
8725 case X86::SI: DestReg = X86::RSI; break;
8726 case X86::DI: DestReg = X86::RDI; break;
8727 case X86::BP: DestReg = X86::RBP; break;
8728 case X86::SP: DestReg = X86::RSP; break;
8729 }
8730 if (DestReg) {
8731 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008732 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008733 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008734 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008735 } else if (Res.second == X86::FR32RegisterClass ||
8736 Res.second == X86::FR64RegisterClass ||
8737 Res.second == X86::VR128RegisterClass) {
8738 // Handle references to XMM physical registers that got mapped into the
8739 // wrong class. This can happen with constraints like {xmm0} where the
8740 // target independent register mapper will just pick the first match it can
8741 // find, ignoring the required type.
8742 if (VT == MVT::f32)
8743 Res.second = X86::FR32RegisterClass;
8744 else if (VT == MVT::f64)
8745 Res.second = X86::FR64RegisterClass;
8746 else if (X86::VR128RegisterClass->hasType(VT))
8747 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008748 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008749
Chris Lattnerf76d1802006-07-31 23:26:50 +00008750 return Res;
8751}
Mon P Wang0c397192008-10-30 08:01:45 +00008752
8753//===----------------------------------------------------------------------===//
8754// X86 Widen vector type
8755//===----------------------------------------------------------------------===//
8756
8757/// getWidenVectorType: given a vector type, returns the type to widen
8758/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8759/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008760/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008761/// scalarizing vs using the wider vector type.
8762
Dan Gohmanc13cf132009-01-15 17:34:08 +00008763MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008764 assert(VT.isVector());
8765 if (isTypeLegal(VT))
8766 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008767
Mon P Wang0c397192008-10-30 08:01:45 +00008768 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8769 // type based on element type. This would speed up our search (though
8770 // it may not be worth it since the size of the list is relatively
8771 // small).
8772 MVT EltVT = VT.getVectorElementType();
8773 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008774
Mon P Wang0c397192008-10-30 08:01:45 +00008775 // On X86, it make sense to widen any vector wider than 1
8776 if (NElts <= 1)
8777 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008778
8779 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008780 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8781 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008782
8783 if (isTypeLegal(SVT) &&
8784 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008785 SVT.getVectorNumElements() > NElts)
8786 return SVT;
8787 }
8788 return MVT::Other;
8789}