blob: b449c7303ea6b73eda31777055f14c555c3e539b [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);
Evan Chengd30bf012006-03-01 01:11:20 +0000553 }
554
Evan Chengc7ce29b2009-02-13 22:36:38 +0000555 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
556 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000557 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000558 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
559 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
560 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000561 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000562 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000563
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000564 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
565 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
566 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000567 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000568
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000569 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
570 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
571 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000572 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000573
Bill Wendling74027e92007-03-15 21:24:36 +0000574 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
575 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
576
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000577 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000578 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000579 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000580 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
581 setOperationAction(ISD::AND, MVT::v2i32, Promote);
582 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
583 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000584
585 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000586 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000587 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000588 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
589 setOperationAction(ISD::OR, MVT::v2i32, Promote);
590 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
591 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000592
593 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000594 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000595 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000596 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
597 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
598 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
599 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000600
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000601 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000602 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000603 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000604 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
605 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
606 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000607 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
608 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000609 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000610
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000611 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
612 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
613 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000614 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000615 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000616
617 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
618 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000621
Evan Cheng52672b82008-07-22 18:39:19 +0000622 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000623 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
624 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000625 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000626
627 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000628
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000629 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000630 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
631 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
632 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
633 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
634 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000635 }
636
Evan Cheng92722532009-03-26 23:06:32 +0000637 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000638 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
639
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000640 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
641 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
642 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
643 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000644 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
645 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000646 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000649 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000650 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000651 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000652 }
653
Evan Cheng92722532009-03-26 23:06:32 +0000654 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000655 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000657 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
658 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000659 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
660 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
661 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
662 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
663
Evan Chengf7c378e2006-04-10 07:23:14 +0000664 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
665 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
666 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000667 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000668 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000669 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
670 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
671 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000672 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000673 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000674 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
675 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
676 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
677 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000678 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
679 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000680
Nate Begeman30a0de92008-07-17 16:51:19 +0000681 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
682 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
683 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
684 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000685
Evan Chengf7c378e2006-04-10 07:23:14 +0000686 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
687 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000689 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000690 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000691
Evan Cheng2c3ae372006-04-12 21:21:57 +0000692 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000693 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
694 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000695 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000696 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000697 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000698 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
699 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000701 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000702
Evan Cheng2c3ae372006-04-12 21:21:57 +0000703 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
704 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
705 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
706 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000707 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000708 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000709
Nate Begemancdd1eec2008-02-12 22:51:28 +0000710 if (Subtarget->is64Bit()) {
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000713 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000714
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000715 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000716 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000717 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
718 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
719 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
720 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
721 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
722 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
723 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
724 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
725 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
726 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000727 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000728
Chris Lattnerddf89562008-01-17 19:59:44 +0000729 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000730
Evan Cheng2c3ae372006-04-12 21:21:57 +0000731 // Custom lower v2i64 and v2f64 selects.
732 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000733 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000734 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000735 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000736
Evan Cheng470a6ad2006-02-22 02:26:30 +0000737 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000738
Nate Begeman14d12ca2008-02-11 04:19:36 +0000739 if (Subtarget->hasSSE41()) {
740 // FIXME: Do we need to handle scalar-to-vector here?
741 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
742
743 // i8 and i16 vectors are custom , because the source register and source
744 // source memory operand types are not the same width. f32 vectors are
745 // custom since the immediate controlling the insert encodes additional
746 // information.
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000749 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000750 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
751
752 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
753 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000754 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000755 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000756
757 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
759 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000760 }
761 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000762
Nate Begeman30a0de92008-07-17 16:51:19 +0000763 if (Subtarget->hasSSE42()) {
764 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
765 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000766
Evan Cheng6be2c582006-04-05 23:38:46 +0000767 // We want to custom lower some of our intrinsics.
768 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
769
Bill Wendling74c37652008-12-09 22:08:41 +0000770 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000771 setOperationAction(ISD::SADDO, MVT::i32, Custom);
772 setOperationAction(ISD::SADDO, MVT::i64, Custom);
773 setOperationAction(ISD::UADDO, MVT::i32, Custom);
774 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000775 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
776 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
777 setOperationAction(ISD::USUBO, MVT::i32, Custom);
778 setOperationAction(ISD::USUBO, MVT::i64, Custom);
779 setOperationAction(ISD::SMULO, MVT::i32, Custom);
780 setOperationAction(ISD::SMULO, MVT::i64, Custom);
781 setOperationAction(ISD::UMULO, MVT::i32, Custom);
782 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000783
Evan Chengd54f2d52009-03-31 19:38:51 +0000784 if (!Subtarget->is64Bit()) {
785 // These libcalls are not available in 32-bit.
786 setLibcallName(RTLIB::SHL_I128, 0);
787 setLibcallName(RTLIB::SRL_I128, 0);
788 setLibcallName(RTLIB::SRA_I128, 0);
789 }
790
Evan Cheng206ee9d2006-07-07 08:33:52 +0000791 // We have target-specific dag combine patterns for the following nodes:
792 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000793 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000794 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000795 setTargetDAGCombine(ISD::SHL);
796 setTargetDAGCombine(ISD::SRA);
797 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000798 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000799 if (Subtarget->is64Bit())
800 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000801
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000802 computeRegisterProperties();
803
Evan Cheng87ed7162006-02-14 08:25:08 +0000804 // FIXME: These should be based on subtarget info. Plus, the values should
805 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000806 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
807 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
808 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000809 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000810 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000811 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000812}
813
Scott Michel5b8f82e2008-03-10 15:42:14 +0000814
Duncan Sands5480c042009-01-01 15:52:00 +0000815MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000816 return MVT::i8;
817}
818
819
Evan Cheng29286502008-01-23 23:17:41 +0000820/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
821/// the desired ByVal argument alignment.
822static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
823 if (MaxAlign == 16)
824 return;
825 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
826 if (VTy->getBitWidth() == 128)
827 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000828 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
829 unsigned EltAlign = 0;
830 getMaxByValAlign(ATy->getElementType(), EltAlign);
831 if (EltAlign > MaxAlign)
832 MaxAlign = EltAlign;
833 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
834 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
835 unsigned EltAlign = 0;
836 getMaxByValAlign(STy->getElementType(i), EltAlign);
837 if (EltAlign > MaxAlign)
838 MaxAlign = EltAlign;
839 if (MaxAlign == 16)
840 break;
841 }
842 }
843 return;
844}
845
846/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
847/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000848/// that contain SSE vectors are placed at 16-byte boundaries while the rest
849/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000850unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000851 if (Subtarget->is64Bit()) {
852 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000853 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000854 if (TyAlign > 8)
855 return TyAlign;
856 return 8;
857 }
858
Evan Cheng29286502008-01-23 23:17:41 +0000859 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000860 if (Subtarget->hasSSE1())
861 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000862 return Align;
863}
Chris Lattner2b02a442007-02-25 08:29:00 +0000864
Evan Chengf0df0312008-05-15 08:39:06 +0000865/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000866/// and store operations as a result of memset, memcpy, and memmove
867/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000868/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000869MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000870X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +0000871 bool isSrcConst, bool isSrcStr,
872 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000873 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
874 // linux. This is because the stack realignment code can't handle certain
875 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +0000876 const Function *F = DAG.getMachineFunction().getFunction();
877 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
878 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000879 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
880 return MVT::v4i32;
881 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
882 return MVT::v4f32;
883 }
Evan Chengf0df0312008-05-15 08:39:06 +0000884 if (Subtarget->is64Bit() && Size >= 8)
885 return MVT::i64;
886 return MVT::i32;
887}
888
Evan Chengcc415862007-11-09 01:32:10 +0000889/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
890/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000891SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000892 SelectionDAG &DAG) const {
893 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000894 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000895 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000896 // This doesn't have DebugLoc associated with it, but is not really the
897 // same as a Register.
898 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
899 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000900 return Table;
901}
902
Chris Lattner2b02a442007-02-25 08:29:00 +0000903//===----------------------------------------------------------------------===//
904// Return Value Calling Convention Implementation
905//===----------------------------------------------------------------------===//
906
Chris Lattner59ed56b2007-02-28 04:55:35 +0000907#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000908
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000909/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000910SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000911 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000912 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000913
Chris Lattner9774c912007-02-27 05:28:59 +0000914 SmallVector<CCValAssign, 16> RVLocs;
915 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000916 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
917 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000918 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000919
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000920 // If this is the first return lowered for this function, add the regs to the
921 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000922 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000923 for (unsigned i = 0; i != RVLocs.size(); ++i)
924 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000925 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000926 }
Dan Gohman475871a2008-07-27 21:46:04 +0000927 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000928
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000929 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000930 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000931 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000932 SDValue TailCall = Chain;
933 SDValue TargetAddress = TailCall.getOperand(1);
934 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000935 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000936 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000937 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000938 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000939 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000940 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000941 assert(StackAdjustment.getOpcode() == ISD::Constant &&
942 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000943
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000945 Operands.push_back(Chain.getOperand(0));
946 Operands.push_back(TargetAddress);
947 Operands.push_back(StackAdjustment);
948 // Copy registers used by the call. Last operand is a flag so it is not
949 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000950 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000951 Operands.push_back(Chain.getOperand(i));
952 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000953 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000954 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000955 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000956
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000957 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000958 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000959
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000961 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
962 // Operand #1 = Bytes To Pop
963 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000965 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000966 for (unsigned i = 0; i != RVLocs.size(); ++i) {
967 CCValAssign &VA = RVLocs[i];
968 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000969 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000970
Chris Lattner447ff682008-03-11 03:23:40 +0000971 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
972 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000973 if (VA.getLocReg() == X86::ST0 ||
974 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000975 // If this is a copy from an xmm register to ST(0), use an FPExtend to
976 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000977 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000978 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000979 RetOps.push_back(ValToCopy);
980 // Don't emit a copytoreg.
981 continue;
982 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000983
Evan Cheng242b38b2009-02-23 09:03:22 +0000984 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
985 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +0000986 if (Subtarget->is64Bit()) {
987 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +0000988 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +0000989 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +0000990 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
991 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
992 }
Evan Cheng6140a8b2009-02-22 08:05:12 +0000993 }
994
Dale Johannesendd64c412009-02-04 00:33:20 +0000995 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000996 Flag = Chain.getValue(1);
997 }
Dan Gohman61a92132008-04-21 23:59:07 +0000998
999 // The x86-64 ABI for returning structs by value requires that we copy
1000 // the sret argument into %rax for the return. We saved the argument into
1001 // a virtual register in the entry block, so now we copy the value out
1002 // and into %rax.
1003 if (Subtarget->is64Bit() &&
1004 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1005 MachineFunction &MF = DAG.getMachineFunction();
1006 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1007 unsigned Reg = FuncInfo->getSRetReturnReg();
1008 if (!Reg) {
1009 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1010 FuncInfo->setSRetReturnReg(Reg);
1011 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001012 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001013
Dale Johannesendd64c412009-02-04 00:33:20 +00001014 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001015 Flag = Chain.getValue(1);
1016 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001017
Chris Lattner447ff682008-03-11 03:23:40 +00001018 RetOps[0] = Chain; // Update chain.
1019
1020 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001022 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001023
1024 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001025 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001026}
1027
1028
Chris Lattner3085e152007-02-25 08:59:22 +00001029/// LowerCallResult - Lower the result values of an ISD::CALL into the
1030/// appropriate copies out of appropriate physical registers. This assumes that
1031/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1032/// being lowered. The returns a SDNode with the same number of values as the
1033/// ISD::CALL.
1034SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001035LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001036 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001037
Scott Michelfdc40a02009-02-17 22:15:04 +00001038 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001039 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001040 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001041 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001042 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001043 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001044 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1045
Dan Gohman475871a2008-07-27 21:46:04 +00001046 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001047
Chris Lattner3085e152007-02-25 08:59:22 +00001048 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001049 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001050 CCValAssign &VA = RVLocs[i];
1051 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Torok Edwin3f142c32009-02-01 18:15:56 +00001053 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001054 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001055 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1056 cerr << "SSE register return with SSE disabled\n";
1057 exit(1);
1058 }
1059
Chris Lattner8e6da152008-03-10 21:08:41 +00001060 // If this is a call to a function that returns an fp value on the floating
1061 // point stack, but where we prefer to use the value in xmm registers, copy
1062 // 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 +00001063 if ((VA.getLocReg() == X86::ST0 ||
1064 VA.getLocReg() == X86::ST1) &&
1065 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001066 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001068
Evan Cheng79fb3b42009-02-20 20:43:02 +00001069 SDValue Val;
1070 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001071 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1072 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1073 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1074 MVT::v2i64, InFlag).getValue(1);
1075 Val = Chain.getValue(0);
1076 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1077 Val, DAG.getConstant(0, MVT::i64));
1078 } else {
1079 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1080 MVT::i64, InFlag).getValue(1);
1081 Val = Chain.getValue(0);
1082 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001083 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1084 } else {
1085 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1086 CopyVT, InFlag).getValue(1);
1087 Val = Chain.getValue(0);
1088 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001089 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001090
Dan Gohman37eed792009-02-04 17:28:58 +00001091 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001092 // Round the F80 the right size, which also moves to the appropriate xmm
1093 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001094 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001095 // This truncation won't change the value.
1096 DAG.getIntPtrConstant(1));
1097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Chris Lattner8e6da152008-03-10 21:08:41 +00001099 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001100 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001101
Chris Lattner3085e152007-02-25 08:59:22 +00001102 // Merge everything together with a MERGE_VALUES node.
1103 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001104 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1105 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001106}
1107
1108
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001109//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001110// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001111//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001112// StdCall calling convention seems to be standard for many Windows' API
1113// routines and around. It differs from C calling convention just a little:
1114// callee should clean up the stack, not caller. Symbols should be also
1115// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001116// For info on fast calling convention see Fast Calling Convention (tail call)
1117// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001119/// CallIsStructReturn - Determines whether a CALL node uses struct return
1120/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001121static bool CallIsStructReturn(CallSDNode *TheCall) {
1122 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001123 if (!NumOps)
1124 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001125
Dan Gohman095cc292008-09-13 01:54:27 +00001126 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001127}
1128
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001129/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1130/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001131static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001132 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001133 if (!NumArgs)
1134 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001135
1136 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001137}
1138
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001139/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1140/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001141/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001142bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001143 if (IsVarArg)
1144 return false;
1145
Dan Gohman095cc292008-09-13 01:54:27 +00001146 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001147 default:
1148 return false;
1149 case CallingConv::X86_StdCall:
1150 return !Subtarget->is64Bit();
1151 case CallingConv::X86_FastCall:
1152 return !Subtarget->is64Bit();
1153 case CallingConv::Fast:
1154 return PerformTailCallOpt;
1155 }
1156}
1157
Dan Gohman095cc292008-09-13 01:54:27 +00001158/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1159/// given CallingConvention value.
1160CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001161 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001162 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001163 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001164 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1165 return CC_X86_64_TailCall;
1166 else
1167 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001168 }
1169
Gordon Henriksen86737662008-01-05 16:56:59 +00001170 if (CC == CallingConv::X86_FastCall)
1171 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001172 else if (CC == CallingConv::Fast)
1173 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001174 else
1175 return CC_X86_32_C;
1176}
1177
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001178/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1179/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001180NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001181X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001182 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001183 if (CC == CallingConv::X86_FastCall)
1184 return FastCall;
1185 else if (CC == CallingConv::X86_StdCall)
1186 return StdCall;
1187 return None;
1188}
1189
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001190
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001191/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1192/// in a register before calling.
1193bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1194 return !IsTailCall && !Is64Bit &&
1195 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1196 Subtarget->isPICStyleGOT();
1197}
1198
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001199/// CallRequiresFnAddressInReg - Check whether the call requires the function
1200/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001201bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001202X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001203 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001204 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1205 Subtarget->isPICStyleGOT();
1206}
1207
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001208/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1209/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001210/// the specific parameter attribute. The copy will be passed as a byval
1211/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001212static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001213CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001214 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1215 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001216 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001217 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001218 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001219}
1220
Dan Gohman475871a2008-07-27 21:46:04 +00001221SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001222 const CCValAssign &VA,
1223 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001224 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001226 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001227 ISD::ArgFlagsTy Flags =
1228 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001229 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001230 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001231
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001232 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001233 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001234 // In case of tail call optimization mark all arguments mutable. Since they
1235 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001236 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001237 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001238 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001239 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001240 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001241 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001242 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001243}
1244
Dan Gohman475871a2008-07-27 21:46:04 +00001245SDValue
1246X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001247 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001248 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001249 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001250
Gordon Henriksen86737662008-01-05 16:56:59 +00001251 const Function* Fn = MF.getFunction();
1252 if (Fn->hasExternalLinkage() &&
1253 Subtarget->isTargetCygMing() &&
1254 Fn->getName() == "main")
1255 FuncInfo->setForceFramePointer(true);
1256
1257 // Decorate the function name.
1258 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001259
Evan Cheng1bc78042006-04-26 01:20:17 +00001260 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001261 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001262 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001263 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001264 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001265 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001266
1267 assert(!(isVarArg && CC == CallingConv::Fast) &&
1268 "Var args not supported with calling convention fastcc");
1269
Chris Lattner638402b2007-02-28 07:00:42 +00001270 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001271 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001272 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001273 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001274
Dan Gohman475871a2008-07-27 21:46:04 +00001275 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001276 unsigned LastVal = ~0U;
1277 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1278 CCValAssign &VA = ArgLocs[i];
1279 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1280 // places.
1281 assert(VA.getValNo() != LastVal &&
1282 "Don't support value assigned to multiple locs yet");
1283 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001284
Chris Lattnerf39f7712007-02-28 05:46:49 +00001285 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001286 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001287 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001288 if (RegVT == MVT::i32)
1289 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001290 else if (Is64Bit && RegVT == MVT::i64)
1291 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001292 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001293 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001294 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001295 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001296 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001297 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001298 else if (RegVT.isVector()) {
1299 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001300 if (!Is64Bit)
1301 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1302 else {
1303 // Darwin calling convention passes MMX values in either GPRs or
1304 // XMMs in x86-64. Other targets pass them in memory.
1305 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1306 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1307 RegVT = MVT::v2i64;
1308 } else {
1309 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1310 RegVT = MVT::i64;
1311 }
1312 }
1313 } else {
1314 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001315 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001316
Bob Wilson998e1252009-04-20 18:36:57 +00001317 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001318 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001319
Chris Lattnerf39f7712007-02-28 05:46:49 +00001320 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1321 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1322 // right size.
1323 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001324 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001325 DAG.getValueType(VA.getValVT()));
1326 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001327 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001328 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001329
Chris Lattnerf39f7712007-02-28 05:46:49 +00001330 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001331 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001332
Gordon Henriksen86737662008-01-05 16:56:59 +00001333 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001334 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001335 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001336 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001337 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001338 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1339 ArgValue, DAG.getConstant(0, MVT::i64));
1340 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001341 }
1342 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001343
Chris Lattnerf39f7712007-02-28 05:46:49 +00001344 ArgValues.push_back(ArgValue);
1345 } else {
1346 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001347 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001348 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001349 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001350
Dan Gohman61a92132008-04-21 23:59:07 +00001351 // The x86-64 ABI for returning structs by value requires that we copy
1352 // the sret argument into %rax for the return. Save the argument into
1353 // a virtual register so that we can access it from the return points.
1354 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1355 MachineFunction &MF = DAG.getMachineFunction();
1356 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1357 unsigned Reg = FuncInfo->getSRetReturnReg();
1358 if (!Reg) {
1359 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1360 FuncInfo->setSRetReturnReg(Reg);
1361 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001362 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001363 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001364 }
1365
Chris Lattnerf39f7712007-02-28 05:46:49 +00001366 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001367 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001368 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001369 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001370
Evan Cheng1bc78042006-04-26 01:20:17 +00001371 // If the function takes variable number of arguments, make a frame index for
1372 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001373 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001374 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1375 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1376 }
1377 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001378 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1379
1380 // FIXME: We should really autogenerate these arrays
1381 static const unsigned GPR64ArgRegsWin64[] = {
1382 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001383 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001384 static const unsigned XMMArgRegsWin64[] = {
1385 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1386 };
1387 static const unsigned GPR64ArgRegs64Bit[] = {
1388 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1389 };
1390 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001391 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1392 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1393 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001394 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1395
1396 if (IsWin64) {
1397 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1398 GPR64ArgRegs = GPR64ArgRegsWin64;
1399 XMMArgRegs = XMMArgRegsWin64;
1400 } else {
1401 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1402 GPR64ArgRegs = GPR64ArgRegs64Bit;
1403 XMMArgRegs = XMMArgRegs64Bit;
1404 }
1405 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1406 TotalNumIntRegs);
1407 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1408 TotalNumXMMRegs);
1409
Devang Patel578efa92009-06-05 21:57:13 +00001410 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001411 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001412 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001413 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001414 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001415 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001416 // Kernel mode asks for SSE to be disabled, so don't push them
1417 // on the stack.
1418 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001419
Gordon Henriksen86737662008-01-05 16:56:59 +00001420 // For X86-64, if there are vararg parameters that are passed via
1421 // registers, then we must store them to their spots on the stack so they
1422 // may be loaded by deferencing the result of va_next.
1423 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001424 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1425 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1426 TotalNumXMMRegs * 16, 16);
1427
Gordon Henriksen86737662008-01-05 16:56:59 +00001428 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001429 SmallVector<SDValue, 8> MemOps;
1430 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001431 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001432 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001433 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001434 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1435 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001436 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001437 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001438 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001439 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001440 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001441 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001442 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001443 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001444
Gordon Henriksen86737662008-01-05 16:56:59 +00001445 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001446 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001447 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001448 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001449 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1450 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001451 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001452 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001453 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001454 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001455 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001456 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001457 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001458 }
1459 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001460 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001461 &MemOps[0], MemOps.size());
1462 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001463 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001464
Gordon Henriksenae636f82008-01-03 16:47:34 +00001465 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001466
Gordon Henriksen86737662008-01-05 16:56:59 +00001467 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001468 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001469 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001470 BytesCallerReserves = 0;
1471 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001472 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001473 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001474 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001475 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001476 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001477 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001478
Gordon Henriksen86737662008-01-05 16:56:59 +00001479 if (!Is64Bit) {
1480 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1481 if (CC == CallingConv::X86_FastCall)
1482 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1483 }
Evan Cheng25caf632006-05-23 21:06:34 +00001484
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001485 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001486
Evan Cheng25caf632006-05-23 21:06:34 +00001487 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001488 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001489 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001490}
1491
Dan Gohman475871a2008-07-27 21:46:04 +00001492SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001493X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001494 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001495 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001496 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001497 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001498 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001499 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001500 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001501 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001502 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001503 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001504 }
Dale Johannesenace16102009-02-03 19:33:06 +00001505 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001506 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001507}
1508
Bill Wendling64e87322009-01-16 19:25:27 +00001509/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001510/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001511SDValue
1512X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001513 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001514 SDValue Chain,
1515 bool IsTailCall,
1516 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001517 int FPDiff,
1518 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001519 if (!IsTailCall || FPDiff==0) return Chain;
1520
1521 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001522 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001523 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001524
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001525 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001526 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001528}
1529
1530/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1531/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001532static SDValue
1533EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001534 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001535 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001536 // Store the return address to the appropriate stack slot.
1537 if (!FPDiff) return Chain;
1538 // Calculate the new stack slot for the return address.
1539 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001540 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001541 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001542 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001543 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001545 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001546 return Chain;
1547}
1548
Dan Gohman475871a2008-07-27 21:46:04 +00001549SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001550 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001551 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1552 SDValue Chain = TheCall->getChain();
1553 unsigned CC = TheCall->getCallingConv();
1554 bool isVarArg = TheCall->isVarArg();
1555 bool IsTailCall = TheCall->isTailCall() &&
1556 CC == CallingConv::Fast && PerformTailCallOpt;
1557 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001558 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001559 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001560 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001561
1562 assert(!(isVarArg && CC == CallingConv::Fast) &&
1563 "Var args not supported with calling convention fastcc");
1564
Chris Lattner638402b2007-02-28 07:00:42 +00001565 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001566 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001567 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001568 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Chris Lattner423c5f42007-02-28 05:31:48 +00001570 // Get a count of how many bytes are to be pushed on the stack.
1571 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001572 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001573 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001574
Gordon Henriksen86737662008-01-05 16:56:59 +00001575 int FPDiff = 0;
1576 if (IsTailCall) {
1577 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001578 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001579 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1580 FPDiff = NumBytesCallerPushed - NumBytes;
1581
1582 // Set the delta of movement of the returnaddr stackslot.
1583 // But only set if delta is greater than previous delta.
1584 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1585 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1586 }
1587
Chris Lattnere563bbc2008-10-11 22:08:30 +00001588 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001589
Dan Gohman475871a2008-07-27 21:46:04 +00001590 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001591 // Load return adress for tail calls.
1592 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001593 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001594
Dan Gohman475871a2008-07-27 21:46:04 +00001595 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1596 SmallVector<SDValue, 8> MemOpChains;
1597 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001598
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001599 // Walk the register/memloc assignments, inserting copies/loads. In the case
1600 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001601 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1602 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001603 SDValue Arg = TheCall->getArg(i);
1604 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1605 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Chris Lattner423c5f42007-02-28 05:31:48 +00001607 // Promote the value if needed.
1608 switch (VA.getLocInfo()) {
1609 default: assert(0 && "Unknown loc info!");
1610 case CCValAssign::Full: break;
1611 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001612 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001613 break;
1614 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001615 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001616 break;
1617 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001618 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001619 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001620 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001621
Chris Lattner423c5f42007-02-28 05:31:48 +00001622 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001623 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001624 MVT RegVT = VA.getLocVT();
1625 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001626 switch (VA.getLocReg()) {
1627 default:
1628 break;
1629 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1630 case X86::R8: {
1631 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001632 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001633 break;
1634 }
1635 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1636 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1637 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001638 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1639 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001640 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001641 break;
1642 }
1643 }
1644 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001645 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1646 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001647 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001648 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001649 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001650 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001651
Dan Gohman095cc292008-09-13 01:54:27 +00001652 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1653 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001654 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001655 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001656 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001657
Evan Cheng32fe1032006-05-25 00:59:30 +00001658 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001659 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001660 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001661
Evan Cheng347d5f72006-04-28 21:29:37 +00001662 // Build a sequence of copy-to-reg nodes chained together with token chain
1663 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001664 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001665 // Tail call byval lowering might overwrite argument registers so in case of
1666 // tail call optimization the copies to registers are lowered later.
1667 if (!IsTailCall)
1668 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001669 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001670 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001671 InFlag = Chain.getValue(1);
1672 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001673
Evan Chengf4684712007-02-21 21:18:14 +00001674 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001675 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001676 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001677 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001678 DAG.getNode(X86ISD::GlobalBaseReg,
1679 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001680 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001681 InFlag);
1682 InFlag = Chain.getValue(1);
1683 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001684 // If we are tail calling and generating PIC/GOT style code load the address
1685 // of the callee into ecx. The value in ecx is used as target of the tail
1686 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1687 // calls on PIC/GOT architectures. Normally we would just put the address of
1688 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1689 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001690 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001691 // Note: The actual moving to ecx is done further down.
1692 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001693 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001694 !G->getGlobal()->hasProtectedVisibility())
1695 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001696 else if (isa<ExternalSymbolSDNode>(Callee))
1697 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001698 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001699
Gordon Henriksen86737662008-01-05 16:56:59 +00001700 if (Is64Bit && isVarArg) {
1701 // From AMD64 ABI document:
1702 // For calls that may call functions that use varargs or stdargs
1703 // (prototype-less calls or calls to functions containing ellipsis (...) in
1704 // the declaration) %al is used as hidden argument to specify the number
1705 // of SSE registers used. The contents of %al do not need to match exactly
1706 // the number of registers, but must be an ubound on the number of SSE
1707 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001708
1709 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 // Count the number of XMM registers allocated.
1711 static const unsigned XMMArgRegs[] = {
1712 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1713 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1714 };
1715 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001716 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001717 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Dale Johannesendd64c412009-02-04 00:33:20 +00001719 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1721 InFlag = Chain.getValue(1);
1722 }
1723
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001724
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001725 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001726 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001727 SmallVector<SDValue, 8> MemOpChains2;
1728 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001730 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001731 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1733 CCValAssign &VA = ArgLocs[i];
1734 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001735 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001736 SDValue Arg = TheCall->getArg(i);
1737 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001738 // Create frame index.
1739 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001740 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001741 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001742 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001743
Duncan Sands276dcbd2008-03-21 09:14:45 +00001744 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001745 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001746 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001747 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001748 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001749 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001750 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001751
1752 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001753 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001754 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001755 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001756 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001757 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001758 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001759 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001760 }
1761 }
1762
1763 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001764 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001765 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001766
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001767 // Copy arguments to their registers.
1768 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001769 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001770 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001771 InFlag = Chain.getValue(1);
1772 }
Dan Gohman475871a2008-07-27 21:46:04 +00001773 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001774
Gordon Henriksen86737662008-01-05 16:56:59 +00001775 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001776 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001777 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001778 }
1779
Evan Cheng32fe1032006-05-25 00:59:30 +00001780 // If the callee is a GlobalAddress node (quite common, every direct call is)
1781 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001782 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001783 // We should use extra load for direct calls to dllimported functions in
1784 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001785 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1786 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001787 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1788 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001789 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1790 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001792 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001793
Dale Johannesendd64c412009-02-04 00:33:20 +00001794 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001795 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001796 Callee,InFlag);
1797 Callee = DAG.getRegister(Opc, getPointerTy());
1798 // Add register as live out.
1799 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001800 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001801
Chris Lattnerd96d0722007-02-25 06:40:16 +00001802 // Returns a chain & a flag for retval copy to use.
1803 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001804 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001805
1806 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001807 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1808 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001810
Gordon Henriksen86737662008-01-05 16:56:59 +00001811 // Returns a chain & a flag for retval copy to use.
1812 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1813 Ops.clear();
1814 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001815
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001816 Ops.push_back(Chain);
1817 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001818
Gordon Henriksen86737662008-01-05 16:56:59 +00001819 if (IsTailCall)
1820 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001821
Gordon Henriksen86737662008-01-05 16:56:59 +00001822 // Add argument registers to the end of the list so that they are known live
1823 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001824 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1825 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1826 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001827
Evan Cheng586ccac2008-03-18 23:36:35 +00001828 // Add an implicit use GOT pointer in EBX.
1829 if (!IsTailCall && !Is64Bit &&
1830 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1831 Subtarget->isPICStyleGOT())
1832 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1833
1834 // Add an implicit use of AL for x86 vararg functions.
1835 if (Is64Bit && isVarArg)
1836 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1837
Gabor Greifba36cb52008-08-28 21:40:38 +00001838 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001839 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001840
Gordon Henriksen86737662008-01-05 16:56:59 +00001841 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001842 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001844 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001845 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001846
Gabor Greifba36cb52008-08-28 21:40:38 +00001847 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001848 }
1849
Dale Johannesenace16102009-02-03 19:33:06 +00001850 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001851 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001852
Chris Lattner2d297092006-05-23 18:50:38 +00001853 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001855 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001857 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001858 // If this is is a call to a struct-return function, the callee
1859 // pops the hidden struct pointer, so we have to push it back.
1860 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001863 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001864
Gordon Henriksenae636f82008-01-03 16:47:34 +00001865 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001866 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001867 DAG.getIntPtrConstant(NumBytes, true),
1868 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1869 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001870 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001871 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001872
Chris Lattner3085e152007-02-25 08:59:22 +00001873 // Handle result values, copying them out of physregs into vregs that we
1874 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001875 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001876 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001877}
1878
Evan Cheng25ab6902006-09-08 06:48:29 +00001879
1880//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001881// Fast Calling Convention (tail call) implementation
1882//===----------------------------------------------------------------------===//
1883
1884// Like std call, callee cleans arguments, convention except that ECX is
1885// reserved for storing the tail called function address. Only 2 registers are
1886// free for argument passing (inreg). Tail call optimization is performed
1887// provided:
1888// * tailcallopt is enabled
1889// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001890// On X86_64 architecture with GOT-style position independent code only local
1891// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001892// To keep the stack aligned according to platform abi the function
1893// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1894// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001895// If a tail called function callee has more arguments than the caller the
1896// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001897// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001898// original REtADDR, but before the saved framepointer or the spilled registers
1899// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1900// stack layout:
1901// arg1
1902// arg2
1903// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001904// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001905// move area ]
1906// (possible EBP)
1907// ESI
1908// EDI
1909// local1 ..
1910
1911/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1912/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001913unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001914 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001915 MachineFunction &MF = DAG.getMachineFunction();
1916 const TargetMachine &TM = MF.getTarget();
1917 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1918 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001919 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001920 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001921 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001922 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1923 // Number smaller than 12 so just add the difference.
1924 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1925 } else {
1926 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001927 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001928 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001929 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001930 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001931}
1932
1933/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001934/// following the call is a return. A function is eligible if caller/callee
1935/// calling conventions match, currently only fastcc supports tail calls, and
1936/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001937bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001938 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001939 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001940 if (!PerformTailCallOpt)
1941 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001942
Dan Gohman095cc292008-09-13 01:54:27 +00001943 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001944 MachineFunction &MF = DAG.getMachineFunction();
1945 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001946 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001947 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001948 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001949 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001950 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001951 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001952 return true;
1953
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001954 // Can only do local tail calls (in same module, hidden or protected) on
1955 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001956 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1957 return G->getGlobal()->hasHiddenVisibility()
1958 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001959 }
1960 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001961
1962 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001963}
1964
Dan Gohman3df24e62008-09-03 23:12:08 +00001965FastISel *
1966X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001967 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001968 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001969 DenseMap<const Value *, unsigned> &vm,
1970 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001971 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001972 DenseMap<const AllocaInst *, int> &am
1973#ifndef NDEBUG
1974 , SmallSet<Instruction*, 8> &cil
1975#endif
1976 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001977 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001978#ifndef NDEBUG
1979 , cil
1980#endif
1981 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001982}
1983
1984
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001985//===----------------------------------------------------------------------===//
1986// Other Lowering Hooks
1987//===----------------------------------------------------------------------===//
1988
1989
Dan Gohman475871a2008-07-27 21:46:04 +00001990SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001991 MachineFunction &MF = DAG.getMachineFunction();
1992 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1993 int ReturnAddrIndex = FuncInfo->getRAIndex();
1994
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001995 if (ReturnAddrIndex == 0) {
1996 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00001997 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001998 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001999 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002000 }
2001
Evan Cheng25ab6902006-09-08 06:48:29 +00002002 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002003}
2004
2005
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002006/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2007/// specific condition code, returning the condition code and the LHS/RHS of the
2008/// comparison to make.
2009static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2010 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002011 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002012 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2013 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2014 // X > -1 -> X == 0, jump !sign.
2015 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002016 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002017 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2018 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002019 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002020 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002021 // X < 1 -> X <= 0
2022 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002023 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002024 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002025 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002026
Evan Chengd9558e02006-01-06 00:43:03 +00002027 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002028 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002029 case ISD::SETEQ: return X86::COND_E;
2030 case ISD::SETGT: return X86::COND_G;
2031 case ISD::SETGE: return X86::COND_GE;
2032 case ISD::SETLT: return X86::COND_L;
2033 case ISD::SETLE: return X86::COND_LE;
2034 case ISD::SETNE: return X86::COND_NE;
2035 case ISD::SETULT: return X86::COND_B;
2036 case ISD::SETUGT: return X86::COND_A;
2037 case ISD::SETULE: return X86::COND_BE;
2038 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002039 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002040 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002041
Chris Lattner4c78e022008-12-23 23:42:27 +00002042 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002043
Chris Lattner4c78e022008-12-23 23:42:27 +00002044 // If LHS is a foldable load, but RHS is not, flip the condition.
2045 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2046 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2047 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2048 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002049 }
2050
Chris Lattner4c78e022008-12-23 23:42:27 +00002051 switch (SetCCOpcode) {
2052 default: break;
2053 case ISD::SETOLT:
2054 case ISD::SETOLE:
2055 case ISD::SETUGT:
2056 case ISD::SETUGE:
2057 std::swap(LHS, RHS);
2058 break;
2059 }
2060
2061 // On a floating point condition, the flags are set as follows:
2062 // ZF PF CF op
2063 // 0 | 0 | 0 | X > Y
2064 // 0 | 0 | 1 | X < Y
2065 // 1 | 0 | 0 | X == Y
2066 // 1 | 1 | 1 | unordered
2067 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002068 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002069 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002070 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002071 case ISD::SETOLT: // flipped
2072 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002073 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002074 case ISD::SETOLE: // flipped
2075 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002076 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002077 case ISD::SETUGT: // flipped
2078 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002079 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002080 case ISD::SETUGE: // flipped
2081 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002082 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002083 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002084 case ISD::SETNE: return X86::COND_NE;
2085 case ISD::SETUO: return X86::COND_P;
2086 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002087 }
Evan Chengd9558e02006-01-06 00:43:03 +00002088}
2089
Evan Cheng4a460802006-01-11 00:33:36 +00002090/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2091/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002092/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002093static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002094 switch (X86CC) {
2095 default:
2096 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002097 case X86::COND_B:
2098 case X86::COND_BE:
2099 case X86::COND_E:
2100 case X86::COND_P:
2101 case X86::COND_A:
2102 case X86::COND_AE:
2103 case X86::COND_NE:
2104 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002105 return true;
2106 }
2107}
2108
Nate Begeman9008ca62009-04-27 18:41:29 +00002109/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2110/// the specified range (L, H].
2111static bool isUndefOrInRange(int Val, int Low, int Hi) {
2112 return (Val < 0) || (Val >= Low && Val < Hi);
2113}
2114
2115/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2116/// specified value.
2117static bool isUndefOrEqual(int Val, int CmpVal) {
2118 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002119 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002120 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002121}
2122
Nate Begeman9008ca62009-04-27 18:41:29 +00002123/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2124/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2125/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002126static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002127 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2128 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2129 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2130 return (Mask[0] < 2 && Mask[1] < 2);
2131 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002132}
2133
Nate Begeman9008ca62009-04-27 18:41:29 +00002134bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2135 SmallVector<int, 8> M;
2136 N->getMask(M);
2137 return ::isPSHUFDMask(M, N->getValueType(0));
2138}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002139
Nate Begeman9008ca62009-04-27 18:41:29 +00002140/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2141/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002142static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002143 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002144 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002145
2146 // Lower quadword copied in order or undef.
2147 for (int i = 0; i != 4; ++i)
2148 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002149 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002150
Evan Cheng506d3df2006-03-29 23:07:14 +00002151 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002152 for (int i = 4; i != 8; ++i)
2153 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002154 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002155
Evan Cheng506d3df2006-03-29 23:07:14 +00002156 return true;
2157}
2158
Nate Begeman9008ca62009-04-27 18:41:29 +00002159bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2160 SmallVector<int, 8> M;
2161 N->getMask(M);
2162 return ::isPSHUFHWMask(M, N->getValueType(0));
2163}
Evan Cheng506d3df2006-03-29 23:07:14 +00002164
Nate Begeman9008ca62009-04-27 18:41:29 +00002165/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2166/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002167static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002168 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002169 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002170
Rafael Espindola15684b22009-04-24 12:40:33 +00002171 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002172 for (int i = 4; i != 8; ++i)
2173 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002174 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002175
Rafael Espindola15684b22009-04-24 12:40:33 +00002176 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002177 for (int i = 0; i != 4; ++i)
2178 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002179 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002180
Rafael Espindola15684b22009-04-24 12:40:33 +00002181 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002182}
2183
Nate Begeman9008ca62009-04-27 18:41:29 +00002184bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2185 SmallVector<int, 8> M;
2186 N->getMask(M);
2187 return ::isPSHUFLWMask(M, N->getValueType(0));
2188}
2189
Evan Cheng14aed5e2006-03-24 01:18:28 +00002190/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2191/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002192static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002193 int NumElems = VT.getVectorNumElements();
2194 if (NumElems != 2 && NumElems != 4)
2195 return false;
2196
2197 int Half = NumElems / 2;
2198 for (int i = 0; i < Half; ++i)
2199 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002200 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002201 for (int i = Half; i < NumElems; ++i)
2202 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002203 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002204
Evan Cheng14aed5e2006-03-24 01:18:28 +00002205 return true;
2206}
2207
Nate Begeman9008ca62009-04-27 18:41:29 +00002208bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2209 SmallVector<int, 8> M;
2210 N->getMask(M);
2211 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002212}
2213
Evan Cheng213d2cf2007-05-17 18:45:50 +00002214/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002215/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2216/// half elements to come from vector 1 (which would equal the dest.) and
2217/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002218static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002219 int NumElems = VT.getVectorNumElements();
2220
2221 if (NumElems != 2 && NumElems != 4)
2222 return false;
2223
2224 int Half = NumElems / 2;
2225 for (int i = 0; i < Half; ++i)
2226 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002227 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002228 for (int i = Half; i < NumElems; ++i)
2229 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002230 return false;
2231 return true;
2232}
2233
Nate Begeman9008ca62009-04-27 18:41:29 +00002234static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2235 SmallVector<int, 8> M;
2236 N->getMask(M);
2237 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002238}
2239
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002240/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2241/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002242bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2243 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002244 return false;
2245
Evan Cheng2064a2b2006-03-28 06:50:32 +00002246 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002247 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2248 isUndefOrEqual(N->getMaskElt(1), 7) &&
2249 isUndefOrEqual(N->getMaskElt(2), 2) &&
2250 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002251}
2252
Evan Cheng5ced1d82006-04-06 23:23:56 +00002253/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2254/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002255bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2256 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002257
Evan Cheng5ced1d82006-04-06 23:23:56 +00002258 if (NumElems != 2 && NumElems != 4)
2259 return false;
2260
Evan Chengc5cdff22006-04-07 21:53:05 +00002261 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002262 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002263 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002264
Evan Chengc5cdff22006-04-07 21:53:05 +00002265 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002266 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002267 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002268
2269 return true;
2270}
2271
2272/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002273/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2274/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002275bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2276 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002277
Evan Cheng5ced1d82006-04-06 23:23:56 +00002278 if (NumElems != 2 && NumElems != 4)
2279 return false;
2280
Evan Chengc5cdff22006-04-07 21:53:05 +00002281 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002282 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002283 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002284
Nate Begeman9008ca62009-04-27 18:41:29 +00002285 for (unsigned i = 0; i < NumElems/2; ++i)
2286 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002287 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002288
2289 return true;
2290}
2291
Nate Begeman9008ca62009-04-27 18:41:29 +00002292/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2293/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2294/// <2, 3, 2, 3>
2295bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2296 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2297
2298 if (NumElems != 4)
2299 return false;
2300
2301 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2302 isUndefOrEqual(N->getMaskElt(1), 3) &&
2303 isUndefOrEqual(N->getMaskElt(2), 2) &&
2304 isUndefOrEqual(N->getMaskElt(3), 3);
2305}
2306
Evan Cheng0038e592006-03-28 00:39:58 +00002307/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2308/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002309static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002310 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002311 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002312 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002313 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002314
2315 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2316 int BitI = Mask[i];
2317 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002318 if (!isUndefOrEqual(BitI, j))
2319 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002320 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002321 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002322 return false;
2323 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002324 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002325 return false;
2326 }
Evan Cheng0038e592006-03-28 00:39:58 +00002327 }
Evan Cheng0038e592006-03-28 00:39:58 +00002328 return true;
2329}
2330
Nate Begeman9008ca62009-04-27 18:41:29 +00002331bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2332 SmallVector<int, 8> M;
2333 N->getMask(M);
2334 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002335}
2336
Evan Cheng4fcb9222006-03-28 02:43:26 +00002337/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2338/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002339static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002340 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002341 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002342 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002343 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002344
2345 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2346 int BitI = Mask[i];
2347 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002348 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002349 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002350 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002351 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002352 return false;
2353 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002354 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002355 return false;
2356 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002357 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002358 return true;
2359}
2360
Nate Begeman9008ca62009-04-27 18:41:29 +00002361bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2362 SmallVector<int, 8> M;
2363 N->getMask(M);
2364 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002365}
2366
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002367/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2368/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2369/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002370static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002371 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002372 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002373 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002374
2375 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2376 int BitI = Mask[i];
2377 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002378 if (!isUndefOrEqual(BitI, j))
2379 return false;
2380 if (!isUndefOrEqual(BitI1, j))
2381 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002382 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002383 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002384}
2385
Nate Begeman9008ca62009-04-27 18:41:29 +00002386bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2387 SmallVector<int, 8> M;
2388 N->getMask(M);
2389 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2390}
2391
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002392/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2393/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2394/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002395static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002396 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002397 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2398 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002399
2400 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2401 int BitI = Mask[i];
2402 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002403 if (!isUndefOrEqual(BitI, j))
2404 return false;
2405 if (!isUndefOrEqual(BitI1, j))
2406 return false;
2407 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002408 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002409}
2410
Nate Begeman9008ca62009-04-27 18:41:29 +00002411bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2412 SmallVector<int, 8> M;
2413 N->getMask(M);
2414 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2415}
2416
Evan Cheng017dcc62006-04-21 01:05:10 +00002417/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2418/// specifies a shuffle of elements that is suitable for input to MOVSS,
2419/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002420static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002421 int NumElts = VT.getVectorNumElements();
Evan Cheng10762102007-12-06 22:14:22 +00002422 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002423 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002424
2425 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002426 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002427
2428 for (int i = 1; i < NumElts; ++i)
2429 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002430 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002431
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002432 return true;
2433}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002434
Nate Begeman9008ca62009-04-27 18:41:29 +00002435bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2436 SmallVector<int, 8> M;
2437 N->getMask(M);
2438 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002439}
2440
Evan Cheng017dcc62006-04-21 01:05:10 +00002441/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2442/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002443/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002444static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002445 bool V2IsSplat = false, bool V2IsUndef = false) {
2446 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002447 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002448 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002449
2450 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002451 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002452
2453 for (int i = 1; i < NumOps; ++i)
2454 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2455 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2456 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002457 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002458
Evan Cheng39623da2006-04-20 08:58:49 +00002459 return true;
2460}
2461
Nate Begeman9008ca62009-04-27 18:41:29 +00002462static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002463 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 SmallVector<int, 8> M;
2465 N->getMask(M);
2466 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002467}
2468
Evan Chengd9539472006-04-14 21:59:03 +00002469/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2470/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002471bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2472 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002473 return false;
2474
2475 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002476 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002477 int Elt = N->getMaskElt(i);
2478 if (Elt >= 0 && Elt != 1)
2479 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002480 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002481
2482 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002483 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002484 int Elt = N->getMaskElt(i);
2485 if (Elt >= 0 && Elt != 3)
2486 return false;
2487 if (Elt == 3)
2488 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002489 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002490 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002491 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002492 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002493}
2494
2495/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2496/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002497bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2498 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002499 return false;
2500
2501 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002502 for (unsigned i = 0; i < 2; ++i)
2503 if (N->getMaskElt(i) > 0)
2504 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002505
2506 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002507 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002508 int Elt = N->getMaskElt(i);
2509 if (Elt >= 0 && Elt != 2)
2510 return false;
2511 if (Elt == 2)
2512 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002513 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002514 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002515 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002516}
2517
Evan Cheng0b457f02008-09-25 20:50:48 +00002518/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2519/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002520bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2521 int e = N->getValueType(0).getVectorNumElements() / 2;
2522
2523 for (int i = 0; i < e; ++i)
2524 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002525 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002526 for (int i = 0; i < e; ++i)
2527 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002528 return false;
2529 return true;
2530}
2531
Evan Cheng63d33002006-03-22 08:01:21 +00002532/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2533/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2534/// instructions.
2535unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002536 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2537 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2538
Evan Chengb9df0ca2006-03-22 02:53:00 +00002539 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2540 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002541 for (int i = 0; i < NumOperands; ++i) {
2542 int Val = SVOp->getMaskElt(NumOperands-i-1);
2543 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002544 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002545 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002546 if (i != NumOperands - 1)
2547 Mask <<= Shift;
2548 }
Evan Cheng63d33002006-03-22 08:01:21 +00002549 return Mask;
2550}
2551
Evan Cheng506d3df2006-03-29 23:07:14 +00002552/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2553/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2554/// instructions.
2555unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002556 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002557 unsigned Mask = 0;
2558 // 8 nodes, but we only care about the last 4.
2559 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002560 int Val = SVOp->getMaskElt(i);
2561 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002562 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002563 if (i != 4)
2564 Mask <<= 2;
2565 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002566 return Mask;
2567}
2568
2569/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2570/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2571/// instructions.
2572unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002573 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002574 unsigned Mask = 0;
2575 // 8 nodes, but we only care about the first 4.
2576 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002577 int Val = SVOp->getMaskElt(i);
2578 if (Val >= 0)
2579 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002580 if (i != 0)
2581 Mask <<= 2;
2582 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002583 return Mask;
2584}
2585
Nate Begeman9008ca62009-04-27 18:41:29 +00002586/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2587/// their permute mask.
2588static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2589 SelectionDAG &DAG) {
2590 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002591 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002592 SmallVector<int, 8> MaskVec;
2593
Nate Begeman5a5ca152009-04-29 05:20:52 +00002594 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002595 int idx = SVOp->getMaskElt(i);
2596 if (idx < 0)
2597 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002598 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002600 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002602 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2604 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002605}
2606
Evan Cheng779ccea2007-12-07 21:30:01 +00002607/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2608/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002609static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002610 unsigned NumElems = VT.getVectorNumElements();
2611 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 int idx = Mask[i];
2613 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002614 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002615 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002616 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002617 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002618 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002619 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002620}
2621
Evan Cheng533a0aa2006-04-19 20:35:22 +00002622/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2623/// match movhlps. The lower half elements should come from upper half of
2624/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002625/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002626static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2627 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002628 return false;
2629 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002631 return false;
2632 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002634 return false;
2635 return true;
2636}
2637
Evan Cheng5ced1d82006-04-06 23:23:56 +00002638/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002639/// is promoted to a vector. It also returns the LoadSDNode by reference if
2640/// required.
2641static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002642 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2643 return false;
2644 N = N->getOperand(0).getNode();
2645 if (!ISD::isNON_EXTLoad(N))
2646 return false;
2647 if (LD)
2648 *LD = cast<LoadSDNode>(N);
2649 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002650}
2651
Evan Cheng533a0aa2006-04-19 20:35:22 +00002652/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2653/// match movlp{s|d}. The lower half elements should come from lower half of
2654/// V1 (and in order), and the upper half elements should come from the upper
2655/// half of V2 (and in order). And since V1 will become the source of the
2656/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002657static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2658 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002659 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002660 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002661 // Is V2 is a vector load, don't do this transformation. We will try to use
2662 // load folding shufps op.
2663 if (ISD::isNON_EXTLoad(V2))
2664 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002665
Nate Begeman5a5ca152009-04-29 05:20:52 +00002666 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002667
Evan Cheng533a0aa2006-04-19 20:35:22 +00002668 if (NumElems != 2 && NumElems != 4)
2669 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002670 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002672 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002673 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002674 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002675 return false;
2676 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002677}
2678
Evan Cheng39623da2006-04-20 08:58:49 +00002679/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2680/// all the same.
2681static bool isSplatVector(SDNode *N) {
2682 if (N->getOpcode() != ISD::BUILD_VECTOR)
2683 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002684
Dan Gohman475871a2008-07-27 21:46:04 +00002685 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002686 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2687 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002688 return false;
2689 return true;
2690}
2691
Evan Cheng213d2cf2007-05-17 18:45:50 +00002692/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2693/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002694static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002695 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002696 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002697 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002698 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002699}
2700
2701/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002702/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002703/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002704static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002705 SDValue V1 = N->getOperand(0);
2706 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002707 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2708 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002710 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002711 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002712 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2713 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002714 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2715 return false;
2716 } else if (Idx >= 0) {
2717 unsigned Opc = V1.getOpcode();
2718 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2719 continue;
2720 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002721 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002722 }
2723 }
2724 return true;
2725}
2726
2727/// getZeroVector - Returns a vector of specified type with all zero elements.
2728///
Dale Johannesenace16102009-02-03 19:33:06 +00002729static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2730 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002731 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002732
Chris Lattner8a594482007-11-25 00:24:49 +00002733 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2734 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002735 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002736 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002737 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002738 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002739 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002740 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002741 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002742 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002743 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002744 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002745 }
Dale Johannesenace16102009-02-03 19:33:06 +00002746 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002747}
2748
Chris Lattner8a594482007-11-25 00:24:49 +00002749/// getOnesVector - Returns a vector of specified type with all bits set.
2750///
Dale Johannesenace16102009-02-03 19:33:06 +00002751static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002753
Chris Lattner8a594482007-11-25 00:24:49 +00002754 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2755 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002756 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2757 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002758 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002759 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002760 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002761 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002762 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002763}
2764
2765
Evan Cheng39623da2006-04-20 08:58:49 +00002766/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2767/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002768static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2769 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002770 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002771
Evan Cheng39623da2006-04-20 08:58:49 +00002772 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002773 SmallVector<int, 8> MaskVec;
2774 SVOp->getMask(MaskVec);
2775
Nate Begeman5a5ca152009-04-29 05:20:52 +00002776 for (unsigned i = 0; i != NumElems; ++i) {
2777 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002778 MaskVec[i] = NumElems;
2779 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002780 }
Evan Cheng39623da2006-04-20 08:58:49 +00002781 }
Evan Cheng39623da2006-04-20 08:58:49 +00002782 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002783 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2784 SVOp->getOperand(1), &MaskVec[0]);
2785 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002786}
2787
Evan Cheng017dcc62006-04-21 01:05:10 +00002788/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2789/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002790static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2791 SDValue V2) {
2792 unsigned NumElems = VT.getVectorNumElements();
2793 SmallVector<int, 8> Mask;
2794 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002795 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002796 Mask.push_back(i);
2797 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002798}
2799
Nate Begeman9008ca62009-04-27 18:41:29 +00002800/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2801static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2802 SDValue V2) {
2803 unsigned NumElems = VT.getVectorNumElements();
2804 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002805 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002806 Mask.push_back(i);
2807 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002808 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002809 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002810}
2811
Nate Begeman9008ca62009-04-27 18:41:29 +00002812/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2813static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2814 SDValue V2) {
2815 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002816 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002817 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002818 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002819 Mask.push_back(i + Half);
2820 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002821 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002822 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002823}
2824
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002825/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002826static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2827 bool HasSSE2) {
2828 if (SV->getValueType(0).getVectorNumElements() <= 4)
2829 return SDValue(SV, 0);
2830
2831 MVT PVT = MVT::v4f32;
2832 MVT VT = SV->getValueType(0);
2833 DebugLoc dl = SV->getDebugLoc();
2834 SDValue V1 = SV->getOperand(0);
2835 int NumElems = VT.getVectorNumElements();
2836 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002837
Nate Begeman9008ca62009-04-27 18:41:29 +00002838 // unpack elements to the correct location
2839 while (NumElems > 4) {
2840 if (EltNo < NumElems/2) {
2841 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2842 } else {
2843 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2844 EltNo -= NumElems/2;
2845 }
2846 NumElems >>= 1;
2847 }
2848
2849 // Perform the splat.
2850 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002851 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002852 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2853 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002854}
2855
Evan Chengba05f722006-04-21 23:03:30 +00002856/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002857/// vector of zero or undef vector. This produces a shuffle where the low
2858/// element of V2 is swizzled into the zero/undef vector, landing at element
2859/// 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 +00002860static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002861 bool isZero, bool HasSSE2,
2862 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002863 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002864 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002865 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2866 unsigned NumElems = VT.getVectorNumElements();
2867 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002868 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 // If this is the insertion idx, put the low elt of V2 here.
2870 MaskVec.push_back(i == Idx ? NumElems : i);
2871 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002872}
2873
Evan Chengf26ffe92008-05-29 08:22:04 +00002874/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2875/// a shuffle that is zero.
2876static
Nate Begeman9008ca62009-04-27 18:41:29 +00002877unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2878 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002879 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002880 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002881 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002882 int Idx = SVOp->getMaskElt(Index);
2883 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002884 ++NumZeros;
2885 continue;
2886 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002888 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002889 ++NumZeros;
2890 else
2891 break;
2892 }
2893 return NumZeros;
2894}
2895
2896/// isVectorShift - Returns true if the shuffle can be implemented as a
2897/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002898/// FIXME: split into pslldqi, psrldqi, palignr variants.
2899static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002900 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002901 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002902
2903 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002904 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002905 if (!NumZeros) {
2906 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002907 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002908 if (!NumZeros)
2909 return false;
2910 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002911 bool SeenV1 = false;
2912 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 for (int i = NumZeros; i < NumElems; ++i) {
2914 int Val = isLeft ? (i - NumZeros) : i;
2915 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2916 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002917 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002918 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002919 SeenV1 = true;
2920 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002921 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002922 SeenV2 = true;
2923 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002924 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002925 return false;
2926 }
2927 if (SeenV1 && SeenV2)
2928 return false;
2929
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002931 ShAmt = NumZeros;
2932 return true;
2933}
2934
2935
Evan Chengc78d3b42006-04-24 18:01:45 +00002936/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2937///
Dan Gohman475871a2008-07-27 21:46:04 +00002938static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002939 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002940 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002941 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002942 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002943
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002944 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002945 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002946 bool First = true;
2947 for (unsigned i = 0; i < 16; ++i) {
2948 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2949 if (ThisIsNonZero && First) {
2950 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002951 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002952 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002953 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002954 First = false;
2955 }
2956
2957 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002958 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002959 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2960 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002961 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002962 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002963 }
2964 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002965 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2966 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002967 ThisElt, DAG.getConstant(8, MVT::i8));
2968 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002969 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00002970 } else
2971 ThisElt = LastElt;
2972
Gabor Greifba36cb52008-08-28 21:40:38 +00002973 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00002974 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002975 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002976 }
2977 }
2978
Dale Johannesenace16102009-02-03 19:33:06 +00002979 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00002980}
2981
Bill Wendlinga348c562007-03-22 18:42:45 +00002982/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002983///
Dan Gohman475871a2008-07-27 21:46:04 +00002984static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002985 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002986 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002987 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00002988 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002989
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002990 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002991 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002992 bool First = true;
2993 for (unsigned i = 0; i < 8; ++i) {
2994 bool isNonZero = (NonZeros & (1 << i)) != 0;
2995 if (isNonZero) {
2996 if (First) {
2997 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002998 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002999 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003000 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003001 First = false;
3002 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003003 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003004 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003005 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003006 }
3007 }
3008
3009 return V;
3010}
3011
Evan Chengf26ffe92008-05-29 08:22:04 +00003012/// getVShift - Return a vector logical shift node.
3013///
Dan Gohman475871a2008-07-27 21:46:04 +00003014static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003015 unsigned NumBits, SelectionDAG &DAG,
3016 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003017 bool isMMX = VT.getSizeInBits() == 64;
3018 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003019 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003020 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3021 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3022 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003023 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003024}
3025
Dan Gohman475871a2008-07-27 21:46:04 +00003026SDValue
3027X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003028 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003029 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003030 if (ISD::isBuildVectorAllZeros(Op.getNode())
3031 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003032 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3033 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3034 // eliminated on x86-32 hosts.
3035 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3036 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003037
Gabor Greifba36cb52008-08-28 21:40:38 +00003038 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003039 return getOnesVector(Op.getValueType(), DAG, dl);
3040 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003041 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003042
Duncan Sands83ec4b62008-06-06 12:08:01 +00003043 MVT VT = Op.getValueType();
3044 MVT EVT = VT.getVectorElementType();
3045 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003046
3047 unsigned NumElems = Op.getNumOperands();
3048 unsigned NumZero = 0;
3049 unsigned NumNonZero = 0;
3050 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003051 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003052 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003053 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003054 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003055 if (Elt.getOpcode() == ISD::UNDEF)
3056 continue;
3057 Values.insert(Elt);
3058 if (Elt.getOpcode() != ISD::Constant &&
3059 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003060 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003061 if (isZeroNode(Elt))
3062 NumZero++;
3063 else {
3064 NonZeros |= (1 << i);
3065 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003066 }
3067 }
3068
Dan Gohman7f321562007-06-25 16:23:39 +00003069 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003070 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003071 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003072 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003073
Chris Lattner67f453a2008-03-09 05:42:06 +00003074 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003075 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003076 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003078
Chris Lattner62098042008-03-09 01:05:04 +00003079 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3080 // the value are obviously zero, truncate the value to i32 and do the
3081 // insertion that way. Only do this if the value is non-constant or if the
3082 // value is a constant being inserted into element 0. It is cheaper to do
3083 // a constant pool load than it is to do a movd + shuffle.
3084 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3085 (!IsAllConstants || Idx == 0)) {
3086 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3087 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003088 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3089 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003090
Chris Lattner62098042008-03-09 01:05:04 +00003091 // Truncate the value (which may itself be a constant) to i32, and
3092 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003093 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3094 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003095 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3096 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003097
Chris Lattner62098042008-03-09 01:05:04 +00003098 // Now we have our 32-bit value zero extended in the low element of
3099 // a vector. If Idx != 0, swizzle it into place.
3100 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003101 SmallVector<int, 4> Mask;
3102 Mask.push_back(Idx);
3103 for (unsigned i = 1; i != VecElts; ++i)
3104 Mask.push_back(i);
3105 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3106 DAG.getUNDEF(Item.getValueType()),
3107 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003108 }
Dale Johannesenace16102009-02-03 19:33:06 +00003109 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003110 }
3111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003112
Chris Lattner19f79692008-03-08 22:59:52 +00003113 // If we have a constant or non-constant insertion into the low element of
3114 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3115 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3116 // depending on what the source datatype is. Because we can only get here
3117 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3118 if (Idx == 0 &&
3119 // Don't do this for i64 values on x86-32.
3120 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003121 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003122 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003123 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3124 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003125 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003126
3127 // Is it a vector logical left shift?
3128 if (NumElems == 2 && Idx == 1 &&
3129 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003130 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003131 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003132 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003133 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003134 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003136
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003137 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003138 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003139
Chris Lattner19f79692008-03-08 22:59:52 +00003140 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3141 // is a non-constant being inserted into an element other than the low one,
3142 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3143 // movd/movss) to move this into the low element, then shuffle it into
3144 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003145 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003146 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003147
Evan Cheng0db9fe62006-04-25 20:13:52 +00003148 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003149 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3150 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003152 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 MaskVec.push_back(i == Idx ? 0 : 1);
3154 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003155 }
3156 }
3157
Chris Lattner67f453a2008-03-09 05:42:06 +00003158 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3159 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003160 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003161
Dan Gohmana3941172007-07-24 22:55:08 +00003162 // A vector full of immediates; various special cases are already
3163 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003164 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003165 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003166
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003167 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003168 if (EVTBits == 64) {
3169 if (NumNonZero == 1) {
3170 // One half is zero or undef.
3171 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003172 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003173 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003174 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3175 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003176 }
Dan Gohman475871a2008-07-27 21:46:04 +00003177 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003178 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003179
3180 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003181 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003182 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003183 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003184 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003185 }
3186
Bill Wendling826f36f2007-03-28 00:57:11 +00003187 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003188 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003189 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003190 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003191 }
3192
3193 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003194 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003195 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003196 if (NumElems == 4 && NumZero > 0) {
3197 for (unsigned i = 0; i < 4; ++i) {
3198 bool isZero = !(NonZeros & (1 << i));
3199 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003200 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003201 else
Dale Johannesenace16102009-02-03 19:33:06 +00003202 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 }
3204
3205 for (unsigned i = 0; i < 2; ++i) {
3206 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3207 default: break;
3208 case 0:
3209 V[i] = V[i*2]; // Must be a zero vector.
3210 break;
3211 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003213 break;
3214 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003216 break;
3217 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003219 break;
3220 }
3221 }
3222
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003224 bool Reverse = (NonZeros & 0x3) == 2;
3225 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003227 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3228 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3230 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003231 }
3232
3233 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3235 // values to be inserted is equal to the number of elements, in which case
3236 // use the unpack code below in the hopes of matching the consecutive elts
3237 // load merge pattern for shuffles.
3238 // FIXME: We could probably just check that here directly.
3239 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3240 getSubtarget()->hasSSE41()) {
3241 V[0] = DAG.getUNDEF(VT);
3242 for (unsigned i = 0; i < NumElems; ++i)
3243 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3244 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3245 Op.getOperand(i), DAG.getIntPtrConstant(i));
3246 return V[0];
3247 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003248 // Expand into a number of unpckl*.
3249 // e.g. for v4f32
3250 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3251 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3252 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003253 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003254 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003255 NumElems >>= 1;
3256 while (NumElems != 0) {
3257 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003259 NumElems >>= 1;
3260 }
3261 return V[0];
3262 }
3263
Dan Gohman475871a2008-07-27 21:46:04 +00003264 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003265}
3266
Nate Begemanb9a47b82009-02-23 08:49:38 +00003267// v8i16 shuffles - Prefer shuffles in the following order:
3268// 1. [all] pshuflw, pshufhw, optional move
3269// 2. [ssse3] 1 x pshufb
3270// 3. [ssse3] 2 x pshufb + 1 x por
3271// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003272static
Nate Begeman9008ca62009-04-27 18:41:29 +00003273SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3274 SelectionDAG &DAG, X86TargetLowering &TLI) {
3275 SDValue V1 = SVOp->getOperand(0);
3276 SDValue V2 = SVOp->getOperand(1);
3277 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003278 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003279
Nate Begemanb9a47b82009-02-23 08:49:38 +00003280 // Determine if more than 1 of the words in each of the low and high quadwords
3281 // of the result come from the same quadword of one of the two inputs. Undef
3282 // mask values count as coming from any quadword, for better codegen.
3283 SmallVector<unsigned, 4> LoQuad(4);
3284 SmallVector<unsigned, 4> HiQuad(4);
3285 BitVector InputQuads(4);
3286 for (unsigned i = 0; i < 8; ++i) {
3287 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003288 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003289 MaskVals.push_back(EltIdx);
3290 if (EltIdx < 0) {
3291 ++Quad[0];
3292 ++Quad[1];
3293 ++Quad[2];
3294 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003295 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003296 }
3297 ++Quad[EltIdx / 4];
3298 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003299 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003300
Nate Begemanb9a47b82009-02-23 08:49:38 +00003301 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003302 unsigned MaxQuad = 1;
3303 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003304 if (LoQuad[i] > MaxQuad) {
3305 BestLoQuad = i;
3306 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003307 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003308 }
3309
Nate Begemanb9a47b82009-02-23 08:49:38 +00003310 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003311 MaxQuad = 1;
3312 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003313 if (HiQuad[i] > MaxQuad) {
3314 BestHiQuad = i;
3315 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003316 }
3317 }
3318
Nate Begemanb9a47b82009-02-23 08:49:38 +00003319 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3320 // of the two input vectors, shuffle them into one input vector so only a
3321 // single pshufb instruction is necessary. If There are more than 2 input
3322 // quads, disable the next transformation since it does not help SSSE3.
3323 bool V1Used = InputQuads[0] || InputQuads[1];
3324 bool V2Used = InputQuads[2] || InputQuads[3];
3325 if (TLI.getSubtarget()->hasSSSE3()) {
3326 if (InputQuads.count() == 2 && V1Used && V2Used) {
3327 BestLoQuad = InputQuads.find_first();
3328 BestHiQuad = InputQuads.find_next(BestLoQuad);
3329 }
3330 if (InputQuads.count() > 2) {
3331 BestLoQuad = -1;
3332 BestHiQuad = -1;
3333 }
3334 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003335
Nate Begemanb9a47b82009-02-23 08:49:38 +00003336 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3337 // the shuffle mask. If a quad is scored as -1, that means that it contains
3338 // words from all 4 input quadwords.
3339 SDValue NewV;
3340 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 SmallVector<int, 8> MaskV;
3342 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3343 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3344 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3345 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3346 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003347 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003348
Nate Begemanb9a47b82009-02-23 08:49:38 +00003349 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3350 // source words for the shuffle, to aid later transformations.
3351 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003352 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003353 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003354 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003355 if (idx != (int)i)
3356 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003357 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003358 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003359 AllWordsInNewV = false;
3360 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003361 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003362
Nate Begemanb9a47b82009-02-23 08:49:38 +00003363 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3364 if (AllWordsInNewV) {
3365 for (int i = 0; i != 8; ++i) {
3366 int idx = MaskVals[i];
3367 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003368 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003369 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3370 if ((idx != i) && idx < 4)
3371 pshufhw = false;
3372 if ((idx != i) && idx > 3)
3373 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003374 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003375 V1 = NewV;
3376 V2Used = false;
3377 BestLoQuad = 0;
3378 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003379 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003380
Nate Begemanb9a47b82009-02-23 08:49:38 +00003381 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3382 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003383 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3385 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003386 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003387 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003388
3389 // If we have SSSE3, and all words of the result are from 1 input vector,
3390 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3391 // is present, fall back to case 4.
3392 if (TLI.getSubtarget()->hasSSSE3()) {
3393 SmallVector<SDValue,16> pshufbMask;
3394
3395 // If we have elements from both input vectors, set the high bit of the
3396 // shuffle mask element to zero out elements that come from V2 in the V1
3397 // mask, and elements that come from V1 in the V2 mask, so that the two
3398 // results can be OR'd together.
3399 bool TwoInputs = V1Used && V2Used;
3400 for (unsigned i = 0; i != 8; ++i) {
3401 int EltIdx = MaskVals[i] * 2;
3402 if (TwoInputs && (EltIdx >= 16)) {
3403 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3404 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3405 continue;
3406 }
3407 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3408 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3409 }
3410 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3411 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003412 DAG.getNode(ISD::BUILD_VECTOR, dl,
3413 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003414 if (!TwoInputs)
3415 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3416
3417 // Calculate the shuffle mask for the second input, shuffle it, and
3418 // OR it with the first shuffled input.
3419 pshufbMask.clear();
3420 for (unsigned i = 0; i != 8; ++i) {
3421 int EltIdx = MaskVals[i] * 2;
3422 if (EltIdx < 16) {
3423 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3424 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3425 continue;
3426 }
3427 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3428 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3429 }
3430 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3431 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003432 DAG.getNode(ISD::BUILD_VECTOR, dl,
3433 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003434 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3435 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3436 }
3437
3438 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3439 // and update MaskVals with new element order.
3440 BitVector InOrder(8);
3441 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003443 for (int i = 0; i != 4; ++i) {
3444 int idx = MaskVals[i];
3445 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003447 InOrder.set(i);
3448 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003450 InOrder.set(i);
3451 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003453 }
3454 }
3455 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 MaskV.push_back(i);
3457 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3458 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003459 }
3460
3461 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3462 // and update MaskVals with the new element order.
3463 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003465 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003466 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003467 for (unsigned i = 4; i != 8; ++i) {
3468 int idx = MaskVals[i];
3469 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003470 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003471 InOrder.set(i);
3472 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003474 InOrder.set(i);
3475 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003477 }
3478 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3480 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003481 }
3482
3483 // In case BestHi & BestLo were both -1, which means each quadword has a word
3484 // from each of the four input quadwords, calculate the InOrder bitvector now
3485 // before falling through to the insert/extract cleanup.
3486 if (BestLoQuad == -1 && BestHiQuad == -1) {
3487 NewV = V1;
3488 for (int i = 0; i != 8; ++i)
3489 if (MaskVals[i] < 0 || MaskVals[i] == i)
3490 InOrder.set(i);
3491 }
3492
3493 // The other elements are put in the right place using pextrw and pinsrw.
3494 for (unsigned i = 0; i != 8; ++i) {
3495 if (InOrder[i])
3496 continue;
3497 int EltIdx = MaskVals[i];
3498 if (EltIdx < 0)
3499 continue;
3500 SDValue ExtOp = (EltIdx < 8)
3501 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3502 DAG.getIntPtrConstant(EltIdx))
3503 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3504 DAG.getIntPtrConstant(EltIdx - 8));
3505 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3506 DAG.getIntPtrConstant(i));
3507 }
3508 return NewV;
3509}
3510
3511// v16i8 shuffles - Prefer shuffles in the following order:
3512// 1. [ssse3] 1 x pshufb
3513// 2. [ssse3] 2 x pshufb + 1 x por
3514// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3515static
Nate Begeman9008ca62009-04-27 18:41:29 +00003516SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3517 SelectionDAG &DAG, X86TargetLowering &TLI) {
3518 SDValue V1 = SVOp->getOperand(0);
3519 SDValue V2 = SVOp->getOperand(1);
3520 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003521 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003522 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003523
3524 // If we have SSSE3, case 1 is generated when all result bytes come from
3525 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3526 // present, fall back to case 3.
3527 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3528 bool V1Only = true;
3529 bool V2Only = true;
3530 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003531 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003532 if (EltIdx < 0)
3533 continue;
3534 if (EltIdx < 16)
3535 V2Only = false;
3536 else
3537 V1Only = false;
3538 }
3539
3540 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3541 if (TLI.getSubtarget()->hasSSSE3()) {
3542 SmallVector<SDValue,16> pshufbMask;
3543
3544 // If all result elements are from one input vector, then only translate
3545 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3546 //
3547 // Otherwise, we have elements from both input vectors, and must zero out
3548 // elements that come from V2 in the first mask, and V1 in the second mask
3549 // so that we can OR them together.
3550 bool TwoInputs = !(V1Only || V2Only);
3551 for (unsigned i = 0; i != 16; ++i) {
3552 int EltIdx = MaskVals[i];
3553 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3554 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3555 continue;
3556 }
3557 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3558 }
3559 // If all the elements are from V2, assign it to V1 and return after
3560 // building the first pshufb.
3561 if (V2Only)
3562 V1 = V2;
3563 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003564 DAG.getNode(ISD::BUILD_VECTOR, dl,
3565 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003566 if (!TwoInputs)
3567 return V1;
3568
3569 // Calculate the shuffle mask for the second input, shuffle it, and
3570 // OR it with the first shuffled input.
3571 pshufbMask.clear();
3572 for (unsigned i = 0; i != 16; ++i) {
3573 int EltIdx = MaskVals[i];
3574 if (EltIdx < 16) {
3575 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3576 continue;
3577 }
3578 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3579 }
3580 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003581 DAG.getNode(ISD::BUILD_VECTOR, dl,
3582 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003583 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3584 }
3585
3586 // No SSSE3 - Calculate in place words and then fix all out of place words
3587 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3588 // the 16 different words that comprise the two doublequadword input vectors.
3589 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3590 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3591 SDValue NewV = V2Only ? V2 : V1;
3592 for (int i = 0; i != 8; ++i) {
3593 int Elt0 = MaskVals[i*2];
3594 int Elt1 = MaskVals[i*2+1];
3595
3596 // This word of the result is all undef, skip it.
3597 if (Elt0 < 0 && Elt1 < 0)
3598 continue;
3599
3600 // This word of the result is already in the correct place, skip it.
3601 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3602 continue;
3603 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3604 continue;
3605
3606 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3607 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3608 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003609
3610 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3611 // using a single extract together, load it and store it.
3612 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3613 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3614 DAG.getIntPtrConstant(Elt1 / 2));
3615 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3616 DAG.getIntPtrConstant(i));
3617 continue;
3618 }
3619
Nate Begemanb9a47b82009-02-23 08:49:38 +00003620 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003621 // source byte is not also odd, shift the extracted word left 8 bits
3622 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003623 if (Elt1 >= 0) {
3624 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3625 DAG.getIntPtrConstant(Elt1 / 2));
3626 if ((Elt1 & 1) == 0)
3627 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3628 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003629 else if (Elt0 >= 0)
3630 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3631 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003632 }
3633 // If Elt0 is defined, extract it from the appropriate source. If the
3634 // source byte is not also even, shift the extracted word right 8 bits. If
3635 // Elt1 was also defined, OR the extracted values together before
3636 // inserting them in the result.
3637 if (Elt0 >= 0) {
3638 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3639 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3640 if ((Elt0 & 1) != 0)
3641 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3642 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003643 else if (Elt1 >= 0)
3644 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3645 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003646 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3647 : InsElt0;
3648 }
3649 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3650 DAG.getIntPtrConstant(i));
3651 }
3652 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003653}
3654
Evan Cheng7a831ce2007-12-15 03:00:47 +00003655/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3656/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3657/// done when every pair / quad of shuffle mask elements point to elements in
3658/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003659/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3660static
Nate Begeman9008ca62009-04-27 18:41:29 +00003661SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3662 SelectionDAG &DAG,
3663 TargetLowering &TLI, DebugLoc dl) {
3664 MVT VT = SVOp->getValueType(0);
3665 SDValue V1 = SVOp->getOperand(0);
3666 SDValue V2 = SVOp->getOperand(1);
3667 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003668 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003669 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003670 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003671 MVT NewVT = MaskVT;
3672 switch (VT.getSimpleVT()) {
3673 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003674 case MVT::v4f32: NewVT = MVT::v2f64; break;
3675 case MVT::v4i32: NewVT = MVT::v2i64; break;
3676 case MVT::v8i16: NewVT = MVT::v4i32; break;
3677 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003678 }
3679
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003680 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003681 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003682 NewVT = MVT::v2i64;
3683 else
3684 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003685 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003686 int Scale = NumElems / NewWidth;
3687 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003688 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 int StartIdx = -1;
3690 for (int j = 0; j < Scale; ++j) {
3691 int EltIdx = SVOp->getMaskElt(i+j);
3692 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003693 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003694 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003695 StartIdx = EltIdx - (EltIdx % Scale);
3696 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003697 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003698 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 if (StartIdx == -1)
3700 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003701 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003702 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003703 }
3704
Dale Johannesenace16102009-02-03 19:33:06 +00003705 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3706 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003708}
3709
Evan Chengd880b972008-05-09 21:53:03 +00003710/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003711///
Dan Gohman475871a2008-07-27 21:46:04 +00003712static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003713 SDValue SrcOp, SelectionDAG &DAG,
3714 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003715 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3716 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003717 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003718 LD = dyn_cast<LoadSDNode>(SrcOp);
3719 if (!LD) {
3720 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3721 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003722 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003723 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3724 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3725 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3726 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3727 // PR2108
3728 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003729 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3730 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3731 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3732 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003733 SrcOp.getOperand(0)
3734 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003735 }
3736 }
3737 }
3738
Dale Johannesenace16102009-02-03 19:33:06 +00003739 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3740 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003741 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003742 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003743}
3744
Evan Chengace3c172008-07-22 21:13:36 +00003745/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3746/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003747static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003748LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3749 SDValue V1 = SVOp->getOperand(0);
3750 SDValue V2 = SVOp->getOperand(1);
3751 DebugLoc dl = SVOp->getDebugLoc();
3752 MVT VT = SVOp->getValueType(0);
3753
Evan Chengace3c172008-07-22 21:13:36 +00003754 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003755 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003756 SmallVector<int, 8> Mask1(4U, -1);
3757 SmallVector<int, 8> PermMask;
3758 SVOp->getMask(PermMask);
3759
Evan Chengace3c172008-07-22 21:13:36 +00003760 unsigned NumHi = 0;
3761 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003762 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003763 int Idx = PermMask[i];
3764 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003765 Locs[i] = std::make_pair(-1, -1);
3766 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003767 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3768 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003769 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003770 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003771 NumLo++;
3772 } else {
3773 Locs[i] = std::make_pair(1, NumHi);
3774 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003775 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003776 NumHi++;
3777 }
3778 }
3779 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003780
Evan Chengace3c172008-07-22 21:13:36 +00003781 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003782 // If no more than two elements come from either vector. This can be
3783 // implemented with two shuffles. First shuffle gather the elements.
3784 // The second shuffle, which takes the first shuffle as both of its
3785 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003787
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 SmallVector<int, 8> Mask2(4U, -1);
3789
Evan Chengace3c172008-07-22 21:13:36 +00003790 for (unsigned i = 0; i != 4; ++i) {
3791 if (Locs[i].first == -1)
3792 continue;
3793 else {
3794 unsigned Idx = (i < 2) ? 0 : 4;
3795 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003797 }
3798 }
3799
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003801 } else if (NumLo == 3 || NumHi == 3) {
3802 // Otherwise, we must have three elements from one vector, call it X, and
3803 // one element from the other, call it Y. First, use a shufps to build an
3804 // intermediate vector with the one element from Y and the element from X
3805 // that will be in the same half in the final destination (the indexes don't
3806 // matter). Then, use a shufps to build the final vector, taking the half
3807 // containing the element from Y from the intermediate, and the other half
3808 // from X.
3809 if (NumHi == 3) {
3810 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003812 std::swap(V1, V2);
3813 }
3814
3815 // Find the element from V2.
3816 unsigned HiIndex;
3817 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 int Val = PermMask[HiIndex];
3819 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003820 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003821 if (Val >= 4)
3822 break;
3823 }
3824
Nate Begeman9008ca62009-04-27 18:41:29 +00003825 Mask1[0] = PermMask[HiIndex];
3826 Mask1[1] = -1;
3827 Mask1[2] = PermMask[HiIndex^1];
3828 Mask1[3] = -1;
3829 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003830
3831 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 Mask1[0] = PermMask[0];
3833 Mask1[1] = PermMask[1];
3834 Mask1[2] = HiIndex & 1 ? 6 : 4;
3835 Mask1[3] = HiIndex & 1 ? 4 : 6;
3836 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003837 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 Mask1[0] = HiIndex & 1 ? 2 : 0;
3839 Mask1[1] = HiIndex & 1 ? 0 : 2;
3840 Mask1[2] = PermMask[2];
3841 Mask1[3] = PermMask[3];
3842 if (Mask1[2] >= 0)
3843 Mask1[2] += 4;
3844 if (Mask1[3] >= 0)
3845 Mask1[3] += 4;
3846 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003847 }
Evan Chengace3c172008-07-22 21:13:36 +00003848 }
3849
3850 // Break it into (shuffle shuffle_hi, shuffle_lo).
3851 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003852 SmallVector<int,8> LoMask(4U, -1);
3853 SmallVector<int,8> HiMask(4U, -1);
3854
3855 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003856 unsigned MaskIdx = 0;
3857 unsigned LoIdx = 0;
3858 unsigned HiIdx = 2;
3859 for (unsigned i = 0; i != 4; ++i) {
3860 if (i == 2) {
3861 MaskPtr = &HiMask;
3862 MaskIdx = 1;
3863 LoIdx = 0;
3864 HiIdx = 2;
3865 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003866 int Idx = PermMask[i];
3867 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003868 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003870 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003872 LoIdx++;
3873 } else {
3874 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003876 HiIdx++;
3877 }
3878 }
3879
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3881 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3882 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003883 for (unsigned i = 0; i != 4; ++i) {
3884 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003886 } else {
3887 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003888 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003889 }
3890 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003891 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003892}
3893
Dan Gohman475871a2008-07-27 21:46:04 +00003894SDValue
3895X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003897 SDValue V1 = Op.getOperand(0);
3898 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003900 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003902 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003903 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3904 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003905 bool V1IsSplat = false;
3906 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003907
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003909 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003910
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 // Promote splats to v4f32.
3912 if (SVOp->isSplat()) {
3913 if (isMMX || NumElems < 4)
3914 return Op;
3915 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003916 }
3917
Evan Cheng7a831ce2007-12-15 03:00:47 +00003918 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3919 // do it!
3920 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003922 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003923 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003924 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003925 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3926 // FIXME: Figure out a cleaner way to do this.
3927 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003928 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003929 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003930 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3932 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3933 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003934 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003935 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003936 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3937 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003938 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003940 }
3941 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003942
3943 if (X86::isPSHUFDMask(SVOp))
3944 return Op;
3945
Evan Chengf26ffe92008-05-29 08:22:04 +00003946 // Check if this can be converted into a logical shift.
3947 bool isLeft = false;
3948 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003949 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 bool isShift = getSubtarget()->hasSSE2() &&
3951 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003952 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003953 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003954 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003955 MVT EVT = VT.getVectorElementType();
3956 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003957 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003958 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003959
3960 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003961 if (V1IsUndef)
3962 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003963 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003964 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003965 if (!isMMX)
3966 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003967 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003968
3969 // FIXME: fold these into legal mask.
3970 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
3971 X86::isMOVSLDUPMask(SVOp) ||
3972 X86::isMOVHLPSMask(SVOp) ||
3973 X86::isMOVHPMask(SVOp) ||
3974 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003975 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003976
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 if (ShouldXformToMOVHLPS(SVOp) ||
3978 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
3979 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003980
Evan Chengf26ffe92008-05-29 08:22:04 +00003981 if (isShift) {
3982 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003983 MVT EVT = VT.getVectorElementType();
3984 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003985 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003986 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003987
Evan Cheng9eca5e82006-10-25 21:49:50 +00003988 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003989 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3990 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00003991 V1IsSplat = isSplatVector(V1.getNode());
3992 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00003993
Chris Lattner8a594482007-11-25 00:24:49 +00003994 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003995 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003996 Op = CommuteVectorShuffle(SVOp, DAG);
3997 SVOp = cast<ShuffleVectorSDNode>(Op);
3998 V1 = SVOp->getOperand(0);
3999 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004000 std::swap(V1IsSplat, V2IsSplat);
4001 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004002 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004003 }
4004
Nate Begeman9008ca62009-04-27 18:41:29 +00004005 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4006 // Shuffling low element of v1 into undef, just return v1.
4007 if (V2IsUndef)
4008 return V1;
4009 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4010 // the instruction selector will not match, so get a canonical MOVL with
4011 // swapped operands to undo the commute.
4012 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004013 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004014
Nate Begeman9008ca62009-04-27 18:41:29 +00004015 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4016 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4017 X86::isUNPCKLMask(SVOp) ||
4018 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004019 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004020
Evan Cheng9bbbb982006-10-25 20:48:19 +00004021 if (V2IsSplat) {
4022 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004023 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004024 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004025 SDValue NewMask = NormalizeMask(SVOp, DAG);
4026 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4027 if (NSVOp != SVOp) {
4028 if (X86::isUNPCKLMask(NSVOp, true)) {
4029 return NewMask;
4030 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4031 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004032 }
4033 }
4034 }
4035
Evan Cheng9eca5e82006-10-25 21:49:50 +00004036 if (Commuted) {
4037 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004038 // FIXME: this seems wrong.
4039 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4040 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4041 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4042 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4043 X86::isUNPCKLMask(NewSVOp) ||
4044 X86::isUNPCKHMask(NewSVOp))
4045 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004046 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004047
Nate Begemanb9a47b82009-02-23 08:49:38 +00004048 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004049
4050 // Normalize the node to match x86 shuffle ops if needed
4051 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4052 return CommuteVectorShuffle(SVOp, DAG);
4053
4054 // Check for legal shuffle and return?
4055 SmallVector<int, 16> PermMask;
4056 SVOp->getMask(PermMask);
4057 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004058 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004059
Evan Cheng14b32e12007-12-11 01:46:18 +00004060 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4061 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004063 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004064 return NewOp;
4065 }
4066
Nate Begemanb9a47b82009-02-23 08:49:38 +00004067 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004068 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004069 if (NewOp.getNode())
4070 return NewOp;
4071 }
4072
Evan Chengace3c172008-07-22 21:13:36 +00004073 // Handle all 4 wide cases with a number of shuffles except for MMX.
4074 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004075 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004076
Dan Gohman475871a2008-07-27 21:46:04 +00004077 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004078}
4079
Dan Gohman475871a2008-07-27 21:46:04 +00004080SDValue
4081X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004082 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004084 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004085 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004086 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004087 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004088 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004089 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004090 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004091 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004092 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4093 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4094 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004095 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4096 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4097 DAG.getNode(ISD::BIT_CONVERT, dl,
4098 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004099 Op.getOperand(0)),
4100 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004101 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004102 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004103 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004104 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004105 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004106 } else if (VT == MVT::f32) {
4107 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4108 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004109 // result has a single use which is a store or a bitcast to i32. And in
4110 // the case of a store, it's not worth it if the index is a constant 0,
4111 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004112 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004113 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004114 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004115 if ((User->getOpcode() != ISD::STORE ||
4116 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4117 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004118 (User->getOpcode() != ISD::BIT_CONVERT ||
4119 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004120 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004121 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004122 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004123 Op.getOperand(0)),
4124 Op.getOperand(1));
4125 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004126 } else if (VT == MVT::i32) {
4127 // ExtractPS works with constant index.
4128 if (isa<ConstantSDNode>(Op.getOperand(1)))
4129 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004130 }
Dan Gohman475871a2008-07-27 21:46:04 +00004131 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004132}
4133
4134
Dan Gohman475871a2008-07-27 21:46:04 +00004135SDValue
4136X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004137 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004138 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004139
Evan Cheng62a3f152008-03-24 21:52:23 +00004140 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004142 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004143 return Res;
4144 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004145
Duncan Sands83ec4b62008-06-06 12:08:01 +00004146 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004147 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004148 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004149 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004150 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004151 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004152 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004153 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4154 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004155 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004156 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004157 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004158 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004159 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004160 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004161 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004162 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004163 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004164 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004165 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004166 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004167 if (Idx == 0)
4168 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004169
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004171 int Mask[4] = { Idx, -1, -1, -1 };
4172 MVT VVT = Op.getOperand(0).getValueType();
4173 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4174 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004175 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004176 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004177 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004178 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4179 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4180 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004181 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004182 if (Idx == 0)
4183 return Op;
4184
4185 // UNPCKHPD the element to the lowest double word, then movsd.
4186 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4187 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004188 int Mask[2] = { 1, -1 };
4189 MVT VVT = Op.getOperand(0).getValueType();
4190 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4191 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004192 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004193 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004194 }
4195
Dan Gohman475871a2008-07-27 21:46:04 +00004196 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004197}
4198
Dan Gohman475871a2008-07-27 21:46:04 +00004199SDValue
4200X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004201 MVT VT = Op.getValueType();
4202 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004203 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004204
Dan Gohman475871a2008-07-27 21:46:04 +00004205 SDValue N0 = Op.getOperand(0);
4206 SDValue N1 = Op.getOperand(1);
4207 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004208
Dan Gohmanef521f12008-08-14 22:53:18 +00004209 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4210 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004211 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004212 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004213 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4214 // argument.
4215 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004216 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004217 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004218 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004219 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004220 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004221 // Bits [7:6] of the constant are the source select. This will always be
4222 // zero here. The DAG Combiner may combine an extract_elt index into these
4223 // bits. For example (insert (extract, 3), 2) could be matched by putting
4224 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004225 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004226 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004227 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004228 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004229 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004230 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004231 } else if (EVT == MVT::i32) {
4232 // InsertPS works with constant index.
4233 if (isa<ConstantSDNode>(N2))
4234 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004235 }
Dan Gohman475871a2008-07-27 21:46:04 +00004236 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004237}
4238
Dan Gohman475871a2008-07-27 21:46:04 +00004239SDValue
4240X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004241 MVT VT = Op.getValueType();
4242 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004243
4244 if (Subtarget->hasSSE41())
4245 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4246
Evan Cheng794405e2007-12-12 07:55:34 +00004247 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004248 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004249
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004250 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004251 SDValue N0 = Op.getOperand(0);
4252 SDValue N1 = Op.getOperand(1);
4253 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004254
Duncan Sands83ec4b62008-06-06 12:08:01 +00004255 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004256 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4257 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004258 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004259 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004260 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004261 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004262 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004263 }
Dan Gohman475871a2008-07-27 21:46:04 +00004264 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004265}
4266
Dan Gohman475871a2008-07-27 21:46:04 +00004267SDValue
4268X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004269 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004270 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004271 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4272 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4273 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004274 Op.getOperand(0))));
4275
Dale Johannesenace16102009-02-03 19:33:06 +00004276 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004277 MVT VT = MVT::v2i32;
4278 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004279 default: break;
4280 case MVT::v16i8:
4281 case MVT::v8i16:
4282 VT = MVT::v4i32;
4283 break;
4284 }
Dale Johannesenace16102009-02-03 19:33:06 +00004285 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4286 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004287}
4288
Bill Wendling056292f2008-09-16 21:48:12 +00004289// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4290// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4291// one of the above mentioned nodes. It has to be wrapped because otherwise
4292// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4293// be used to form addressing mode. These wrapped nodes will be selected
4294// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004295SDValue
4296X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004297 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004298 // FIXME there isn't really any debug info here, should come from the parent
4299 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004300 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4301 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004302 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004303 // With PIC, the address is actually $g + Offset.
4304 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4305 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004306 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004307 DAG.getNode(X86ISD::GlobalBaseReg,
4308 DebugLoc::getUnknownLoc(),
4309 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004310 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004311 }
4312
4313 return Result;
4314}
4315
Dan Gohman475871a2008-07-27 21:46:04 +00004316SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004317X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004318 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004319 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004320 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4321 bool ExtraLoadRequired =
4322 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4323
4324 // Create the TargetGlobalAddress node, folding in the constant
4325 // offset if it is legal.
4326 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004327 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004328 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4329 Offset = 0;
4330 } else
4331 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004332 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004333
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004334 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004335 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004336 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4337 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004338 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004340
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004341 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4342 // load the value at address GV, not the value of GV itself. This means that
4343 // the GlobalAddress must be in the base or index register of the address, not
4344 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004345 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004346 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004347 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004348 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349
Dan Gohman6520e202008-10-18 02:06:02 +00004350 // If there was a non-zero offset that we didn't fold, create an explicit
4351 // addition for it.
4352 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004353 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004354 DAG.getConstant(Offset, getPointerTy()));
4355
Evan Cheng0db9fe62006-04-25 20:13:52 +00004356 return Result;
4357}
4358
Evan Chengda43bcf2008-09-24 00:05:32 +00004359SDValue
4360X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4361 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004362 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004363 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004364}
4365
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004366static SDValue
4367GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004368 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004369 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4370 DebugLoc dl = GA->getDebugLoc();
4371 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4372 GA->getValueType(0),
4373 GA->getOffset());
4374 if (InFlag) {
4375 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004376 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004377 } else {
4378 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004379 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004380 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004381 SDValue Flag = Chain.getValue(1);
4382 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004383}
4384
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004385// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004386static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004387LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004388 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004389 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004390 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4391 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004392 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004393 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004394 PtrVT), InFlag);
4395 InFlag = Chain.getValue(1);
4396
Rafael Espindola15f1b662009-04-24 12:59:40 +00004397 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004398}
4399
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004400// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004401static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004402LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004403 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004404 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004405}
4406
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004407// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4408// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004409static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004410 const MVT PtrVT, TLSModel::Model model,
4411 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004412 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004413 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004414 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4415 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004416 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4417 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004418
4419 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4420 NULL, 0);
4421
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004422 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4423 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004424 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004425 GA->getValueType(0),
4426 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004427 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004428
Rafael Espindola9a580232009-02-27 13:37:18 +00004429 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004430 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004431 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004432
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004433 // The address of the thread local variable is the add of the thread
4434 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004435 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004436}
4437
Dan Gohman475871a2008-07-27 21:46:04 +00004438SDValue
4439X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004440 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004441 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004442 assert(Subtarget->isTargetELF() &&
4443 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004444 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004445 GlobalValue *GV = GA->getGlobal();
4446 TLSModel::Model model =
4447 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004448 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004449 switch (model) {
4450 case TLSModel::GeneralDynamic:
4451 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004452 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004453
4454 case TLSModel::InitialExec:
4455 case TLSModel::LocalExec:
4456 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004457 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004458 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004459 switch (model) {
4460 case TLSModel::GeneralDynamic:
4461 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004462 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004463
4464 case TLSModel::InitialExec:
4465 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004466 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004467 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004468 }
Chris Lattner5867de12009-04-01 22:14:45 +00004469 assert(0 && "Unreachable");
4470 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004471}
4472
Dan Gohman475871a2008-07-27 21:46:04 +00004473SDValue
4474X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004475 // FIXME there isn't really any debug info here
4476 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004477 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4478 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004479 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004480 // With PIC, the address is actually $g + Offset.
4481 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4482 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004483 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004484 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004485 DebugLoc::getUnknownLoc(),
4486 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004487 Result);
4488 }
4489
4490 return Result;
4491}
4492
Dan Gohman475871a2008-07-27 21:46:04 +00004493SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004494 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004495 // FIXME there isn't really any debug into here
4496 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004497 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004498 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004499 // With PIC, the address is actually $g + Offset.
4500 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4501 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004502 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004503 DAG.getNode(X86ISD::GlobalBaseReg,
4504 DebugLoc::getUnknownLoc(),
4505 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004506 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 }
4508
4509 return Result;
4510}
4511
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004512/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004513/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004514SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004515 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004516 MVT VT = Op.getValueType();
4517 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004518 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004519 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004520 SDValue ShOpLo = Op.getOperand(0);
4521 SDValue ShOpHi = Op.getOperand(1);
4522 SDValue ShAmt = Op.getOperand(2);
4523 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004524 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004525 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004526 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004527
Dan Gohman475871a2008-07-27 21:46:04 +00004528 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004529 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004530 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4531 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004532 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004533 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4534 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004535 }
Evan Chenge3413162006-01-09 18:33:28 +00004536
Dale Johannesenace16102009-02-03 19:33:06 +00004537 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004538 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004539 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004540 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004541
Dan Gohman475871a2008-07-27 21:46:04 +00004542 SDValue Hi, Lo;
4543 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4544 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4545 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004546
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004547 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004548 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4549 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004550 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004551 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4552 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004553 }
4554
Dan Gohman475871a2008-07-27 21:46:04 +00004555 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004556 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557}
Evan Chenga3195e82006-01-12 22:54:21 +00004558
Dan Gohman475871a2008-07-27 21:46:04 +00004559SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004560 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004561 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004562 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004563
Eli Friedman36df4992009-05-27 00:47:34 +00004564 // These are really Legal; return the operand so the caller accepts it as
4565 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004566 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004567 return Op;
4568 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4569 Subtarget->is64Bit()) {
4570 return Op;
4571 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004572
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004573 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004574 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004575 MachineFunction &MF = DAG.getMachineFunction();
4576 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004577 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004578 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004579 StackSlot,
4580 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004581 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4582}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004583
Eli Friedman948e95a2009-05-23 09:59:16 +00004584SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4585 SDValue StackSlot,
4586 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004587 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004588 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004589 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004590 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004591 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004592 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4593 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004594 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004596 Ops.push_back(Chain);
4597 Ops.push_back(StackSlot);
4598 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004599 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004600 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004602 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004603 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004604 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605
4606 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4607 // shouldn't be necessary except that RFP cannot be live across
4608 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004609 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004610 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004611 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004612 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004613 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004614 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004615 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004616 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617 Ops.push_back(DAG.getValueType(Op.getValueType()));
4618 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004619 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4620 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004621 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004622 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004623
Evan Cheng0db9fe62006-04-25 20:13:52 +00004624 return Result;
4625}
4626
Bill Wendling8b8a6362009-01-17 03:56:04 +00004627// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4628SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4629 // This algorithm is not obvious. Here it is in C code, more or less:
4630 /*
4631 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4632 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4633 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004634
Bill Wendling8b8a6362009-01-17 03:56:04 +00004635 // Copy ints to xmm registers.
4636 __m128i xh = _mm_cvtsi32_si128( hi );
4637 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004638
Bill Wendling8b8a6362009-01-17 03:56:04 +00004639 // Combine into low half of a single xmm register.
4640 __m128i x = _mm_unpacklo_epi32( xh, xl );
4641 __m128d d;
4642 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004643
Bill Wendling8b8a6362009-01-17 03:56:04 +00004644 // Merge in appropriate exponents to give the integer bits the right
4645 // magnitude.
4646 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004647
Bill Wendling8b8a6362009-01-17 03:56:04 +00004648 // Subtract away the biases to deal with the IEEE-754 double precision
4649 // implicit 1.
4650 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004651
Bill Wendling8b8a6362009-01-17 03:56:04 +00004652 // All conversions up to here are exact. The correctly rounded result is
4653 // calculated using the current rounding mode using the following
4654 // horizontal add.
4655 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4656 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4657 // store doesn't really need to be here (except
4658 // maybe to zero the other double)
4659 return sd;
4660 }
4661 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004662
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004663 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004664
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004665 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004666 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004667 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4668 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4669 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4670 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4671 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004672 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004673
Bill Wendling8b8a6362009-01-17 03:56:04 +00004674 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004675 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4676 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4677 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004678 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004679
Dale Johannesenace16102009-02-03 19:33:06 +00004680 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4681 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004682 Op.getOperand(0),
4683 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004684 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4685 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004686 Op.getOperand(0),
4687 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004689 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004690 PseudoSourceValue::getConstantPool(), 0,
4691 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004692 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004693 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4694 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004695 PseudoSourceValue::getConstantPool(), 0,
4696 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004697 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004698
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004699 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004700 int ShufMask[2] = { 1, -1 };
4701 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4702 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004703 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4704 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004705 DAG.getIntPtrConstant(0));
4706}
4707
Bill Wendling8b8a6362009-01-17 03:56:04 +00004708// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4709SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004710 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004711 // FP constant to bias correct the final result.
4712 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4713 MVT::f64);
4714
4715 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004716 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4717 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004718 Op.getOperand(0),
4719 DAG.getIntPtrConstant(0)));
4720
Dale Johannesenace16102009-02-03 19:33:06 +00004721 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4722 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004723 DAG.getIntPtrConstant(0));
4724
4725 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004726 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4727 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4728 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004729 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004730 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4731 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004732 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004733 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4734 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004735 DAG.getIntPtrConstant(0));
4736
4737 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004738 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004739
4740 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004741 MVT DestVT = Op.getValueType();
4742
4743 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004744 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004745 DAG.getIntPtrConstant(0));
4746 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004747 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004748 }
4749
4750 // Handle final rounding.
4751 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004752}
4753
4754SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004755 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004756 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004757
Evan Chenga06ec9e2009-01-19 08:08:22 +00004758 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4759 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4760 // the optimization here.
4761 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004762 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004763
4764 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004765 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004766 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004767 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004768 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004769
Bill Wendling8b8a6362009-01-17 03:56:04 +00004770 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004771 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004772 return LowerUINT_TO_FP_i32(Op, DAG);
4773 }
4774
Eli Friedman948e95a2009-05-23 09:59:16 +00004775 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4776
4777 // Make a 64-bit buffer, and use it to build an FILD.
4778 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4779 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4780 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4781 getPointerTy(), StackSlot, WordOff);
4782 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4783 StackSlot, NULL, 0);
4784 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4785 OffsetSlot, NULL, 0);
4786 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004787}
4788
Dan Gohman475871a2008-07-27 21:46:04 +00004789std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004790FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004791 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004792
4793 MVT DstTy = Op.getValueType();
4794
4795 if (!IsSigned) {
4796 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4797 DstTy = MVT::i64;
4798 }
4799
4800 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4801 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004803
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004804 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004805 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004806 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004808 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004809 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00004810 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004812
Evan Cheng87c89352007-10-15 20:11:21 +00004813 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4814 // stack slot.
4815 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004816 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004817 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004818 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004819
Evan Cheng0db9fe62006-04-25 20:13:52 +00004820 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004821 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004822 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4823 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4824 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4825 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004827
Dan Gohman475871a2008-07-27 21:46:04 +00004828 SDValue Chain = DAG.getEntryNode();
4829 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004830 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004831 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004832 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004833 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004834 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004835 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004836 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4837 };
Dale Johannesenace16102009-02-03 19:33:06 +00004838 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004839 Chain = Value.getValue(1);
4840 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4841 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4842 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004843
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004845 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004846 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004847
Chris Lattner27a6c732007-11-24 07:07:01 +00004848 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004849}
4850
Dan Gohman475871a2008-07-27 21:46:04 +00004851SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004852 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004853 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00004854 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
4855 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004856
Chris Lattner27a6c732007-11-24 07:07:01 +00004857 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004858 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004859 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004860}
4861
Eli Friedman948e95a2009-05-23 09:59:16 +00004862SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4863 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4864 SDValue FIST = Vals.first, StackSlot = Vals.second;
4865 assert(FIST.getNode() && "Unexpected failure");
4866
4867 // Load the result.
4868 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4869 FIST, StackSlot, NULL, 0);
4870}
4871
Dan Gohman475871a2008-07-27 21:46:04 +00004872SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004873 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004874 MVT VT = Op.getValueType();
4875 MVT EltVT = VT;
4876 if (VT.isVector())
4877 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004878 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004879 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004880 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004881 CV.push_back(C);
4882 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004883 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004884 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004885 CV.push_back(C);
4886 CV.push_back(C);
4887 CV.push_back(C);
4888 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004889 }
Dan Gohmand3006222007-07-27 17:16:43 +00004890 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004891 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004892 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004893 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004894 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004895 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004896}
4897
Dan Gohman475871a2008-07-27 21:46:04 +00004898SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004899 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004900 MVT VT = Op.getValueType();
4901 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004902 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004903 if (VT.isVector()) {
4904 EltVT = VT.getVectorElementType();
4905 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004906 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004907 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004908 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004909 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004910 CV.push_back(C);
4911 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004912 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004913 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004914 CV.push_back(C);
4915 CV.push_back(C);
4916 CV.push_back(C);
4917 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004918 }
Dan Gohmand3006222007-07-27 17:16:43 +00004919 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004920 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004921 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004922 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004923 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004924 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004925 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4926 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004927 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004928 Op.getOperand(0)),
4929 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004930 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004931 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004932 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933}
4934
Dan Gohman475871a2008-07-27 21:46:04 +00004935SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4936 SDValue Op0 = Op.getOperand(0);
4937 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004938 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004939 MVT VT = Op.getValueType();
4940 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004941
4942 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004943 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004944 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004945 SrcVT = VT;
4946 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004947 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004948 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004949 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004950 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004951 }
4952
4953 // At this point the operands and the result should have the same
4954 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004955
Evan Cheng68c47cb2007-01-05 07:55:56 +00004956 // First get the sign bit of second operand.
4957 std::vector<Constant*> CV;
4958 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004959 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4960 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004961 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004962 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4963 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4964 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4965 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004966 }
Dan Gohmand3006222007-07-27 17:16:43 +00004967 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004968 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004969 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004970 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004971 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004972 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004973
4974 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004975 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004976 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00004977 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
4978 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00004979 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004980 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
4981 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004982 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004983 }
4984
Evan Cheng73d6cf12007-01-05 21:37:56 +00004985 // Clear first operand sign bit.
4986 CV.clear();
4987 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004988 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4989 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004990 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004991 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4993 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4994 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004995 }
Dan Gohmand3006222007-07-27 17:16:43 +00004996 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004997 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004998 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004999 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005000 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005001 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005002
5003 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005004 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005005}
5006
Dan Gohman076aee32009-03-04 19:44:21 +00005007/// Emit nodes that will be selected as "test Op0,Op0", or something
5008/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005009SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5010 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005011 DebugLoc dl = Op.getDebugLoc();
5012
Dan Gohman31125812009-03-07 01:58:32 +00005013 // CF and OF aren't always set the way we want. Determine which
5014 // of these we need.
5015 bool NeedCF = false;
5016 bool NeedOF = false;
5017 switch (X86CC) {
5018 case X86::COND_A: case X86::COND_AE:
5019 case X86::COND_B: case X86::COND_BE:
5020 NeedCF = true;
5021 break;
5022 case X86::COND_G: case X86::COND_GE:
5023 case X86::COND_L: case X86::COND_LE:
5024 case X86::COND_O: case X86::COND_NO:
5025 NeedOF = true;
5026 break;
5027 default: break;
5028 }
5029
Dan Gohman076aee32009-03-04 19:44:21 +00005030 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005031 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5032 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5033 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005034 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005035 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005036 switch (Op.getNode()->getOpcode()) {
5037 case ISD::ADD:
5038 // Due to an isel shortcoming, be conservative if this add is likely to
5039 // be selected as part of a load-modify-store instruction. When the root
5040 // node in a match is a store, isel doesn't know how to remap non-chain
5041 // non-flag uses of other nodes in the match, such as the ADD in this
5042 // case. This leads to the ADD being left around and reselected, with
5043 // the result being two adds in the output.
5044 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5045 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5046 if (UI->getOpcode() == ISD::STORE)
5047 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005048 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005049 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5050 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005051 if (C->getAPIntValue() == 1) {
5052 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005053 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005054 break;
5055 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005056 // An add of negative one (subtract of one) will be selected as a DEC.
5057 if (C->getAPIntValue().isAllOnesValue()) {
5058 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005059 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005060 break;
5061 }
5062 }
Dan Gohman076aee32009-03-04 19:44:21 +00005063 // Otherwise use a regular EFLAGS-setting add.
5064 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005065 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005066 break;
5067 case ISD::SUB:
5068 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5069 // likely to be selected as part of a load-modify-store instruction.
5070 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5071 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5072 if (UI->getOpcode() == ISD::STORE)
5073 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005074 // Otherwise use a regular EFLAGS-setting sub.
5075 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005076 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005077 break;
5078 case X86ISD::ADD:
5079 case X86ISD::SUB:
5080 case X86ISD::INC:
5081 case X86ISD::DEC:
5082 return SDValue(Op.getNode(), 1);
5083 default:
5084 default_case:
5085 break;
5086 }
5087 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005088 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005089 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005090 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005091 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005092 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005093 DAG.ReplaceAllUsesWith(Op, New);
5094 return SDValue(New.getNode(), 1);
5095 }
5096 }
5097
5098 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5099 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5100 DAG.getConstant(0, Op.getValueType()));
5101}
5102
5103/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5104/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005105SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5106 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005107 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5108 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005109 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005110
5111 DebugLoc dl = Op0.getDebugLoc();
5112 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5113}
5114
Dan Gohman475871a2008-07-27 21:46:04 +00005115SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005116 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005117 SDValue Op0 = Op.getOperand(0);
5118 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005119 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005120 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005121
Dan Gohmane5af2d32009-01-29 01:59:02 +00005122 // Lower (X & (1 << N)) == 0 to BT(X, N).
5123 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5124 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005125 if (Op0.getOpcode() == ISD::AND &&
5126 Op0.hasOneUse() &&
5127 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005128 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005129 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005130 SDValue LHS, RHS;
5131 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5132 if (ConstantSDNode *Op010C =
5133 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5134 if (Op010C->getZExtValue() == 1) {
5135 LHS = Op0.getOperand(0);
5136 RHS = Op0.getOperand(1).getOperand(1);
5137 }
5138 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5139 if (ConstantSDNode *Op000C =
5140 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5141 if (Op000C->getZExtValue() == 1) {
5142 LHS = Op0.getOperand(1);
5143 RHS = Op0.getOperand(0).getOperand(1);
5144 }
5145 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5146 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5147 SDValue AndLHS = Op0.getOperand(0);
5148 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5149 LHS = AndLHS.getOperand(0);
5150 RHS = AndLHS.getOperand(1);
5151 }
5152 }
Evan Cheng0488db92007-09-25 01:57:46 +00005153
Dan Gohmane5af2d32009-01-29 01:59:02 +00005154 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005155 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5156 // instruction. Since the shift amount is in-range-or-undefined, we know
5157 // that doing a bittest on the i16 value is ok. We extend to i32 because
5158 // the encoding for the i16 version is larger than the i32 version.
5159 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005160 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005161
5162 // If the operand types disagree, extend the shift amount to match. Since
5163 // BT ignores high bits (like shifts) we can use anyextend.
5164 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005165 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005166
Dale Johannesenace16102009-02-03 19:33:06 +00005167 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005168 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005169 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005170 DAG.getConstant(Cond, MVT::i8), BT);
5171 }
5172 }
5173
5174 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5175 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005176
Dan Gohman31125812009-03-07 01:58:32 +00005177 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005178 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005179 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005180}
5181
Dan Gohman475871a2008-07-27 21:46:04 +00005182SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5183 SDValue Cond;
5184 SDValue Op0 = Op.getOperand(0);
5185 SDValue Op1 = Op.getOperand(1);
5186 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005187 MVT VT = Op.getValueType();
5188 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5189 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005190 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005191
5192 if (isFP) {
5193 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005194 MVT VT0 = Op0.getValueType();
5195 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5196 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005197 bool Swap = false;
5198
5199 switch (SetCCOpcode) {
5200 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005201 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005202 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005203 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005204 case ISD::SETGT: Swap = true; // Fallthrough
5205 case ISD::SETLT:
5206 case ISD::SETOLT: SSECC = 1; break;
5207 case ISD::SETOGE:
5208 case ISD::SETGE: Swap = true; // Fallthrough
5209 case ISD::SETLE:
5210 case ISD::SETOLE: SSECC = 2; break;
5211 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005212 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005213 case ISD::SETNE: SSECC = 4; break;
5214 case ISD::SETULE: Swap = true;
5215 case ISD::SETUGE: SSECC = 5; break;
5216 case ISD::SETULT: Swap = true;
5217 case ISD::SETUGT: SSECC = 6; break;
5218 case ISD::SETO: SSECC = 7; break;
5219 }
5220 if (Swap)
5221 std::swap(Op0, Op1);
5222
Nate Begemanfb8ead02008-07-25 19:05:58 +00005223 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005224 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005225 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005227 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5228 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5229 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005230 }
5231 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005232 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005233 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5234 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5235 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005236 }
5237 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005238 }
5239 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005240 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005242
Nate Begeman30a0de92008-07-17 16:51:19 +00005243 // We are handling one of the integer comparisons here. Since SSE only has
5244 // GT and EQ comparisons for integer, swapping operands and multiple
5245 // operations may be required for some comparisons.
5246 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5247 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005248
Nate Begeman30a0de92008-07-17 16:51:19 +00005249 switch (VT.getSimpleVT()) {
5250 default: break;
5251 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5252 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5253 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5254 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5255 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005256
Nate Begeman30a0de92008-07-17 16:51:19 +00005257 switch (SetCCOpcode) {
5258 default: break;
5259 case ISD::SETNE: Invert = true;
5260 case ISD::SETEQ: Opc = EQOpc; break;
5261 case ISD::SETLT: Swap = true;
5262 case ISD::SETGT: Opc = GTOpc; break;
5263 case ISD::SETGE: Swap = true;
5264 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5265 case ISD::SETULT: Swap = true;
5266 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5267 case ISD::SETUGE: Swap = true;
5268 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5269 }
5270 if (Swap)
5271 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005272
Nate Begeman30a0de92008-07-17 16:51:19 +00005273 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5274 // bits of the inputs before performing those operations.
5275 if (FlipSigns) {
5276 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005277 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5278 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005279 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005280 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5281 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005282 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5283 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005284 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005285
Dale Johannesenace16102009-02-03 19:33:06 +00005286 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005287
5288 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005289 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005290 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005291
Nate Begeman30a0de92008-07-17 16:51:19 +00005292 return Result;
5293}
Evan Cheng0488db92007-09-25 01:57:46 +00005294
Evan Cheng370e5342008-12-03 08:38:43 +00005295// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005296static bool isX86LogicalCmp(SDValue Op) {
5297 unsigned Opc = Op.getNode()->getOpcode();
5298 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5299 return true;
5300 if (Op.getResNo() == 1 &&
5301 (Opc == X86ISD::ADD ||
5302 Opc == X86ISD::SUB ||
5303 Opc == X86ISD::SMUL ||
5304 Opc == X86ISD::UMUL ||
5305 Opc == X86ISD::INC ||
5306 Opc == X86ISD::DEC))
5307 return true;
5308
5309 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005310}
5311
Dan Gohman475871a2008-07-27 21:46:04 +00005312SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005313 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005314 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005315 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005316 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005317
Evan Cheng734503b2006-09-11 02:19:56 +00005318 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005319 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005320
Evan Cheng3f41d662007-10-08 22:16:29 +00005321 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5322 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005323 if (Cond.getOpcode() == X86ISD::SETCC) {
5324 CC = Cond.getOperand(0);
5325
Dan Gohman475871a2008-07-27 21:46:04 +00005326 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005327 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005328 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005329
Evan Cheng3f41d662007-10-08 22:16:29 +00005330 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005331 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005332 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005333 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005334
Chris Lattnerd1980a52009-03-12 06:52:53 +00005335 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5336 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005337 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005338 addTest = false;
5339 }
5340 }
5341
5342 if (addTest) {
5343 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005344 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005345 }
5346
Dan Gohmanfc166572009-04-09 23:54:40 +00005347 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005348 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005349 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5350 // condition is true.
5351 Ops.push_back(Op.getOperand(2));
5352 Ops.push_back(Op.getOperand(1));
5353 Ops.push_back(CC);
5354 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005355 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005356}
5357
Evan Cheng370e5342008-12-03 08:38:43 +00005358// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5359// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5360// from the AND / OR.
5361static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5362 Opc = Op.getOpcode();
5363 if (Opc != ISD::OR && Opc != ISD::AND)
5364 return false;
5365 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5366 Op.getOperand(0).hasOneUse() &&
5367 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5368 Op.getOperand(1).hasOneUse());
5369}
5370
Evan Cheng961d6d42009-02-02 08:19:07 +00005371// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5372// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005373static bool isXor1OfSetCC(SDValue Op) {
5374 if (Op.getOpcode() != ISD::XOR)
5375 return false;
5376 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5377 if (N1C && N1C->getAPIntValue() == 1) {
5378 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5379 Op.getOperand(0).hasOneUse();
5380 }
5381 return false;
5382}
5383
Dan Gohman475871a2008-07-27 21:46:04 +00005384SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005385 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue Chain = Op.getOperand(0);
5387 SDValue Cond = Op.getOperand(1);
5388 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005389 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005390 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005391
Evan Cheng0db9fe62006-04-25 20:13:52 +00005392 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005393 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005394#if 0
5395 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005396 else if (Cond.getOpcode() == X86ISD::ADD ||
5397 Cond.getOpcode() == X86ISD::SUB ||
5398 Cond.getOpcode() == X86ISD::SMUL ||
5399 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005400 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005401#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005402
Evan Cheng3f41d662007-10-08 22:16:29 +00005403 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5404 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005405 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005406 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407
Dan Gohman475871a2008-07-27 21:46:04 +00005408 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005409 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005410 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005411 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005412 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005413 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005414 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005415 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005416 default: break;
5417 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005418 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005419 // These can only come from an arithmetic instruction with overflow,
5420 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005421 Cond = Cond.getNode()->getOperand(1);
5422 addTest = false;
5423 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005424 }
Evan Cheng0488db92007-09-25 01:57:46 +00005425 }
Evan Cheng370e5342008-12-03 08:38:43 +00005426 } else {
5427 unsigned CondOpc;
5428 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5429 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005430 if (CondOpc == ISD::OR) {
5431 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5432 // two branches instead of an explicit OR instruction with a
5433 // separate test.
5434 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005435 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005436 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005437 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005438 Chain, Dest, CC, Cmp);
5439 CC = Cond.getOperand(1).getOperand(0);
5440 Cond = Cmp;
5441 addTest = false;
5442 }
5443 } else { // ISD::AND
5444 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5445 // two branches instead of an explicit AND instruction with a
5446 // separate test. However, we only do this if this block doesn't
5447 // have a fall-through edge, because this requires an explicit
5448 // jmp when the condition is false.
5449 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005450 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005451 Op.getNode()->hasOneUse()) {
5452 X86::CondCode CCode =
5453 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5454 CCode = X86::GetOppositeBranchCondition(CCode);
5455 CC = DAG.getConstant(CCode, MVT::i8);
5456 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5457 // Look for an unconditional branch following this conditional branch.
5458 // We need this because we need to reverse the successors in order
5459 // to implement FCMP_OEQ.
5460 if (User.getOpcode() == ISD::BR) {
5461 SDValue FalseBB = User.getOperand(1);
5462 SDValue NewBR =
5463 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5464 assert(NewBR == User);
5465 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005466
Dale Johannesene4d209d2009-02-03 20:21:25 +00005467 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005468 Chain, Dest, CC, Cmp);
5469 X86::CondCode CCode =
5470 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5471 CCode = X86::GetOppositeBranchCondition(CCode);
5472 CC = DAG.getConstant(CCode, MVT::i8);
5473 Cond = Cmp;
5474 addTest = false;
5475 }
5476 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005477 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005478 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5479 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5480 // It should be transformed during dag combiner except when the condition
5481 // is set by a arithmetics with overflow node.
5482 X86::CondCode CCode =
5483 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5484 CCode = X86::GetOppositeBranchCondition(CCode);
5485 CC = DAG.getConstant(CCode, MVT::i8);
5486 Cond = Cond.getOperand(0).getOperand(1);
5487 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005488 }
Evan Cheng0488db92007-09-25 01:57:46 +00005489 }
5490
5491 if (addTest) {
5492 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005493 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005494 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005495 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005496 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005497}
5498
Anton Korobeynikove060b532007-04-17 19:34:00 +00005499
5500// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5501// Calls to _alloca is needed to probe the stack when allocating more than 4k
5502// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5503// that the guard pages used by the OS virtual memory manager are allocated in
5504// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005505SDValue
5506X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005507 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005508 assert(Subtarget->isTargetCygMing() &&
5509 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005510 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005511
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005512 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005513 SDValue Chain = Op.getOperand(0);
5514 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005515 // FIXME: Ensure alignment here
5516
Dan Gohman475871a2008-07-27 21:46:04 +00005517 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005518
Duncan Sands83ec4b62008-06-06 12:08:01 +00005519 MVT IntPtr = getPointerTy();
5520 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005521
Chris Lattnere563bbc2008-10-11 22:08:30 +00005522 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005523
Dale Johannesendd64c412009-02-04 00:33:20 +00005524 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005525 Flag = Chain.getValue(1);
5526
5527 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005528 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005529 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005530 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005531 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005532 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005533 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005534 Flag = Chain.getValue(1);
5535
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005536 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005537 DAG.getIntPtrConstant(0, true),
5538 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005539 Flag);
5540
Dale Johannesendd64c412009-02-04 00:33:20 +00005541 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005542
Dan Gohman475871a2008-07-27 21:46:04 +00005543 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005544 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005545}
5546
Dan Gohman475871a2008-07-27 21:46:04 +00005547SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005548X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005549 SDValue Chain,
5550 SDValue Dst, SDValue Src,
5551 SDValue Size, unsigned Align,
5552 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005553 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005554 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555
Bill Wendling6f287b22008-09-30 21:22:07 +00005556 // If not DWORD aligned or size is more than the threshold, call the library.
5557 // The libc version is likely to be faster for these cases. It can use the
5558 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005559 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005560 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005561 ConstantSize->getZExtValue() >
5562 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005564
5565 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005566 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005567
Bill Wendling6158d842008-10-01 00:59:58 +00005568 if (const char *bzeroEntry = V &&
5569 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5570 MVT IntPtr = getPointerTy();
5571 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005572 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005573 TargetLowering::ArgListEntry Entry;
5574 Entry.Node = Dst;
5575 Entry.Ty = IntPtrTy;
5576 Args.push_back(Entry);
5577 Entry.Node = Size;
5578 Args.push_back(Entry);
5579 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005580 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5581 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005582 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005583 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005584 }
5585
Dan Gohman707e0182008-04-12 04:36:06 +00005586 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005587 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005588 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005589
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005590 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005591 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005592 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005593 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005594 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005595 unsigned BytesLeft = 0;
5596 bool TwoRepStos = false;
5597 if (ValC) {
5598 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005599 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005600
Evan Cheng0db9fe62006-04-25 20:13:52 +00005601 // If the value is a constant, then we can potentially use larger sets.
5602 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005603 case 2: // WORD aligned
5604 AVT = MVT::i16;
5605 ValReg = X86::AX;
5606 Val = (Val << 8) | Val;
5607 break;
5608 case 0: // DWORD aligned
5609 AVT = MVT::i32;
5610 ValReg = X86::EAX;
5611 Val = (Val << 8) | Val;
5612 Val = (Val << 16) | Val;
5613 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5614 AVT = MVT::i64;
5615 ValReg = X86::RAX;
5616 Val = (Val << 32) | Val;
5617 }
5618 break;
5619 default: // Byte aligned
5620 AVT = MVT::i8;
5621 ValReg = X86::AL;
5622 Count = DAG.getIntPtrConstant(SizeVal);
5623 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005624 }
5625
Duncan Sands8e4eb092008-06-08 20:54:56 +00005626 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005627 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005628 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5629 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005630 }
5631
Dale Johannesen0f502f62009-02-03 22:26:09 +00005632 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005633 InFlag);
5634 InFlag = Chain.getValue(1);
5635 } else {
5636 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005637 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005638 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005639 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005640 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005641
Scott Michelfdc40a02009-02-17 22:15:04 +00005642 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005643 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005644 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005645 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005646 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005647 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005648 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005649 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005650
Chris Lattnerd96d0722007-02-25 06:40:16 +00005651 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005652 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005653 Ops.push_back(Chain);
5654 Ops.push_back(DAG.getValueType(AVT));
5655 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005656 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005657
Evan Cheng0db9fe62006-04-25 20:13:52 +00005658 if (TwoRepStos) {
5659 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005660 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005661 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005662 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005663 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005664 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005665 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005666 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005667 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005668 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005669 Ops.clear();
5670 Ops.push_back(Chain);
5671 Ops.push_back(DAG.getValueType(MVT::i8));
5672 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005673 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005674 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005675 // Handle the last 1 - 7 bytes.
5676 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005677 MVT AddrVT = Dst.getValueType();
5678 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005679
Dale Johannesen0f502f62009-02-03 22:26:09 +00005680 Chain = DAG.getMemset(Chain, dl,
5681 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005682 DAG.getConstant(Offset, AddrVT)),
5683 Src,
5684 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005685 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005686 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005687
Dan Gohman707e0182008-04-12 04:36:06 +00005688 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005689 return Chain;
5690}
Evan Cheng11e15b32006-04-03 20:53:28 +00005691
Dan Gohman475871a2008-07-27 21:46:04 +00005692SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005693X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005694 SDValue Chain, SDValue Dst, SDValue Src,
5695 SDValue Size, unsigned Align,
5696 bool AlwaysInline,
5697 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005698 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005699 // This requires the copy size to be a constant, preferrably
5700 // within a subtarget-specific limit.
5701 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5702 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005703 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005704 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005705 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005706 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005707
Evan Cheng1887c1c2008-08-21 21:00:15 +00005708 /// If not DWORD aligned, call the library.
5709 if ((Align & 3) != 0)
5710 return SDValue();
5711
5712 // DWORD aligned
5713 MVT AVT = MVT::i32;
5714 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005715 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005716
Duncan Sands83ec4b62008-06-06 12:08:01 +00005717 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005718 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005719 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005720 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005721
Dan Gohman475871a2008-07-27 21:46:04 +00005722 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005723 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005724 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005725 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005727 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005728 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005729 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005730 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005731 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005732 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005733 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005734 InFlag = Chain.getValue(1);
5735
Chris Lattnerd96d0722007-02-25 06:40:16 +00005736 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005737 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005738 Ops.push_back(Chain);
5739 Ops.push_back(DAG.getValueType(AVT));
5740 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005741 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005742
Dan Gohman475871a2008-07-27 21:46:04 +00005743 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005744 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005745 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005746 // Handle the last 1 - 7 bytes.
5747 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005748 MVT DstVT = Dst.getValueType();
5749 MVT SrcVT = Src.getValueType();
5750 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005751 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005752 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005753 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005754 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005755 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005756 DAG.getConstant(BytesLeft, SizeVT),
5757 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005758 DstSV, DstSVOff + Offset,
5759 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005760 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005761
Scott Michelfdc40a02009-02-17 22:15:04 +00005762 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005763 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005764}
5765
Dan Gohman475871a2008-07-27 21:46:04 +00005766SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005767 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005768 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005769
Evan Cheng25ab6902006-09-08 06:48:29 +00005770 if (!Subtarget->is64Bit()) {
5771 // vastart just stores the address of the VarArgsFrameIndex slot into the
5772 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005773 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005774 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005775 }
5776
5777 // __va_list_tag:
5778 // gp_offset (0 - 6 * 8)
5779 // fp_offset (48 - 48 + 8 * 16)
5780 // overflow_arg_area (point to parameters coming in memory).
5781 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005782 SmallVector<SDValue, 8> MemOps;
5783 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005784 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005785 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005786 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005787 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005788 MemOps.push_back(Store);
5789
5790 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005791 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005792 FIN, DAG.getIntPtrConstant(4));
5793 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005794 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005795 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005796 MemOps.push_back(Store);
5797
5798 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005799 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005800 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005801 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005802 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005803 MemOps.push_back(Store);
5804
5805 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005806 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005807 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005808 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005809 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005810 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005811 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005812 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005813}
5814
Dan Gohman475871a2008-07-27 21:46:04 +00005815SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005816 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5817 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SDValue Chain = Op.getOperand(0);
5819 SDValue SrcPtr = Op.getOperand(1);
5820 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005821
5822 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5823 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005824 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005825}
5826
Dan Gohman475871a2008-07-27 21:46:04 +00005827SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005828 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005829 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005830 SDValue Chain = Op.getOperand(0);
5831 SDValue DstPtr = Op.getOperand(1);
5832 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005833 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5834 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005835 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005836
Dale Johannesendd64c412009-02-04 00:33:20 +00005837 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005838 DAG.getIntPtrConstant(24), 8, false,
5839 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005840}
5841
Dan Gohman475871a2008-07-27 21:46:04 +00005842SDValue
5843X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005844 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005845 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005846 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005847 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005848 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005849 case Intrinsic::x86_sse_comieq_ss:
5850 case Intrinsic::x86_sse_comilt_ss:
5851 case Intrinsic::x86_sse_comile_ss:
5852 case Intrinsic::x86_sse_comigt_ss:
5853 case Intrinsic::x86_sse_comige_ss:
5854 case Intrinsic::x86_sse_comineq_ss:
5855 case Intrinsic::x86_sse_ucomieq_ss:
5856 case Intrinsic::x86_sse_ucomilt_ss:
5857 case Intrinsic::x86_sse_ucomile_ss:
5858 case Intrinsic::x86_sse_ucomigt_ss:
5859 case Intrinsic::x86_sse_ucomige_ss:
5860 case Intrinsic::x86_sse_ucomineq_ss:
5861 case Intrinsic::x86_sse2_comieq_sd:
5862 case Intrinsic::x86_sse2_comilt_sd:
5863 case Intrinsic::x86_sse2_comile_sd:
5864 case Intrinsic::x86_sse2_comigt_sd:
5865 case Intrinsic::x86_sse2_comige_sd:
5866 case Intrinsic::x86_sse2_comineq_sd:
5867 case Intrinsic::x86_sse2_ucomieq_sd:
5868 case Intrinsic::x86_sse2_ucomilt_sd:
5869 case Intrinsic::x86_sse2_ucomile_sd:
5870 case Intrinsic::x86_sse2_ucomigt_sd:
5871 case Intrinsic::x86_sse2_ucomige_sd:
5872 case Intrinsic::x86_sse2_ucomineq_sd: {
5873 unsigned Opc = 0;
5874 ISD::CondCode CC = ISD::SETCC_INVALID;
5875 switch (IntNo) {
5876 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005877 case Intrinsic::x86_sse_comieq_ss:
5878 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005879 Opc = X86ISD::COMI;
5880 CC = ISD::SETEQ;
5881 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005882 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005883 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005884 Opc = X86ISD::COMI;
5885 CC = ISD::SETLT;
5886 break;
5887 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005888 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005889 Opc = X86ISD::COMI;
5890 CC = ISD::SETLE;
5891 break;
5892 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005893 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005894 Opc = X86ISD::COMI;
5895 CC = ISD::SETGT;
5896 break;
5897 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005898 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005899 Opc = X86ISD::COMI;
5900 CC = ISD::SETGE;
5901 break;
5902 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005903 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005904 Opc = X86ISD::COMI;
5905 CC = ISD::SETNE;
5906 break;
5907 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005908 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005909 Opc = X86ISD::UCOMI;
5910 CC = ISD::SETEQ;
5911 break;
5912 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005913 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005914 Opc = X86ISD::UCOMI;
5915 CC = ISD::SETLT;
5916 break;
5917 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005918 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005919 Opc = X86ISD::UCOMI;
5920 CC = ISD::SETLE;
5921 break;
5922 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005923 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005924 Opc = X86ISD::UCOMI;
5925 CC = ISD::SETGT;
5926 break;
5927 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005928 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005929 Opc = X86ISD::UCOMI;
5930 CC = ISD::SETGE;
5931 break;
5932 case Intrinsic::x86_sse_ucomineq_ss:
5933 case Intrinsic::x86_sse2_ucomineq_sd:
5934 Opc = X86ISD::UCOMI;
5935 CC = ISD::SETNE;
5936 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005937 }
Evan Cheng734503b2006-09-11 02:19:56 +00005938
Dan Gohman475871a2008-07-27 21:46:04 +00005939 SDValue LHS = Op.getOperand(1);
5940 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005941 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005942 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5943 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005944 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005945 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005946 }
Evan Cheng5759f972008-05-04 09:15:50 +00005947
5948 // Fix vector shift instructions where the last operand is a non-immediate
5949 // i32 value.
5950 case Intrinsic::x86_sse2_pslli_w:
5951 case Intrinsic::x86_sse2_pslli_d:
5952 case Intrinsic::x86_sse2_pslli_q:
5953 case Intrinsic::x86_sse2_psrli_w:
5954 case Intrinsic::x86_sse2_psrli_d:
5955 case Intrinsic::x86_sse2_psrli_q:
5956 case Intrinsic::x86_sse2_psrai_w:
5957 case Intrinsic::x86_sse2_psrai_d:
5958 case Intrinsic::x86_mmx_pslli_w:
5959 case Intrinsic::x86_mmx_pslli_d:
5960 case Intrinsic::x86_mmx_pslli_q:
5961 case Intrinsic::x86_mmx_psrli_w:
5962 case Intrinsic::x86_mmx_psrli_d:
5963 case Intrinsic::x86_mmx_psrli_q:
5964 case Intrinsic::x86_mmx_psrai_w:
5965 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005966 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005967 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005968 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005969
5970 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005971 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005972 switch (IntNo) {
5973 case Intrinsic::x86_sse2_pslli_w:
5974 NewIntNo = Intrinsic::x86_sse2_psll_w;
5975 break;
5976 case Intrinsic::x86_sse2_pslli_d:
5977 NewIntNo = Intrinsic::x86_sse2_psll_d;
5978 break;
5979 case Intrinsic::x86_sse2_pslli_q:
5980 NewIntNo = Intrinsic::x86_sse2_psll_q;
5981 break;
5982 case Intrinsic::x86_sse2_psrli_w:
5983 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5984 break;
5985 case Intrinsic::x86_sse2_psrli_d:
5986 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5987 break;
5988 case Intrinsic::x86_sse2_psrli_q:
5989 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5990 break;
5991 case Intrinsic::x86_sse2_psrai_w:
5992 NewIntNo = Intrinsic::x86_sse2_psra_w;
5993 break;
5994 case Intrinsic::x86_sse2_psrai_d:
5995 NewIntNo = Intrinsic::x86_sse2_psra_d;
5996 break;
5997 default: {
5998 ShAmtVT = MVT::v2i32;
5999 switch (IntNo) {
6000 case Intrinsic::x86_mmx_pslli_w:
6001 NewIntNo = Intrinsic::x86_mmx_psll_w;
6002 break;
6003 case Intrinsic::x86_mmx_pslli_d:
6004 NewIntNo = Intrinsic::x86_mmx_psll_d;
6005 break;
6006 case Intrinsic::x86_mmx_pslli_q:
6007 NewIntNo = Intrinsic::x86_mmx_psll_q;
6008 break;
6009 case Intrinsic::x86_mmx_psrli_w:
6010 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6011 break;
6012 case Intrinsic::x86_mmx_psrli_d:
6013 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6014 break;
6015 case Intrinsic::x86_mmx_psrli_q:
6016 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6017 break;
6018 case Intrinsic::x86_mmx_psrai_w:
6019 NewIntNo = Intrinsic::x86_mmx_psra_w;
6020 break;
6021 case Intrinsic::x86_mmx_psrai_d:
6022 NewIntNo = Intrinsic::x86_mmx_psra_d;
6023 break;
6024 default: abort(); // Can't reach here.
6025 }
6026 break;
6027 }
6028 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006029 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006030 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6031 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6032 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006033 DAG.getConstant(NewIntNo, MVT::i32),
6034 Op.getOperand(1), ShAmt);
6035 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006036 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006037}
Evan Cheng72261582005-12-20 06:22:03 +00006038
Dan Gohman475871a2008-07-27 21:46:04 +00006039SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006040 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006041 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006042
6043 if (Depth > 0) {
6044 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6045 SDValue Offset =
6046 DAG.getConstant(TD->getPointerSize(),
6047 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006048 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006049 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006050 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006051 NULL, 0);
6052 }
6053
6054 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006055 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006056 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006057 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006058}
6059
Dan Gohman475871a2008-07-27 21:46:04 +00006060SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006061 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6062 MFI->setFrameAddressIsTaken(true);
6063 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006064 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006065 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6066 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006067 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006068 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006069 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006070 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006071}
6072
Dan Gohman475871a2008-07-27 21:46:04 +00006073SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006074 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006075 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006076}
6077
Dan Gohman475871a2008-07-27 21:46:04 +00006078SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006079{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006080 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006081 SDValue Chain = Op.getOperand(0);
6082 SDValue Offset = Op.getOperand(1);
6083 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006084 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006085
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006086 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6087 getPointerTy());
6088 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006089
Dale Johannesene4d209d2009-02-03 20:21:25 +00006090 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006091 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006092 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6093 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006094 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006095 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006096
Dale Johannesene4d209d2009-02-03 20:21:25 +00006097 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006098 MVT::Other,
6099 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006100}
6101
Dan Gohman475871a2008-07-27 21:46:04 +00006102SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006103 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006104 SDValue Root = Op.getOperand(0);
6105 SDValue Trmp = Op.getOperand(1); // trampoline
6106 SDValue FPtr = Op.getOperand(2); // nested function
6107 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006108 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006109
Dan Gohman69de1932008-02-06 22:27:42 +00006110 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006111
Duncan Sands339e14f2008-01-16 22:55:25 +00006112 const X86InstrInfo *TII =
6113 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6114
Duncan Sandsb116fac2007-07-27 20:02:49 +00006115 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006116 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006117
6118 // Large code-model.
6119
6120 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6121 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6122
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006123 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6124 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006125
6126 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6127
6128 // Load the pointer to the nested function into R11.
6129 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006130 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006131 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6132 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006133
Scott Michelfdc40a02009-02-17 22:15:04 +00006134 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006135 DAG.getConstant(2, MVT::i64));
6136 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006137
6138 // Load the 'nest' parameter value into R10.
6139 // R10 is specified in X86CallingConv.td
6140 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006141 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006142 DAG.getConstant(10, MVT::i64));
6143 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6144 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006145
Scott Michelfdc40a02009-02-17 22:15:04 +00006146 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006147 DAG.getConstant(12, MVT::i64));
6148 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006149
6150 // Jump to the nested function.
6151 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006152 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006153 DAG.getConstant(20, MVT::i64));
6154 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6155 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006156
6157 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006158 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006159 DAG.getConstant(22, MVT::i64));
6160 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006161 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006162
Dan Gohman475871a2008-07-27 21:46:04 +00006163 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006164 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6165 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006166 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006167 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006168 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6169 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006170 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006171
6172 switch (CC) {
6173 default:
6174 assert(0 && "Unsupported calling convention");
6175 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006176 case CallingConv::X86_StdCall: {
6177 // Pass 'nest' parameter in ECX.
6178 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006179 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006180
6181 // Check that ECX wasn't needed by an 'inreg' parameter.
6182 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006183 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006184
Chris Lattner58d74912008-03-12 17:45:29 +00006185 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006186 unsigned InRegCount = 0;
6187 unsigned Idx = 1;
6188
6189 for (FunctionType::param_iterator I = FTy->param_begin(),
6190 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006191 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006192 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006193 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006194
6195 if (InRegCount > 2) {
6196 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6197 abort();
6198 }
6199 }
6200 break;
6201 }
6202 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006203 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006204 // Pass 'nest' parameter in EAX.
6205 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006206 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006207 break;
6208 }
6209
Dan Gohman475871a2008-07-27 21:46:04 +00006210 SDValue OutChains[4];
6211 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006212
Scott Michelfdc40a02009-02-17 22:15:04 +00006213 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006214 DAG.getConstant(10, MVT::i32));
6215 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006216
Duncan Sands339e14f2008-01-16 22:55:25 +00006217 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006218 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006219 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006220 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006221 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006222
Scott Michelfdc40a02009-02-17 22:15:04 +00006223 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006224 DAG.getConstant(1, MVT::i32));
6225 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006226
Duncan Sands339e14f2008-01-16 22:55:25 +00006227 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006228 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006229 DAG.getConstant(5, MVT::i32));
6230 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006231 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006232
Scott Michelfdc40a02009-02-17 22:15:04 +00006233 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006234 DAG.getConstant(6, MVT::i32));
6235 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006236
Dan Gohman475871a2008-07-27 21:46:04 +00006237 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006238 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6239 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006240 }
6241}
6242
Dan Gohman475871a2008-07-27 21:46:04 +00006243SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006244 /*
6245 The rounding mode is in bits 11:10 of FPSR, and has the following
6246 settings:
6247 00 Round to nearest
6248 01 Round to -inf
6249 10 Round to +inf
6250 11 Round to 0
6251
6252 FLT_ROUNDS, on the other hand, expects the following:
6253 -1 Undefined
6254 0 Round to 0
6255 1 Round to nearest
6256 2 Round to +inf
6257 3 Round to -inf
6258
6259 To perform the conversion, we do:
6260 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6261 */
6262
6263 MachineFunction &MF = DAG.getMachineFunction();
6264 const TargetMachine &TM = MF.getTarget();
6265 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6266 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006267 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006268 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006269
6270 // Save FP Control Word to stack slot
6271 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006272 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006273
Dale Johannesene4d209d2009-02-03 20:21:25 +00006274 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006275 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006276
6277 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006278 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006279
6280 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006281 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006282 DAG.getNode(ISD::SRL, dl, MVT::i16,
6283 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006284 CWD, DAG.getConstant(0x800, MVT::i16)),
6285 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006286 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006287 DAG.getNode(ISD::SRL, dl, MVT::i16,
6288 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006289 CWD, DAG.getConstant(0x400, MVT::i16)),
6290 DAG.getConstant(9, MVT::i8));
6291
Dan Gohman475871a2008-07-27 21:46:04 +00006292 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006293 DAG.getNode(ISD::AND, dl, MVT::i16,
6294 DAG.getNode(ISD::ADD, dl, MVT::i16,
6295 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006296 DAG.getConstant(1, MVT::i16)),
6297 DAG.getConstant(3, MVT::i16));
6298
6299
Duncan Sands83ec4b62008-06-06 12:08:01 +00006300 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006301 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006302}
6303
Dan Gohman475871a2008-07-27 21:46:04 +00006304SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006305 MVT VT = Op.getValueType();
6306 MVT OpVT = VT;
6307 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006308 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006309
6310 Op = Op.getOperand(0);
6311 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006312 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006313 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006314 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006315 }
Evan Cheng18efe262007-12-14 02:13:44 +00006316
Evan Cheng152804e2007-12-14 08:30:15 +00006317 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6318 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006319 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006320
6321 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006322 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006323 Ops.push_back(Op);
6324 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6325 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6326 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006327 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006328
6329 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006330 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006331
Evan Cheng18efe262007-12-14 02:13:44 +00006332 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006333 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006334 return Op;
6335}
6336
Dan Gohman475871a2008-07-27 21:46:04 +00006337SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006338 MVT VT = Op.getValueType();
6339 MVT OpVT = VT;
6340 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006341 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006342
6343 Op = Op.getOperand(0);
6344 if (VT == MVT::i8) {
6345 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006346 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006347 }
Evan Cheng152804e2007-12-14 08:30:15 +00006348
6349 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6350 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006351 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006352
6353 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006355 Ops.push_back(Op);
6356 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6357 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6358 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006359 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006360
Evan Cheng18efe262007-12-14 02:13:44 +00006361 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006362 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006363 return Op;
6364}
6365
Mon P Wangaf9b9522008-12-18 21:42:19 +00006366SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6367 MVT VT = Op.getValueType();
6368 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006369 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006370
Mon P Wangaf9b9522008-12-18 21:42:19 +00006371 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6372 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6373 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6374 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6375 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6376 //
6377 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6378 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6379 // return AloBlo + AloBhi + AhiBlo;
6380
6381 SDValue A = Op.getOperand(0);
6382 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006383
Dale Johannesene4d209d2009-02-03 20:21:25 +00006384 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006385 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6386 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006387 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006388 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6389 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006390 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006391 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6392 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006393 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006394 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6395 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006396 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006397 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6398 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006399 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006400 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6401 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006402 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006403 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6404 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006405 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6406 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006407 return Res;
6408}
6409
6410
Bill Wendling74c37652008-12-09 22:08:41 +00006411SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6412 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6413 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006414 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6415 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006416 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006417 SDValue LHS = N->getOperand(0);
6418 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006419 unsigned BaseOp = 0;
6420 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006421 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006422
6423 switch (Op.getOpcode()) {
6424 default: assert(0 && "Unknown ovf instruction!");
6425 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006426 // A subtract of one will be selected as a INC. Note that INC doesn't
6427 // set CF, so we can't do this for UADDO.
6428 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6429 if (C->getAPIntValue() == 1) {
6430 BaseOp = X86ISD::INC;
6431 Cond = X86::COND_O;
6432 break;
6433 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006434 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006435 Cond = X86::COND_O;
6436 break;
6437 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006438 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006439 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006440 break;
6441 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006442 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6443 // set CF, so we can't do this for USUBO.
6444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6445 if (C->getAPIntValue() == 1) {
6446 BaseOp = X86ISD::DEC;
6447 Cond = X86::COND_O;
6448 break;
6449 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006450 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006451 Cond = X86::COND_O;
6452 break;
6453 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006454 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006455 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006456 break;
6457 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006458 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006459 Cond = X86::COND_O;
6460 break;
6461 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006462 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006463 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006464 break;
6465 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006466
Bill Wendling61edeb52008-12-02 01:06:39 +00006467 // Also sets EFLAGS.
6468 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006469 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006470
Bill Wendling61edeb52008-12-02 01:06:39 +00006471 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006472 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006473 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006474
Bill Wendling61edeb52008-12-02 01:06:39 +00006475 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6476 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006477}
6478
Dan Gohman475871a2008-07-27 21:46:04 +00006479SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006480 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006481 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006482 unsigned Reg = 0;
6483 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006484 switch(T.getSimpleVT()) {
6485 default:
6486 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006487 case MVT::i8: Reg = X86::AL; size = 1; break;
6488 case MVT::i16: Reg = X86::AX; size = 2; break;
6489 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006490 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006491 assert(Subtarget->is64Bit() && "Node not type legal!");
6492 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006493 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006494 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006495 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006496 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006497 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006498 Op.getOperand(1),
6499 Op.getOperand(3),
6500 DAG.getTargetConstant(size, MVT::i8),
6501 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006502 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006503 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006504 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006505 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006506 return cpOut;
6507}
6508
Duncan Sands1607f052008-12-01 11:39:25 +00006509SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006510 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006511 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006512 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006513 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006514 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006515 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006516 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6517 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006518 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006519 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006520 DAG.getConstant(32, MVT::i8));
6521 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006522 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006523 rdx.getValue(1)
6524 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006525 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006526}
6527
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006528SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6529 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006530 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006531 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006532 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006533 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006534 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006535 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006536 Node->getOperand(0),
6537 Node->getOperand(1), negOp,
6538 cast<AtomicSDNode>(Node)->getSrcValue(),
6539 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006540}
6541
Evan Cheng0db9fe62006-04-25 20:13:52 +00006542/// LowerOperation - Provide custom lowering hooks for some operations.
6543///
Dan Gohman475871a2008-07-27 21:46:04 +00006544SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006545 switch (Op.getOpcode()) {
6546 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006547 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6548 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006549 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6550 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6551 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6552 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6553 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6554 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6555 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006556 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006557 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006558 case ISD::SHL_PARTS:
6559 case ISD::SRA_PARTS:
6560 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6561 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006562 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006563 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006564 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006565 case ISD::FABS: return LowerFABS(Op, DAG);
6566 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006567 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006568 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006569 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006570 case ISD::SELECT: return LowerSELECT(Op, DAG);
6571 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006572 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006573 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006574 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006575 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006576 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006577 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006578 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006579 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006580 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6581 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006582 case ISD::FRAME_TO_ARGS_OFFSET:
6583 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006584 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006585 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006586 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006587 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006588 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6589 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006590 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006591 case ISD::SADDO:
6592 case ISD::UADDO:
6593 case ISD::SSUBO:
6594 case ISD::USUBO:
6595 case ISD::SMULO:
6596 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006597 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006598 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006599}
6600
Duncan Sands1607f052008-12-01 11:39:25 +00006601void X86TargetLowering::
6602ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6603 SelectionDAG &DAG, unsigned NewOp) {
6604 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006605 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006606 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6607
6608 SDValue Chain = Node->getOperand(0);
6609 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006610 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006611 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006612 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006613 Node->getOperand(2), DAG.getIntPtrConstant(1));
6614 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6615 // have a MemOperand. Pass the info through as a normal operand.
6616 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6617 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6618 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006619 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006620 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006621 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006622 Results.push_back(Result.getValue(2));
6623}
6624
Duncan Sands126d9072008-07-04 11:47:58 +00006625/// ReplaceNodeResults - Replace a node with an illegal result type
6626/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006627void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6628 SmallVectorImpl<SDValue>&Results,
6629 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006630 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006631 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006632 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006633 assert(false && "Do not know how to custom type legalize this operation!");
6634 return;
6635 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006636 std::pair<SDValue,SDValue> Vals =
6637 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006638 SDValue FIST = Vals.first, StackSlot = Vals.second;
6639 if (FIST.getNode() != 0) {
6640 MVT VT = N->getValueType(0);
6641 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006642 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006643 }
6644 return;
6645 }
6646 case ISD::READCYCLECOUNTER: {
6647 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6648 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006649 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006650 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006651 rd.getValue(1));
6652 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006653 eax.getValue(2));
6654 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6655 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006656 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006657 Results.push_back(edx.getValue(1));
6658 return;
6659 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006660 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006661 MVT T = N->getValueType(0);
6662 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6663 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006664 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006665 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006666 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006667 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006668 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6669 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006670 cpInL.getValue(1));
6671 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006672 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006673 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006674 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006675 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006676 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006677 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006678 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006679 swapInL.getValue(1));
6680 SDValue Ops[] = { swapInH.getValue(0),
6681 N->getOperand(1),
6682 swapInH.getValue(1) };
6683 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006684 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006685 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6686 MVT::i32, Result.getValue(1));
6687 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6688 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006689 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006690 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006691 Results.push_back(cpOutH.getValue(1));
6692 return;
6693 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006694 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006695 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6696 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006697 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006698 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6699 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006700 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006701 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6702 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006703 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006704 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6705 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006706 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006707 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6708 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006709 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006710 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6711 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006712 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006713 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6714 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006715 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006716}
6717
Evan Cheng72261582005-12-20 06:22:03 +00006718const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6719 switch (Opcode) {
6720 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006721 case X86ISD::BSF: return "X86ISD::BSF";
6722 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006723 case X86ISD::SHLD: return "X86ISD::SHLD";
6724 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006725 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006726 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006727 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006728 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006729 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006730 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006731 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6732 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6733 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006734 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006735 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006736 case X86ISD::CALL: return "X86ISD::CALL";
6737 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6738 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006739 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006740 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006741 case X86ISD::COMI: return "X86ISD::COMI";
6742 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006743 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006744 case X86ISD::CMOV: return "X86ISD::CMOV";
6745 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006746 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006747 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6748 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006749 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006750 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006751 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006752 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006753 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6754 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006755 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006756 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006757 case X86ISD::FMAX: return "X86ISD::FMAX";
6758 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006759 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6760 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006761 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006762 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006763 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006764 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006765 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006766 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6767 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006768 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6769 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6770 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6771 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6772 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6773 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006774 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6775 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006776 case X86ISD::VSHL: return "X86ISD::VSHL";
6777 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006778 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6779 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6780 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6781 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6782 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6783 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6784 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6785 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6786 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6787 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006788 case X86ISD::ADD: return "X86ISD::ADD";
6789 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006790 case X86ISD::SMUL: return "X86ISD::SMUL";
6791 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006792 case X86ISD::INC: return "X86ISD::INC";
6793 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006794 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006795 }
6796}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006797
Chris Lattnerc9addb72007-03-30 23:15:24 +00006798// isLegalAddressingMode - Return true if the addressing mode represented
6799// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006800bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006801 const Type *Ty) const {
6802 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006803
Chris Lattnerc9addb72007-03-30 23:15:24 +00006804 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6805 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6806 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006807
Chris Lattnerc9addb72007-03-30 23:15:24 +00006808 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006809 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006810 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6811 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006812 // If BaseGV requires a register, we cannot also have a BaseReg.
6813 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6814 AM.HasBaseReg)
6815 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006816
6817 // X86-64 only supports addr of globals in small code model.
6818 if (Subtarget->is64Bit()) {
6819 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6820 return false;
6821 // If lower 4G is not available, then we must use rip-relative addressing.
6822 if (AM.BaseOffs || AM.Scale > 1)
6823 return false;
6824 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006825 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006826
Chris Lattnerc9addb72007-03-30 23:15:24 +00006827 switch (AM.Scale) {
6828 case 0:
6829 case 1:
6830 case 2:
6831 case 4:
6832 case 8:
6833 // These scales always work.
6834 break;
6835 case 3:
6836 case 5:
6837 case 9:
6838 // These scales are formed with basereg+scalereg. Only accept if there is
6839 // no basereg yet.
6840 if (AM.HasBaseReg)
6841 return false;
6842 break;
6843 default: // Other stuff never works.
6844 return false;
6845 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006846
Chris Lattnerc9addb72007-03-30 23:15:24 +00006847 return true;
6848}
6849
6850
Evan Cheng2bd122c2007-10-26 01:56:11 +00006851bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6852 if (!Ty1->isInteger() || !Ty2->isInteger())
6853 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006854 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6855 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006856 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006857 return false;
6858 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006859}
6860
Duncan Sands83ec4b62008-06-06 12:08:01 +00006861bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6862 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006863 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006864 unsigned NumBits1 = VT1.getSizeInBits();
6865 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006866 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006867 return false;
6868 return Subtarget->is64Bit() || NumBits1 < 64;
6869}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006870
Dan Gohman97121ba2009-04-08 00:15:30 +00006871bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006872 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006873 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6874}
6875
6876bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006877 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006878 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6879}
6880
Evan Cheng8b944d32009-05-28 00:35:15 +00006881bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
6882 // i16 instructions are longer (0x66 prefix) and potentially slower.
6883 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
6884}
6885
Evan Cheng60c07e12006-07-05 22:17:51 +00006886/// isShuffleMaskLegal - Targets can use this to indicate that they only
6887/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6888/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6889/// are assumed to be legal.
6890bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006891X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6892 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006893 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006894 if (VT.getSizeInBits() == 64)
6895 return false;
6896
6897 // FIXME: pshufb, blends, palignr, shifts.
6898 return (VT.getVectorNumElements() == 2 ||
6899 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6900 isMOVLMask(M, VT) ||
6901 isSHUFPMask(M, VT) ||
6902 isPSHUFDMask(M, VT) ||
6903 isPSHUFHWMask(M, VT) ||
6904 isPSHUFLWMask(M, VT) ||
6905 isUNPCKLMask(M, VT) ||
6906 isUNPCKHMask(M, VT) ||
6907 isUNPCKL_v_undef_Mask(M, VT) ||
6908 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006909}
6910
Dan Gohman7d8143f2008-04-09 20:09:42 +00006911bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006912X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006913 MVT VT) const {
6914 unsigned NumElts = VT.getVectorNumElements();
6915 // FIXME: This collection of masks seems suspect.
6916 if (NumElts == 2)
6917 return true;
6918 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6919 return (isMOVLMask(Mask, VT) ||
6920 isCommutedMOVLMask(Mask, VT, true) ||
6921 isSHUFPMask(Mask, VT) ||
6922 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006923 }
6924 return false;
6925}
6926
6927//===----------------------------------------------------------------------===//
6928// X86 Scheduler Hooks
6929//===----------------------------------------------------------------------===//
6930
Mon P Wang63307c32008-05-05 19:05:59 +00006931// private utility function
6932MachineBasicBlock *
6933X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6934 MachineBasicBlock *MBB,
6935 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006936 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006937 unsigned LoadOpc,
6938 unsigned CXchgOpc,
6939 unsigned copyOpc,
6940 unsigned notOpc,
6941 unsigned EAXreg,
6942 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006943 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006944 // For the atomic bitwise operator, we generate
6945 // thisMBB:
6946 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006947 // ld t1 = [bitinstr.addr]
6948 // op t2 = t1, [bitinstr.val]
6949 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006950 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6951 // bz newMBB
6952 // fallthrough -->nextMBB
6953 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6954 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006955 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006956 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006957
Mon P Wang63307c32008-05-05 19:05:59 +00006958 /// First build the CFG
6959 MachineFunction *F = MBB->getParent();
6960 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006961 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6962 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6963 F->insert(MBBIter, newMBB);
6964 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006965
Mon P Wang63307c32008-05-05 19:05:59 +00006966 // Move all successors to thisMBB to nextMBB
6967 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006968
Mon P Wang63307c32008-05-05 19:05:59 +00006969 // Update thisMBB to fall through to newMBB
6970 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006971
Mon P Wang63307c32008-05-05 19:05:59 +00006972 // newMBB jumps to itself and fall through to nextMBB
6973 newMBB->addSuccessor(nextMBB);
6974 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006975
Mon P Wang63307c32008-05-05 19:05:59 +00006976 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006977 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00006978 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00006979 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00006980 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006981 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00006982 int numArgs = bInstr->getNumOperands() - 1;
6983 for (int i=0; i < numArgs; ++i)
6984 argOpers[i] = &bInstr->getOperand(i+1);
6985
6986 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006987 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
6988 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00006989
Dale Johannesen140be2d2008-08-19 18:47:28 +00006990 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006991 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006992 for (int i=0; i <= lastAddrIndx; ++i)
6993 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006994
Dale Johannesen140be2d2008-08-19 18:47:28 +00006995 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006996 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006997 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006999 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007000 tt = t1;
7001
Dale Johannesen140be2d2008-08-19 18:47:28 +00007002 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007003 assert((argOpers[valArgIndx]->isReg() ||
7004 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007005 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007006 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007007 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007008 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007009 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007010 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007011 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007012
Dale Johannesene4d209d2009-02-03 20:21:25 +00007013 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007014 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007015
Dale Johannesene4d209d2009-02-03 20:21:25 +00007016 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007017 for (int i=0; i <= lastAddrIndx; ++i)
7018 (*MIB).addOperand(*argOpers[i]);
7019 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007020 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7021 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7022
Dale Johannesene4d209d2009-02-03 20:21:25 +00007023 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007024 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007025
Mon P Wang63307c32008-05-05 19:05:59 +00007026 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007027 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007028
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007029 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007030 return nextMBB;
7031}
7032
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007033// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007034MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007035X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7036 MachineBasicBlock *MBB,
7037 unsigned regOpcL,
7038 unsigned regOpcH,
7039 unsigned immOpcL,
7040 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007041 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007042 // For the atomic bitwise operator, we generate
7043 // thisMBB (instructions are in pairs, except cmpxchg8b)
7044 // ld t1,t2 = [bitinstr.addr]
7045 // newMBB:
7046 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7047 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007048 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007049 // mov ECX, EBX <- t5, t6
7050 // mov EAX, EDX <- t1, t2
7051 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7052 // mov t3, t4 <- EAX, EDX
7053 // bz newMBB
7054 // result in out1, out2
7055 // fallthrough -->nextMBB
7056
7057 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7058 const unsigned LoadOpc = X86::MOV32rm;
7059 const unsigned copyOpc = X86::MOV32rr;
7060 const unsigned NotOpc = X86::NOT32r;
7061 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7062 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7063 MachineFunction::iterator MBBIter = MBB;
7064 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007065
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007066 /// First build the CFG
7067 MachineFunction *F = MBB->getParent();
7068 MachineBasicBlock *thisMBB = MBB;
7069 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7070 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7071 F->insert(MBBIter, newMBB);
7072 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007073
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007074 // Move all successors to thisMBB to nextMBB
7075 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007076
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007077 // Update thisMBB to fall through to newMBB
7078 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007079
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007080 // newMBB jumps to itself and fall through to nextMBB
7081 newMBB->addSuccessor(nextMBB);
7082 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007083
Dale Johannesene4d209d2009-02-03 20:21:25 +00007084 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007085 // Insert instructions into newMBB based on incoming instruction
7086 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007087 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007088 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007089 MachineOperand& dest1Oper = bInstr->getOperand(0);
7090 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007091 MachineOperand* argOpers[2 + X86AddrNumOperands];
7092 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007093 argOpers[i] = &bInstr->getOperand(i+2);
7094
7095 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007096 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007097
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007098 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007099 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007100 for (int i=0; i <= lastAddrIndx; ++i)
7101 (*MIB).addOperand(*argOpers[i]);
7102 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007103 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007104 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007105 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007106 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007107 MachineOperand newOp3 = *(argOpers[3]);
7108 if (newOp3.isImm())
7109 newOp3.setImm(newOp3.getImm()+4);
7110 else
7111 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007112 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007113 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007114
7115 // t3/4 are defined later, at the bottom of the loop
7116 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7117 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007118 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007119 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007120 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007121 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7122
7123 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7124 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007125 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007126 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7127 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007128 } else {
7129 tt1 = t1;
7130 tt2 = t2;
7131 }
7132
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007133 int valArgIndx = lastAddrIndx + 1;
7134 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007135 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007136 "invalid operand");
7137 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7138 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007139 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007140 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007141 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007142 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007143 if (regOpcL != X86::MOV32rr)
7144 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007145 (*MIB).addOperand(*argOpers[valArgIndx]);
7146 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007147 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007148 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007149 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007150 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007151 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007152 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007153 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007154 if (regOpcH != X86::MOV32rr)
7155 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007156 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007157
Dale Johannesene4d209d2009-02-03 20:21:25 +00007158 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007159 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007160 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007161 MIB.addReg(t2);
7162
Dale Johannesene4d209d2009-02-03 20:21:25 +00007163 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007164 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007165 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007166 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007167
Dale Johannesene4d209d2009-02-03 20:21:25 +00007168 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007169 for (int i=0; i <= lastAddrIndx; ++i)
7170 (*MIB).addOperand(*argOpers[i]);
7171
7172 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7173 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7174
Dale Johannesene4d209d2009-02-03 20:21:25 +00007175 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007176 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007177 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007178 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007179
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007180 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007182
7183 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7184 return nextMBB;
7185}
7186
7187// private utility function
7188MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007189X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7190 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007191 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007192 // For the atomic min/max operator, we generate
7193 // thisMBB:
7194 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007195 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007196 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007197 // cmp t1, t2
7198 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007199 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007200 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7201 // bz newMBB
7202 // fallthrough -->nextMBB
7203 //
7204 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7205 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007206 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007207 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007208
Mon P Wang63307c32008-05-05 19:05:59 +00007209 /// First build the CFG
7210 MachineFunction *F = MBB->getParent();
7211 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007212 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7213 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7214 F->insert(MBBIter, newMBB);
7215 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007216
Mon P Wang63307c32008-05-05 19:05:59 +00007217 // Move all successors to thisMBB to nextMBB
7218 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007219
Mon P Wang63307c32008-05-05 19:05:59 +00007220 // Update thisMBB to fall through to newMBB
7221 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007222
Mon P Wang63307c32008-05-05 19:05:59 +00007223 // newMBB jumps to newMBB and fall through to nextMBB
7224 newMBB->addSuccessor(nextMBB);
7225 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007226
Dale Johannesene4d209d2009-02-03 20:21:25 +00007227 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007228 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007229 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007230 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007231 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007232 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007233 int numArgs = mInstr->getNumOperands() - 1;
7234 for (int i=0; i < numArgs; ++i)
7235 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007236
Mon P Wang63307c32008-05-05 19:05:59 +00007237 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007238 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7239 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007240
Mon P Wangab3e7472008-05-05 22:56:23 +00007241 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007242 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007243 for (int i=0; i <= lastAddrIndx; ++i)
7244 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007245
Mon P Wang63307c32008-05-05 19:05:59 +00007246 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007247 assert((argOpers[valArgIndx]->isReg() ||
7248 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007249 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007250
7251 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007252 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007253 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007254 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007255 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007256 (*MIB).addOperand(*argOpers[valArgIndx]);
7257
Dale Johannesene4d209d2009-02-03 20:21:25 +00007258 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007259 MIB.addReg(t1);
7260
Dale Johannesene4d209d2009-02-03 20:21:25 +00007261 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007262 MIB.addReg(t1);
7263 MIB.addReg(t2);
7264
7265 // Generate movc
7266 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007267 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007268 MIB.addReg(t2);
7269 MIB.addReg(t1);
7270
7271 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007272 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007273 for (int i=0; i <= lastAddrIndx; ++i)
7274 (*MIB).addOperand(*argOpers[i]);
7275 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007276 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7277 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007278
Dale Johannesene4d209d2009-02-03 20:21:25 +00007279 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007280 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007281
Mon P Wang63307c32008-05-05 19:05:59 +00007282 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007283 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007284
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007285 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007286 return nextMBB;
7287}
7288
7289
Evan Cheng60c07e12006-07-05 22:17:51 +00007290MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007291X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007292 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007293 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007294 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007295 switch (MI->getOpcode()) {
7296 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007297 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007298 case X86::CMOV_FR32:
7299 case X86::CMOV_FR64:
7300 case X86::CMOV_V4F32:
7301 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007302 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007303 // To "insert" a SELECT_CC instruction, we actually have to insert the
7304 // diamond control-flow pattern. The incoming instruction knows the
7305 // destination vreg to set, the condition code register to branch on, the
7306 // true/false values to select between, and a branch opcode to use.
7307 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007308 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007309 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007310
Evan Cheng60c07e12006-07-05 22:17:51 +00007311 // thisMBB:
7312 // ...
7313 // TrueVal = ...
7314 // cmpTY ccX, r1, r2
7315 // bCC copy1MBB
7316 // fallthrough --> copy0MBB
7317 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007318 MachineFunction *F = BB->getParent();
7319 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7320 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007321 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007322 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007323 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007324 F->insert(It, copy0MBB);
7325 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007326 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007327 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007328 sinkMBB->transferSuccessors(BB);
7329
7330 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007331 BB->addSuccessor(copy0MBB);
7332 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007333
Evan Cheng60c07e12006-07-05 22:17:51 +00007334 // copy0MBB:
7335 // %FalseValue = ...
7336 // # fallthrough to sinkMBB
7337 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007338
Evan Cheng60c07e12006-07-05 22:17:51 +00007339 // Update machine-CFG edges
7340 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007341
Evan Cheng60c07e12006-07-05 22:17:51 +00007342 // sinkMBB:
7343 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7344 // ...
7345 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007346 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007347 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7348 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7349
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007350 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007351 return BB;
7352 }
7353
Dale Johannesen849f2142007-07-03 00:53:03 +00007354 case X86::FP32_TO_INT16_IN_MEM:
7355 case X86::FP32_TO_INT32_IN_MEM:
7356 case X86::FP32_TO_INT64_IN_MEM:
7357 case X86::FP64_TO_INT16_IN_MEM:
7358 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007359 case X86::FP64_TO_INT64_IN_MEM:
7360 case X86::FP80_TO_INT16_IN_MEM:
7361 case X86::FP80_TO_INT32_IN_MEM:
7362 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007363 // Change the floating point control register to use "round towards zero"
7364 // mode when truncating to an integer value.
7365 MachineFunction *F = BB->getParent();
7366 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007367 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007368
7369 // Load the old value of the high byte of the control word...
7370 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007371 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007372 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007373 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007374
7375 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007376 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007377 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007378
7379 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007380 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007381
7382 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007383 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007384 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007385
7386 // Get the X86 opcode to use.
7387 unsigned Opc;
7388 switch (MI->getOpcode()) {
7389 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007390 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7391 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7392 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7393 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7394 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7395 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007396 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7397 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7398 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007399 }
7400
7401 X86AddressMode AM;
7402 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007403 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007404 AM.BaseType = X86AddressMode::RegBase;
7405 AM.Base.Reg = Op.getReg();
7406 } else {
7407 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007408 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007409 }
7410 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007411 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007412 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007413 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007414 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007415 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007416 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007417 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007418 AM.GV = Op.getGlobal();
7419 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007420 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007421 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007422 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007423 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007424
7425 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007426 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007427
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007428 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007429 return BB;
7430 }
Mon P Wang63307c32008-05-05 19:05:59 +00007431 case X86::ATOMAND32:
7432 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007433 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007434 X86::LCMPXCHG32, X86::MOV32rr,
7435 X86::NOT32r, X86::EAX,
7436 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007437 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007438 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7439 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007440 X86::LCMPXCHG32, X86::MOV32rr,
7441 X86::NOT32r, X86::EAX,
7442 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007443 case X86::ATOMXOR32:
7444 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007445 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007446 X86::LCMPXCHG32, X86::MOV32rr,
7447 X86::NOT32r, X86::EAX,
7448 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007449 case X86::ATOMNAND32:
7450 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007451 X86::AND32ri, X86::MOV32rm,
7452 X86::LCMPXCHG32, X86::MOV32rr,
7453 X86::NOT32r, X86::EAX,
7454 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007455 case X86::ATOMMIN32:
7456 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7457 case X86::ATOMMAX32:
7458 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7459 case X86::ATOMUMIN32:
7460 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7461 case X86::ATOMUMAX32:
7462 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007463
7464 case X86::ATOMAND16:
7465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7466 X86::AND16ri, X86::MOV16rm,
7467 X86::LCMPXCHG16, X86::MOV16rr,
7468 X86::NOT16r, X86::AX,
7469 X86::GR16RegisterClass);
7470 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007472 X86::OR16ri, X86::MOV16rm,
7473 X86::LCMPXCHG16, X86::MOV16rr,
7474 X86::NOT16r, X86::AX,
7475 X86::GR16RegisterClass);
7476 case X86::ATOMXOR16:
7477 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7478 X86::XOR16ri, X86::MOV16rm,
7479 X86::LCMPXCHG16, X86::MOV16rr,
7480 X86::NOT16r, X86::AX,
7481 X86::GR16RegisterClass);
7482 case X86::ATOMNAND16:
7483 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7484 X86::AND16ri, X86::MOV16rm,
7485 X86::LCMPXCHG16, X86::MOV16rr,
7486 X86::NOT16r, X86::AX,
7487 X86::GR16RegisterClass, true);
7488 case X86::ATOMMIN16:
7489 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7490 case X86::ATOMMAX16:
7491 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7492 case X86::ATOMUMIN16:
7493 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7494 case X86::ATOMUMAX16:
7495 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7496
7497 case X86::ATOMAND8:
7498 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7499 X86::AND8ri, X86::MOV8rm,
7500 X86::LCMPXCHG8, X86::MOV8rr,
7501 X86::NOT8r, X86::AL,
7502 X86::GR8RegisterClass);
7503 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007504 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007505 X86::OR8ri, X86::MOV8rm,
7506 X86::LCMPXCHG8, X86::MOV8rr,
7507 X86::NOT8r, X86::AL,
7508 X86::GR8RegisterClass);
7509 case X86::ATOMXOR8:
7510 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7511 X86::XOR8ri, X86::MOV8rm,
7512 X86::LCMPXCHG8, X86::MOV8rr,
7513 X86::NOT8r, X86::AL,
7514 X86::GR8RegisterClass);
7515 case X86::ATOMNAND8:
7516 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7517 X86::AND8ri, X86::MOV8rm,
7518 X86::LCMPXCHG8, X86::MOV8rr,
7519 X86::NOT8r, X86::AL,
7520 X86::GR8RegisterClass, true);
7521 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007522 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007523 case X86::ATOMAND64:
7524 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007525 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007526 X86::LCMPXCHG64, X86::MOV64rr,
7527 X86::NOT64r, X86::RAX,
7528 X86::GR64RegisterClass);
7529 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007530 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7531 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007532 X86::LCMPXCHG64, X86::MOV64rr,
7533 X86::NOT64r, X86::RAX,
7534 X86::GR64RegisterClass);
7535 case X86::ATOMXOR64:
7536 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007537 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007538 X86::LCMPXCHG64, X86::MOV64rr,
7539 X86::NOT64r, X86::RAX,
7540 X86::GR64RegisterClass);
7541 case X86::ATOMNAND64:
7542 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7543 X86::AND64ri32, X86::MOV64rm,
7544 X86::LCMPXCHG64, X86::MOV64rr,
7545 X86::NOT64r, X86::RAX,
7546 X86::GR64RegisterClass, true);
7547 case X86::ATOMMIN64:
7548 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7549 case X86::ATOMMAX64:
7550 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7551 case X86::ATOMUMIN64:
7552 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7553 case X86::ATOMUMAX64:
7554 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007555
7556 // This group does 64-bit operations on a 32-bit host.
7557 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007558 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007559 X86::AND32rr, X86::AND32rr,
7560 X86::AND32ri, X86::AND32ri,
7561 false);
7562 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007563 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007564 X86::OR32rr, X86::OR32rr,
7565 X86::OR32ri, X86::OR32ri,
7566 false);
7567 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007568 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007569 X86::XOR32rr, X86::XOR32rr,
7570 X86::XOR32ri, X86::XOR32ri,
7571 false);
7572 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007573 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007574 X86::AND32rr, X86::AND32rr,
7575 X86::AND32ri, X86::AND32ri,
7576 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007577 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007578 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007579 X86::ADD32rr, X86::ADC32rr,
7580 X86::ADD32ri, X86::ADC32ri,
7581 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007582 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007583 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007584 X86::SUB32rr, X86::SBB32rr,
7585 X86::SUB32ri, X86::SBB32ri,
7586 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007587 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007588 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007589 X86::MOV32rr, X86::MOV32rr,
7590 X86::MOV32ri, X86::MOV32ri,
7591 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007592 }
7593}
7594
7595//===----------------------------------------------------------------------===//
7596// X86 Optimization Hooks
7597//===----------------------------------------------------------------------===//
7598
Dan Gohman475871a2008-07-27 21:46:04 +00007599void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007600 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007601 APInt &KnownZero,
7602 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007603 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007604 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007605 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007606 assert((Opc >= ISD::BUILTIN_OP_END ||
7607 Opc == ISD::INTRINSIC_WO_CHAIN ||
7608 Opc == ISD::INTRINSIC_W_CHAIN ||
7609 Opc == ISD::INTRINSIC_VOID) &&
7610 "Should use MaskedValueIsZero if you don't know whether Op"
7611 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007612
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007613 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007614 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007615 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007616 case X86ISD::ADD:
7617 case X86ISD::SUB:
7618 case X86ISD::SMUL:
7619 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007620 case X86ISD::INC:
7621 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007622 // These nodes' second result is a boolean.
7623 if (Op.getResNo() == 0)
7624 break;
7625 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007626 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007627 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7628 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007629 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007630 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007631}
Chris Lattner259e97c2006-01-31 19:43:35 +00007632
Evan Cheng206ee9d2006-07-07 08:33:52 +00007633/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007634/// node is a GlobalAddress + offset.
7635bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7636 GlobalValue* &GA, int64_t &Offset) const{
7637 if (N->getOpcode() == X86ISD::Wrapper) {
7638 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007639 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007640 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007641 return true;
7642 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007643 }
Evan Chengad4196b2008-05-12 19:56:52 +00007644 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007645}
7646
Evan Chengad4196b2008-05-12 19:56:52 +00007647static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7648 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007649 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007650 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007651 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007652 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007653 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007654 return false;
7655}
7656
Nate Begeman9008ca62009-04-27 18:41:29 +00007657static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7658 MVT EVT, SDNode *&Base,
Evan Chengad4196b2008-05-12 19:56:52 +00007659 SelectionDAG &DAG, MachineFrameInfo *MFI,
7660 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007661 Base = NULL;
7662 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007663 if (N->getMaskElt(i) < 0) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007664 if (!Base)
7665 return false;
7666 continue;
7667 }
7668
Dan Gohman475871a2008-07-27 21:46:04 +00007669 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007670 if (!Elt.getNode() ||
7671 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007672 return false;
7673 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007674 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007675 if (Base->getOpcode() == ISD::UNDEF)
7676 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007677 continue;
7678 }
7679 if (Elt.getOpcode() == ISD::UNDEF)
7680 continue;
7681
Nate Begemanabc01992009-06-05 21:37:30 +00007682 LoadSDNode *LD = cast<LoadSDNode>(Elt);
7683 LoadSDNode *LDBase = cast<LoadSDNode>(Base);
7684 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007685 return false;
7686 }
7687 return true;
7688}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007689
7690/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7691/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7692/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007693/// order. In the case of v2i64, it will see if it can rewrite the
7694/// shuffle to be an appropriate build vector so it can take advantage of
7695// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007696static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007697 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007698 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007699 MVT VT = N->getValueType(0);
7700 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007701 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7702 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007703
7704 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7705 // where the upper half is 0, it is advantageous to rewrite it as a build
7706 // vector of (0, val) so it can use movq.
7707 if (VT == MVT::v2i64) {
7708 SDValue In[2];
7709 In[0] = N->getOperand(0);
7710 In[1] = N->getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00007711 int Idx0 = SVN->getMaskElt(0);
7712 int Idx1 = SVN->getMaskElt(1);
7713 // FIXME: can we take advantage of undef index?
7714 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang1e955802009-04-03 02:43:30 +00007715 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7716 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7717 ConstantSDNode* InsertVecIdx =
7718 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7719 if (InsertVecIdx &&
Nate Begeman9008ca62009-04-27 18:41:29 +00007720 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang1e955802009-04-03 02:43:30 +00007721 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7722 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7723 In[Idx0/2].getOperand(1),
7724 In[Idx1/2].getOperand(Idx1 % 2));
7725 }
7726 }
7727 }
7728
7729 // Try to combine a vector_shuffle into a 128-bit load.
7730 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007731 SDNode *Base = NULL;
Nate Begeman9008ca62009-04-27 18:41:29 +00007732 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007733 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007734
Dan Gohmand3006222007-07-27 17:16:43 +00007735 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007736 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007737 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007738 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007739 LD->isVolatile());
7740 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7741 LD->getSrcValue(), LD->getSrcValueOffset(),
7742 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007743}
7744
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007745/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007746static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007747 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007748 const X86Subtarget *Subtarget,
7749 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007750 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007751 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007752
Evan Chengd880b972008-05-09 21:53:03 +00007753 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007754 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007755 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007756
Duncan Sands83ec4b62008-06-06 12:08:01 +00007757 MVT VT = N->getValueType(0);
7758 MVT EVT = VT.getVectorElementType();
Nate Begemanabc01992009-06-05 21:37:30 +00007759
7760 // Before or during type legalization, we want to try and convert a
7761 // build_vector of an i64 load and a zero value into vzext_movl before the
7762 // legalizer can break it up.
7763 // FIXME: does the case below remove the need to do this?
7764 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) {
7765 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00007766 return SDValue();
Nate Begemanabc01992009-06-05 21:37:30 +00007767
7768 // This must be an insertion into a zero vector.
7769 SDValue HighElt = N->getOperand(1);
7770 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007771 return SDValue();
Nate Begemanabc01992009-06-05 21:37:30 +00007772
7773 // Value must be a load.
7774 SDNode *Base = N->getOperand(0).getNode();
7775 if (!isa<LoadSDNode>(Base)) {
7776 if (Base->getOpcode() != ISD::BIT_CONVERT)
7777 return SDValue();
7778 Base = Base->getOperand(0).getNode();
7779 if (!isa<LoadSDNode>(Base))
7780 return SDValue();
7781 }
7782
7783 // Transform it into VZEXT_LOAD addr.
7784 LoadSDNode *LD = cast<LoadSDNode>(Base);
7785
7786 // Load must not be an extload.
7787 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
7788 return SDValue();
7789
7790 // Load type should legal type so we don't have to legalize it.
7791 if (!TLI.isTypeLegal(VT))
7792 return SDValue();
7793
7794 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7795 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7796 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
7797 TargetLowering::TargetLoweringOpt TLO(DAG);
7798 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7799 DCI.CommitTargetLoweringOpt(TLO);
7800 return ResNode;
Evan Chengd880b972008-05-09 21:53:03 +00007801 }
Evan Chengd880b972008-05-09 21:53:03 +00007802
Nate Begemanabc01992009-06-05 21:37:30 +00007803 // The type legalizer will have broken apart v2i64 build_vector created during
7804 // widening before the code which handles that case is run. Look for build
7805 // vector (load, load + 4, 0/undef, 0/undef)
7806 if (VT == MVT::v4i32 || VT == MVT::v4f32) {
7807 LoadSDNode *LD0 = dyn_cast<LoadSDNode>(N->getOperand(0));
7808 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(N->getOperand(1));
7809 if (!LD0 || !LD1)
7810 return SDValue();
7811 if (LD0->getExtensionType() != ISD::NON_EXTLOAD ||
7812 LD1->getExtensionType() != ISD::NON_EXTLOAD)
7813 return SDValue();
7814 // Make sure the second elt is a consecutive load.
7815 if (!TLI.isConsecutiveLoad(LD1, LD0, EVT.getSizeInBits()/8, 1,
7816 DAG.getMachineFunction().getFrameInfo()))
7817 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007818
Nate Begemanabc01992009-06-05 21:37:30 +00007819 SDValue N2 = N->getOperand(2);
7820 SDValue N3 = N->getOperand(3);
7821 if (!isZeroNode(N2) && N2.getOpcode() != ISD::UNDEF)
7822 return SDValue();
7823 if (!isZeroNode(N3) && N3.getOpcode() != ISD::UNDEF)
7824 return SDValue();
7825
7826 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
7827 SDValue Ops[] = { LD0->getChain(), LD0->getBasePtr() };
7828 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
7829 TargetLowering::TargetLoweringOpt TLO(DAG);
7830 TLO.CombineTo(SDValue(LD0, 1), ResNode.getValue(1));
7831 DCI.CommitTargetLoweringOpt(TLO);
7832 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7833 }
7834 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007835}
Evan Chengd880b972008-05-09 21:53:03 +00007836
Chris Lattner83e6c992006-10-04 06:57:07 +00007837/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007838static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007839 const X86Subtarget *Subtarget) {
7840 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007841 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007842 // Get the LHS/RHS of the select.
7843 SDValue LHS = N->getOperand(1);
7844 SDValue RHS = N->getOperand(2);
7845
Chris Lattner83e6c992006-10-04 06:57:07 +00007846 // If we have SSE[12] support, try to form min/max nodes.
7847 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007848 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7849 Cond.getOpcode() == ISD::SETCC) {
7850 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007851
Chris Lattner47b4ce82009-03-11 05:48:52 +00007852 unsigned Opcode = 0;
7853 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7854 switch (CC) {
7855 default: break;
7856 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7857 case ISD::SETULE:
7858 case ISD::SETLE:
7859 if (!UnsafeFPMath) break;
7860 // FALL THROUGH.
7861 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7862 case ISD::SETLT:
7863 Opcode = X86ISD::FMIN;
7864 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007865
Chris Lattner47b4ce82009-03-11 05:48:52 +00007866 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7867 case ISD::SETUGT:
7868 case ISD::SETGT:
7869 if (!UnsafeFPMath) break;
7870 // FALL THROUGH.
7871 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7872 case ISD::SETGE:
7873 Opcode = X86ISD::FMAX;
7874 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007875 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007876 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7877 switch (CC) {
7878 default: break;
7879 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7880 case ISD::SETUGT:
7881 case ISD::SETGT:
7882 if (!UnsafeFPMath) break;
7883 // FALL THROUGH.
7884 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7885 case ISD::SETGE:
7886 Opcode = X86ISD::FMIN;
7887 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007888
Chris Lattner47b4ce82009-03-11 05:48:52 +00007889 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7890 case ISD::SETULE:
7891 case ISD::SETLE:
7892 if (!UnsafeFPMath) break;
7893 // FALL THROUGH.
7894 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7895 case ISD::SETLT:
7896 Opcode = X86ISD::FMAX;
7897 break;
7898 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007899 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007900
Chris Lattner47b4ce82009-03-11 05:48:52 +00007901 if (Opcode)
7902 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007903 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007904
Chris Lattnerd1980a52009-03-12 06:52:53 +00007905 // If this is a select between two integer constants, try to do some
7906 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007907 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7908 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007909 // Don't do this for crazy integer types.
7910 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7911 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007912 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007913 bool NeedsCondInvert = false;
7914
Chris Lattnercee56e72009-03-13 05:53:31 +00007915 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007916 // Efficiently invertible.
7917 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7918 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7919 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7920 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007921 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007922 }
7923
7924 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007925 if (FalseC->getAPIntValue() == 0 &&
7926 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007927 if (NeedsCondInvert) // Invert the condition if needed.
7928 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7929 DAG.getConstant(1, Cond.getValueType()));
7930
7931 // Zero extend the condition if needed.
7932 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7933
Chris Lattnercee56e72009-03-13 05:53:31 +00007934 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007935 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7936 DAG.getConstant(ShAmt, MVT::i8));
7937 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007938
7939 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007940 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007941 if (NeedsCondInvert) // Invert the condition if needed.
7942 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7943 DAG.getConstant(1, Cond.getValueType()));
7944
7945 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007946 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7947 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007948 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007949 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007950 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007951
7952 // Optimize cases that will turn into an LEA instruction. This requires
7953 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7954 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7955 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7956 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7957
7958 bool isFastMultiplier = false;
7959 if (Diff < 10) {
7960 switch ((unsigned char)Diff) {
7961 default: break;
7962 case 1: // result = add base, cond
7963 case 2: // result = lea base( , cond*2)
7964 case 3: // result = lea base(cond, cond*2)
7965 case 4: // result = lea base( , cond*4)
7966 case 5: // result = lea base(cond, cond*4)
7967 case 8: // result = lea base( , cond*8)
7968 case 9: // result = lea base(cond, cond*8)
7969 isFastMultiplier = true;
7970 break;
7971 }
7972 }
7973
7974 if (isFastMultiplier) {
7975 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7976 if (NeedsCondInvert) // Invert the condition if needed.
7977 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7978 DAG.getConstant(1, Cond.getValueType()));
7979
7980 // Zero extend the condition if needed.
7981 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7982 Cond);
7983 // Scale the condition by the difference.
7984 if (Diff != 1)
7985 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7986 DAG.getConstant(Diff, Cond.getValueType()));
7987
7988 // Add the base if non-zero.
7989 if (FalseC->getAPIntValue() != 0)
7990 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7991 SDValue(FalseC, 0));
7992 return Cond;
7993 }
7994 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007995 }
7996 }
7997
Dan Gohman475871a2008-07-27 21:46:04 +00007998 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007999}
8000
Chris Lattnerd1980a52009-03-12 06:52:53 +00008001/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8002static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8003 TargetLowering::DAGCombinerInfo &DCI) {
8004 DebugLoc DL = N->getDebugLoc();
8005
8006 // If the flag operand isn't dead, don't touch this CMOV.
8007 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8008 return SDValue();
8009
8010 // If this is a select between two integer constants, try to do some
8011 // optimizations. Note that the operands are ordered the opposite of SELECT
8012 // operands.
8013 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8014 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8015 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8016 // larger than FalseC (the false value).
8017 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8018
8019 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8020 CC = X86::GetOppositeBranchCondition(CC);
8021 std::swap(TrueC, FalseC);
8022 }
8023
8024 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008025 // This is efficient for any integer data type (including i8/i16) and
8026 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008027 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8028 SDValue Cond = N->getOperand(3);
8029 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8030 DAG.getConstant(CC, MVT::i8), Cond);
8031
8032 // Zero extend the condition if needed.
8033 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8034
8035 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8036 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8037 DAG.getConstant(ShAmt, MVT::i8));
8038 if (N->getNumValues() == 2) // Dead flag value?
8039 return DCI.CombineTo(N, Cond, SDValue());
8040 return Cond;
8041 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008042
8043 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8044 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008045 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8046 SDValue Cond = N->getOperand(3);
8047 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8048 DAG.getConstant(CC, MVT::i8), Cond);
8049
8050 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008051 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8052 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008053 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8054 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008055
Chris Lattner97a29a52009-03-13 05:22:11 +00008056 if (N->getNumValues() == 2) // Dead flag value?
8057 return DCI.CombineTo(N, Cond, SDValue());
8058 return Cond;
8059 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008060
8061 // Optimize cases that will turn into an LEA instruction. This requires
8062 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8063 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8064 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8065 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8066
8067 bool isFastMultiplier = false;
8068 if (Diff < 10) {
8069 switch ((unsigned char)Diff) {
8070 default: break;
8071 case 1: // result = add base, cond
8072 case 2: // result = lea base( , cond*2)
8073 case 3: // result = lea base(cond, cond*2)
8074 case 4: // result = lea base( , cond*4)
8075 case 5: // result = lea base(cond, cond*4)
8076 case 8: // result = lea base( , cond*8)
8077 case 9: // result = lea base(cond, cond*8)
8078 isFastMultiplier = true;
8079 break;
8080 }
8081 }
8082
8083 if (isFastMultiplier) {
8084 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8085 SDValue Cond = N->getOperand(3);
8086 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8087 DAG.getConstant(CC, MVT::i8), Cond);
8088 // Zero extend the condition if needed.
8089 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8090 Cond);
8091 // Scale the condition by the difference.
8092 if (Diff != 1)
8093 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8094 DAG.getConstant(Diff, Cond.getValueType()));
8095
8096 // Add the base if non-zero.
8097 if (FalseC->getAPIntValue() != 0)
8098 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8099 SDValue(FalseC, 0));
8100 if (N->getNumValues() == 2) // Dead flag value?
8101 return DCI.CombineTo(N, Cond, SDValue());
8102 return Cond;
8103 }
8104 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008105 }
8106 }
8107 return SDValue();
8108}
8109
8110
Evan Cheng0b0cd912009-03-28 05:57:29 +00008111/// PerformMulCombine - Optimize a single multiply with constant into two
8112/// in order to implement it with two cheaper instructions, e.g.
8113/// LEA + SHL, LEA + LEA.
8114static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8115 TargetLowering::DAGCombinerInfo &DCI) {
8116 if (DAG.getMachineFunction().
8117 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8118 return SDValue();
8119
8120 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8121 return SDValue();
8122
8123 MVT VT = N->getValueType(0);
8124 if (VT != MVT::i64)
8125 return SDValue();
8126
8127 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8128 if (!C)
8129 return SDValue();
8130 uint64_t MulAmt = C->getZExtValue();
8131 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8132 return SDValue();
8133
8134 uint64_t MulAmt1 = 0;
8135 uint64_t MulAmt2 = 0;
8136 if ((MulAmt % 9) == 0) {
8137 MulAmt1 = 9;
8138 MulAmt2 = MulAmt / 9;
8139 } else if ((MulAmt % 5) == 0) {
8140 MulAmt1 = 5;
8141 MulAmt2 = MulAmt / 5;
8142 } else if ((MulAmt % 3) == 0) {
8143 MulAmt1 = 3;
8144 MulAmt2 = MulAmt / 3;
8145 }
8146 if (MulAmt2 &&
8147 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8148 DebugLoc DL = N->getDebugLoc();
8149
8150 if (isPowerOf2_64(MulAmt2) &&
8151 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8152 // If second multiplifer is pow2, issue it first. We want the multiply by
8153 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8154 // is an add.
8155 std::swap(MulAmt1, MulAmt2);
8156
8157 SDValue NewMul;
8158 if (isPowerOf2_64(MulAmt1))
8159 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8160 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8161 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008162 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008163 DAG.getConstant(MulAmt1, VT));
8164
8165 if (isPowerOf2_64(MulAmt2))
8166 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8167 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8168 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008169 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008170 DAG.getConstant(MulAmt2, VT));
8171
8172 // Do not add new nodes to DAG combiner worklist.
8173 DCI.CombineTo(N, NewMul, false);
8174 }
8175 return SDValue();
8176}
8177
8178
Nate Begeman740ab032009-01-26 00:52:55 +00008179/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8180/// when possible.
8181static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8182 const X86Subtarget *Subtarget) {
8183 // On X86 with SSE2 support, we can transform this to a vector shift if
8184 // all elements are shifted by the same amount. We can't do this in legalize
8185 // because the a constant vector is typically transformed to a constant pool
8186 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008187 if (!Subtarget->hasSSE2())
8188 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008189
Nate Begeman740ab032009-01-26 00:52:55 +00008190 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008191 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8192 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008193
Mon P Wang3becd092009-01-28 08:12:05 +00008194 SDValue ShAmtOp = N->getOperand(1);
8195 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008196 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008197 SDValue BaseShAmt;
8198 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8199 unsigned NumElts = VT.getVectorNumElements();
8200 unsigned i = 0;
8201 for (; i != NumElts; ++i) {
8202 SDValue Arg = ShAmtOp.getOperand(i);
8203 if (Arg.getOpcode() == ISD::UNDEF) continue;
8204 BaseShAmt = Arg;
8205 break;
8206 }
8207 for (; i != NumElts; ++i) {
8208 SDValue Arg = ShAmtOp.getOperand(i);
8209 if (Arg.getOpcode() == ISD::UNDEF) continue;
8210 if (Arg != BaseShAmt) {
8211 return SDValue();
8212 }
8213 }
8214 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008215 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8216 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8217 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008218 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008219 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008220
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008221 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008222 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008223 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008224 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008225
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008226 // The shift amount is identical so we can do a vector shift.
8227 SDValue ValOp = N->getOperand(0);
8228 switch (N->getOpcode()) {
8229 default:
8230 assert(0 && "Unknown shift opcode!");
8231 break;
8232 case ISD::SHL:
8233 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008234 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008235 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8236 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008237 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008238 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008239 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8240 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008241 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008242 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008243 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8244 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008245 break;
8246 case ISD::SRA:
8247 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008248 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008249 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8250 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008251 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008252 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008253 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8254 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008255 break;
8256 case ISD::SRL:
8257 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008258 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008259 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8260 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008261 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008262 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008263 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8264 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008265 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008266 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008267 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8268 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008269 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008270 }
8271 return SDValue();
8272}
8273
Chris Lattner149a4e52008-02-22 02:09:43 +00008274/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008275static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008276 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008277 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8278 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008279 // A preferable solution to the general problem is to figure out the right
8280 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008281
8282 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008283 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008284 MVT VT = St->getValue().getValueType();
8285 if (VT.getSizeInBits() != 64)
8286 return SDValue();
8287
Devang Patel578efa92009-06-05 21:57:13 +00008288 const Function *F = DAG.getMachineFunction().getFunction();
8289 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8290 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8291 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008292 if ((VT.isVector() ||
8293 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008294 isa<LoadSDNode>(St->getValue()) &&
8295 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8296 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008297 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008298 LoadSDNode *Ld = 0;
8299 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008300 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008301 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008302 // Must be a store of a load. We currently handle two cases: the load
8303 // is a direct child, and it's under an intervening TokenFactor. It is
8304 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008305 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008306 Ld = cast<LoadSDNode>(St->getChain());
8307 else if (St->getValue().hasOneUse() &&
8308 ChainVal->getOpcode() == ISD::TokenFactor) {
8309 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008310 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008311 TokenFactorIndex = i;
8312 Ld = cast<LoadSDNode>(St->getValue());
8313 } else
8314 Ops.push_back(ChainVal->getOperand(i));
8315 }
8316 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008317
Evan Cheng536e6672009-03-12 05:59:15 +00008318 if (!Ld || !ISD::isNormalLoad(Ld))
8319 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008320
Evan Cheng536e6672009-03-12 05:59:15 +00008321 // If this is not the MMX case, i.e. we are just turning i64 load/store
8322 // into f64 load/store, avoid the transformation if there are multiple
8323 // uses of the loaded value.
8324 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8325 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008326
Evan Cheng536e6672009-03-12 05:59:15 +00008327 DebugLoc LdDL = Ld->getDebugLoc();
8328 DebugLoc StDL = N->getDebugLoc();
8329 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8330 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8331 // pair instead.
8332 if (Subtarget->is64Bit() || F64IsLegal) {
8333 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8334 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8335 Ld->getBasePtr(), Ld->getSrcValue(),
8336 Ld->getSrcValueOffset(), Ld->isVolatile(),
8337 Ld->getAlignment());
8338 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008339 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008340 Ops.push_back(NewChain);
8341 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008342 Ops.size());
8343 }
Evan Cheng536e6672009-03-12 05:59:15 +00008344 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008345 St->getSrcValue(), St->getSrcValueOffset(),
8346 St->isVolatile(), St->getAlignment());
8347 }
Evan Cheng536e6672009-03-12 05:59:15 +00008348
8349 // Otherwise, lower to two pairs of 32-bit loads / stores.
8350 SDValue LoAddr = Ld->getBasePtr();
8351 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8352 DAG.getConstant(4, MVT::i32));
8353
8354 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8355 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8356 Ld->isVolatile(), Ld->getAlignment());
8357 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8358 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8359 Ld->isVolatile(),
8360 MinAlign(Ld->getAlignment(), 4));
8361
8362 SDValue NewChain = LoLd.getValue(1);
8363 if (TokenFactorIndex != -1) {
8364 Ops.push_back(LoLd);
8365 Ops.push_back(HiLd);
8366 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8367 Ops.size());
8368 }
8369
8370 LoAddr = St->getBasePtr();
8371 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8372 DAG.getConstant(4, MVT::i32));
8373
8374 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8375 St->getSrcValue(), St->getSrcValueOffset(),
8376 St->isVolatile(), St->getAlignment());
8377 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8378 St->getSrcValue(),
8379 St->getSrcValueOffset() + 4,
8380 St->isVolatile(),
8381 MinAlign(St->getAlignment(), 4));
8382 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008383 }
Dan Gohman475871a2008-07-27 21:46:04 +00008384 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008385}
8386
Chris Lattner6cf73262008-01-25 06:14:17 +00008387/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8388/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008389static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008390 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8391 // F[X]OR(0.0, x) -> x
8392 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008393 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8394 if (C->getValueAPF().isPosZero())
8395 return N->getOperand(1);
8396 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8397 if (C->getValueAPF().isPosZero())
8398 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008399 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008400}
8401
8402/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008403static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008404 // FAND(0.0, x) -> 0.0
8405 // FAND(x, 0.0) -> 0.0
8406 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8407 if (C->getValueAPF().isPosZero())
8408 return N->getOperand(0);
8409 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8410 if (C->getValueAPF().isPosZero())
8411 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008412 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008413}
8414
Dan Gohmane5af2d32009-01-29 01:59:02 +00008415static SDValue PerformBTCombine(SDNode *N,
8416 SelectionDAG &DAG,
8417 TargetLowering::DAGCombinerInfo &DCI) {
8418 // BT ignores high bits in the bit index operand.
8419 SDValue Op1 = N->getOperand(1);
8420 if (Op1.hasOneUse()) {
8421 unsigned BitWidth = Op1.getValueSizeInBits();
8422 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8423 APInt KnownZero, KnownOne;
8424 TargetLowering::TargetLoweringOpt TLO(DAG);
8425 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8426 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8427 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8428 DCI.CommitTargetLoweringOpt(TLO);
8429 }
8430 return SDValue();
8431}
Chris Lattner83e6c992006-10-04 06:57:07 +00008432
Dan Gohman475871a2008-07-27 21:46:04 +00008433SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008434 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008435 SelectionDAG &DAG = DCI.DAG;
8436 switch (N->getOpcode()) {
8437 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008438 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8439 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008440 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008441 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008442 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008443 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008444 case ISD::SHL:
8445 case ISD::SRA:
8446 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008447 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008448 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008449 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8450 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008451 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008452 }
8453
Dan Gohman475871a2008-07-27 21:46:04 +00008454 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008455}
8456
Evan Cheng60c07e12006-07-05 22:17:51 +00008457//===----------------------------------------------------------------------===//
8458// X86 Inline Assembly Support
8459//===----------------------------------------------------------------------===//
8460
Chris Lattnerf4dff842006-07-11 02:54:03 +00008461/// getConstraintType - Given a constraint letter, return the type of
8462/// constraint it is for this target.
8463X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008464X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8465 if (Constraint.size() == 1) {
8466 switch (Constraint[0]) {
8467 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008468 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008469 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008470 case 'r':
8471 case 'R':
8472 case 'l':
8473 case 'q':
8474 case 'Q':
8475 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008476 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008477 case 'Y':
8478 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008479 case 'e':
8480 case 'Z':
8481 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008482 default:
8483 break;
8484 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008485 }
Chris Lattner4234f572007-03-25 02:14:49 +00008486 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008487}
8488
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008489/// LowerXConstraint - try to replace an X constraint, which matches anything,
8490/// with another that has more specific requirements based on the type of the
8491/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008492const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008493LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008494 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8495 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008496 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008497 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008498 return "Y";
8499 if (Subtarget->hasSSE1())
8500 return "x";
8501 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008502
Chris Lattner5e764232008-04-26 23:02:14 +00008503 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008504}
8505
Chris Lattner48884cd2007-08-25 00:47:38 +00008506/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8507/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008508void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008509 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008510 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008511 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008512 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008513 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008514
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008515 switch (Constraint) {
8516 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008517 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008518 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008519 if (C->getZExtValue() <= 31) {
8520 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008521 break;
8522 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008523 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008524 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008525 case 'J':
8526 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8527 if (C->getZExtValue() <= 63) {
8528 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8529 break;
8530 }
8531 }
8532 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008533 case 'N':
8534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008535 if (C->getZExtValue() <= 255) {
8536 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008537 break;
8538 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008539 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008540 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008541 case 'e': {
8542 // 32-bit signed value
8543 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8544 const ConstantInt *CI = C->getConstantIntValue();
8545 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8546 // Widen to 64 bits here to get it sign extended.
8547 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8548 break;
8549 }
8550 // FIXME gcc accepts some relocatable values here too, but only in certain
8551 // memory models; it's complicated.
8552 }
8553 return;
8554 }
8555 case 'Z': {
8556 // 32-bit unsigned value
8557 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8558 const ConstantInt *CI = C->getConstantIntValue();
8559 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8560 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8561 break;
8562 }
8563 }
8564 // FIXME gcc accepts some relocatable values here too, but only in certain
8565 // memory models; it's complicated.
8566 return;
8567 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008568 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008569 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008570 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008571 // Widen to 64 bits here to get it sign extended.
8572 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008573 break;
8574 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008575
Chris Lattnerdc43a882007-05-03 16:52:29 +00008576 // If we are in non-pic codegen mode, we allow the address of a global (with
8577 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008578 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008579 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008580
Chris Lattner49921962009-05-08 18:23:14 +00008581 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8582 while (1) {
8583 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8584 Offset += GA->getOffset();
8585 break;
8586 } else if (Op.getOpcode() == ISD::ADD) {
8587 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8588 Offset += C->getZExtValue();
8589 Op = Op.getOperand(0);
8590 continue;
8591 }
8592 } else if (Op.getOpcode() == ISD::SUB) {
8593 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8594 Offset += -C->getZExtValue();
8595 Op = Op.getOperand(0);
8596 continue;
8597 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008598 }
Chris Lattner49921962009-05-08 18:23:14 +00008599
8600 // Otherwise, this isn't something we can handle, reject it.
8601 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008603
Chris Lattner49921962009-05-08 18:23:14 +00008604 if (hasMemory)
8605 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8606 else
8607 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8608 Offset);
8609 Result = Op;
8610 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008611 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008612 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008613
Gabor Greifba36cb52008-08-28 21:40:38 +00008614 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008615 Ops.push_back(Result);
8616 return;
8617 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008618 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8619 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008620}
8621
Chris Lattner259e97c2006-01-31 19:43:35 +00008622std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008623getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008624 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008625 if (Constraint.size() == 1) {
8626 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008627 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008628 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008629 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8630 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008631 if (VT == MVT::i32)
8632 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8633 else if (VT == MVT::i16)
8634 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8635 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008636 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008637 else if (VT == MVT::i64)
8638 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8639 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008640 }
8641 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008642
Chris Lattner1efa40f2006-02-22 00:56:39 +00008643 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008644}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008645
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008646std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008647X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008648 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008649 // First, see if this is a constraint that directly corresponds to an LLVM
8650 // register class.
8651 if (Constraint.size() == 1) {
8652 // GCC Constraint Letters
8653 switch (Constraint[0]) {
8654 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008655 case 'r': // GENERAL_REGS
8656 case 'R': // LEGACY_REGS
8657 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008658 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008659 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008660 if (VT == MVT::i16)
8661 return std::make_pair(0U, X86::GR16RegisterClass);
8662 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008663 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008664 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008665 case 'f': // FP Stack registers.
8666 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8667 // value to the correct fpstack register class.
8668 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8669 return std::make_pair(0U, X86::RFP32RegisterClass);
8670 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8671 return std::make_pair(0U, X86::RFP64RegisterClass);
8672 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008673 case 'y': // MMX_REGS if MMX allowed.
8674 if (!Subtarget->hasMMX()) break;
8675 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008676 case 'Y': // SSE_REGS if SSE2 allowed
8677 if (!Subtarget->hasSSE2()) break;
8678 // FALL THROUGH.
8679 case 'x': // SSE_REGS if SSE1 allowed
8680 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008681
8682 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008683 default: break;
8684 // Scalar SSE types.
8685 case MVT::f32:
8686 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008687 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008688 case MVT::f64:
8689 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008690 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008691 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008692 case MVT::v16i8:
8693 case MVT::v8i16:
8694 case MVT::v4i32:
8695 case MVT::v2i64:
8696 case MVT::v4f32:
8697 case MVT::v2f64:
8698 return std::make_pair(0U, X86::VR128RegisterClass);
8699 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008700 break;
8701 }
8702 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008703
Chris Lattnerf76d1802006-07-31 23:26:50 +00008704 // Use the default implementation in TargetLowering to convert the register
8705 // constraint into a member of a register class.
8706 std::pair<unsigned, const TargetRegisterClass*> Res;
8707 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008708
8709 // Not found as a standard register?
8710 if (Res.second == 0) {
8711 // GCC calls "st(0)" just plain "st".
8712 if (StringsEqualNoCase("{st}", Constraint)) {
8713 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008714 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008715 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008716 // 'A' means EAX + EDX.
8717 if (Constraint == "A") {
8718 Res.first = X86::EAX;
8719 Res.second = X86::GRADRegisterClass;
8720 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008721 return Res;
8722 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008723
Chris Lattnerf76d1802006-07-31 23:26:50 +00008724 // Otherwise, check to see if this is a register class of the wrong value
8725 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8726 // turn into {ax},{dx}.
8727 if (Res.second->hasType(VT))
8728 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008729
Chris Lattnerf76d1802006-07-31 23:26:50 +00008730 // All of the single-register GCC register classes map their values onto
8731 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8732 // really want an 8-bit or 32-bit register, map to the appropriate register
8733 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008734 if (Res.second == X86::GR16RegisterClass) {
8735 if (VT == MVT::i8) {
8736 unsigned DestReg = 0;
8737 switch (Res.first) {
8738 default: break;
8739 case X86::AX: DestReg = X86::AL; break;
8740 case X86::DX: DestReg = X86::DL; break;
8741 case X86::CX: DestReg = X86::CL; break;
8742 case X86::BX: DestReg = X86::BL; break;
8743 }
8744 if (DestReg) {
8745 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008746 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008747 }
8748 } else if (VT == MVT::i32) {
8749 unsigned DestReg = 0;
8750 switch (Res.first) {
8751 default: break;
8752 case X86::AX: DestReg = X86::EAX; break;
8753 case X86::DX: DestReg = X86::EDX; break;
8754 case X86::CX: DestReg = X86::ECX; break;
8755 case X86::BX: DestReg = X86::EBX; break;
8756 case X86::SI: DestReg = X86::ESI; break;
8757 case X86::DI: DestReg = X86::EDI; break;
8758 case X86::BP: DestReg = X86::EBP; break;
8759 case X86::SP: DestReg = X86::ESP; break;
8760 }
8761 if (DestReg) {
8762 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008763 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008764 }
8765 } else if (VT == MVT::i64) {
8766 unsigned DestReg = 0;
8767 switch (Res.first) {
8768 default: break;
8769 case X86::AX: DestReg = X86::RAX; break;
8770 case X86::DX: DestReg = X86::RDX; break;
8771 case X86::CX: DestReg = X86::RCX; break;
8772 case X86::BX: DestReg = X86::RBX; break;
8773 case X86::SI: DestReg = X86::RSI; break;
8774 case X86::DI: DestReg = X86::RDI; break;
8775 case X86::BP: DestReg = X86::RBP; break;
8776 case X86::SP: DestReg = X86::RSP; break;
8777 }
8778 if (DestReg) {
8779 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008780 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008781 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008782 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008783 } else if (Res.second == X86::FR32RegisterClass ||
8784 Res.second == X86::FR64RegisterClass ||
8785 Res.second == X86::VR128RegisterClass) {
8786 // Handle references to XMM physical registers that got mapped into the
8787 // wrong class. This can happen with constraints like {xmm0} where the
8788 // target independent register mapper will just pick the first match it can
8789 // find, ignoring the required type.
8790 if (VT == MVT::f32)
8791 Res.second = X86::FR32RegisterClass;
8792 else if (VT == MVT::f64)
8793 Res.second = X86::FR64RegisterClass;
8794 else if (X86::VR128RegisterClass->hasType(VT))
8795 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008796 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008797
Chris Lattnerf76d1802006-07-31 23:26:50 +00008798 return Res;
8799}
Mon P Wang0c397192008-10-30 08:01:45 +00008800
8801//===----------------------------------------------------------------------===//
8802// X86 Widen vector type
8803//===----------------------------------------------------------------------===//
8804
8805/// getWidenVectorType: given a vector type, returns the type to widen
8806/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8807/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008808/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008809/// scalarizing vs using the wider vector type.
8810
Dan Gohmanc13cf132009-01-15 17:34:08 +00008811MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008812 assert(VT.isVector());
8813 if (isTypeLegal(VT))
8814 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008815
Mon P Wang0c397192008-10-30 08:01:45 +00008816 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8817 // type based on element type. This would speed up our search (though
8818 // it may not be worth it since the size of the list is relatively
8819 // small).
8820 MVT EltVT = VT.getVectorElementType();
8821 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008822
Mon P Wang0c397192008-10-30 08:01:45 +00008823 // On X86, it make sense to widen any vector wider than 1
8824 if (NElts <= 1)
8825 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008826
8827 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008828 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8829 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008830
8831 if (isTypeLegal(SVT) &&
8832 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008833 SVT.getVectorNumElements() > NElts)
8834 return SVT;
8835 }
8836 return MVT::Other;
8837}