blob: 1de51ee51ae20048f086b6ffd332c624db74e733 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000022#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000025#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000026#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000032#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000033#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000034#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000037#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039using namespace llvm;
40
Mon P Wang3c81d352008-11-23 04:37:22 +000041static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000042DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000043
Evan Cheng10e86422008-04-25 19:11:04 +000044// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000045static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
46 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000047
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000048X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000050 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000051 X86ScalarSSEf64 = Subtarget->hasSSE2();
52 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000053 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000056 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000057
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000058 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000062 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000063 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000065 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000066
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000068 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000071 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Evan Cheng03294662008-10-14 21:26:46 +000087 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000088
Scott Michelfdc40a02009-02-17 22:15:04 +000089 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +000090 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000095 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
97 // SETOEQ and SETUNE require checking two conditions.
98 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
99 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
100 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
101 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
102 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
103 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000104
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
106 // operation.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
108 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
109 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000110
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000112 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000113 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000114 } else if (!UseSoftFloat) {
115 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000116 // We have an impenetrably clever algorithm for ui64->double only.
117 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000118 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000119 // We have an algorithm for SSE2, and we turn this into a 64-bit
120 // FILD for other targets.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000122 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000123
124 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
125 // this operation.
126 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
127 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000128
Devang Patel6a784892009-06-05 18:48:29 +0000129 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000130 // SSE has no i16 to fp conversion, only i32
131 if (X86ScalarSSEf32) {
132 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
133 // f32 and f64 cases are Legal, f80 case is not
134 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
135 } else {
136 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000139 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000140 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
141 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000142 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143
Dale Johannesen73328d12007-09-19 23:55:34 +0000144 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
145 // are Legal, f80 is custom lowered.
146 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
147 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000148
Evan Cheng02568ff2006-01-30 22:13:22 +0000149 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
150 // this operation.
151 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
152 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
153
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000154 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000155 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000156 // f32 and f64 cases are Legal, f80 case is not
157 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000158 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000159 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000160 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 }
162
163 // Handle FP_TO_UINT by promoting the destination to a larger signed
164 // conversion.
165 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
166 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
167 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
168
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 if (Subtarget->is64Bit()) {
170 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000172 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000173 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 // Expand FP_TO_UINT into a select.
175 // FIXME: We would like to use a Custom expander here eventually to do
176 // the optimal thing for SSE vs. the default expansion in the legalizer.
177 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
178 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000179 // With SSE3 we can use fisttpll to convert to a signed i64; without
180 // SSE, we're stuck with a fistpll.
181 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000182 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000183
Chris Lattner399610a2006-12-05 18:22:22 +0000184 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000185 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000186 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
187 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
188 }
Chris Lattner21f66852005-12-23 05:15:23 +0000189
Dan Gohmanb00ee212008-02-18 19:34:53 +0000190 // Scalar integer divide and remainder are lowered to use operations that
191 // produce two results, to match the available instructions. This exposes
192 // the two-result form to trivial CSE, which is able to combine x/y and x%y
193 // into a single instruction.
194 //
195 // Scalar integer multiply-high is also lowered to use two-result
196 // operations, to match the available instructions. However, plain multiply
197 // (low) operations are left as Legal, as there are single-result
198 // instructions for this in x86. Using the two-result multiply instructions
199 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
204 setOperationAction(ISD::SREM , MVT::i8 , Expand);
205 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000206 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
207 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
208 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
209 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
210 setOperationAction(ISD::SREM , MVT::i16 , Expand);
211 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000212 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
213 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
214 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
215 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
216 setOperationAction(ISD::SREM , MVT::i32 , Expand);
217 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000218 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
219 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
220 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
221 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
222 setOperationAction(ISD::SREM , MVT::i64 , Expand);
223 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000224
Evan Chengc35497f2006-10-30 08:02:39 +0000225 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000226 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000227 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
228 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
232 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
234 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000235 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000237 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000238 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000241 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
242 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000243 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000247 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
248 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit()) {
250 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000251 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
252 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 }
254
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000255 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000256 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000257
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258 // These should be promoted to a larger select which is supported.
259 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
260 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000261 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000262 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
263 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
264 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
265 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000266 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
268 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
269 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
270 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
271 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000272 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (Subtarget->is64Bit()) {
274 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
275 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
276 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000277 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000278 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000279 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000280
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000281 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000282 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000283 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000285 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000286 if (Subtarget->is64Bit())
287 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000288 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
290 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
291 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
292 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000293 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000294 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000295 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000296 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
297 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
298 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000299 if (Subtarget->is64Bit()) {
300 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
301 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
302 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
303 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000304
Evan Chengd2cde682008-03-10 19:38:10 +0000305 if (Subtarget->hasSSE1())
306 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000307
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000308 if (!Subtarget->hasSSE2())
309 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
310
Mon P Wang63307c32008-05-05 19:05:59 +0000311 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
313 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
314 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
315 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000316
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
319 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000321
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000322 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000323 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
328 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000330 }
331
Dan Gohman7f460202008-06-30 20:59:49 +0000332 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
333 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000334 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000335 if (!Subtarget->isTargetDarwin() &&
336 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000337 !Subtarget->isTargetCygMing()) {
338 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
339 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
340 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000341
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000342 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
343 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
344 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
345 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
346 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000347 setExceptionPointerRegister(X86::RAX);
348 setExceptionSelectorRegister(X86::RDX);
349 } else {
350 setExceptionPointerRegister(X86::EAX);
351 setExceptionSelectorRegister(X86::EDX);
352 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000353 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000354 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
355
Duncan Sandsf7331b32007-09-11 14:10:23 +0000356 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000357
Chris Lattnerda68d302008-01-15 21:58:22 +0000358 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000359
Nate Begemanacc398c2006-01-25 18:21:52 +0000360 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
361 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000362 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000363 if (Subtarget->is64Bit()) {
364 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000365 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000366 } else {
367 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000368 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000369 }
Evan Chengae642192007-03-02 23:16:35 +0000370
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000371 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000372 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
374 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000375 if (Subtarget->isTargetCygMing())
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
377 else
378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000379
Evan Chengc7ce29b2009-02-13 22:36:38 +0000380 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000381 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000382 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000383 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
384 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000385
Evan Cheng223547a2006-01-31 22:28:30 +0000386 // Use ANDPD to simulate FABS.
387 setOperationAction(ISD::FABS , MVT::f64, Custom);
388 setOperationAction(ISD::FABS , MVT::f32, Custom);
389
390 // Use XORP to simulate FNEG.
391 setOperationAction(ISD::FNEG , MVT::f64, Custom);
392 setOperationAction(ISD::FNEG , MVT::f32, Custom);
393
Evan Cheng68c47cb2007-01-05 07:55:56 +0000394 // Use ANDPD and ORPD to simulate FCOPYSIGN.
395 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
396 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
397
Evan Chengd25e9e82006-02-02 00:28:23 +0000398 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000399 setOperationAction(ISD::FSIN , MVT::f64, Expand);
400 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000401 setOperationAction(ISD::FSIN , MVT::f32, Expand);
402 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403
Chris Lattnera54aa942006-01-29 06:26:08 +0000404 // Expand FP immediates into loads from the stack, except for the special
405 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000406 addLegalFPImmediate(APFloat(+0.0)); // xorpd
407 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000408 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000409 // Use SSE for f32, x87 for f64.
410 // Set up the FP register classes.
411 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
412 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
413
414 // Use ANDPS to simulate FABS.
415 setOperationAction(ISD::FABS , MVT::f32, Custom);
416
417 // Use XORP to simulate FNEG.
418 setOperationAction(ISD::FNEG , MVT::f32, Custom);
419
420 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
421
422 // Use ANDPS and ORPS to simulate FCOPYSIGN.
423 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
424 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
425
426 // We don't support sin/cos/fmod
427 setOperationAction(ISD::FSIN , MVT::f32, Expand);
428 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429
Nate Begemane1795842008-02-14 08:57:00 +0000430 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000431 addLegalFPImmediate(APFloat(+0.0f)); // xorps
432 addLegalFPImmediate(APFloat(+0.0)); // FLD0
433 addLegalFPImmediate(APFloat(+1.0)); // FLD1
434 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
435 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
436
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000441 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000442 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000444 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
445 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000446
Evan Cheng68c47cb2007-01-05 07:55:56 +0000447 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000448 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
450 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000451
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000456 addLegalFPImmediate(APFloat(+0.0)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000464 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000465
Dale Johannesen59a58732007-08-05 18:49:15 +0000466 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000467 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000468 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
469 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
471 {
472 bool ignored;
473 APFloat TmpFlt(+0.0);
474 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
475 &ignored);
476 addLegalFPImmediate(TmpFlt); // FLD0
477 TmpFlt.changeSign();
478 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
479 APFloat TmpFlt2(+1.0);
480 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
481 &ignored);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000486
Evan Chengc7ce29b2009-02-13 22:36:38 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000491 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000492
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000493 // Always use a library call for pow.
494 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
496 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
497
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000498 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000499 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000500 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000502 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
503
Mon P Wangf007a8b2008-11-06 05:31:54 +0000504 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000505 // (for widening) or expand (for scalarization). Then we will selectively
506 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000507 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
508 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000509 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000522 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000524 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000525 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000526 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000548 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Eli Friedman23ef1052009-06-06 03:57:58 +0000553 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000557 }
558
Evan Chengc7ce29b2009-02-13 22:36:38 +0000559 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
560 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000561 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000562 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
563 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
564 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000565 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000566 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000567
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000568 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
569 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
570 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000571 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000572
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000573 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
574 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
575 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000576 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000577
Bill Wendling74027e92007-03-15 21:24:36 +0000578 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
579 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
580
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000581 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000582 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000583 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000584 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
585 setOperationAction(ISD::AND, MVT::v2i32, Promote);
586 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
587 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000588
589 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000590 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000591 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000592 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
593 setOperationAction(ISD::OR, MVT::v2i32, Promote);
594 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
595 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000596
597 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000598 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000599 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000600 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
601 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
602 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
603 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000604
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000605 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000606 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000607 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000608 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
609 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
610 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000611 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
612 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000613 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000614
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000615 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
616 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
617 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000618 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000619 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000620
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
623 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000624 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000625
Evan Cheng52672b82008-07-22 18:39:19 +0000626 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000627 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000630
631 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000632
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000633 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000634 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
635 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
636 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
637 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
638 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000639 }
640
Evan Cheng92722532009-03-26 23:06:32 +0000641 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000642 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
643
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000644 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
645 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
646 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
647 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000648 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
649 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000650 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
651 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
652 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000653 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000654 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000655 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000656 }
657
Evan Cheng92722532009-03-26 23:06:32 +0000658 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000659 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000660
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000661 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
662 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000663 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
664 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
665 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
666 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
667
Evan Chengf7c378e2006-04-10 07:23:14 +0000668 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
669 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
670 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000671 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000672 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000673 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
674 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
675 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000676 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000677 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000678 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
679 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
680 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
681 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000682 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
683 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000684
Nate Begeman30a0de92008-07-17 16:51:19 +0000685 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
686 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
687 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
688 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000689
Evan Chengf7c378e2006-04-10 07:23:14 +0000690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
691 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000694 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000695
Evan Cheng2c3ae372006-04-12 21:21:57 +0000696 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000697 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
698 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000699 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000700 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000701 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000702 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
703 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
704 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000705 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000706
Evan Cheng2c3ae372006-04-12 21:21:57 +0000707 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
708 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
709 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
710 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000713
Nate Begemancdd1eec2008-02-12 22:51:28 +0000714 if (Subtarget->is64Bit()) {
715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000716 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000717 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000718
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000719 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000720 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
722 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
723 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
724 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
725 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
726 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
727 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
728 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
729 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
730 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000731 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000732
Chris Lattnerddf89562008-01-17 19:59:44 +0000733 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000734
Evan Cheng2c3ae372006-04-12 21:21:57 +0000735 // Custom lower v2i64 and v2f64 selects.
736 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000737 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000738 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000740
Eli Friedman23ef1052009-06-06 03:57:58 +0000741 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
742 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
743 if (!DisableMMX && Subtarget->hasMMX()) {
744 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
745 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
746 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000747 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000748
Nate Begeman14d12ca2008-02-11 04:19:36 +0000749 if (Subtarget->hasSSE41()) {
750 // FIXME: Do we need to handle scalar-to-vector here?
751 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
752
753 // i8 and i16 vectors are custom , because the source register and source
754 // source memory operand types are not the same width. f32 vectors are
755 // custom since the immediate controlling the insert encodes additional
756 // information.
757 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000760 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
761
762 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
763 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000764 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000765 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000766
767 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
769 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000770 }
771 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000772
Nate Begeman30a0de92008-07-17 16:51:19 +0000773 if (Subtarget->hasSSE42()) {
774 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
775 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Evan Cheng6be2c582006-04-05 23:38:46 +0000777 // We want to custom lower some of our intrinsics.
778 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
779
Bill Wendling74c37652008-12-09 22:08:41 +0000780 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000781 setOperationAction(ISD::SADDO, MVT::i32, Custom);
782 setOperationAction(ISD::SADDO, MVT::i64, Custom);
783 setOperationAction(ISD::UADDO, MVT::i32, Custom);
784 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000785 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
786 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
787 setOperationAction(ISD::USUBO, MVT::i32, Custom);
788 setOperationAction(ISD::USUBO, MVT::i64, Custom);
789 setOperationAction(ISD::SMULO, MVT::i32, Custom);
790 setOperationAction(ISD::SMULO, MVT::i64, Custom);
791 setOperationAction(ISD::UMULO, MVT::i32, Custom);
792 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000793
Evan Chengd54f2d52009-03-31 19:38:51 +0000794 if (!Subtarget->is64Bit()) {
795 // These libcalls are not available in 32-bit.
796 setLibcallName(RTLIB::SHL_I128, 0);
797 setLibcallName(RTLIB::SRL_I128, 0);
798 setLibcallName(RTLIB::SRA_I128, 0);
799 }
800
Evan Cheng206ee9d2006-07-07 08:33:52 +0000801 // We have target-specific dag combine patterns for the following nodes:
802 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000803 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000804 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000805 setTargetDAGCombine(ISD::SHL);
806 setTargetDAGCombine(ISD::SRA);
807 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000808 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000809 if (Subtarget->is64Bit())
810 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000811
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000812 computeRegisterProperties();
813
Evan Cheng87ed7162006-02-14 08:25:08 +0000814 // FIXME: These should be based on subtarget info. Plus, the values should
815 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000816 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
817 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
818 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000819 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000820 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000821 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000822}
823
Scott Michel5b8f82e2008-03-10 15:42:14 +0000824
Duncan Sands5480c042009-01-01 15:52:00 +0000825MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000826 return MVT::i8;
827}
828
829
Evan Cheng29286502008-01-23 23:17:41 +0000830/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
831/// the desired ByVal argument alignment.
832static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
833 if (MaxAlign == 16)
834 return;
835 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
836 if (VTy->getBitWidth() == 128)
837 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000838 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
839 unsigned EltAlign = 0;
840 getMaxByValAlign(ATy->getElementType(), EltAlign);
841 if (EltAlign > MaxAlign)
842 MaxAlign = EltAlign;
843 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
844 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
845 unsigned EltAlign = 0;
846 getMaxByValAlign(STy->getElementType(i), EltAlign);
847 if (EltAlign > MaxAlign)
848 MaxAlign = EltAlign;
849 if (MaxAlign == 16)
850 break;
851 }
852 }
853 return;
854}
855
856/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
857/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000858/// that contain SSE vectors are placed at 16-byte boundaries while the rest
859/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000860unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000861 if (Subtarget->is64Bit()) {
862 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000863 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000864 if (TyAlign > 8)
865 return TyAlign;
866 return 8;
867 }
868
Evan Cheng29286502008-01-23 23:17:41 +0000869 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000870 if (Subtarget->hasSSE1())
871 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000872 return Align;
873}
Chris Lattner2b02a442007-02-25 08:29:00 +0000874
Evan Chengf0df0312008-05-15 08:39:06 +0000875/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000876/// and store operations as a result of memset, memcpy, and memmove
877/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000878/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000879MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000880X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +0000881 bool isSrcConst, bool isSrcStr,
882 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000883 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
884 // linux. This is because the stack realignment code can't handle certain
885 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +0000886 const Function *F = DAG.getMachineFunction().getFunction();
887 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
888 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000889 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
890 return MVT::v4i32;
891 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
892 return MVT::v4f32;
893 }
Evan Chengf0df0312008-05-15 08:39:06 +0000894 if (Subtarget->is64Bit() && Size >= 8)
895 return MVT::i64;
896 return MVT::i32;
897}
898
Evan Chengcc415862007-11-09 01:32:10 +0000899/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
900/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000901SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000902 SelectionDAG &DAG) const {
903 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000904 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000905 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000906 // This doesn't have DebugLoc associated with it, but is not really the
907 // same as a Register.
908 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
909 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000910 return Table;
911}
912
Chris Lattner2b02a442007-02-25 08:29:00 +0000913//===----------------------------------------------------------------------===//
914// Return Value Calling Convention Implementation
915//===----------------------------------------------------------------------===//
916
Chris Lattner59ed56b2007-02-28 04:55:35 +0000917#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000918
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000919/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000920SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000921 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000922 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000923
Chris Lattner9774c912007-02-27 05:28:59 +0000924 SmallVector<CCValAssign, 16> RVLocs;
925 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000926 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
927 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000928 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000929
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000930 // If this is the first return lowered for this function, add the regs to the
931 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000932 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000933 for (unsigned i = 0; i != RVLocs.size(); ++i)
934 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000935 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000936 }
Dan Gohman475871a2008-07-27 21:46:04 +0000937 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000938
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000939 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000940 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000941 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000942 SDValue TailCall = Chain;
943 SDValue TargetAddress = TailCall.getOperand(1);
944 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000945 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000946 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000947 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000948 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000949 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000950 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000951 assert(StackAdjustment.getOpcode() == ISD::Constant &&
952 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000953
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000955 Operands.push_back(Chain.getOperand(0));
956 Operands.push_back(TargetAddress);
957 Operands.push_back(StackAdjustment);
958 // Copy registers used by the call. Last operand is a flag so it is not
959 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000960 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000961 Operands.push_back(Chain.getOperand(i));
962 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000963 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000964 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000965 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000966
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000967 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000968 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000969
Dan Gohman475871a2008-07-27 21:46:04 +0000970 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000971 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
972 // Operand #1 = Bytes To Pop
973 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000974
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000975 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000976 for (unsigned i = 0; i != RVLocs.size(); ++i) {
977 CCValAssign &VA = RVLocs[i];
978 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000980
Chris Lattner447ff682008-03-11 03:23:40 +0000981 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
982 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000983 if (VA.getLocReg() == X86::ST0 ||
984 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000985 // If this is a copy from an xmm register to ST(0), use an FPExtend to
986 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000987 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000988 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000989 RetOps.push_back(ValToCopy);
990 // Don't emit a copytoreg.
991 continue;
992 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000993
Evan Cheng242b38b2009-02-23 09:03:22 +0000994 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
995 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +0000996 if (Subtarget->is64Bit()) {
997 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +0000998 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +0000999 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001000 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1001 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1002 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001003 }
1004
Dale Johannesendd64c412009-02-04 00:33:20 +00001005 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001006 Flag = Chain.getValue(1);
1007 }
Dan Gohman61a92132008-04-21 23:59:07 +00001008
1009 // The x86-64 ABI for returning structs by value requires that we copy
1010 // the sret argument into %rax for the return. We saved the argument into
1011 // a virtual register in the entry block, so now we copy the value out
1012 // and into %rax.
1013 if (Subtarget->is64Bit() &&
1014 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1015 MachineFunction &MF = DAG.getMachineFunction();
1016 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1017 unsigned Reg = FuncInfo->getSRetReturnReg();
1018 if (!Reg) {
1019 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1020 FuncInfo->setSRetReturnReg(Reg);
1021 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001022 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001023
Dale Johannesendd64c412009-02-04 00:33:20 +00001024 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001025 Flag = Chain.getValue(1);
1026 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Chris Lattner447ff682008-03-11 03:23:40 +00001028 RetOps[0] = Chain; // Update chain.
1029
1030 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001031 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001032 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001033
1034 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001035 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001036}
1037
1038
Chris Lattner3085e152007-02-25 08:59:22 +00001039/// LowerCallResult - Lower the result values of an ISD::CALL into the
1040/// appropriate copies out of appropriate physical registers. This assumes that
1041/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1042/// being lowered. The returns a SDNode with the same number of values as the
1043/// ISD::CALL.
1044SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001045LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001046 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001047
Scott Michelfdc40a02009-02-17 22:15:04 +00001048 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001049 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001050 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001051 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001052 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001053 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001054 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1055
Dan Gohman475871a2008-07-27 21:46:04 +00001056 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001057
Chris Lattner3085e152007-02-25 08:59:22 +00001058 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001059 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001060 CCValAssign &VA = RVLocs[i];
1061 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001062
Torok Edwin3f142c32009-02-01 18:15:56 +00001063 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001064 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001065 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1066 cerr << "SSE register return with SSE disabled\n";
1067 exit(1);
1068 }
1069
Chris Lattner8e6da152008-03-10 21:08:41 +00001070 // If this is a call to a function that returns an fp value on the floating
1071 // point stack, but where we prefer to use the value in xmm registers, copy
1072 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001073 if ((VA.getLocReg() == X86::ST0 ||
1074 VA.getLocReg() == X86::ST1) &&
1075 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001076 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001077 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Evan Cheng79fb3b42009-02-20 20:43:02 +00001079 SDValue Val;
1080 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001081 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1082 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1083 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1084 MVT::v2i64, InFlag).getValue(1);
1085 Val = Chain.getValue(0);
1086 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1087 Val, DAG.getConstant(0, MVT::i64));
1088 } else {
1089 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1090 MVT::i64, InFlag).getValue(1);
1091 Val = Chain.getValue(0);
1092 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001093 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1094 } else {
1095 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1096 CopyVT, InFlag).getValue(1);
1097 Val = Chain.getValue(0);
1098 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001099 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001100
Dan Gohman37eed792009-02-04 17:28:58 +00001101 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001102 // Round the F80 the right size, which also moves to the appropriate xmm
1103 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001104 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001105 // This truncation won't change the value.
1106 DAG.getIntPtrConstant(1));
1107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001108
Chris Lattner8e6da152008-03-10 21:08:41 +00001109 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001110 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001111
Chris Lattner3085e152007-02-25 08:59:22 +00001112 // Merge everything together with a MERGE_VALUES node.
1113 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001114 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1115 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001116}
1117
1118
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001119//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001120// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001121//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001122// StdCall calling convention seems to be standard for many Windows' API
1123// routines and around. It differs from C calling convention just a little:
1124// callee should clean up the stack, not caller. Symbols should be also
1125// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001126// For info on fast calling convention see Fast Calling Convention (tail call)
1127// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001128
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001129/// CallIsStructReturn - Determines whether a CALL node uses struct return
1130/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001131static bool CallIsStructReturn(CallSDNode *TheCall) {
1132 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001133 if (!NumOps)
1134 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001135
Dan Gohman095cc292008-09-13 01:54:27 +00001136 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001137}
1138
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001139/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1140/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001141static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001142 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001143 if (!NumArgs)
1144 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001145
1146 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001147}
1148
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001149/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1150/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001151/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001152bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001153 if (IsVarArg)
1154 return false;
1155
Dan Gohman095cc292008-09-13 01:54:27 +00001156 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001157 default:
1158 return false;
1159 case CallingConv::X86_StdCall:
1160 return !Subtarget->is64Bit();
1161 case CallingConv::X86_FastCall:
1162 return !Subtarget->is64Bit();
1163 case CallingConv::Fast:
1164 return PerformTailCallOpt;
1165 }
1166}
1167
Dan Gohman095cc292008-09-13 01:54:27 +00001168/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1169/// given CallingConvention value.
1170CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001171 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001172 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001173 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001174 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1175 return CC_X86_64_TailCall;
1176 else
1177 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001178 }
1179
Gordon Henriksen86737662008-01-05 16:56:59 +00001180 if (CC == CallingConv::X86_FastCall)
1181 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001182 else if (CC == CallingConv::Fast)
1183 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001184 else
1185 return CC_X86_32_C;
1186}
1187
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001188/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1189/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001190NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001191X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001192 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001193 if (CC == CallingConv::X86_FastCall)
1194 return FastCall;
1195 else if (CC == CallingConv::X86_StdCall)
1196 return StdCall;
1197 return None;
1198}
1199
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001200
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001201/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1202/// in a register before calling.
1203bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1204 return !IsTailCall && !Is64Bit &&
1205 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1206 Subtarget->isPICStyleGOT();
1207}
1208
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001209/// CallRequiresFnAddressInReg - Check whether the call requires the function
1210/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001211bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001212X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001213 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001214 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1215 Subtarget->isPICStyleGOT();
1216}
1217
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001218/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1219/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001220/// the specific parameter attribute. The copy will be passed as a byval
1221/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001222static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001223CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001224 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1225 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001226 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001227 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001228 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001229}
1230
Dan Gohman475871a2008-07-27 21:46:04 +00001231SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001232 const CCValAssign &VA,
1233 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001234 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001236 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001237 ISD::ArgFlagsTy Flags =
1238 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001239 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001240 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001241
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001242 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001243 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001244 // In case of tail call optimization mark all arguments mutable. Since they
1245 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001246 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001247 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001248 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001249 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001250 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001251 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001252 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001253}
1254
Dan Gohman475871a2008-07-27 21:46:04 +00001255SDValue
1256X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001257 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001258 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001259 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001260
Gordon Henriksen86737662008-01-05 16:56:59 +00001261 const Function* Fn = MF.getFunction();
1262 if (Fn->hasExternalLinkage() &&
1263 Subtarget->isTargetCygMing() &&
1264 Fn->getName() == "main")
1265 FuncInfo->setForceFramePointer(true);
1266
1267 // Decorate the function name.
1268 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001269
Evan Cheng1bc78042006-04-26 01:20:17 +00001270 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001271 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001272 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001273 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001274 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001275 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001276
1277 assert(!(isVarArg && CC == CallingConv::Fast) &&
1278 "Var args not supported with calling convention fastcc");
1279
Chris Lattner638402b2007-02-28 07:00:42 +00001280 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001281 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001282 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001283 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001284
Dan Gohman475871a2008-07-27 21:46:04 +00001285 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001286 unsigned LastVal = ~0U;
1287 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1288 CCValAssign &VA = ArgLocs[i];
1289 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1290 // places.
1291 assert(VA.getValNo() != LastVal &&
1292 "Don't support value assigned to multiple locs yet");
1293 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001294
Chris Lattnerf39f7712007-02-28 05:46:49 +00001295 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001296 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001297 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001298 if (RegVT == MVT::i32)
1299 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001300 else if (Is64Bit && RegVT == MVT::i64)
1301 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001302 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001303 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001304 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001305 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001306 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001307 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001308 else if (RegVT.isVector()) {
1309 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001310 if (!Is64Bit)
1311 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1312 else {
1313 // Darwin calling convention passes MMX values in either GPRs or
1314 // XMMs in x86-64. Other targets pass them in memory.
1315 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1316 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1317 RegVT = MVT::v2i64;
1318 } else {
1319 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1320 RegVT = MVT::i64;
1321 }
1322 }
1323 } else {
1324 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001325 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001326
Bob Wilson998e1252009-04-20 18:36:57 +00001327 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001328 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001329
Chris Lattnerf39f7712007-02-28 05:46:49 +00001330 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1331 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1332 // right size.
1333 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001334 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001335 DAG.getValueType(VA.getValVT()));
1336 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001337 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001338 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001339
Chris Lattnerf39f7712007-02-28 05:46:49 +00001340 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001341 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Gordon Henriksen86737662008-01-05 16:56:59 +00001343 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001344 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001345 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001346 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001347 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001348 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1349 ArgValue, DAG.getConstant(0, MVT::i64));
1350 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001351 }
1352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001353
Chris Lattnerf39f7712007-02-28 05:46:49 +00001354 ArgValues.push_back(ArgValue);
1355 } else {
1356 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001357 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001358 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001359 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001360
Dan Gohman61a92132008-04-21 23:59:07 +00001361 // The x86-64 ABI for returning structs by value requires that we copy
1362 // the sret argument into %rax for the return. Save the argument into
1363 // a virtual register so that we can access it from the return points.
1364 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1365 MachineFunction &MF = DAG.getMachineFunction();
1366 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1367 unsigned Reg = FuncInfo->getSRetReturnReg();
1368 if (!Reg) {
1369 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1370 FuncInfo->setSRetReturnReg(Reg);
1371 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001372 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001373 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001374 }
1375
Chris Lattnerf39f7712007-02-28 05:46:49 +00001376 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001377 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001378 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001379 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001380
Evan Cheng1bc78042006-04-26 01:20:17 +00001381 // If the function takes variable number of arguments, make a frame index for
1382 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001383 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001384 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1385 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1386 }
1387 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001388 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1389
1390 // FIXME: We should really autogenerate these arrays
1391 static const unsigned GPR64ArgRegsWin64[] = {
1392 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001393 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001394 static const unsigned XMMArgRegsWin64[] = {
1395 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1396 };
1397 static const unsigned GPR64ArgRegs64Bit[] = {
1398 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1399 };
1400 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001401 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1402 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1403 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001404 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1405
1406 if (IsWin64) {
1407 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1408 GPR64ArgRegs = GPR64ArgRegsWin64;
1409 XMMArgRegs = XMMArgRegsWin64;
1410 } else {
1411 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1412 GPR64ArgRegs = GPR64ArgRegs64Bit;
1413 XMMArgRegs = XMMArgRegs64Bit;
1414 }
1415 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1416 TotalNumIntRegs);
1417 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1418 TotalNumXMMRegs);
1419
Devang Patel578efa92009-06-05 21:57:13 +00001420 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001421 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001422 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001423 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001424 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001425 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001426 // Kernel mode asks for SSE to be disabled, so don't push them
1427 // on the stack.
1428 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001429
Gordon Henriksen86737662008-01-05 16:56:59 +00001430 // For X86-64, if there are vararg parameters that are passed via
1431 // registers, then we must store them to their spots on the stack so they
1432 // may be loaded by deferencing the result of va_next.
1433 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001434 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1435 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1436 TotalNumXMMRegs * 16, 16);
1437
Gordon Henriksen86737662008-01-05 16:56:59 +00001438 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001439 SmallVector<SDValue, 8> MemOps;
1440 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001441 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001442 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001443 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001444 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1445 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001446 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001448 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001449 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001450 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001451 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001452 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001453 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001454
Gordon Henriksen86737662008-01-05 16:56:59 +00001455 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001456 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001457 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001458 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001459 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1460 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001461 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001462 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001463 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001464 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001465 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001466 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001467 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001468 }
1469 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001470 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001471 &MemOps[0], MemOps.size());
1472 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001473 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001474
Gordon Henriksenae636f82008-01-03 16:47:34 +00001475 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001476
Gordon Henriksen86737662008-01-05 16:56:59 +00001477 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001478 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001479 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001480 BytesCallerReserves = 0;
1481 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001482 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001483 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001484 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001485 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001486 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001487 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001488
Gordon Henriksen86737662008-01-05 16:56:59 +00001489 if (!Is64Bit) {
1490 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1491 if (CC == CallingConv::X86_FastCall)
1492 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1493 }
Evan Cheng25caf632006-05-23 21:06:34 +00001494
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001495 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001496
Evan Cheng25caf632006-05-23 21:06:34 +00001497 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001498 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001499 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001500}
1501
Dan Gohman475871a2008-07-27 21:46:04 +00001502SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001503X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001504 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001505 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001506 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001507 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001508 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001509 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001510 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001511 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001512 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001513 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001514 }
Dale Johannesenace16102009-02-03 19:33:06 +00001515 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001516 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001517}
1518
Bill Wendling64e87322009-01-16 19:25:27 +00001519/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001520/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001521SDValue
1522X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001523 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001524 SDValue Chain,
1525 bool IsTailCall,
1526 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001527 int FPDiff,
1528 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001529 if (!IsTailCall || FPDiff==0) return Chain;
1530
1531 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001533 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001534
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001535 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001536 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001537 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001538}
1539
1540/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1541/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001542static SDValue
1543EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001544 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001545 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001546 // Store the return address to the appropriate stack slot.
1547 if (!FPDiff) return Chain;
1548 // Calculate the new stack slot for the return address.
1549 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001550 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001551 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001552 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001553 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001554 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001555 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001556 return Chain;
1557}
1558
Dan Gohman475871a2008-07-27 21:46:04 +00001559SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001560 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001561 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1562 SDValue Chain = TheCall->getChain();
1563 unsigned CC = TheCall->getCallingConv();
1564 bool isVarArg = TheCall->isVarArg();
1565 bool IsTailCall = TheCall->isTailCall() &&
1566 CC == CallingConv::Fast && PerformTailCallOpt;
1567 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001568 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001569 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001570 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001571
1572 assert(!(isVarArg && CC == CallingConv::Fast) &&
1573 "Var args not supported with calling convention fastcc");
1574
Chris Lattner638402b2007-02-28 07:00:42 +00001575 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001576 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001577 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001578 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001579
Chris Lattner423c5f42007-02-28 05:31:48 +00001580 // Get a count of how many bytes are to be pushed on the stack.
1581 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001582 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001583 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001584
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 int FPDiff = 0;
1586 if (IsTailCall) {
1587 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001588 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1590 FPDiff = NumBytesCallerPushed - NumBytes;
1591
1592 // Set the delta of movement of the returnaddr stackslot.
1593 // But only set if delta is greater than previous delta.
1594 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1595 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1596 }
1597
Chris Lattnere563bbc2008-10-11 22:08:30 +00001598 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001599
Dan Gohman475871a2008-07-27 21:46:04 +00001600 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001601 // Load return adress for tail calls.
1602 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001603 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001604
Dan Gohman475871a2008-07-27 21:46:04 +00001605 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1606 SmallVector<SDValue, 8> MemOpChains;
1607 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001608
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001609 // Walk the register/memloc assignments, inserting copies/loads. In the case
1610 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001611 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1612 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001613 SDValue Arg = TheCall->getArg(i);
1614 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1615 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001616
Chris Lattner423c5f42007-02-28 05:31:48 +00001617 // Promote the value if needed.
1618 switch (VA.getLocInfo()) {
1619 default: assert(0 && "Unknown loc info!");
1620 case CCValAssign::Full: break;
1621 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001622 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001623 break;
1624 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001625 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001626 break;
1627 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001628 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001629 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001631
Chris Lattner423c5f42007-02-28 05:31:48 +00001632 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001633 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001634 MVT RegVT = VA.getLocVT();
1635 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001636 switch (VA.getLocReg()) {
1637 default:
1638 break;
1639 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1640 case X86::R8: {
1641 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001642 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001643 break;
1644 }
1645 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1646 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1647 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001648 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1649 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001650 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001651 break;
1652 }
1653 }
1654 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001655 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1656 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001657 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001658 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001659 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001660 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001661
Dan Gohman095cc292008-09-13 01:54:27 +00001662 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1663 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001664 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001665 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001666 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001667
Evan Cheng32fe1032006-05-25 00:59:30 +00001668 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001669 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001670 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001671
Evan Cheng347d5f72006-04-28 21:29:37 +00001672 // Build a sequence of copy-to-reg nodes chained together with token chain
1673 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001674 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001675 // Tail call byval lowering might overwrite argument registers so in case of
1676 // tail call optimization the copies to registers are lowered later.
1677 if (!IsTailCall)
1678 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001679 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001680 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001681 InFlag = Chain.getValue(1);
1682 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001683
Evan Chengf4684712007-02-21 21:18:14 +00001684 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001685 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001686 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001688 DAG.getNode(X86ISD::GlobalBaseReg,
1689 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001690 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001691 InFlag);
1692 InFlag = Chain.getValue(1);
1693 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001694 // If we are tail calling and generating PIC/GOT style code load the address
1695 // of the callee into ecx. The value in ecx is used as target of the tail
1696 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1697 // calls on PIC/GOT architectures. Normally we would just put the address of
1698 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1699 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001700 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001701 // Note: The actual moving to ecx is done further down.
1702 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001703 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001704 !G->getGlobal()->hasProtectedVisibility())
1705 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001706 else if (isa<ExternalSymbolSDNode>(Callee))
1707 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001708 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001709
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 if (Is64Bit && isVarArg) {
1711 // From AMD64 ABI document:
1712 // For calls that may call functions that use varargs or stdargs
1713 // (prototype-less calls or calls to functions containing ellipsis (...) in
1714 // the declaration) %al is used as hidden argument to specify the number
1715 // of SSE registers used. The contents of %al do not need to match exactly
1716 // the number of registers, but must be an ubound on the number of SSE
1717 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001718
1719 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 // Count the number of XMM registers allocated.
1721 static const unsigned XMMArgRegs[] = {
1722 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1723 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1724 };
1725 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001726 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001727 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001728
Dale Johannesendd64c412009-02-04 00:33:20 +00001729 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001730 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1731 InFlag = Chain.getValue(1);
1732 }
1733
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001734
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001735 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001736 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001737 SmallVector<SDValue, 8> MemOpChains2;
1738 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001739 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001740 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001741 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001742 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1743 CCValAssign &VA = ArgLocs[i];
1744 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001745 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001746 SDValue Arg = TheCall->getArg(i);
1747 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001748 // Create frame index.
1749 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001750 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001752 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001753
Duncan Sands276dcbd2008-03-21 09:14:45 +00001754 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001755 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001756 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001757 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001758 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001759 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001760 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001761
1762 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001763 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001765 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001766 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001767 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001768 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001769 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
1771 }
1772
1773 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001774 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001775 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001776
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001777 // Copy arguments to their registers.
1778 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001779 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001780 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001781 InFlag = Chain.getValue(1);
1782 }
Dan Gohman475871a2008-07-27 21:46:04 +00001783 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001784
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001786 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001787 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001788 }
1789
Evan Cheng32fe1032006-05-25 00:59:30 +00001790 // If the callee is a GlobalAddress node (quite common, every direct call is)
1791 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001792 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001793 // We should use extra load for direct calls to dllimported functions in
1794 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001795 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1796 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001797 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1798 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001799 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1800 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001801 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001802 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001803
Dale Johannesendd64c412009-02-04 00:33:20 +00001804 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001805 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001806 Callee,InFlag);
1807 Callee = DAG.getRegister(Opc, getPointerTy());
1808 // Add register as live out.
1809 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001810 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001811
Chris Lattnerd96d0722007-02-25 06:40:16 +00001812 // Returns a chain & a flag for retval copy to use.
1813 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001815
1816 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001817 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1818 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001819 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001820
Gordon Henriksen86737662008-01-05 16:56:59 +00001821 // Returns a chain & a flag for retval copy to use.
1822 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1823 Ops.clear();
1824 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001825
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001826 Ops.push_back(Chain);
1827 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001828
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 if (IsTailCall)
1830 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001831
Gordon Henriksen86737662008-01-05 16:56:59 +00001832 // Add argument registers to the end of the list so that they are known live
1833 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001834 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1835 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1836 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001837
Evan Cheng586ccac2008-03-18 23:36:35 +00001838 // Add an implicit use GOT pointer in EBX.
1839 if (!IsTailCall && !Is64Bit &&
1840 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1841 Subtarget->isPICStyleGOT())
1842 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1843
1844 // Add an implicit use of AL for x86 vararg functions.
1845 if (Is64Bit && isVarArg)
1846 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1847
Gabor Greifba36cb52008-08-28 21:40:38 +00001848 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001849 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001850
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001852 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001854 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001855 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001856
Gabor Greifba36cb52008-08-28 21:40:38 +00001857 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001858 }
1859
Dale Johannesenace16102009-02-03 19:33:06 +00001860 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001861 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001862
Chris Lattner2d297092006-05-23 18:50:38 +00001863 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001864 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001865 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001866 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001867 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001868 // If this is is a call to a struct-return function, the callee
1869 // pops the hidden struct pointer, so we have to push it back.
1870 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001871 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001873 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Gordon Henriksenae636f82008-01-03 16:47:34 +00001875 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001876 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001877 DAG.getIntPtrConstant(NumBytes, true),
1878 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1879 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001880 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001881 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001882
Chris Lattner3085e152007-02-25 08:59:22 +00001883 // Handle result values, copying them out of physregs into vregs that we
1884 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001885 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001886 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001887}
1888
Evan Cheng25ab6902006-09-08 06:48:29 +00001889
1890//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001891// Fast Calling Convention (tail call) implementation
1892//===----------------------------------------------------------------------===//
1893
1894// Like std call, callee cleans arguments, convention except that ECX is
1895// reserved for storing the tail called function address. Only 2 registers are
1896// free for argument passing (inreg). Tail call optimization is performed
1897// provided:
1898// * tailcallopt is enabled
1899// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001900// On X86_64 architecture with GOT-style position independent code only local
1901// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001902// To keep the stack aligned according to platform abi the function
1903// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1904// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001905// If a tail called function callee has more arguments than the caller the
1906// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001907// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001908// original REtADDR, but before the saved framepointer or the spilled registers
1909// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1910// stack layout:
1911// arg1
1912// arg2
1913// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001914// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001915// move area ]
1916// (possible EBP)
1917// ESI
1918// EDI
1919// local1 ..
1920
1921/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1922/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001923unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001924 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001925 MachineFunction &MF = DAG.getMachineFunction();
1926 const TargetMachine &TM = MF.getTarget();
1927 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1928 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001929 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001930 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001931 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001932 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1933 // Number smaller than 12 so just add the difference.
1934 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1935 } else {
1936 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001937 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001938 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001939 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001940 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001941}
1942
1943/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001944/// following the call is a return. A function is eligible if caller/callee
1945/// calling conventions match, currently only fastcc supports tail calls, and
1946/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001947bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001948 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001949 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001950 if (!PerformTailCallOpt)
1951 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001952
Dan Gohman095cc292008-09-13 01:54:27 +00001953 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001954 MachineFunction &MF = DAG.getMachineFunction();
1955 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001956 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001957 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001958 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001959 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001960 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001961 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001962 return true;
1963
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001964 // Can only do local tail calls (in same module, hidden or protected) on
1965 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1967 return G->getGlobal()->hasHiddenVisibility()
1968 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001969 }
1970 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001971
1972 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001973}
1974
Dan Gohman3df24e62008-09-03 23:12:08 +00001975FastISel *
1976X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001977 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001978 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001979 DenseMap<const Value *, unsigned> &vm,
1980 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001981 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001982 DenseMap<const AllocaInst *, int> &am
1983#ifndef NDEBUG
1984 , SmallSet<Instruction*, 8> &cil
1985#endif
1986 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001987 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001988#ifndef NDEBUG
1989 , cil
1990#endif
1991 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001992}
1993
1994
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001995//===----------------------------------------------------------------------===//
1996// Other Lowering Hooks
1997//===----------------------------------------------------------------------===//
1998
1999
Dan Gohman475871a2008-07-27 21:46:04 +00002000SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002001 MachineFunction &MF = DAG.getMachineFunction();
2002 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2003 int ReturnAddrIndex = FuncInfo->getRAIndex();
2004
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002005 if (ReturnAddrIndex == 0) {
2006 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002007 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002008 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002009 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002010 }
2011
Evan Cheng25ab6902006-09-08 06:48:29 +00002012 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002013}
2014
2015
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002016/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2017/// specific condition code, returning the condition code and the LHS/RHS of the
2018/// comparison to make.
2019static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2020 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002021 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002022 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2023 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2024 // X > -1 -> X == 0, jump !sign.
2025 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002026 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002027 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2028 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002029 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002030 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002031 // X < 1 -> X <= 0
2032 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002033 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002034 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002035 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002036
Evan Chengd9558e02006-01-06 00:43:03 +00002037 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002038 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002039 case ISD::SETEQ: return X86::COND_E;
2040 case ISD::SETGT: return X86::COND_G;
2041 case ISD::SETGE: return X86::COND_GE;
2042 case ISD::SETLT: return X86::COND_L;
2043 case ISD::SETLE: return X86::COND_LE;
2044 case ISD::SETNE: return X86::COND_NE;
2045 case ISD::SETULT: return X86::COND_B;
2046 case ISD::SETUGT: return X86::COND_A;
2047 case ISD::SETULE: return X86::COND_BE;
2048 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002049 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002050 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002051
Chris Lattner4c78e022008-12-23 23:42:27 +00002052 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002053
Chris Lattner4c78e022008-12-23 23:42:27 +00002054 // If LHS is a foldable load, but RHS is not, flip the condition.
2055 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2056 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2057 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2058 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002059 }
2060
Chris Lattner4c78e022008-12-23 23:42:27 +00002061 switch (SetCCOpcode) {
2062 default: break;
2063 case ISD::SETOLT:
2064 case ISD::SETOLE:
2065 case ISD::SETUGT:
2066 case ISD::SETUGE:
2067 std::swap(LHS, RHS);
2068 break;
2069 }
2070
2071 // On a floating point condition, the flags are set as follows:
2072 // ZF PF CF op
2073 // 0 | 0 | 0 | X > Y
2074 // 0 | 0 | 1 | X < Y
2075 // 1 | 0 | 0 | X == Y
2076 // 1 | 1 | 1 | unordered
2077 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002078 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002079 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002080 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002081 case ISD::SETOLT: // flipped
2082 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002083 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002084 case ISD::SETOLE: // flipped
2085 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002086 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002087 case ISD::SETUGT: // flipped
2088 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002089 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002090 case ISD::SETUGE: // flipped
2091 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002092 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002093 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002094 case ISD::SETNE: return X86::COND_NE;
2095 case ISD::SETUO: return X86::COND_P;
2096 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002097 }
Evan Chengd9558e02006-01-06 00:43:03 +00002098}
2099
Evan Cheng4a460802006-01-11 00:33:36 +00002100/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2101/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002102/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002103static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002104 switch (X86CC) {
2105 default:
2106 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002107 case X86::COND_B:
2108 case X86::COND_BE:
2109 case X86::COND_E:
2110 case X86::COND_P:
2111 case X86::COND_A:
2112 case X86::COND_AE:
2113 case X86::COND_NE:
2114 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002115 return true;
2116 }
2117}
2118
Nate Begeman9008ca62009-04-27 18:41:29 +00002119/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2120/// the specified range (L, H].
2121static bool isUndefOrInRange(int Val, int Low, int Hi) {
2122 return (Val < 0) || (Val >= Low && Val < Hi);
2123}
2124
2125/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2126/// specified value.
2127static bool isUndefOrEqual(int Val, int CmpVal) {
2128 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002129 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002130 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002131}
2132
Nate Begeman9008ca62009-04-27 18:41:29 +00002133/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2134/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2135/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002136static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002137 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2138 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2139 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2140 return (Mask[0] < 2 && Mask[1] < 2);
2141 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002142}
2143
Nate Begeman9008ca62009-04-27 18:41:29 +00002144bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2145 SmallVector<int, 8> M;
2146 N->getMask(M);
2147 return ::isPSHUFDMask(M, N->getValueType(0));
2148}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002149
Nate Begeman9008ca62009-04-27 18:41:29 +00002150/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2151/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002152static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002153 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002154 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002155
2156 // Lower quadword copied in order or undef.
2157 for (int i = 0; i != 4; ++i)
2158 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002159 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002160
Evan Cheng506d3df2006-03-29 23:07:14 +00002161 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002162 for (int i = 4; i != 8; ++i)
2163 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002164 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002165
Evan Cheng506d3df2006-03-29 23:07:14 +00002166 return true;
2167}
2168
Nate Begeman9008ca62009-04-27 18:41:29 +00002169bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2170 SmallVector<int, 8> M;
2171 N->getMask(M);
2172 return ::isPSHUFHWMask(M, N->getValueType(0));
2173}
Evan Cheng506d3df2006-03-29 23:07:14 +00002174
Nate Begeman9008ca62009-04-27 18:41:29 +00002175/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2176/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002177static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002178 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002179 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002180
Rafael Espindola15684b22009-04-24 12:40:33 +00002181 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002182 for (int i = 4; i != 8; ++i)
2183 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002184 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002185
Rafael Espindola15684b22009-04-24 12:40:33 +00002186 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002187 for (int i = 0; i != 4; ++i)
2188 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002189 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002190
Rafael Espindola15684b22009-04-24 12:40:33 +00002191 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002192}
2193
Nate Begeman9008ca62009-04-27 18:41:29 +00002194bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2195 SmallVector<int, 8> M;
2196 N->getMask(M);
2197 return ::isPSHUFLWMask(M, N->getValueType(0));
2198}
2199
Evan Cheng14aed5e2006-03-24 01:18:28 +00002200/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2201/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002202static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002203 int NumElems = VT.getVectorNumElements();
2204 if (NumElems != 2 && NumElems != 4)
2205 return false;
2206
2207 int Half = NumElems / 2;
2208 for (int i = 0; i < Half; ++i)
2209 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002210 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002211 for (int i = Half; i < NumElems; ++i)
2212 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002213 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002214
Evan Cheng14aed5e2006-03-24 01:18:28 +00002215 return true;
2216}
2217
Nate Begeman9008ca62009-04-27 18:41:29 +00002218bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2219 SmallVector<int, 8> M;
2220 N->getMask(M);
2221 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002222}
2223
Evan Cheng213d2cf2007-05-17 18:45:50 +00002224/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002225/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2226/// half elements to come from vector 1 (which would equal the dest.) and
2227/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002228static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002229 int NumElems = VT.getVectorNumElements();
2230
2231 if (NumElems != 2 && NumElems != 4)
2232 return false;
2233
2234 int Half = NumElems / 2;
2235 for (int i = 0; i < Half; ++i)
2236 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002237 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002238 for (int i = Half; i < NumElems; ++i)
2239 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002240 return false;
2241 return true;
2242}
2243
Nate Begeman9008ca62009-04-27 18:41:29 +00002244static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2245 SmallVector<int, 8> M;
2246 N->getMask(M);
2247 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002248}
2249
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002250/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2251/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002252bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2253 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002254 return false;
2255
Evan Cheng2064a2b2006-03-28 06:50:32 +00002256 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002257 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2258 isUndefOrEqual(N->getMaskElt(1), 7) &&
2259 isUndefOrEqual(N->getMaskElt(2), 2) &&
2260 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002261}
2262
Evan Cheng5ced1d82006-04-06 23:23:56 +00002263/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2264/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002265bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2266 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002267
Evan Cheng5ced1d82006-04-06 23:23:56 +00002268 if (NumElems != 2 && NumElems != 4)
2269 return false;
2270
Evan Chengc5cdff22006-04-07 21:53:05 +00002271 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002272 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002273 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002274
Evan Chengc5cdff22006-04-07 21:53:05 +00002275 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002276 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002277 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002278
2279 return true;
2280}
2281
2282/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002283/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2284/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002285bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2286 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002287
Evan Cheng5ced1d82006-04-06 23:23:56 +00002288 if (NumElems != 2 && NumElems != 4)
2289 return false;
2290
Evan Chengc5cdff22006-04-07 21:53:05 +00002291 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002292 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002293 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002294
Nate Begeman9008ca62009-04-27 18:41:29 +00002295 for (unsigned i = 0; i < NumElems/2; ++i)
2296 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002297 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002298
2299 return true;
2300}
2301
Nate Begeman9008ca62009-04-27 18:41:29 +00002302/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2303/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2304/// <2, 3, 2, 3>
2305bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2306 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2307
2308 if (NumElems != 4)
2309 return false;
2310
2311 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2312 isUndefOrEqual(N->getMaskElt(1), 3) &&
2313 isUndefOrEqual(N->getMaskElt(2), 2) &&
2314 isUndefOrEqual(N->getMaskElt(3), 3);
2315}
2316
Evan Cheng0038e592006-03-28 00:39:58 +00002317/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2318/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002319static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002320 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002321 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002322 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002323 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002324
2325 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2326 int BitI = Mask[i];
2327 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002328 if (!isUndefOrEqual(BitI, j))
2329 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002330 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002331 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002332 return false;
2333 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002334 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002335 return false;
2336 }
Evan Cheng0038e592006-03-28 00:39:58 +00002337 }
Evan Cheng0038e592006-03-28 00:39:58 +00002338 return true;
2339}
2340
Nate Begeman9008ca62009-04-27 18:41:29 +00002341bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2342 SmallVector<int, 8> M;
2343 N->getMask(M);
2344 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002345}
2346
Evan Cheng4fcb9222006-03-28 02:43:26 +00002347/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2348/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002349static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002350 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002351 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002352 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002353 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002354
2355 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2356 int BitI = Mask[i];
2357 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002358 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002359 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002360 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002361 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002362 return false;
2363 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002364 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002365 return false;
2366 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002367 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002368 return true;
2369}
2370
Nate Begeman9008ca62009-04-27 18:41:29 +00002371bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2372 SmallVector<int, 8> M;
2373 N->getMask(M);
2374 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002375}
2376
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002377/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2378/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2379/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002380static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002381 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002382 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002383 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002384
2385 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2386 int BitI = Mask[i];
2387 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002388 if (!isUndefOrEqual(BitI, j))
2389 return false;
2390 if (!isUndefOrEqual(BitI1, j))
2391 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002392 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002393 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002394}
2395
Nate Begeman9008ca62009-04-27 18:41:29 +00002396bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2397 SmallVector<int, 8> M;
2398 N->getMask(M);
2399 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2400}
2401
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002402/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2403/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2404/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002405static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002406 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002407 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2408 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002409
2410 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2411 int BitI = Mask[i];
2412 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002413 if (!isUndefOrEqual(BitI, j))
2414 return false;
2415 if (!isUndefOrEqual(BitI1, j))
2416 return false;
2417 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002418 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002419}
2420
Nate Begeman9008ca62009-04-27 18:41:29 +00002421bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2422 SmallVector<int, 8> M;
2423 N->getMask(M);
2424 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2425}
2426
Evan Cheng017dcc62006-04-21 01:05:10 +00002427/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2428/// specifies a shuffle of elements that is suitable for input to MOVSS,
2429/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002430static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002431 int NumElts = VT.getVectorNumElements();
Evan Cheng10762102007-12-06 22:14:22 +00002432 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002433 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002434
2435 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002436 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002437
2438 for (int i = 1; i < NumElts; ++i)
2439 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002440 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002441
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002442 return true;
2443}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002444
Nate Begeman9008ca62009-04-27 18:41:29 +00002445bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2446 SmallVector<int, 8> M;
2447 N->getMask(M);
2448 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002449}
2450
Evan Cheng017dcc62006-04-21 01:05:10 +00002451/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2452/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002453/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002454static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002455 bool V2IsSplat = false, bool V2IsUndef = false) {
2456 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002457 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002458 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002459
2460 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002461 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002462
2463 for (int i = 1; i < NumOps; ++i)
2464 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2465 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2466 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002467 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002468
Evan Cheng39623da2006-04-20 08:58:49 +00002469 return true;
2470}
2471
Nate Begeman9008ca62009-04-27 18:41:29 +00002472static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002473 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002474 SmallVector<int, 8> M;
2475 N->getMask(M);
2476 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002477}
2478
Evan Chengd9539472006-04-14 21:59:03 +00002479/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2480/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002481bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2482 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002483 return false;
2484
2485 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002486 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002487 int Elt = N->getMaskElt(i);
2488 if (Elt >= 0 && Elt != 1)
2489 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002490 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002491
2492 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002493 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002494 int Elt = N->getMaskElt(i);
2495 if (Elt >= 0 && Elt != 3)
2496 return false;
2497 if (Elt == 3)
2498 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002499 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002500 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002501 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002502 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002503}
2504
2505/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2506/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002507bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2508 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002509 return false;
2510
2511 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002512 for (unsigned i = 0; i < 2; ++i)
2513 if (N->getMaskElt(i) > 0)
2514 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002515
2516 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002517 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002518 int Elt = N->getMaskElt(i);
2519 if (Elt >= 0 && Elt != 2)
2520 return false;
2521 if (Elt == 2)
2522 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002523 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002524 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002525 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002526}
2527
Evan Cheng0b457f02008-09-25 20:50:48 +00002528/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2529/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002530bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2531 int e = N->getValueType(0).getVectorNumElements() / 2;
2532
2533 for (int i = 0; i < e; ++i)
2534 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002535 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002536 for (int i = 0; i < e; ++i)
2537 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002538 return false;
2539 return true;
2540}
2541
Evan Cheng63d33002006-03-22 08:01:21 +00002542/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2543/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2544/// instructions.
2545unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2547 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2548
Evan Chengb9df0ca2006-03-22 02:53:00 +00002549 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2550 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002551 for (int i = 0; i < NumOperands; ++i) {
2552 int Val = SVOp->getMaskElt(NumOperands-i-1);
2553 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002554 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002555 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002556 if (i != NumOperands - 1)
2557 Mask <<= Shift;
2558 }
Evan Cheng63d33002006-03-22 08:01:21 +00002559 return Mask;
2560}
2561
Evan Cheng506d3df2006-03-29 23:07:14 +00002562/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2563/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2564/// instructions.
2565unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002566 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002567 unsigned Mask = 0;
2568 // 8 nodes, but we only care about the last 4.
2569 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002570 int Val = SVOp->getMaskElt(i);
2571 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002572 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002573 if (i != 4)
2574 Mask <<= 2;
2575 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002576 return Mask;
2577}
2578
2579/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2580/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2581/// instructions.
2582unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002583 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002584 unsigned Mask = 0;
2585 // 8 nodes, but we only care about the first 4.
2586 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002587 int Val = SVOp->getMaskElt(i);
2588 if (Val >= 0)
2589 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002590 if (i != 0)
2591 Mask <<= 2;
2592 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002593 return Mask;
2594}
2595
Nate Begeman9008ca62009-04-27 18:41:29 +00002596/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2597/// their permute mask.
2598static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2599 SelectionDAG &DAG) {
2600 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002601 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002602 SmallVector<int, 8> MaskVec;
2603
Nate Begeman5a5ca152009-04-29 05:20:52 +00002604 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 int idx = SVOp->getMaskElt(i);
2606 if (idx < 0)
2607 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002608 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002609 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002610 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002611 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002612 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002613 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2614 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002615}
2616
Evan Cheng779ccea2007-12-07 21:30:01 +00002617/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2618/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002619static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002620 unsigned NumElems = VT.getVectorNumElements();
2621 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002622 int idx = Mask[i];
2623 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002624 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002625 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002626 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002627 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002628 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002629 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002630}
2631
Evan Cheng533a0aa2006-04-19 20:35:22 +00002632/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2633/// match movhlps. The lower half elements should come from upper half of
2634/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002635/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002636static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2637 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002638 return false;
2639 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002641 return false;
2642 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002643 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002644 return false;
2645 return true;
2646}
2647
Evan Cheng5ced1d82006-04-06 23:23:56 +00002648/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002649/// is promoted to a vector. It also returns the LoadSDNode by reference if
2650/// required.
2651static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002652 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2653 return false;
2654 N = N->getOperand(0).getNode();
2655 if (!ISD::isNON_EXTLoad(N))
2656 return false;
2657 if (LD)
2658 *LD = cast<LoadSDNode>(N);
2659 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002660}
2661
Evan Cheng533a0aa2006-04-19 20:35:22 +00002662/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2663/// match movlp{s|d}. The lower half elements should come from lower half of
2664/// V1 (and in order), and the upper half elements should come from the upper
2665/// half of V2 (and in order). And since V1 will become the source of the
2666/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002667static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2668 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002669 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002670 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002671 // Is V2 is a vector load, don't do this transformation. We will try to use
2672 // load folding shufps op.
2673 if (ISD::isNON_EXTLoad(V2))
2674 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002675
Nate Begeman5a5ca152009-04-29 05:20:52 +00002676 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002677
Evan Cheng533a0aa2006-04-19 20:35:22 +00002678 if (NumElems != 2 && NumElems != 4)
2679 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002680 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002681 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002682 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002683 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002684 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002685 return false;
2686 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002687}
2688
Evan Cheng39623da2006-04-20 08:58:49 +00002689/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2690/// all the same.
2691static bool isSplatVector(SDNode *N) {
2692 if (N->getOpcode() != ISD::BUILD_VECTOR)
2693 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002694
Dan Gohman475871a2008-07-27 21:46:04 +00002695 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002696 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2697 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002698 return false;
2699 return true;
2700}
2701
Evan Cheng213d2cf2007-05-17 18:45:50 +00002702/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2703/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002704static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002705 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002706 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002707 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002708 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002709}
2710
2711/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002712/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002713/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002714static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002715 SDValue V1 = N->getOperand(0);
2716 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002717 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2718 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002719 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002720 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002721 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002722 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2723 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002724 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2725 return false;
2726 } else if (Idx >= 0) {
2727 unsigned Opc = V1.getOpcode();
2728 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2729 continue;
2730 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002731 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002732 }
2733 }
2734 return true;
2735}
2736
2737/// getZeroVector - Returns a vector of specified type with all zero elements.
2738///
Dale Johannesenace16102009-02-03 19:33:06 +00002739static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2740 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002741 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002742
Chris Lattner8a594482007-11-25 00:24:49 +00002743 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2744 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002747 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002748 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002749 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002750 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002751 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002752 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002753 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002754 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002755 }
Dale Johannesenace16102009-02-03 19:33:06 +00002756 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002757}
2758
Chris Lattner8a594482007-11-25 00:24:49 +00002759/// getOnesVector - Returns a vector of specified type with all bits set.
2760///
Dale Johannesenace16102009-02-03 19:33:06 +00002761static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002763
Chris Lattner8a594482007-11-25 00:24:49 +00002764 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2765 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002766 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2767 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002768 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002769 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002770 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002771 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002772 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002773}
2774
2775
Evan Cheng39623da2006-04-20 08:58:49 +00002776/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2777/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002778static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2779 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002780 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002781
Evan Cheng39623da2006-04-20 08:58:49 +00002782 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002783 SmallVector<int, 8> MaskVec;
2784 SVOp->getMask(MaskVec);
2785
Nate Begeman5a5ca152009-04-29 05:20:52 +00002786 for (unsigned i = 0; i != NumElems; ++i) {
2787 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002788 MaskVec[i] = NumElems;
2789 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002790 }
Evan Cheng39623da2006-04-20 08:58:49 +00002791 }
Evan Cheng39623da2006-04-20 08:58:49 +00002792 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002793 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2794 SVOp->getOperand(1), &MaskVec[0]);
2795 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002796}
2797
Evan Cheng017dcc62006-04-21 01:05:10 +00002798/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2799/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002800static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2801 SDValue V2) {
2802 unsigned NumElems = VT.getVectorNumElements();
2803 SmallVector<int, 8> Mask;
2804 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002805 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002806 Mask.push_back(i);
2807 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002808}
2809
Nate Begeman9008ca62009-04-27 18:41:29 +00002810/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2811static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2812 SDValue V2) {
2813 unsigned NumElems = VT.getVectorNumElements();
2814 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002815 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002816 Mask.push_back(i);
2817 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002818 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002819 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002820}
2821
Nate Begeman9008ca62009-04-27 18:41:29 +00002822/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2823static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2824 SDValue V2) {
2825 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002826 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002828 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 Mask.push_back(i + Half);
2830 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002831 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002832 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002833}
2834
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002835/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002836static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2837 bool HasSSE2) {
2838 if (SV->getValueType(0).getVectorNumElements() <= 4)
2839 return SDValue(SV, 0);
2840
2841 MVT PVT = MVT::v4f32;
2842 MVT VT = SV->getValueType(0);
2843 DebugLoc dl = SV->getDebugLoc();
2844 SDValue V1 = SV->getOperand(0);
2845 int NumElems = VT.getVectorNumElements();
2846 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002847
Nate Begeman9008ca62009-04-27 18:41:29 +00002848 // unpack elements to the correct location
2849 while (NumElems > 4) {
2850 if (EltNo < NumElems/2) {
2851 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2852 } else {
2853 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2854 EltNo -= NumElems/2;
2855 }
2856 NumElems >>= 1;
2857 }
2858
2859 // Perform the splat.
2860 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002861 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002862 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2863 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002864}
2865
Evan Chengba05f722006-04-21 23:03:30 +00002866/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002867/// vector of zero or undef vector. This produces a shuffle where the low
2868/// element of V2 is swizzled into the zero/undef vector, landing at element
2869/// 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 +00002870static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002871 bool isZero, bool HasSSE2,
2872 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002874 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002875 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2876 unsigned NumElems = VT.getVectorNumElements();
2877 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002878 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002879 // If this is the insertion idx, put the low elt of V2 here.
2880 MaskVec.push_back(i == Idx ? NumElems : i);
2881 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002882}
2883
Evan Chengf26ffe92008-05-29 08:22:04 +00002884/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2885/// a shuffle that is zero.
2886static
Nate Begeman9008ca62009-04-27 18:41:29 +00002887unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2888 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002889 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002890 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002891 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002892 int Idx = SVOp->getMaskElt(Index);
2893 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002894 ++NumZeros;
2895 continue;
2896 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002897 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002898 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002899 ++NumZeros;
2900 else
2901 break;
2902 }
2903 return NumZeros;
2904}
2905
2906/// isVectorShift - Returns true if the shuffle can be implemented as a
2907/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002908/// FIXME: split into pslldqi, psrldqi, palignr variants.
2909static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002910 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002911 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002912
2913 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002915 if (!NumZeros) {
2916 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002917 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002918 if (!NumZeros)
2919 return false;
2920 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002921 bool SeenV1 = false;
2922 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002923 for (int i = NumZeros; i < NumElems; ++i) {
2924 int Val = isLeft ? (i - NumZeros) : i;
2925 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2926 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002927 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002928 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002929 SeenV1 = true;
2930 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002932 SeenV2 = true;
2933 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002935 return false;
2936 }
2937 if (SeenV1 && SeenV2)
2938 return false;
2939
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002941 ShAmt = NumZeros;
2942 return true;
2943}
2944
2945
Evan Chengc78d3b42006-04-24 18:01:45 +00002946/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2947///
Dan Gohman475871a2008-07-27 21:46:04 +00002948static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002949 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002950 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002951 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002952 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002953
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002954 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002955 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002956 bool First = true;
2957 for (unsigned i = 0; i < 16; ++i) {
2958 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2959 if (ThisIsNonZero && First) {
2960 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002961 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002962 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002963 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002964 First = false;
2965 }
2966
2967 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002968 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002969 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2970 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002971 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002972 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002973 }
2974 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002975 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2976 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002977 ThisElt, DAG.getConstant(8, MVT::i8));
2978 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002979 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00002980 } else
2981 ThisElt = LastElt;
2982
Gabor Greifba36cb52008-08-28 21:40:38 +00002983 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00002984 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002985 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002986 }
2987 }
2988
Dale Johannesenace16102009-02-03 19:33:06 +00002989 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00002990}
2991
Bill Wendlinga348c562007-03-22 18:42:45 +00002992/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002993///
Dan Gohman475871a2008-07-27 21:46:04 +00002994static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002995 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002996 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002997 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00002998 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002999
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003000 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003001 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003002 bool First = true;
3003 for (unsigned i = 0; i < 8; ++i) {
3004 bool isNonZero = (NonZeros & (1 << i)) != 0;
3005 if (isNonZero) {
3006 if (First) {
3007 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003008 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003009 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003010 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003011 First = false;
3012 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003013 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003014 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003015 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003016 }
3017 }
3018
3019 return V;
3020}
3021
Evan Chengf26ffe92008-05-29 08:22:04 +00003022/// getVShift - Return a vector logical shift node.
3023///
Dan Gohman475871a2008-07-27 21:46:04 +00003024static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003025 unsigned NumBits, SelectionDAG &DAG,
3026 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003027 bool isMMX = VT.getSizeInBits() == 64;
3028 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003029 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003030 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3031 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3032 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003033 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003034}
3035
Dan Gohman475871a2008-07-27 21:46:04 +00003036SDValue
3037X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003038 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003039 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003040 if (ISD::isBuildVectorAllZeros(Op.getNode())
3041 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003042 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3043 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3044 // eliminated on x86-32 hosts.
3045 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3046 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003047
Gabor Greifba36cb52008-08-28 21:40:38 +00003048 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003049 return getOnesVector(Op.getValueType(), DAG, dl);
3050 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003051 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003052
Duncan Sands83ec4b62008-06-06 12:08:01 +00003053 MVT VT = Op.getValueType();
3054 MVT EVT = VT.getVectorElementType();
3055 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003056
3057 unsigned NumElems = Op.getNumOperands();
3058 unsigned NumZero = 0;
3059 unsigned NumNonZero = 0;
3060 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003061 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003062 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003063 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003064 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003065 if (Elt.getOpcode() == ISD::UNDEF)
3066 continue;
3067 Values.insert(Elt);
3068 if (Elt.getOpcode() != ISD::Constant &&
3069 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003070 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003071 if (isZeroNode(Elt))
3072 NumZero++;
3073 else {
3074 NonZeros |= (1 << i);
3075 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003076 }
3077 }
3078
Dan Gohman7f321562007-06-25 16:23:39 +00003079 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003080 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003081 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003082 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003083
Chris Lattner67f453a2008-03-09 05:42:06 +00003084 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003085 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003086 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003087 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003088
Chris Lattner62098042008-03-09 01:05:04 +00003089 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3090 // the value are obviously zero, truncate the value to i32 and do the
3091 // insertion that way. Only do this if the value is non-constant or if the
3092 // value is a constant being inserted into element 0. It is cheaper to do
3093 // a constant pool load than it is to do a movd + shuffle.
3094 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3095 (!IsAllConstants || Idx == 0)) {
3096 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3097 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003098 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3099 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003100
Chris Lattner62098042008-03-09 01:05:04 +00003101 // Truncate the value (which may itself be a constant) to i32, and
3102 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003103 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3104 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003105 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3106 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003107
Chris Lattner62098042008-03-09 01:05:04 +00003108 // Now we have our 32-bit value zero extended in the low element of
3109 // a vector. If Idx != 0, swizzle it into place.
3110 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003111 SmallVector<int, 4> Mask;
3112 Mask.push_back(Idx);
3113 for (unsigned i = 1; i != VecElts; ++i)
3114 Mask.push_back(i);
3115 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3116 DAG.getUNDEF(Item.getValueType()),
3117 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003118 }
Dale Johannesenace16102009-02-03 19:33:06 +00003119 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003120 }
3121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003122
Chris Lattner19f79692008-03-08 22:59:52 +00003123 // If we have a constant or non-constant insertion into the low element of
3124 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3125 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3126 // depending on what the source datatype is. Because we can only get here
3127 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3128 if (Idx == 0 &&
3129 // Don't do this for i64 values on x86-32.
3130 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003131 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003132 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003133 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3134 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003135 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003136
3137 // Is it a vector logical left shift?
3138 if (NumElems == 2 && Idx == 1 &&
3139 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003140 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003141 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003142 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003143 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003144 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003145 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003146
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003147 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003148 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003149
Chris Lattner19f79692008-03-08 22:59:52 +00003150 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3151 // is a non-constant being inserted into an element other than the low one,
3152 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3153 // movd/movss) to move this into the low element, then shuffle it into
3154 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003155 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003156 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003157
Evan Cheng0db9fe62006-04-25 20:13:52 +00003158 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003159 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3160 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003162 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003163 MaskVec.push_back(i == Idx ? 0 : 1);
3164 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003165 }
3166 }
3167
Chris Lattner67f453a2008-03-09 05:42:06 +00003168 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3169 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003170 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003171
Dan Gohmana3941172007-07-24 22:55:08 +00003172 // A vector full of immediates; various special cases are already
3173 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003174 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003175 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003176
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003177 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003178 if (EVTBits == 64) {
3179 if (NumNonZero == 1) {
3180 // One half is zero or undef.
3181 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003182 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003183 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003184 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3185 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003186 }
Dan Gohman475871a2008-07-27 21:46:04 +00003187 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003188 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003189
3190 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003191 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003192 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003193 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003194 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003195 }
3196
Bill Wendling826f36f2007-03-28 00:57:11 +00003197 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003198 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003199 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003200 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003201 }
3202
3203 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003205 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003206 if (NumElems == 4 && NumZero > 0) {
3207 for (unsigned i = 0; i < 4; ++i) {
3208 bool isZero = !(NonZeros & (1 << i));
3209 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003210 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003211 else
Dale Johannesenace16102009-02-03 19:33:06 +00003212 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003213 }
3214
3215 for (unsigned i = 0; i < 2; ++i) {
3216 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3217 default: break;
3218 case 0:
3219 V[i] = V[i*2]; // Must be a zero vector.
3220 break;
3221 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003222 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223 break;
3224 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003226 break;
3227 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003228 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003229 break;
3230 }
3231 }
3232
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003234 bool Reverse = (NonZeros & 0x3) == 2;
3235 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003237 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3238 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3240 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003241 }
3242
3243 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003244 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3245 // values to be inserted is equal to the number of elements, in which case
3246 // use the unpack code below in the hopes of matching the consecutive elts
3247 // load merge pattern for shuffles.
3248 // FIXME: We could probably just check that here directly.
3249 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3250 getSubtarget()->hasSSE41()) {
3251 V[0] = DAG.getUNDEF(VT);
3252 for (unsigned i = 0; i < NumElems; ++i)
3253 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3254 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3255 Op.getOperand(i), DAG.getIntPtrConstant(i));
3256 return V[0];
3257 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003258 // Expand into a number of unpckl*.
3259 // e.g. for v4f32
3260 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3261 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3262 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003263 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003264 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003265 NumElems >>= 1;
3266 while (NumElems != 0) {
3267 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003269 NumElems >>= 1;
3270 }
3271 return V[0];
3272 }
3273
Dan Gohman475871a2008-07-27 21:46:04 +00003274 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003275}
3276
Nate Begemanb9a47b82009-02-23 08:49:38 +00003277// v8i16 shuffles - Prefer shuffles in the following order:
3278// 1. [all] pshuflw, pshufhw, optional move
3279// 2. [ssse3] 1 x pshufb
3280// 3. [ssse3] 2 x pshufb + 1 x por
3281// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003282static
Nate Begeman9008ca62009-04-27 18:41:29 +00003283SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3284 SelectionDAG &DAG, X86TargetLowering &TLI) {
3285 SDValue V1 = SVOp->getOperand(0);
3286 SDValue V2 = SVOp->getOperand(1);
3287 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003288 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003289
Nate Begemanb9a47b82009-02-23 08:49:38 +00003290 // Determine if more than 1 of the words in each of the low and high quadwords
3291 // of the result come from the same quadword of one of the two inputs. Undef
3292 // mask values count as coming from any quadword, for better codegen.
3293 SmallVector<unsigned, 4> LoQuad(4);
3294 SmallVector<unsigned, 4> HiQuad(4);
3295 BitVector InputQuads(4);
3296 for (unsigned i = 0; i < 8; ++i) {
3297 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003299 MaskVals.push_back(EltIdx);
3300 if (EltIdx < 0) {
3301 ++Quad[0];
3302 ++Quad[1];
3303 ++Quad[2];
3304 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003305 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003306 }
3307 ++Quad[EltIdx / 4];
3308 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003309 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003310
Nate Begemanb9a47b82009-02-23 08:49:38 +00003311 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003312 unsigned MaxQuad = 1;
3313 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003314 if (LoQuad[i] > MaxQuad) {
3315 BestLoQuad = i;
3316 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003317 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003318 }
3319
Nate Begemanb9a47b82009-02-23 08:49:38 +00003320 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003321 MaxQuad = 1;
3322 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003323 if (HiQuad[i] > MaxQuad) {
3324 BestHiQuad = i;
3325 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003326 }
3327 }
3328
Nate Begemanb9a47b82009-02-23 08:49:38 +00003329 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3330 // of the two input vectors, shuffle them into one input vector so only a
3331 // single pshufb instruction is necessary. If There are more than 2 input
3332 // quads, disable the next transformation since it does not help SSSE3.
3333 bool V1Used = InputQuads[0] || InputQuads[1];
3334 bool V2Used = InputQuads[2] || InputQuads[3];
3335 if (TLI.getSubtarget()->hasSSSE3()) {
3336 if (InputQuads.count() == 2 && V1Used && V2Used) {
3337 BestLoQuad = InputQuads.find_first();
3338 BestHiQuad = InputQuads.find_next(BestLoQuad);
3339 }
3340 if (InputQuads.count() > 2) {
3341 BestLoQuad = -1;
3342 BestHiQuad = -1;
3343 }
3344 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003345
Nate Begemanb9a47b82009-02-23 08:49:38 +00003346 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3347 // the shuffle mask. If a quad is scored as -1, that means that it contains
3348 // words from all 4 input quadwords.
3349 SDValue NewV;
3350 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 SmallVector<int, 8> MaskV;
3352 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3353 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3354 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3355 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3356 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003357 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003358
Nate Begemanb9a47b82009-02-23 08:49:38 +00003359 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3360 // source words for the shuffle, to aid later transformations.
3361 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003362 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003363 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003364 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003365 if (idx != (int)i)
3366 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003367 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003368 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003369 AllWordsInNewV = false;
3370 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003371 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003372
Nate Begemanb9a47b82009-02-23 08:49:38 +00003373 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3374 if (AllWordsInNewV) {
3375 for (int i = 0; i != 8; ++i) {
3376 int idx = MaskVals[i];
3377 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003378 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003379 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3380 if ((idx != i) && idx < 4)
3381 pshufhw = false;
3382 if ((idx != i) && idx > 3)
3383 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003384 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003385 V1 = NewV;
3386 V2Used = false;
3387 BestLoQuad = 0;
3388 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003389 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003390
Nate Begemanb9a47b82009-02-23 08:49:38 +00003391 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3392 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003393 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3395 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003396 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003397 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003398
3399 // If we have SSSE3, and all words of the result are from 1 input vector,
3400 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3401 // is present, fall back to case 4.
3402 if (TLI.getSubtarget()->hasSSSE3()) {
3403 SmallVector<SDValue,16> pshufbMask;
3404
3405 // If we have elements from both input vectors, set the high bit of the
3406 // shuffle mask element to zero out elements that come from V2 in the V1
3407 // mask, and elements that come from V1 in the V2 mask, so that the two
3408 // results can be OR'd together.
3409 bool TwoInputs = V1Used && V2Used;
3410 for (unsigned i = 0; i != 8; ++i) {
3411 int EltIdx = MaskVals[i] * 2;
3412 if (TwoInputs && (EltIdx >= 16)) {
3413 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3414 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3415 continue;
3416 }
3417 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3418 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3419 }
3420 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3421 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003422 DAG.getNode(ISD::BUILD_VECTOR, dl,
3423 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003424 if (!TwoInputs)
3425 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3426
3427 // Calculate the shuffle mask for the second input, shuffle it, and
3428 // OR it with the first shuffled input.
3429 pshufbMask.clear();
3430 for (unsigned i = 0; i != 8; ++i) {
3431 int EltIdx = MaskVals[i] * 2;
3432 if (EltIdx < 16) {
3433 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3434 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3435 continue;
3436 }
3437 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3438 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3439 }
3440 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3441 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003442 DAG.getNode(ISD::BUILD_VECTOR, dl,
3443 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003444 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3445 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3446 }
3447
3448 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3449 // and update MaskVals with new element order.
3450 BitVector InOrder(8);
3451 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003453 for (int i = 0; i != 4; ++i) {
3454 int idx = MaskVals[i];
3455 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003457 InOrder.set(i);
3458 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003460 InOrder.set(i);
3461 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003463 }
3464 }
3465 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003466 MaskV.push_back(i);
3467 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3468 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003469 }
3470
3471 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3472 // and update MaskVals with the new element order.
3473 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003474 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003475 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003477 for (unsigned i = 4; i != 8; ++i) {
3478 int idx = MaskVals[i];
3479 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003481 InOrder.set(i);
3482 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003484 InOrder.set(i);
3485 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003487 }
3488 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3490 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003491 }
3492
3493 // In case BestHi & BestLo were both -1, which means each quadword has a word
3494 // from each of the four input quadwords, calculate the InOrder bitvector now
3495 // before falling through to the insert/extract cleanup.
3496 if (BestLoQuad == -1 && BestHiQuad == -1) {
3497 NewV = V1;
3498 for (int i = 0; i != 8; ++i)
3499 if (MaskVals[i] < 0 || MaskVals[i] == i)
3500 InOrder.set(i);
3501 }
3502
3503 // The other elements are put in the right place using pextrw and pinsrw.
3504 for (unsigned i = 0; i != 8; ++i) {
3505 if (InOrder[i])
3506 continue;
3507 int EltIdx = MaskVals[i];
3508 if (EltIdx < 0)
3509 continue;
3510 SDValue ExtOp = (EltIdx < 8)
3511 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3512 DAG.getIntPtrConstant(EltIdx))
3513 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3514 DAG.getIntPtrConstant(EltIdx - 8));
3515 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3516 DAG.getIntPtrConstant(i));
3517 }
3518 return NewV;
3519}
3520
3521// v16i8 shuffles - Prefer shuffles in the following order:
3522// 1. [ssse3] 1 x pshufb
3523// 2. [ssse3] 2 x pshufb + 1 x por
3524// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3525static
Nate Begeman9008ca62009-04-27 18:41:29 +00003526SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3527 SelectionDAG &DAG, X86TargetLowering &TLI) {
3528 SDValue V1 = SVOp->getOperand(0);
3529 SDValue V2 = SVOp->getOperand(1);
3530 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003531 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003532 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003533
3534 // If we have SSSE3, case 1 is generated when all result bytes come from
3535 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3536 // present, fall back to case 3.
3537 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3538 bool V1Only = true;
3539 bool V2Only = true;
3540 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003541 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003542 if (EltIdx < 0)
3543 continue;
3544 if (EltIdx < 16)
3545 V2Only = false;
3546 else
3547 V1Only = false;
3548 }
3549
3550 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3551 if (TLI.getSubtarget()->hasSSSE3()) {
3552 SmallVector<SDValue,16> pshufbMask;
3553
3554 // If all result elements are from one input vector, then only translate
3555 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3556 //
3557 // Otherwise, we have elements from both input vectors, and must zero out
3558 // elements that come from V2 in the first mask, and V1 in the second mask
3559 // so that we can OR them together.
3560 bool TwoInputs = !(V1Only || V2Only);
3561 for (unsigned i = 0; i != 16; ++i) {
3562 int EltIdx = MaskVals[i];
3563 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3564 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3565 continue;
3566 }
3567 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3568 }
3569 // If all the elements are from V2, assign it to V1 and return after
3570 // building the first pshufb.
3571 if (V2Only)
3572 V1 = V2;
3573 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003574 DAG.getNode(ISD::BUILD_VECTOR, dl,
3575 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003576 if (!TwoInputs)
3577 return V1;
3578
3579 // Calculate the shuffle mask for the second input, shuffle it, and
3580 // OR it with the first shuffled input.
3581 pshufbMask.clear();
3582 for (unsigned i = 0; i != 16; ++i) {
3583 int EltIdx = MaskVals[i];
3584 if (EltIdx < 16) {
3585 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3586 continue;
3587 }
3588 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3589 }
3590 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003591 DAG.getNode(ISD::BUILD_VECTOR, dl,
3592 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003593 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3594 }
3595
3596 // No SSSE3 - Calculate in place words and then fix all out of place words
3597 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3598 // the 16 different words that comprise the two doublequadword input vectors.
3599 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3600 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3601 SDValue NewV = V2Only ? V2 : V1;
3602 for (int i = 0; i != 8; ++i) {
3603 int Elt0 = MaskVals[i*2];
3604 int Elt1 = MaskVals[i*2+1];
3605
3606 // This word of the result is all undef, skip it.
3607 if (Elt0 < 0 && Elt1 < 0)
3608 continue;
3609
3610 // This word of the result is already in the correct place, skip it.
3611 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3612 continue;
3613 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3614 continue;
3615
3616 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3617 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3618 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003619
3620 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3621 // using a single extract together, load it and store it.
3622 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3623 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3624 DAG.getIntPtrConstant(Elt1 / 2));
3625 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3626 DAG.getIntPtrConstant(i));
3627 continue;
3628 }
3629
Nate Begemanb9a47b82009-02-23 08:49:38 +00003630 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003631 // source byte is not also odd, shift the extracted word left 8 bits
3632 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003633 if (Elt1 >= 0) {
3634 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3635 DAG.getIntPtrConstant(Elt1 / 2));
3636 if ((Elt1 & 1) == 0)
3637 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3638 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003639 else if (Elt0 >= 0)
3640 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3641 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003642 }
3643 // If Elt0 is defined, extract it from the appropriate source. If the
3644 // source byte is not also even, shift the extracted word right 8 bits. If
3645 // Elt1 was also defined, OR the extracted values together before
3646 // inserting them in the result.
3647 if (Elt0 >= 0) {
3648 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3649 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3650 if ((Elt0 & 1) != 0)
3651 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3652 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003653 else if (Elt1 >= 0)
3654 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3655 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003656 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3657 : InsElt0;
3658 }
3659 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3660 DAG.getIntPtrConstant(i));
3661 }
3662 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003663}
3664
Evan Cheng7a831ce2007-12-15 03:00:47 +00003665/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3666/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3667/// done when every pair / quad of shuffle mask elements point to elements in
3668/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003669/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3670static
Nate Begeman9008ca62009-04-27 18:41:29 +00003671SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3672 SelectionDAG &DAG,
3673 TargetLowering &TLI, DebugLoc dl) {
3674 MVT VT = SVOp->getValueType(0);
3675 SDValue V1 = SVOp->getOperand(0);
3676 SDValue V2 = SVOp->getOperand(1);
3677 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003678 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003679 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003680 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003681 MVT NewVT = MaskVT;
3682 switch (VT.getSimpleVT()) {
3683 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003684 case MVT::v4f32: NewVT = MVT::v2f64; break;
3685 case MVT::v4i32: NewVT = MVT::v2i64; break;
3686 case MVT::v8i16: NewVT = MVT::v4i32; break;
3687 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003688 }
3689
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003690 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003691 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003692 NewVT = MVT::v2i64;
3693 else
3694 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003695 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 int Scale = NumElems / NewWidth;
3697 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003698 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 int StartIdx = -1;
3700 for (int j = 0; j < Scale; ++j) {
3701 int EltIdx = SVOp->getMaskElt(i+j);
3702 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003703 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003704 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003705 StartIdx = EltIdx - (EltIdx % Scale);
3706 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003707 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003708 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 if (StartIdx == -1)
3710 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003711 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003713 }
3714
Dale Johannesenace16102009-02-03 19:33:06 +00003715 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3716 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003718}
3719
Evan Chengd880b972008-05-09 21:53:03 +00003720/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003721///
Dan Gohman475871a2008-07-27 21:46:04 +00003722static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003723 SDValue SrcOp, SelectionDAG &DAG,
3724 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003725 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3726 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003727 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003728 LD = dyn_cast<LoadSDNode>(SrcOp);
3729 if (!LD) {
3730 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3731 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003732 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003733 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3734 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3735 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3736 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3737 // PR2108
3738 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003739 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3740 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3741 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3742 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003743 SrcOp.getOperand(0)
3744 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003745 }
3746 }
3747 }
3748
Dale Johannesenace16102009-02-03 19:33:06 +00003749 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3750 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003751 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003752 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003753}
3754
Evan Chengace3c172008-07-22 21:13:36 +00003755/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3756/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003757static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003758LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3759 SDValue V1 = SVOp->getOperand(0);
3760 SDValue V2 = SVOp->getOperand(1);
3761 DebugLoc dl = SVOp->getDebugLoc();
3762 MVT VT = SVOp->getValueType(0);
3763
Evan Chengace3c172008-07-22 21:13:36 +00003764 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003765 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 SmallVector<int, 8> Mask1(4U, -1);
3767 SmallVector<int, 8> PermMask;
3768 SVOp->getMask(PermMask);
3769
Evan Chengace3c172008-07-22 21:13:36 +00003770 unsigned NumHi = 0;
3771 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003772 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 int Idx = PermMask[i];
3774 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003775 Locs[i] = std::make_pair(-1, -1);
3776 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003777 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3778 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003779 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003781 NumLo++;
3782 } else {
3783 Locs[i] = std::make_pair(1, NumHi);
3784 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003786 NumHi++;
3787 }
3788 }
3789 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003790
Evan Chengace3c172008-07-22 21:13:36 +00003791 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003792 // If no more than two elements come from either vector. This can be
3793 // implemented with two shuffles. First shuffle gather the elements.
3794 // The second shuffle, which takes the first shuffle as both of its
3795 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003797
Nate Begeman9008ca62009-04-27 18:41:29 +00003798 SmallVector<int, 8> Mask2(4U, -1);
3799
Evan Chengace3c172008-07-22 21:13:36 +00003800 for (unsigned i = 0; i != 4; ++i) {
3801 if (Locs[i].first == -1)
3802 continue;
3803 else {
3804 unsigned Idx = (i < 2) ? 0 : 4;
3805 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003807 }
3808 }
3809
Nate Begeman9008ca62009-04-27 18:41:29 +00003810 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003811 } else if (NumLo == 3 || NumHi == 3) {
3812 // Otherwise, we must have three elements from one vector, call it X, and
3813 // one element from the other, call it Y. First, use a shufps to build an
3814 // intermediate vector with the one element from Y and the element from X
3815 // that will be in the same half in the final destination (the indexes don't
3816 // matter). Then, use a shufps to build the final vector, taking the half
3817 // containing the element from Y from the intermediate, and the other half
3818 // from X.
3819 if (NumHi == 3) {
3820 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003822 std::swap(V1, V2);
3823 }
3824
3825 // Find the element from V2.
3826 unsigned HiIndex;
3827 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 int Val = PermMask[HiIndex];
3829 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003830 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003831 if (Val >= 4)
3832 break;
3833 }
3834
Nate Begeman9008ca62009-04-27 18:41:29 +00003835 Mask1[0] = PermMask[HiIndex];
3836 Mask1[1] = -1;
3837 Mask1[2] = PermMask[HiIndex^1];
3838 Mask1[3] = -1;
3839 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003840
3841 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003842 Mask1[0] = PermMask[0];
3843 Mask1[1] = PermMask[1];
3844 Mask1[2] = HiIndex & 1 ? 6 : 4;
3845 Mask1[3] = HiIndex & 1 ? 4 : 6;
3846 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003847 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 Mask1[0] = HiIndex & 1 ? 2 : 0;
3849 Mask1[1] = HiIndex & 1 ? 0 : 2;
3850 Mask1[2] = PermMask[2];
3851 Mask1[3] = PermMask[3];
3852 if (Mask1[2] >= 0)
3853 Mask1[2] += 4;
3854 if (Mask1[3] >= 0)
3855 Mask1[3] += 4;
3856 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003857 }
Evan Chengace3c172008-07-22 21:13:36 +00003858 }
3859
3860 // Break it into (shuffle shuffle_hi, shuffle_lo).
3861 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 SmallVector<int,8> LoMask(4U, -1);
3863 SmallVector<int,8> HiMask(4U, -1);
3864
3865 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003866 unsigned MaskIdx = 0;
3867 unsigned LoIdx = 0;
3868 unsigned HiIdx = 2;
3869 for (unsigned i = 0; i != 4; ++i) {
3870 if (i == 2) {
3871 MaskPtr = &HiMask;
3872 MaskIdx = 1;
3873 LoIdx = 0;
3874 HiIdx = 2;
3875 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 int Idx = PermMask[i];
3877 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003878 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003879 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003880 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003881 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003882 LoIdx++;
3883 } else {
3884 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003886 HiIdx++;
3887 }
3888 }
3889
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3891 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3892 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003893 for (unsigned i = 0; i != 4; ++i) {
3894 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003896 } else {
3897 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003899 }
3900 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003902}
3903
Dan Gohman475871a2008-07-27 21:46:04 +00003904SDValue
3905X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue V1 = Op.getOperand(0);
3908 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003910 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003912 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003913 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3914 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003915 bool V1IsSplat = false;
3916 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003917
Nate Begeman9008ca62009-04-27 18:41:29 +00003918 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003919 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003920
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 // Promote splats to v4f32.
3922 if (SVOp->isSplat()) {
3923 if (isMMX || NumElems < 4)
3924 return Op;
3925 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003926 }
3927
Evan Cheng7a831ce2007-12-15 03:00:47 +00003928 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3929 // do it!
3930 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003932 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003933 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003934 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003935 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3936 // FIXME: Figure out a cleaner way to do this.
3937 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003938 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003940 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3942 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3943 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003944 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003945 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3947 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003948 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003950 }
3951 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003952
3953 if (X86::isPSHUFDMask(SVOp))
3954 return Op;
3955
Evan Chengf26ffe92008-05-29 08:22:04 +00003956 // Check if this can be converted into a logical shift.
3957 bool isLeft = false;
3958 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003959 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 bool isShift = getSubtarget()->hasSSE2() &&
3961 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003962 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003963 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003964 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003965 MVT EVT = VT.getVectorElementType();
3966 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003967 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003968 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003969
3970 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003971 if (V1IsUndef)
3972 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003973 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003974 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003975 if (!isMMX)
3976 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003977 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003978
3979 // FIXME: fold these into legal mask.
3980 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
3981 X86::isMOVSLDUPMask(SVOp) ||
3982 X86::isMOVHLPSMask(SVOp) ||
3983 X86::isMOVHPMask(SVOp) ||
3984 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003985 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003986
Nate Begeman9008ca62009-04-27 18:41:29 +00003987 if (ShouldXformToMOVHLPS(SVOp) ||
3988 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
3989 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003990
Evan Chengf26ffe92008-05-29 08:22:04 +00003991 if (isShift) {
3992 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003993 MVT EVT = VT.getVectorElementType();
3994 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003995 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003996 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003997
Evan Cheng9eca5e82006-10-25 21:49:50 +00003998 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003999 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4000 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004001 V1IsSplat = isSplatVector(V1.getNode());
4002 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004003
Chris Lattner8a594482007-11-25 00:24:49 +00004004 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004005 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004006 Op = CommuteVectorShuffle(SVOp, DAG);
4007 SVOp = cast<ShuffleVectorSDNode>(Op);
4008 V1 = SVOp->getOperand(0);
4009 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004010 std::swap(V1IsSplat, V2IsSplat);
4011 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004012 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004013 }
4014
Nate Begeman9008ca62009-04-27 18:41:29 +00004015 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4016 // Shuffling low element of v1 into undef, just return v1.
4017 if (V2IsUndef)
4018 return V1;
4019 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4020 // the instruction selector will not match, so get a canonical MOVL with
4021 // swapped operands to undo the commute.
4022 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004023 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004024
Nate Begeman9008ca62009-04-27 18:41:29 +00004025 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4026 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4027 X86::isUNPCKLMask(SVOp) ||
4028 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004029 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004030
Evan Cheng9bbbb982006-10-25 20:48:19 +00004031 if (V2IsSplat) {
4032 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004033 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004034 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004035 SDValue NewMask = NormalizeMask(SVOp, DAG);
4036 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4037 if (NSVOp != SVOp) {
4038 if (X86::isUNPCKLMask(NSVOp, true)) {
4039 return NewMask;
4040 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4041 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004042 }
4043 }
4044 }
4045
Evan Cheng9eca5e82006-10-25 21:49:50 +00004046 if (Commuted) {
4047 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004048 // FIXME: this seems wrong.
4049 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4050 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4051 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4052 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4053 X86::isUNPCKLMask(NewSVOp) ||
4054 X86::isUNPCKHMask(NewSVOp))
4055 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004056 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004057
Nate Begemanb9a47b82009-02-23 08:49:38 +00004058 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004059
4060 // Normalize the node to match x86 shuffle ops if needed
4061 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4062 return CommuteVectorShuffle(SVOp, DAG);
4063
4064 // Check for legal shuffle and return?
4065 SmallVector<int, 16> PermMask;
4066 SVOp->getMask(PermMask);
4067 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004068 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004069
Evan Cheng14b32e12007-12-11 01:46:18 +00004070 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4071 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004072 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004073 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004074 return NewOp;
4075 }
4076
Nate Begemanb9a47b82009-02-23 08:49:38 +00004077 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004078 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004079 if (NewOp.getNode())
4080 return NewOp;
4081 }
4082
Evan Chengace3c172008-07-22 21:13:36 +00004083 // Handle all 4 wide cases with a number of shuffles except for MMX.
4084 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004086
Dan Gohman475871a2008-07-27 21:46:04 +00004087 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004088}
4089
Dan Gohman475871a2008-07-27 21:46:04 +00004090SDValue
4091X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004092 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004093 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004094 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004096 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004097 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004098 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004099 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004100 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004102 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4103 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4104 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004105 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4106 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4107 DAG.getNode(ISD::BIT_CONVERT, dl,
4108 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004109 Op.getOperand(0)),
4110 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004111 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004112 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004113 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004114 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004115 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004116 } else if (VT == MVT::f32) {
4117 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4118 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004119 // result has a single use which is a store or a bitcast to i32. And in
4120 // the case of a store, it's not worth it if the index is a constant 0,
4121 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004122 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004123 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004124 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004125 if ((User->getOpcode() != ISD::STORE ||
4126 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4127 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004128 (User->getOpcode() != ISD::BIT_CONVERT ||
4129 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004130 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004131 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004132 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004133 Op.getOperand(0)),
4134 Op.getOperand(1));
4135 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004136 } else if (VT == MVT::i32) {
4137 // ExtractPS works with constant index.
4138 if (isa<ConstantSDNode>(Op.getOperand(1)))
4139 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004140 }
Dan Gohman475871a2008-07-27 21:46:04 +00004141 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004142}
4143
4144
Dan Gohman475871a2008-07-27 21:46:04 +00004145SDValue
4146X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004148 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004149
Evan Cheng62a3f152008-03-24 21:52:23 +00004150 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004151 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004152 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004153 return Res;
4154 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004155
Duncan Sands83ec4b62008-06-06 12:08:01 +00004156 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004157 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004158 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004159 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004160 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004161 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004162 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004163 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4164 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004165 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004166 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004167 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004168 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004169 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004170 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004171 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004172 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004173 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004174 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004175 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004176 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004177 if (Idx == 0)
4178 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004179
Evan Cheng0db9fe62006-04-25 20:13:52 +00004180 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 int Mask[4] = { Idx, -1, -1, -1 };
4182 MVT VVT = Op.getOperand(0).getValueType();
4183 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4184 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004185 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004186 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004187 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004188 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4189 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4190 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004191 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004192 if (Idx == 0)
4193 return Op;
4194
4195 // UNPCKHPD the element to the lowest double word, then movsd.
4196 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4197 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 int Mask[2] = { 1, -1 };
4199 MVT VVT = Op.getOperand(0).getValueType();
4200 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4201 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004202 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004203 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004204 }
4205
Dan Gohman475871a2008-07-27 21:46:04 +00004206 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004207}
4208
Dan Gohman475871a2008-07-27 21:46:04 +00004209SDValue
4210X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004211 MVT VT = Op.getValueType();
4212 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004213 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004214
Dan Gohman475871a2008-07-27 21:46:04 +00004215 SDValue N0 = Op.getOperand(0);
4216 SDValue N1 = Op.getOperand(1);
4217 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004218
Dan Gohmanef521f12008-08-14 22:53:18 +00004219 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4220 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004221 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004222 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004223 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4224 // argument.
4225 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004226 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004227 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004228 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004229 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004230 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004231 // Bits [7:6] of the constant are the source select. This will always be
4232 // zero here. The DAG Combiner may combine an extract_elt index into these
4233 // bits. For example (insert (extract, 3), 2) could be matched by putting
4234 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004235 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004236 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004237 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004238 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004239 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004240 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004241 } else if (EVT == MVT::i32) {
4242 // InsertPS works with constant index.
4243 if (isa<ConstantSDNode>(N2))
4244 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004245 }
Dan Gohman475871a2008-07-27 21:46:04 +00004246 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004247}
4248
Dan Gohman475871a2008-07-27 21:46:04 +00004249SDValue
4250X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004251 MVT VT = Op.getValueType();
4252 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004253
4254 if (Subtarget->hasSSE41())
4255 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4256
Evan Cheng794405e2007-12-12 07:55:34 +00004257 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004258 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004259
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004260 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004261 SDValue N0 = Op.getOperand(0);
4262 SDValue N1 = Op.getOperand(1);
4263 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004264
Duncan Sands83ec4b62008-06-06 12:08:01 +00004265 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004266 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4267 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004268 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004269 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004270 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004271 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004272 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004273 }
Dan Gohman475871a2008-07-27 21:46:04 +00004274 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004275}
4276
Dan Gohman475871a2008-07-27 21:46:04 +00004277SDValue
4278X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004279 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004280 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004281 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4282 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4283 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004284 Op.getOperand(0))));
4285
Dale Johannesenace16102009-02-03 19:33:06 +00004286 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 MVT VT = MVT::v2i32;
4288 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004289 default: break;
4290 case MVT::v16i8:
4291 case MVT::v8i16:
4292 VT = MVT::v4i32;
4293 break;
4294 }
Dale Johannesenace16102009-02-03 19:33:06 +00004295 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4296 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004297}
4298
Bill Wendling056292f2008-09-16 21:48:12 +00004299// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4300// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4301// one of the above mentioned nodes. It has to be wrapped because otherwise
4302// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4303// be used to form addressing mode. These wrapped nodes will be selected
4304// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004305SDValue
4306X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004307 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004308 // FIXME there isn't really any debug info here, should come from the parent
4309 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004310 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4311 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004312 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004313 // With PIC, the address is actually $g + Offset.
4314 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4315 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004316 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004317 DAG.getNode(X86ISD::GlobalBaseReg,
4318 DebugLoc::getUnknownLoc(),
4319 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004320 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004321 }
4322
4323 return Result;
4324}
4325
Dan Gohman475871a2008-07-27 21:46:04 +00004326SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004327X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004328 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004329 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004330 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4331 bool ExtraLoadRequired =
4332 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4333
4334 // Create the TargetGlobalAddress node, folding in the constant
4335 // offset if it is legal.
4336 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004337 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004338 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4339 Offset = 0;
4340 } else
4341 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004342 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004343
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004344 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004345 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004346 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4347 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004348 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004350
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004351 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4352 // load the value at address GV, not the value of GV itself. This means that
4353 // the GlobalAddress must be in the base or index register of the address, not
4354 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004355 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004356 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004357 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004358 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004359
Dan Gohman6520e202008-10-18 02:06:02 +00004360 // If there was a non-zero offset that we didn't fold, create an explicit
4361 // addition for it.
4362 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004363 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004364 DAG.getConstant(Offset, getPointerTy()));
4365
Evan Cheng0db9fe62006-04-25 20:13:52 +00004366 return Result;
4367}
4368
Evan Chengda43bcf2008-09-24 00:05:32 +00004369SDValue
4370X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4371 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004372 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004373 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004374}
4375
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004376static SDValue
4377GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004378 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004379 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4380 DebugLoc dl = GA->getDebugLoc();
4381 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4382 GA->getValueType(0),
4383 GA->getOffset());
4384 if (InFlag) {
4385 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004386 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004387 } else {
4388 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004389 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004390 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004391 SDValue Flag = Chain.getValue(1);
4392 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004393}
4394
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004395// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004396static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004397LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004398 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004399 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004400 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4401 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004402 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004403 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004404 PtrVT), InFlag);
4405 InFlag = Chain.getValue(1);
4406
Rafael Espindola15f1b662009-04-24 12:59:40 +00004407 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004408}
4409
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004410// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004411static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004412LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004413 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004414 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004415}
4416
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004417// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4418// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004419static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004420 const MVT PtrVT, TLSModel::Model model,
4421 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004422 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004423 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004424 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4425 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004426 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4427 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004428
4429 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4430 NULL, 0);
4431
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004432 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4433 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004435 GA->getValueType(0),
4436 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004437 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004438
Rafael Espindola9a580232009-02-27 13:37:18 +00004439 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004440 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004441 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004442
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004443 // The address of the thread local variable is the add of the thread
4444 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004445 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004446}
4447
Dan Gohman475871a2008-07-27 21:46:04 +00004448SDValue
4449X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004450 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004451 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004452 assert(Subtarget->isTargetELF() &&
4453 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004454 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004455 GlobalValue *GV = GA->getGlobal();
4456 TLSModel::Model model =
4457 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004458 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004459 switch (model) {
4460 case TLSModel::GeneralDynamic:
4461 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004462 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004463
4464 case TLSModel::InitialExec:
4465 case TLSModel::LocalExec:
4466 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004467 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004468 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004469 switch (model) {
4470 case TLSModel::GeneralDynamic:
4471 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004472 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004473
4474 case TLSModel::InitialExec:
4475 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004476 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004477 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004478 }
Chris Lattner5867de12009-04-01 22:14:45 +00004479 assert(0 && "Unreachable");
4480 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004481}
4482
Dan Gohman475871a2008-07-27 21:46:04 +00004483SDValue
4484X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004485 // FIXME there isn't really any debug info here
4486 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004487 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4488 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004489 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004490 // With PIC, the address is actually $g + Offset.
4491 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4492 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004493 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004494 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004495 DebugLoc::getUnknownLoc(),
4496 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004497 Result);
4498 }
4499
4500 return Result;
4501}
4502
Dan Gohman475871a2008-07-27 21:46:04 +00004503SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004504 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004505 // FIXME there isn't really any debug into here
4506 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004507 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004508 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004509 // With PIC, the address is actually $g + Offset.
4510 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4511 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004512 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004513 DAG.getNode(X86ISD::GlobalBaseReg,
4514 DebugLoc::getUnknownLoc(),
4515 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004516 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004517 }
4518
4519 return Result;
4520}
4521
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004522/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004523/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004524SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004525 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004526 MVT VT = Op.getValueType();
4527 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004528 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004529 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004530 SDValue ShOpLo = Op.getOperand(0);
4531 SDValue ShOpHi = Op.getOperand(1);
4532 SDValue ShAmt = Op.getOperand(2);
4533 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004534 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004535 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004536 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004537
Dan Gohman475871a2008-07-27 21:46:04 +00004538 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004539 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004540 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4541 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004542 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004543 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4544 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004545 }
Evan Chenge3413162006-01-09 18:33:28 +00004546
Dale Johannesenace16102009-02-03 19:33:06 +00004547 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004548 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004549 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004550 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004551
Dan Gohman475871a2008-07-27 21:46:04 +00004552 SDValue Hi, Lo;
4553 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4554 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4555 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004556
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004557 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004558 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4559 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004560 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004561 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4562 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004563 }
4564
Dan Gohman475871a2008-07-27 21:46:04 +00004565 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004566 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567}
Evan Chenga3195e82006-01-12 22:54:21 +00004568
Dan Gohman475871a2008-07-27 21:46:04 +00004569SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004570 MVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004571
4572 if (SrcVT.isVector()) {
4573 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
4574 return Op;
4575 }
4576 return SDValue();
4577 }
4578
Duncan Sands8e4eb092008-06-08 20:54:56 +00004579 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004580 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004581
Eli Friedman36df4992009-05-27 00:47:34 +00004582 // These are really Legal; return the operand so the caller accepts it as
4583 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004584 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004585 return Op;
4586 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4587 Subtarget->is64Bit()) {
4588 return Op;
4589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004590
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004591 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004592 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004593 MachineFunction &MF = DAG.getMachineFunction();
4594 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004596 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004597 StackSlot,
4598 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004599 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4600}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601
Eli Friedman948e95a2009-05-23 09:59:16 +00004602SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4603 SDValue StackSlot,
4604 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004606 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004607 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004608 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004609 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004610 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4611 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004612 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004613 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 Ops.push_back(Chain);
4615 Ops.push_back(StackSlot);
4616 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004617 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004618 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004619
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004620 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004621 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004622 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623
4624 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4625 // shouldn't be necessary except that RFP cannot be live across
4626 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004627 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004628 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004629 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004630 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004631 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004632 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004634 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004635 Ops.push_back(DAG.getValueType(Op.getValueType()));
4636 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004637 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4638 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004639 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004640 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004641
Evan Cheng0db9fe62006-04-25 20:13:52 +00004642 return Result;
4643}
4644
Bill Wendling8b8a6362009-01-17 03:56:04 +00004645// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4646SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4647 // This algorithm is not obvious. Here it is in C code, more or less:
4648 /*
4649 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4650 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4651 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004652
Bill Wendling8b8a6362009-01-17 03:56:04 +00004653 // Copy ints to xmm registers.
4654 __m128i xh = _mm_cvtsi32_si128( hi );
4655 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004656
Bill Wendling8b8a6362009-01-17 03:56:04 +00004657 // Combine into low half of a single xmm register.
4658 __m128i x = _mm_unpacklo_epi32( xh, xl );
4659 __m128d d;
4660 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004661
Bill Wendling8b8a6362009-01-17 03:56:04 +00004662 // Merge in appropriate exponents to give the integer bits the right
4663 // magnitude.
4664 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004665
Bill Wendling8b8a6362009-01-17 03:56:04 +00004666 // Subtract away the biases to deal with the IEEE-754 double precision
4667 // implicit 1.
4668 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004669
Bill Wendling8b8a6362009-01-17 03:56:04 +00004670 // All conversions up to here are exact. The correctly rounded result is
4671 // calculated using the current rounding mode using the following
4672 // horizontal add.
4673 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4674 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4675 // store doesn't really need to be here (except
4676 // maybe to zero the other double)
4677 return sd;
4678 }
4679 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004680
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004681 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004682
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004683 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004684 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004685 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4686 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4687 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4688 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4689 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004690 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004691
Bill Wendling8b8a6362009-01-17 03:56:04 +00004692 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004693 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4694 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4695 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004696 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004697
Dale Johannesenace16102009-02-03 19:33:06 +00004698 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4699 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004700 Op.getOperand(0),
4701 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004702 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4703 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004704 Op.getOperand(0),
4705 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004706 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004707 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004708 PseudoSourceValue::getConstantPool(), 0,
4709 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004710 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004711 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4712 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004713 PseudoSourceValue::getConstantPool(), 0,
4714 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004715 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004716
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004717 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004718 int ShufMask[2] = { 1, -1 };
4719 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4720 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004721 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4722 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004723 DAG.getIntPtrConstant(0));
4724}
4725
Bill Wendling8b8a6362009-01-17 03:56:04 +00004726// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4727SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004728 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004729 // FP constant to bias correct the final result.
4730 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4731 MVT::f64);
4732
4733 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004734 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4735 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004736 Op.getOperand(0),
4737 DAG.getIntPtrConstant(0)));
4738
Dale Johannesenace16102009-02-03 19:33:06 +00004739 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4740 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004741 DAG.getIntPtrConstant(0));
4742
4743 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004744 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4745 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4746 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004747 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004748 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4749 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004750 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004751 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4752 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004753 DAG.getIntPtrConstant(0));
4754
4755 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004756 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004757
4758 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004759 MVT DestVT = Op.getValueType();
4760
4761 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004762 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004763 DAG.getIntPtrConstant(0));
4764 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004765 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004766 }
4767
4768 // Handle final rounding.
4769 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004770}
4771
4772SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004773 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004774 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004775
Evan Chenga06ec9e2009-01-19 08:08:22 +00004776 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4777 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4778 // the optimization here.
4779 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004780 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004781
4782 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004783 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004784 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004785 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004786 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004787
Bill Wendling8b8a6362009-01-17 03:56:04 +00004788 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004789 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004790 return LowerUINT_TO_FP_i32(Op, DAG);
4791 }
4792
Eli Friedman948e95a2009-05-23 09:59:16 +00004793 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4794
4795 // Make a 64-bit buffer, and use it to build an FILD.
4796 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4797 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4798 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4799 getPointerTy(), StackSlot, WordOff);
4800 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4801 StackSlot, NULL, 0);
4802 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4803 OffsetSlot, NULL, 0);
4804 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004805}
4806
Dan Gohman475871a2008-07-27 21:46:04 +00004807std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004808FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004809 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004810
4811 MVT DstTy = Op.getValueType();
4812
4813 if (!IsSigned) {
4814 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4815 DstTy = MVT::i64;
4816 }
4817
4818 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4819 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004820 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004822 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004823 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004824 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004825 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004826 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004827 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00004828 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004829 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004830
Evan Cheng87c89352007-10-15 20:11:21 +00004831 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4832 // stack slot.
4833 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004834 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004835 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004836 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004837
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004839 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004840 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4841 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4842 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4843 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004845
Dan Gohman475871a2008-07-27 21:46:04 +00004846 SDValue Chain = DAG.getEntryNode();
4847 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004848 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004849 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004850 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004851 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004852 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004853 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004854 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4855 };
Dale Johannesenace16102009-02-03 19:33:06 +00004856 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004857 Chain = Value.getValue(1);
4858 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4859 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4860 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004861
Evan Cheng0db9fe62006-04-25 20:13:52 +00004862 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004863 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004864 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004865
Chris Lattner27a6c732007-11-24 07:07:01 +00004866 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004867}
4868
Dan Gohman475871a2008-07-27 21:46:04 +00004869SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00004870 if (Op.getValueType().isVector()) {
4871 if (Op.getValueType() == MVT::v2i32 &&
4872 Op.getOperand(0).getValueType() == MVT::v2f64) {
4873 return Op;
4874 }
4875 return SDValue();
4876 }
4877
Eli Friedman948e95a2009-05-23 09:59:16 +00004878 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004879 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00004880 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
4881 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004882
Chris Lattner27a6c732007-11-24 07:07:01 +00004883 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004884 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004885 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004886}
4887
Eli Friedman948e95a2009-05-23 09:59:16 +00004888SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4889 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4890 SDValue FIST = Vals.first, StackSlot = Vals.second;
4891 assert(FIST.getNode() && "Unexpected failure");
4892
4893 // Load the result.
4894 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4895 FIST, StackSlot, NULL, 0);
4896}
4897
Dan Gohman475871a2008-07-27 21:46:04 +00004898SDValue X86TargetLowering::LowerFABS(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;
4902 if (VT.isVector())
4903 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004904 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004905 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004906 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004907 CV.push_back(C);
4908 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004909 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004910 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004911 CV.push_back(C);
4912 CV.push_back(C);
4913 CV.push_back(C);
4914 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004915 }
Dan Gohmand3006222007-07-27 17:16:43 +00004916 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004917 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004918 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004919 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004920 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004921 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004922}
4923
Dan Gohman475871a2008-07-27 21:46:04 +00004924SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004925 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004926 MVT VT = Op.getValueType();
4927 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004928 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004929 if (VT.isVector()) {
4930 EltVT = VT.getVectorElementType();
4931 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004932 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004934 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004935 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004936 CV.push_back(C);
4937 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004938 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004939 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004940 CV.push_back(C);
4941 CV.push_back(C);
4942 CV.push_back(C);
4943 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004944 }
Dan Gohmand3006222007-07-27 17:16:43 +00004945 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004946 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004947 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004948 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004949 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004950 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004951 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4952 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004953 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004954 Op.getOperand(0)),
4955 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004956 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004957 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004958 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004959}
4960
Dan Gohman475871a2008-07-27 21:46:04 +00004961SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4962 SDValue Op0 = Op.getOperand(0);
4963 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004964 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004965 MVT VT = Op.getValueType();
4966 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004967
4968 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004969 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004970 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004971 SrcVT = VT;
4972 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004973 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004974 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004975 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004976 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004977 }
4978
4979 // At this point the operands and the result should have the same
4980 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004981
Evan Cheng68c47cb2007-01-05 07:55:56 +00004982 // First get the sign bit of second operand.
4983 std::vector<Constant*> CV;
4984 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004985 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4986 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004987 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004988 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4989 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004992 }
Dan Gohmand3006222007-07-27 17:16:43 +00004993 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004994 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004995 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004996 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004997 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004998 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004999
5000 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005001 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005002 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005003 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5004 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005005 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005006 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5007 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005008 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005009 }
5010
Evan Cheng73d6cf12007-01-05 21:37:56 +00005011 // Clear first operand sign bit.
5012 CV.clear();
5013 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005014 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5015 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005016 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005017 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5018 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5019 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5020 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005021 }
Dan Gohmand3006222007-07-27 17:16:43 +00005022 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005023 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005024 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005025 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005026 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005027 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005028
5029 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005030 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005031}
5032
Dan Gohman076aee32009-03-04 19:44:21 +00005033/// Emit nodes that will be selected as "test Op0,Op0", or something
5034/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005035SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5036 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005037 DebugLoc dl = Op.getDebugLoc();
5038
Dan Gohman31125812009-03-07 01:58:32 +00005039 // CF and OF aren't always set the way we want. Determine which
5040 // of these we need.
5041 bool NeedCF = false;
5042 bool NeedOF = false;
5043 switch (X86CC) {
5044 case X86::COND_A: case X86::COND_AE:
5045 case X86::COND_B: case X86::COND_BE:
5046 NeedCF = true;
5047 break;
5048 case X86::COND_G: case X86::COND_GE:
5049 case X86::COND_L: case X86::COND_LE:
5050 case X86::COND_O: case X86::COND_NO:
5051 NeedOF = true;
5052 break;
5053 default: break;
5054 }
5055
Dan Gohman076aee32009-03-04 19:44:21 +00005056 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005057 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5058 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5059 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005060 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005061 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005062 switch (Op.getNode()->getOpcode()) {
5063 case ISD::ADD:
5064 // Due to an isel shortcoming, be conservative if this add is likely to
5065 // be selected as part of a load-modify-store instruction. When the root
5066 // node in a match is a store, isel doesn't know how to remap non-chain
5067 // non-flag uses of other nodes in the match, such as the ADD in this
5068 // case. This leads to the ADD being left around and reselected, with
5069 // the result being two adds in the output.
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 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005075 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5076 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005077 if (C->getAPIntValue() == 1) {
5078 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005079 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005080 break;
5081 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005082 // An add of negative one (subtract of one) will be selected as a DEC.
5083 if (C->getAPIntValue().isAllOnesValue()) {
5084 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005085 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005086 break;
5087 }
5088 }
Dan Gohman076aee32009-03-04 19:44:21 +00005089 // Otherwise use a regular EFLAGS-setting add.
5090 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005091 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005092 break;
5093 case ISD::SUB:
5094 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5095 // likely to be selected as part of a load-modify-store instruction.
5096 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5097 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5098 if (UI->getOpcode() == ISD::STORE)
5099 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005100 // Otherwise use a regular EFLAGS-setting sub.
5101 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005102 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005103 break;
5104 case X86ISD::ADD:
5105 case X86ISD::SUB:
5106 case X86ISD::INC:
5107 case X86ISD::DEC:
5108 return SDValue(Op.getNode(), 1);
5109 default:
5110 default_case:
5111 break;
5112 }
5113 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005114 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005115 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005116 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005117 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005118 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005119 DAG.ReplaceAllUsesWith(Op, New);
5120 return SDValue(New.getNode(), 1);
5121 }
5122 }
5123
5124 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5125 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5126 DAG.getConstant(0, Op.getValueType()));
5127}
5128
5129/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5130/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005131SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5132 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005133 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5134 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005135 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005136
5137 DebugLoc dl = Op0.getDebugLoc();
5138 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5139}
5140
Dan Gohman475871a2008-07-27 21:46:04 +00005141SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005142 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005143 SDValue Op0 = Op.getOperand(0);
5144 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005145 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005146 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005147
Dan Gohmane5af2d32009-01-29 01:59:02 +00005148 // Lower (X & (1 << N)) == 0 to BT(X, N).
5149 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5150 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005151 if (Op0.getOpcode() == ISD::AND &&
5152 Op0.hasOneUse() &&
5153 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005154 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005155 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005156 SDValue LHS, RHS;
5157 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5158 if (ConstantSDNode *Op010C =
5159 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5160 if (Op010C->getZExtValue() == 1) {
5161 LHS = Op0.getOperand(0);
5162 RHS = Op0.getOperand(1).getOperand(1);
5163 }
5164 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5165 if (ConstantSDNode *Op000C =
5166 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5167 if (Op000C->getZExtValue() == 1) {
5168 LHS = Op0.getOperand(1);
5169 RHS = Op0.getOperand(0).getOperand(1);
5170 }
5171 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5172 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5173 SDValue AndLHS = Op0.getOperand(0);
5174 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5175 LHS = AndLHS.getOperand(0);
5176 RHS = AndLHS.getOperand(1);
5177 }
5178 }
Evan Cheng0488db92007-09-25 01:57:46 +00005179
Dan Gohmane5af2d32009-01-29 01:59:02 +00005180 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005181 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5182 // instruction. Since the shift amount is in-range-or-undefined, we know
5183 // that doing a bittest on the i16 value is ok. We extend to i32 because
5184 // the encoding for the i16 version is larger than the i32 version.
5185 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005186 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005187
5188 // If the operand types disagree, extend the shift amount to match. Since
5189 // BT ignores high bits (like shifts) we can use anyextend.
5190 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005191 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005192
Dale Johannesenace16102009-02-03 19:33:06 +00005193 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005194 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005195 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005196 DAG.getConstant(Cond, MVT::i8), BT);
5197 }
5198 }
5199
5200 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5201 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005202
Dan Gohman31125812009-03-07 01:58:32 +00005203 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005204 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005205 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005206}
5207
Dan Gohman475871a2008-07-27 21:46:04 +00005208SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5209 SDValue Cond;
5210 SDValue Op0 = Op.getOperand(0);
5211 SDValue Op1 = Op.getOperand(1);
5212 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005213 MVT VT = Op.getValueType();
5214 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5215 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005216 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005217
5218 if (isFP) {
5219 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005220 MVT VT0 = Op0.getValueType();
5221 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5222 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005223 bool Swap = false;
5224
5225 switch (SetCCOpcode) {
5226 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005227 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005228 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005229 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005230 case ISD::SETGT: Swap = true; // Fallthrough
5231 case ISD::SETLT:
5232 case ISD::SETOLT: SSECC = 1; break;
5233 case ISD::SETOGE:
5234 case ISD::SETGE: Swap = true; // Fallthrough
5235 case ISD::SETLE:
5236 case ISD::SETOLE: SSECC = 2; break;
5237 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005238 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005239 case ISD::SETNE: SSECC = 4; break;
5240 case ISD::SETULE: Swap = true;
5241 case ISD::SETUGE: SSECC = 5; break;
5242 case ISD::SETULT: Swap = true;
5243 case ISD::SETUGT: SSECC = 6; break;
5244 case ISD::SETO: SSECC = 7; break;
5245 }
5246 if (Swap)
5247 std::swap(Op0, Op1);
5248
Nate Begemanfb8ead02008-07-25 19:05:58 +00005249 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005250 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005251 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005252 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005253 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5254 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5255 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005256 }
5257 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005258 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005259 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5260 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5261 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005262 }
5263 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005264 }
5265 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005266 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005268
Nate Begeman30a0de92008-07-17 16:51:19 +00005269 // We are handling one of the integer comparisons here. Since SSE only has
5270 // GT and EQ comparisons for integer, swapping operands and multiple
5271 // operations may be required for some comparisons.
5272 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5273 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005274
Nate Begeman30a0de92008-07-17 16:51:19 +00005275 switch (VT.getSimpleVT()) {
5276 default: break;
5277 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5278 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5279 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5280 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5281 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Nate Begeman30a0de92008-07-17 16:51:19 +00005283 switch (SetCCOpcode) {
5284 default: break;
5285 case ISD::SETNE: Invert = true;
5286 case ISD::SETEQ: Opc = EQOpc; break;
5287 case ISD::SETLT: Swap = true;
5288 case ISD::SETGT: Opc = GTOpc; break;
5289 case ISD::SETGE: Swap = true;
5290 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5291 case ISD::SETULT: Swap = true;
5292 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5293 case ISD::SETUGE: Swap = true;
5294 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5295 }
5296 if (Swap)
5297 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005298
Nate Begeman30a0de92008-07-17 16:51:19 +00005299 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5300 // bits of the inputs before performing those operations.
5301 if (FlipSigns) {
5302 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005303 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5304 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005305 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005306 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5307 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005308 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5309 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005311
Dale Johannesenace16102009-02-03 19:33:06 +00005312 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005313
5314 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005315 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005316 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005317
Nate Begeman30a0de92008-07-17 16:51:19 +00005318 return Result;
5319}
Evan Cheng0488db92007-09-25 01:57:46 +00005320
Evan Cheng370e5342008-12-03 08:38:43 +00005321// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005322static bool isX86LogicalCmp(SDValue Op) {
5323 unsigned Opc = Op.getNode()->getOpcode();
5324 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5325 return true;
5326 if (Op.getResNo() == 1 &&
5327 (Opc == X86ISD::ADD ||
5328 Opc == X86ISD::SUB ||
5329 Opc == X86ISD::SMUL ||
5330 Opc == X86ISD::UMUL ||
5331 Opc == X86ISD::INC ||
5332 Opc == X86ISD::DEC))
5333 return true;
5334
5335 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005336}
5337
Dan Gohman475871a2008-07-27 21:46:04 +00005338SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005339 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005340 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005341 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005342 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005343
Evan Cheng734503b2006-09-11 02:19:56 +00005344 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005345 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005346
Evan Cheng3f41d662007-10-08 22:16:29 +00005347 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5348 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005349 if (Cond.getOpcode() == X86ISD::SETCC) {
5350 CC = Cond.getOperand(0);
5351
Dan Gohman475871a2008-07-27 21:46:04 +00005352 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005353 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005354 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005355
Evan Cheng3f41d662007-10-08 22:16:29 +00005356 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005357 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005358 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005359 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005360
Chris Lattnerd1980a52009-03-12 06:52:53 +00005361 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5362 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005363 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005364 addTest = false;
5365 }
5366 }
5367
5368 if (addTest) {
5369 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005370 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005371 }
5372
Dan Gohmanfc166572009-04-09 23:54:40 +00005373 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005374 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005375 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5376 // condition is true.
5377 Ops.push_back(Op.getOperand(2));
5378 Ops.push_back(Op.getOperand(1));
5379 Ops.push_back(CC);
5380 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005381 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005382}
5383
Evan Cheng370e5342008-12-03 08:38:43 +00005384// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5385// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5386// from the AND / OR.
5387static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5388 Opc = Op.getOpcode();
5389 if (Opc != ISD::OR && Opc != ISD::AND)
5390 return false;
5391 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5392 Op.getOperand(0).hasOneUse() &&
5393 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5394 Op.getOperand(1).hasOneUse());
5395}
5396
Evan Cheng961d6d42009-02-02 08:19:07 +00005397// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5398// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005399static bool isXor1OfSetCC(SDValue Op) {
5400 if (Op.getOpcode() != ISD::XOR)
5401 return false;
5402 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5403 if (N1C && N1C->getAPIntValue() == 1) {
5404 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5405 Op.getOperand(0).hasOneUse();
5406 }
5407 return false;
5408}
5409
Dan Gohman475871a2008-07-27 21:46:04 +00005410SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005411 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SDValue Chain = Op.getOperand(0);
5413 SDValue Cond = Op.getOperand(1);
5414 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005415 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005417
Evan Cheng0db9fe62006-04-25 20:13:52 +00005418 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005419 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005420#if 0
5421 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005422 else if (Cond.getOpcode() == X86ISD::ADD ||
5423 Cond.getOpcode() == X86ISD::SUB ||
5424 Cond.getOpcode() == X86ISD::SMUL ||
5425 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005426 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005427#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005428
Evan Cheng3f41d662007-10-08 22:16:29 +00005429 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5430 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005431 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005432 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005435 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005436 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005437 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005438 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005439 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005440 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005441 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005442 default: break;
5443 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005444 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005445 // These can only come from an arithmetic instruction with overflow,
5446 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005447 Cond = Cond.getNode()->getOperand(1);
5448 addTest = false;
5449 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005450 }
Evan Cheng0488db92007-09-25 01:57:46 +00005451 }
Evan Cheng370e5342008-12-03 08:38:43 +00005452 } else {
5453 unsigned CondOpc;
5454 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5455 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005456 if (CondOpc == ISD::OR) {
5457 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5458 // two branches instead of an explicit OR instruction with a
5459 // separate test.
5460 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005461 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005462 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005463 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005464 Chain, Dest, CC, Cmp);
5465 CC = Cond.getOperand(1).getOperand(0);
5466 Cond = Cmp;
5467 addTest = false;
5468 }
5469 } else { // ISD::AND
5470 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5471 // two branches instead of an explicit AND instruction with a
5472 // separate test. However, we only do this if this block doesn't
5473 // have a fall-through edge, because this requires an explicit
5474 // jmp when the condition is false.
5475 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005476 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005477 Op.getNode()->hasOneUse()) {
5478 X86::CondCode CCode =
5479 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5480 CCode = X86::GetOppositeBranchCondition(CCode);
5481 CC = DAG.getConstant(CCode, MVT::i8);
5482 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5483 // Look for an unconditional branch following this conditional branch.
5484 // We need this because we need to reverse the successors in order
5485 // to implement FCMP_OEQ.
5486 if (User.getOpcode() == ISD::BR) {
5487 SDValue FalseBB = User.getOperand(1);
5488 SDValue NewBR =
5489 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5490 assert(NewBR == User);
5491 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005492
Dale Johannesene4d209d2009-02-03 20:21:25 +00005493 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005494 Chain, Dest, CC, Cmp);
5495 X86::CondCode CCode =
5496 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5497 CCode = X86::GetOppositeBranchCondition(CCode);
5498 CC = DAG.getConstant(CCode, MVT::i8);
5499 Cond = Cmp;
5500 addTest = false;
5501 }
5502 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005503 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005504 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5505 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5506 // It should be transformed during dag combiner except when the condition
5507 // is set by a arithmetics with overflow node.
5508 X86::CondCode CCode =
5509 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5510 CCode = X86::GetOppositeBranchCondition(CCode);
5511 CC = DAG.getConstant(CCode, MVT::i8);
5512 Cond = Cond.getOperand(0).getOperand(1);
5513 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005514 }
Evan Cheng0488db92007-09-25 01:57:46 +00005515 }
5516
5517 if (addTest) {
5518 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005519 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005520 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005521 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005522 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005523}
5524
Anton Korobeynikove060b532007-04-17 19:34:00 +00005525
5526// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5527// Calls to _alloca is needed to probe the stack when allocating more than 4k
5528// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5529// that the guard pages used by the OS virtual memory manager are allocated in
5530// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005531SDValue
5532X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005533 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005534 assert(Subtarget->isTargetCygMing() &&
5535 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005536 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005537
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005538 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue Chain = Op.getOperand(0);
5540 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005541 // FIXME: Ensure alignment here
5542
Dan Gohman475871a2008-07-27 21:46:04 +00005543 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005544
Duncan Sands83ec4b62008-06-06 12:08:01 +00005545 MVT IntPtr = getPointerTy();
5546 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005547
Chris Lattnere563bbc2008-10-11 22:08:30 +00005548 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005549
Dale Johannesendd64c412009-02-04 00:33:20 +00005550 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005551 Flag = Chain.getValue(1);
5552
5553 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005554 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005555 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005556 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005557 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005558 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005559 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005560 Flag = Chain.getValue(1);
5561
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005562 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005563 DAG.getIntPtrConstant(0, true),
5564 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005565 Flag);
5566
Dale Johannesendd64c412009-02-04 00:33:20 +00005567 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005568
Dan Gohman475871a2008-07-27 21:46:04 +00005569 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005570 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005571}
5572
Dan Gohman475871a2008-07-27 21:46:04 +00005573SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005574X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005575 SDValue Chain,
5576 SDValue Dst, SDValue Src,
5577 SDValue Size, unsigned Align,
5578 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005579 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005580 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005581
Bill Wendling6f287b22008-09-30 21:22:07 +00005582 // If not DWORD aligned or size is more than the threshold, call the library.
5583 // The libc version is likely to be faster for these cases. It can use the
5584 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005585 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005586 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005587 ConstantSize->getZExtValue() >
5588 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005589 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005590
5591 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005592 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005593
Bill Wendling6158d842008-10-01 00:59:58 +00005594 if (const char *bzeroEntry = V &&
5595 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5596 MVT IntPtr = getPointerTy();
5597 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005598 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005599 TargetLowering::ArgListEntry Entry;
5600 Entry.Node = Dst;
5601 Entry.Ty = IntPtrTy;
5602 Args.push_back(Entry);
5603 Entry.Node = Size;
5604 Args.push_back(Entry);
5605 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005606 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5607 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005608 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005609 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005610 }
5611
Dan Gohman707e0182008-04-12 04:36:06 +00005612 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005613 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005614 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005615
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005616 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005617 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005618 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005619 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005620 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005621 unsigned BytesLeft = 0;
5622 bool TwoRepStos = false;
5623 if (ValC) {
5624 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005625 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005626
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627 // If the value is a constant, then we can potentially use larger sets.
5628 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005629 case 2: // WORD aligned
5630 AVT = MVT::i16;
5631 ValReg = X86::AX;
5632 Val = (Val << 8) | Val;
5633 break;
5634 case 0: // DWORD aligned
5635 AVT = MVT::i32;
5636 ValReg = X86::EAX;
5637 Val = (Val << 8) | Val;
5638 Val = (Val << 16) | Val;
5639 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5640 AVT = MVT::i64;
5641 ValReg = X86::RAX;
5642 Val = (Val << 32) | Val;
5643 }
5644 break;
5645 default: // Byte aligned
5646 AVT = MVT::i8;
5647 ValReg = X86::AL;
5648 Count = DAG.getIntPtrConstant(SizeVal);
5649 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005650 }
5651
Duncan Sands8e4eb092008-06-08 20:54:56 +00005652 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005653 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005654 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5655 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005656 }
5657
Dale Johannesen0f502f62009-02-03 22:26:09 +00005658 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005659 InFlag);
5660 InFlag = Chain.getValue(1);
5661 } else {
5662 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005663 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005664 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005665 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005666 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005667
Scott Michelfdc40a02009-02-17 22:15:04 +00005668 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005669 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005670 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005671 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005672 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005673 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005674 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005675 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005676
Chris Lattnerd96d0722007-02-25 06:40:16 +00005677 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005678 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005679 Ops.push_back(Chain);
5680 Ops.push_back(DAG.getValueType(AVT));
5681 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005682 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005683
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684 if (TwoRepStos) {
5685 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005686 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005687 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005688 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005689 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005690 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005691 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005692 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005693 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005694 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005695 Ops.clear();
5696 Ops.push_back(Chain);
5697 Ops.push_back(DAG.getValueType(MVT::i8));
5698 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005699 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005701 // Handle the last 1 - 7 bytes.
5702 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005703 MVT AddrVT = Dst.getValueType();
5704 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005705
Dale Johannesen0f502f62009-02-03 22:26:09 +00005706 Chain = DAG.getMemset(Chain, dl,
5707 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005708 DAG.getConstant(Offset, AddrVT)),
5709 Src,
5710 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005711 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005712 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005713
Dan Gohman707e0182008-04-12 04:36:06 +00005714 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005715 return Chain;
5716}
Evan Cheng11e15b32006-04-03 20:53:28 +00005717
Dan Gohman475871a2008-07-27 21:46:04 +00005718SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005719X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005720 SDValue Chain, SDValue Dst, SDValue Src,
5721 SDValue Size, unsigned Align,
5722 bool AlwaysInline,
5723 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005724 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005725 // This requires the copy size to be a constant, preferrably
5726 // within a subtarget-specific limit.
5727 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5728 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005729 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005730 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005731 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005732 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005733
Evan Cheng1887c1c2008-08-21 21:00:15 +00005734 /// If not DWORD aligned, call the library.
5735 if ((Align & 3) != 0)
5736 return SDValue();
5737
5738 // DWORD aligned
5739 MVT AVT = MVT::i32;
5740 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005741 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005742
Duncan Sands83ec4b62008-06-06 12:08:01 +00005743 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005744 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005745 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005746 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005747
Dan Gohman475871a2008-07-27 21:46:04 +00005748 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005749 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005750 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005751 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005752 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005753 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005754 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005755 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005756 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005757 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005758 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005759 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005760 InFlag = Chain.getValue(1);
5761
Chris Lattnerd96d0722007-02-25 06:40:16 +00005762 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005763 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005764 Ops.push_back(Chain);
5765 Ops.push_back(DAG.getValueType(AVT));
5766 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005767 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005768
Dan Gohman475871a2008-07-27 21:46:04 +00005769 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005770 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005771 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005772 // Handle the last 1 - 7 bytes.
5773 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005774 MVT DstVT = Dst.getValueType();
5775 MVT SrcVT = Src.getValueType();
5776 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005777 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005778 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005779 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005780 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005781 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005782 DAG.getConstant(BytesLeft, SizeVT),
5783 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005784 DstSV, DstSVOff + Offset,
5785 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005786 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005787
Scott Michelfdc40a02009-02-17 22:15:04 +00005788 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005789 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005790}
5791
Dan Gohman475871a2008-07-27 21:46:04 +00005792SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005793 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005794 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005795
Evan Cheng25ab6902006-09-08 06:48:29 +00005796 if (!Subtarget->is64Bit()) {
5797 // vastart just stores the address of the VarArgsFrameIndex slot into the
5798 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005799 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005800 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005801 }
5802
5803 // __va_list_tag:
5804 // gp_offset (0 - 6 * 8)
5805 // fp_offset (48 - 48 + 8 * 16)
5806 // overflow_arg_area (point to parameters coming in memory).
5807 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005808 SmallVector<SDValue, 8> MemOps;
5809 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005810 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005811 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005812 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005813 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005814 MemOps.push_back(Store);
5815
5816 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005817 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005818 FIN, DAG.getIntPtrConstant(4));
5819 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005820 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005821 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005822 MemOps.push_back(Store);
5823
5824 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005825 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005826 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005827 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005828 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005829 MemOps.push_back(Store);
5830
5831 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005832 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005833 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005834 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005835 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005836 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005837 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005838 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005839}
5840
Dan Gohman475871a2008-07-27 21:46:04 +00005841SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005842 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5843 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005844 SDValue Chain = Op.getOperand(0);
5845 SDValue SrcPtr = Op.getOperand(1);
5846 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005847
5848 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5849 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005850 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005851}
5852
Dan Gohman475871a2008-07-27 21:46:04 +00005853SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005854 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005855 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005856 SDValue Chain = Op.getOperand(0);
5857 SDValue DstPtr = Op.getOperand(1);
5858 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005859 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5860 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005861 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005862
Dale Johannesendd64c412009-02-04 00:33:20 +00005863 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005864 DAG.getIntPtrConstant(24), 8, false,
5865 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005866}
5867
Dan Gohman475871a2008-07-27 21:46:04 +00005868SDValue
5869X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005870 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005871 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005872 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005873 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005874 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875 case Intrinsic::x86_sse_comieq_ss:
5876 case Intrinsic::x86_sse_comilt_ss:
5877 case Intrinsic::x86_sse_comile_ss:
5878 case Intrinsic::x86_sse_comigt_ss:
5879 case Intrinsic::x86_sse_comige_ss:
5880 case Intrinsic::x86_sse_comineq_ss:
5881 case Intrinsic::x86_sse_ucomieq_ss:
5882 case Intrinsic::x86_sse_ucomilt_ss:
5883 case Intrinsic::x86_sse_ucomile_ss:
5884 case Intrinsic::x86_sse_ucomigt_ss:
5885 case Intrinsic::x86_sse_ucomige_ss:
5886 case Intrinsic::x86_sse_ucomineq_ss:
5887 case Intrinsic::x86_sse2_comieq_sd:
5888 case Intrinsic::x86_sse2_comilt_sd:
5889 case Intrinsic::x86_sse2_comile_sd:
5890 case Intrinsic::x86_sse2_comigt_sd:
5891 case Intrinsic::x86_sse2_comige_sd:
5892 case Intrinsic::x86_sse2_comineq_sd:
5893 case Intrinsic::x86_sse2_ucomieq_sd:
5894 case Intrinsic::x86_sse2_ucomilt_sd:
5895 case Intrinsic::x86_sse2_ucomile_sd:
5896 case Intrinsic::x86_sse2_ucomigt_sd:
5897 case Intrinsic::x86_sse2_ucomige_sd:
5898 case Intrinsic::x86_sse2_ucomineq_sd: {
5899 unsigned Opc = 0;
5900 ISD::CondCode CC = ISD::SETCC_INVALID;
5901 switch (IntNo) {
5902 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005903 case Intrinsic::x86_sse_comieq_ss:
5904 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005905 Opc = X86ISD::COMI;
5906 CC = ISD::SETEQ;
5907 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005908 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005909 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910 Opc = X86ISD::COMI;
5911 CC = ISD::SETLT;
5912 break;
5913 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005914 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005915 Opc = X86ISD::COMI;
5916 CC = ISD::SETLE;
5917 break;
5918 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005919 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005920 Opc = X86ISD::COMI;
5921 CC = ISD::SETGT;
5922 break;
5923 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005924 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925 Opc = X86ISD::COMI;
5926 CC = ISD::SETGE;
5927 break;
5928 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005929 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005930 Opc = X86ISD::COMI;
5931 CC = ISD::SETNE;
5932 break;
5933 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005934 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005935 Opc = X86ISD::UCOMI;
5936 CC = ISD::SETEQ;
5937 break;
5938 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005939 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005940 Opc = X86ISD::UCOMI;
5941 CC = ISD::SETLT;
5942 break;
5943 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005944 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005945 Opc = X86ISD::UCOMI;
5946 CC = ISD::SETLE;
5947 break;
5948 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005949 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005950 Opc = X86ISD::UCOMI;
5951 CC = ISD::SETGT;
5952 break;
5953 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005954 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005955 Opc = X86ISD::UCOMI;
5956 CC = ISD::SETGE;
5957 break;
5958 case Intrinsic::x86_sse_ucomineq_ss:
5959 case Intrinsic::x86_sse2_ucomineq_sd:
5960 Opc = X86ISD::UCOMI;
5961 CC = ISD::SETNE;
5962 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005963 }
Evan Cheng734503b2006-09-11 02:19:56 +00005964
Dan Gohman475871a2008-07-27 21:46:04 +00005965 SDValue LHS = Op.getOperand(1);
5966 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005967 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005968 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5969 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005970 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005971 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005972 }
Evan Cheng5759f972008-05-04 09:15:50 +00005973
5974 // Fix vector shift instructions where the last operand is a non-immediate
5975 // i32 value.
5976 case Intrinsic::x86_sse2_pslli_w:
5977 case Intrinsic::x86_sse2_pslli_d:
5978 case Intrinsic::x86_sse2_pslli_q:
5979 case Intrinsic::x86_sse2_psrli_w:
5980 case Intrinsic::x86_sse2_psrli_d:
5981 case Intrinsic::x86_sse2_psrli_q:
5982 case Intrinsic::x86_sse2_psrai_w:
5983 case Intrinsic::x86_sse2_psrai_d:
5984 case Intrinsic::x86_mmx_pslli_w:
5985 case Intrinsic::x86_mmx_pslli_d:
5986 case Intrinsic::x86_mmx_pslli_q:
5987 case Intrinsic::x86_mmx_psrli_w:
5988 case Intrinsic::x86_mmx_psrli_d:
5989 case Intrinsic::x86_mmx_psrli_q:
5990 case Intrinsic::x86_mmx_psrai_w:
5991 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005992 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005993 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005994 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005995
5996 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005997 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005998 switch (IntNo) {
5999 case Intrinsic::x86_sse2_pslli_w:
6000 NewIntNo = Intrinsic::x86_sse2_psll_w;
6001 break;
6002 case Intrinsic::x86_sse2_pslli_d:
6003 NewIntNo = Intrinsic::x86_sse2_psll_d;
6004 break;
6005 case Intrinsic::x86_sse2_pslli_q:
6006 NewIntNo = Intrinsic::x86_sse2_psll_q;
6007 break;
6008 case Intrinsic::x86_sse2_psrli_w:
6009 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6010 break;
6011 case Intrinsic::x86_sse2_psrli_d:
6012 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6013 break;
6014 case Intrinsic::x86_sse2_psrli_q:
6015 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6016 break;
6017 case Intrinsic::x86_sse2_psrai_w:
6018 NewIntNo = Intrinsic::x86_sse2_psra_w;
6019 break;
6020 case Intrinsic::x86_sse2_psrai_d:
6021 NewIntNo = Intrinsic::x86_sse2_psra_d;
6022 break;
6023 default: {
6024 ShAmtVT = MVT::v2i32;
6025 switch (IntNo) {
6026 case Intrinsic::x86_mmx_pslli_w:
6027 NewIntNo = Intrinsic::x86_mmx_psll_w;
6028 break;
6029 case Intrinsic::x86_mmx_pslli_d:
6030 NewIntNo = Intrinsic::x86_mmx_psll_d;
6031 break;
6032 case Intrinsic::x86_mmx_pslli_q:
6033 NewIntNo = Intrinsic::x86_mmx_psll_q;
6034 break;
6035 case Intrinsic::x86_mmx_psrli_w:
6036 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6037 break;
6038 case Intrinsic::x86_mmx_psrli_d:
6039 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6040 break;
6041 case Intrinsic::x86_mmx_psrli_q:
6042 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6043 break;
6044 case Intrinsic::x86_mmx_psrai_w:
6045 NewIntNo = Intrinsic::x86_mmx_psra_w;
6046 break;
6047 case Intrinsic::x86_mmx_psrai_d:
6048 NewIntNo = Intrinsic::x86_mmx_psra_d;
6049 break;
6050 default: abort(); // Can't reach here.
6051 }
6052 break;
6053 }
6054 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006055 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006056 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6057 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6058 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006059 DAG.getConstant(NewIntNo, MVT::i32),
6060 Op.getOperand(1), ShAmt);
6061 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006062 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006063}
Evan Cheng72261582005-12-20 06:22:03 +00006064
Dan Gohman475871a2008-07-27 21:46:04 +00006065SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006066 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006067 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006068
6069 if (Depth > 0) {
6070 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6071 SDValue Offset =
6072 DAG.getConstant(TD->getPointerSize(),
6073 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006074 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006075 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006076 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006077 NULL, 0);
6078 }
6079
6080 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006081 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006082 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006083 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006084}
6085
Dan Gohman475871a2008-07-27 21:46:04 +00006086SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006087 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6088 MFI->setFrameAddressIsTaken(true);
6089 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006090 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006091 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6092 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006093 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006094 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006095 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006096 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006097}
6098
Dan Gohman475871a2008-07-27 21:46:04 +00006099SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006100 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006101 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006102}
6103
Dan Gohman475871a2008-07-27 21:46:04 +00006104SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006105{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006106 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006107 SDValue Chain = Op.getOperand(0);
6108 SDValue Offset = Op.getOperand(1);
6109 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006110 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006111
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006112 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6113 getPointerTy());
6114 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006115
Dale Johannesene4d209d2009-02-03 20:21:25 +00006116 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006117 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006118 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6119 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006120 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006121 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006122
Dale Johannesene4d209d2009-02-03 20:21:25 +00006123 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006124 MVT::Other,
6125 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006126}
6127
Dan Gohman475871a2008-07-27 21:46:04 +00006128SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006129 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006130 SDValue Root = Op.getOperand(0);
6131 SDValue Trmp = Op.getOperand(1); // trampoline
6132 SDValue FPtr = Op.getOperand(2); // nested function
6133 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006134 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006135
Dan Gohman69de1932008-02-06 22:27:42 +00006136 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006137
Duncan Sands339e14f2008-01-16 22:55:25 +00006138 const X86InstrInfo *TII =
6139 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6140
Duncan Sandsb116fac2007-07-27 20:02:49 +00006141 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006142 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006143
6144 // Large code-model.
6145
6146 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6147 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6148
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006149 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6150 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006151
6152 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6153
6154 // Load the pointer to the nested function into R11.
6155 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006156 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006157 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6158 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006159
Scott Michelfdc40a02009-02-17 22:15:04 +00006160 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006161 DAG.getConstant(2, MVT::i64));
6162 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006163
6164 // Load the 'nest' parameter value into R10.
6165 // R10 is specified in X86CallingConv.td
6166 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006167 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006168 DAG.getConstant(10, MVT::i64));
6169 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6170 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006171
Scott Michelfdc40a02009-02-17 22:15:04 +00006172 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006173 DAG.getConstant(12, MVT::i64));
6174 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006175
6176 // Jump to the nested function.
6177 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006178 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006179 DAG.getConstant(20, MVT::i64));
6180 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6181 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006182
6183 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006184 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006185 DAG.getConstant(22, MVT::i64));
6186 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006187 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006188
Dan Gohman475871a2008-07-27 21:46:04 +00006189 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006190 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6191 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006192 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006193 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006194 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6195 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006196 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006197
6198 switch (CC) {
6199 default:
6200 assert(0 && "Unsupported calling convention");
6201 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006202 case CallingConv::X86_StdCall: {
6203 // Pass 'nest' parameter in ECX.
6204 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006205 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006206
6207 // Check that ECX wasn't needed by an 'inreg' parameter.
6208 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006209 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006210
Chris Lattner58d74912008-03-12 17:45:29 +00006211 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006212 unsigned InRegCount = 0;
6213 unsigned Idx = 1;
6214
6215 for (FunctionType::param_iterator I = FTy->param_begin(),
6216 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006217 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006218 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006219 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006220
6221 if (InRegCount > 2) {
6222 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6223 abort();
6224 }
6225 }
6226 break;
6227 }
6228 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006229 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006230 // Pass 'nest' parameter in EAX.
6231 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006232 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006233 break;
6234 }
6235
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue OutChains[4];
6237 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006238
Scott Michelfdc40a02009-02-17 22:15:04 +00006239 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006240 DAG.getConstant(10, MVT::i32));
6241 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006242
Duncan Sands339e14f2008-01-16 22:55:25 +00006243 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006244 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006245 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006246 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006247 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006248
Scott Michelfdc40a02009-02-17 22:15:04 +00006249 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006250 DAG.getConstant(1, MVT::i32));
6251 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006252
Duncan Sands339e14f2008-01-16 22:55:25 +00006253 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006254 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006255 DAG.getConstant(5, MVT::i32));
6256 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006257 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006258
Scott Michelfdc40a02009-02-17 22:15:04 +00006259 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006260 DAG.getConstant(6, MVT::i32));
6261 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006262
Dan Gohman475871a2008-07-27 21:46:04 +00006263 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006264 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6265 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006266 }
6267}
6268
Dan Gohman475871a2008-07-27 21:46:04 +00006269SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006270 /*
6271 The rounding mode is in bits 11:10 of FPSR, and has the following
6272 settings:
6273 00 Round to nearest
6274 01 Round to -inf
6275 10 Round to +inf
6276 11 Round to 0
6277
6278 FLT_ROUNDS, on the other hand, expects the following:
6279 -1 Undefined
6280 0 Round to 0
6281 1 Round to nearest
6282 2 Round to +inf
6283 3 Round to -inf
6284
6285 To perform the conversion, we do:
6286 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6287 */
6288
6289 MachineFunction &MF = DAG.getMachineFunction();
6290 const TargetMachine &TM = MF.getTarget();
6291 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6292 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006293 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006294 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006295
6296 // Save FP Control Word to stack slot
6297 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006298 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006299
Dale Johannesene4d209d2009-02-03 20:21:25 +00006300 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006301 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006302
6303 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006304 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006305
6306 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006307 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006308 DAG.getNode(ISD::SRL, dl, MVT::i16,
6309 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006310 CWD, DAG.getConstant(0x800, MVT::i16)),
6311 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006312 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006313 DAG.getNode(ISD::SRL, dl, MVT::i16,
6314 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006315 CWD, DAG.getConstant(0x400, MVT::i16)),
6316 DAG.getConstant(9, MVT::i8));
6317
Dan Gohman475871a2008-07-27 21:46:04 +00006318 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006319 DAG.getNode(ISD::AND, dl, MVT::i16,
6320 DAG.getNode(ISD::ADD, dl, MVT::i16,
6321 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006322 DAG.getConstant(1, MVT::i16)),
6323 DAG.getConstant(3, MVT::i16));
6324
6325
Duncan Sands83ec4b62008-06-06 12:08:01 +00006326 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006327 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006328}
6329
Dan Gohman475871a2008-07-27 21:46:04 +00006330SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006331 MVT VT = Op.getValueType();
6332 MVT OpVT = VT;
6333 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006334 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006335
6336 Op = Op.getOperand(0);
6337 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006338 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006339 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006340 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006341 }
Evan Cheng18efe262007-12-14 02:13:44 +00006342
Evan Cheng152804e2007-12-14 08:30:15 +00006343 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6344 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006345 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006346
6347 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006348 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006349 Ops.push_back(Op);
6350 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6351 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6352 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006353 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006354
6355 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006356 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006357
Evan Cheng18efe262007-12-14 02:13:44 +00006358 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006359 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006360 return Op;
6361}
6362
Dan Gohman475871a2008-07-27 21:46:04 +00006363SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006364 MVT VT = Op.getValueType();
6365 MVT OpVT = VT;
6366 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006367 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006368
6369 Op = Op.getOperand(0);
6370 if (VT == MVT::i8) {
6371 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006372 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006373 }
Evan Cheng152804e2007-12-14 08:30:15 +00006374
6375 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6376 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006377 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006378
6379 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006380 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006381 Ops.push_back(Op);
6382 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6383 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6384 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006385 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006386
Evan Cheng18efe262007-12-14 02:13:44 +00006387 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006388 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006389 return Op;
6390}
6391
Mon P Wangaf9b9522008-12-18 21:42:19 +00006392SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6393 MVT VT = Op.getValueType();
6394 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006395 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006396
Mon P Wangaf9b9522008-12-18 21:42:19 +00006397 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6398 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6399 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6400 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6401 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6402 //
6403 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6404 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6405 // return AloBlo + AloBhi + AhiBlo;
6406
6407 SDValue A = Op.getOperand(0);
6408 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006409
Dale Johannesene4d209d2009-02-03 20:21:25 +00006410 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006411 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6412 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006413 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006414 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6415 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006416 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006417 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6418 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006419 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006420 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6421 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006422 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006423 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6424 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006425 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006426 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6427 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006428 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006429 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6430 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006431 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6432 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006433 return Res;
6434}
6435
6436
Bill Wendling74c37652008-12-09 22:08:41 +00006437SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6438 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6439 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006440 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6441 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006442 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006443 SDValue LHS = N->getOperand(0);
6444 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006445 unsigned BaseOp = 0;
6446 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006447 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006448
6449 switch (Op.getOpcode()) {
6450 default: assert(0 && "Unknown ovf instruction!");
6451 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006452 // A subtract of one will be selected as a INC. Note that INC doesn't
6453 // set CF, so we can't do this for UADDO.
6454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6455 if (C->getAPIntValue() == 1) {
6456 BaseOp = X86ISD::INC;
6457 Cond = X86::COND_O;
6458 break;
6459 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006460 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006461 Cond = X86::COND_O;
6462 break;
6463 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006464 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006465 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006466 break;
6467 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006468 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6469 // set CF, so we can't do this for USUBO.
6470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6471 if (C->getAPIntValue() == 1) {
6472 BaseOp = X86ISD::DEC;
6473 Cond = X86::COND_O;
6474 break;
6475 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006476 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006477 Cond = X86::COND_O;
6478 break;
6479 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006480 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006481 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006482 break;
6483 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006484 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006485 Cond = X86::COND_O;
6486 break;
6487 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006488 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006489 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006490 break;
6491 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006492
Bill Wendling61edeb52008-12-02 01:06:39 +00006493 // Also sets EFLAGS.
6494 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006495 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006496
Bill Wendling61edeb52008-12-02 01:06:39 +00006497 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006498 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006499 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006500
Bill Wendling61edeb52008-12-02 01:06:39 +00006501 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6502 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006503}
6504
Dan Gohman475871a2008-07-27 21:46:04 +00006505SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006506 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006507 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006508 unsigned Reg = 0;
6509 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006510 switch(T.getSimpleVT()) {
6511 default:
6512 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006513 case MVT::i8: Reg = X86::AL; size = 1; break;
6514 case MVT::i16: Reg = X86::AX; size = 2; break;
6515 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006516 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006517 assert(Subtarget->is64Bit() && "Node not type legal!");
6518 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006519 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006520 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006521 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006522 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006523 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006524 Op.getOperand(1),
6525 Op.getOperand(3),
6526 DAG.getTargetConstant(size, MVT::i8),
6527 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006528 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006529 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006530 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006531 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006532 return cpOut;
6533}
6534
Duncan Sands1607f052008-12-01 11:39:25 +00006535SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006536 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006537 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006538 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006539 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006540 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006541 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006542 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6543 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006544 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006545 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006546 DAG.getConstant(32, MVT::i8));
6547 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006548 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006549 rdx.getValue(1)
6550 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006551 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006552}
6553
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006554SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6555 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006556 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006557 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006558 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006559 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006560 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006561 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006562 Node->getOperand(0),
6563 Node->getOperand(1), negOp,
6564 cast<AtomicSDNode>(Node)->getSrcValue(),
6565 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006566}
6567
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568/// LowerOperation - Provide custom lowering hooks for some operations.
6569///
Dan Gohman475871a2008-07-27 21:46:04 +00006570SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006571 switch (Op.getOpcode()) {
6572 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006573 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6574 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006575 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6576 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6577 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6578 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6579 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6580 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6581 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006582 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006583 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006584 case ISD::SHL_PARTS:
6585 case ISD::SRA_PARTS:
6586 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6587 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006588 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006589 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006590 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006591 case ISD::FABS: return LowerFABS(Op, DAG);
6592 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006593 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006594 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006595 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006596 case ISD::SELECT: return LowerSELECT(Op, DAG);
6597 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006598 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006599 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006601 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006603 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006604 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006606 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6607 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006608 case ISD::FRAME_TO_ARGS_OFFSET:
6609 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006610 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006611 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006612 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006613 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006614 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6615 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006616 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006617 case ISD::SADDO:
6618 case ISD::UADDO:
6619 case ISD::SSUBO:
6620 case ISD::USUBO:
6621 case ISD::SMULO:
6622 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006623 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006624 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006625}
6626
Duncan Sands1607f052008-12-01 11:39:25 +00006627void X86TargetLowering::
6628ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6629 SelectionDAG &DAG, unsigned NewOp) {
6630 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006631 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006632 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6633
6634 SDValue Chain = Node->getOperand(0);
6635 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006636 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006637 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006638 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006639 Node->getOperand(2), DAG.getIntPtrConstant(1));
6640 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6641 // have a MemOperand. Pass the info through as a normal operand.
6642 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6643 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6644 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006645 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006646 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006647 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006648 Results.push_back(Result.getValue(2));
6649}
6650
Duncan Sands126d9072008-07-04 11:47:58 +00006651/// ReplaceNodeResults - Replace a node with an illegal result type
6652/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006653void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6654 SmallVectorImpl<SDValue>&Results,
6655 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006656 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006657 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006658 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006659 assert(false && "Do not know how to custom type legalize this operation!");
6660 return;
6661 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006662 std::pair<SDValue,SDValue> Vals =
6663 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006664 SDValue FIST = Vals.first, StackSlot = Vals.second;
6665 if (FIST.getNode() != 0) {
6666 MVT VT = N->getValueType(0);
6667 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006668 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006669 }
6670 return;
6671 }
6672 case ISD::READCYCLECOUNTER: {
6673 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6674 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006675 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006676 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006677 rd.getValue(1));
6678 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006679 eax.getValue(2));
6680 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6681 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006682 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006683 Results.push_back(edx.getValue(1));
6684 return;
6685 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006686 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006687 MVT T = N->getValueType(0);
6688 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6689 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006690 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006691 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006692 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006693 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006694 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6695 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006696 cpInL.getValue(1));
6697 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006698 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006699 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006700 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006701 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006702 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006703 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006704 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006705 swapInL.getValue(1));
6706 SDValue Ops[] = { swapInH.getValue(0),
6707 N->getOperand(1),
6708 swapInH.getValue(1) };
6709 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006710 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006711 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6712 MVT::i32, Result.getValue(1));
6713 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6714 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006715 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006716 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006717 Results.push_back(cpOutH.getValue(1));
6718 return;
6719 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006720 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006721 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6722 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006723 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006724 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6725 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006726 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006727 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6728 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006729 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006730 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6731 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006732 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006733 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6734 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006735 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006736 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6737 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006738 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006739 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6740 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006741 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006742}
6743
Evan Cheng72261582005-12-20 06:22:03 +00006744const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6745 switch (Opcode) {
6746 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006747 case X86ISD::BSF: return "X86ISD::BSF";
6748 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006749 case X86ISD::SHLD: return "X86ISD::SHLD";
6750 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006751 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006752 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006753 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006754 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006755 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006756 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006757 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6758 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6759 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006760 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006761 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006762 case X86ISD::CALL: return "X86ISD::CALL";
6763 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6764 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006765 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006766 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006767 case X86ISD::COMI: return "X86ISD::COMI";
6768 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006769 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006770 case X86ISD::CMOV: return "X86ISD::CMOV";
6771 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006772 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006773 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6774 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006775 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006776 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006777 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006778 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006779 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6780 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006781 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006782 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006783 case X86ISD::FMAX: return "X86ISD::FMAX";
6784 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006785 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6786 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006787 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006788 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006789 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006790 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006791 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006792 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6793 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006794 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6795 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6796 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6797 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6798 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6799 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006800 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6801 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006802 case X86ISD::VSHL: return "X86ISD::VSHL";
6803 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006804 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6805 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6806 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6807 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6808 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6809 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6810 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6811 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6812 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6813 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006814 case X86ISD::ADD: return "X86ISD::ADD";
6815 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006816 case X86ISD::SMUL: return "X86ISD::SMUL";
6817 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006818 case X86ISD::INC: return "X86ISD::INC";
6819 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006820 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006821 }
6822}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006823
Chris Lattnerc9addb72007-03-30 23:15:24 +00006824// isLegalAddressingMode - Return true if the addressing mode represented
6825// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006826bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006827 const Type *Ty) const {
6828 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006829
Chris Lattnerc9addb72007-03-30 23:15:24 +00006830 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6831 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6832 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006833
Chris Lattnerc9addb72007-03-30 23:15:24 +00006834 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006835 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006836 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6837 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006838 // If BaseGV requires a register, we cannot also have a BaseReg.
6839 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6840 AM.HasBaseReg)
6841 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006842
6843 // X86-64 only supports addr of globals in small code model.
6844 if (Subtarget->is64Bit()) {
6845 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6846 return false;
6847 // If lower 4G is not available, then we must use rip-relative addressing.
6848 if (AM.BaseOffs || AM.Scale > 1)
6849 return false;
6850 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006851 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006852
Chris Lattnerc9addb72007-03-30 23:15:24 +00006853 switch (AM.Scale) {
6854 case 0:
6855 case 1:
6856 case 2:
6857 case 4:
6858 case 8:
6859 // These scales always work.
6860 break;
6861 case 3:
6862 case 5:
6863 case 9:
6864 // These scales are formed with basereg+scalereg. Only accept if there is
6865 // no basereg yet.
6866 if (AM.HasBaseReg)
6867 return false;
6868 break;
6869 default: // Other stuff never works.
6870 return false;
6871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006872
Chris Lattnerc9addb72007-03-30 23:15:24 +00006873 return true;
6874}
6875
6876
Evan Cheng2bd122c2007-10-26 01:56:11 +00006877bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6878 if (!Ty1->isInteger() || !Ty2->isInteger())
6879 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006880 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6881 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006882 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006883 return false;
6884 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006885}
6886
Duncan Sands83ec4b62008-06-06 12:08:01 +00006887bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6888 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006889 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006890 unsigned NumBits1 = VT1.getSizeInBits();
6891 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006892 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006893 return false;
6894 return Subtarget->is64Bit() || NumBits1 < 64;
6895}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006896
Dan Gohman97121ba2009-04-08 00:15:30 +00006897bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006898 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006899 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6900}
6901
6902bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006903 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006904 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6905}
6906
Evan Cheng8b944d32009-05-28 00:35:15 +00006907bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
6908 // i16 instructions are longer (0x66 prefix) and potentially slower.
6909 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
6910}
6911
Evan Cheng60c07e12006-07-05 22:17:51 +00006912/// isShuffleMaskLegal - Targets can use this to indicate that they only
6913/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6914/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6915/// are assumed to be legal.
6916bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006917X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6918 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006919 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006920 if (VT.getSizeInBits() == 64)
6921 return false;
6922
6923 // FIXME: pshufb, blends, palignr, shifts.
6924 return (VT.getVectorNumElements() == 2 ||
6925 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6926 isMOVLMask(M, VT) ||
6927 isSHUFPMask(M, VT) ||
6928 isPSHUFDMask(M, VT) ||
6929 isPSHUFHWMask(M, VT) ||
6930 isPSHUFLWMask(M, VT) ||
6931 isUNPCKLMask(M, VT) ||
6932 isUNPCKHMask(M, VT) ||
6933 isUNPCKL_v_undef_Mask(M, VT) ||
6934 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006935}
6936
Dan Gohman7d8143f2008-04-09 20:09:42 +00006937bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006938X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006939 MVT VT) const {
6940 unsigned NumElts = VT.getVectorNumElements();
6941 // FIXME: This collection of masks seems suspect.
6942 if (NumElts == 2)
6943 return true;
6944 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6945 return (isMOVLMask(Mask, VT) ||
6946 isCommutedMOVLMask(Mask, VT, true) ||
6947 isSHUFPMask(Mask, VT) ||
6948 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006949 }
6950 return false;
6951}
6952
6953//===----------------------------------------------------------------------===//
6954// X86 Scheduler Hooks
6955//===----------------------------------------------------------------------===//
6956
Mon P Wang63307c32008-05-05 19:05:59 +00006957// private utility function
6958MachineBasicBlock *
6959X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6960 MachineBasicBlock *MBB,
6961 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006962 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006963 unsigned LoadOpc,
6964 unsigned CXchgOpc,
6965 unsigned copyOpc,
6966 unsigned notOpc,
6967 unsigned EAXreg,
6968 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006969 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006970 // For the atomic bitwise operator, we generate
6971 // thisMBB:
6972 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006973 // ld t1 = [bitinstr.addr]
6974 // op t2 = t1, [bitinstr.val]
6975 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006976 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6977 // bz newMBB
6978 // fallthrough -->nextMBB
6979 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6980 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006981 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006982 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006983
Mon P Wang63307c32008-05-05 19:05:59 +00006984 /// First build the CFG
6985 MachineFunction *F = MBB->getParent();
6986 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006987 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6988 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6989 F->insert(MBBIter, newMBB);
6990 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006991
Mon P Wang63307c32008-05-05 19:05:59 +00006992 // Move all successors to thisMBB to nextMBB
6993 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006994
Mon P Wang63307c32008-05-05 19:05:59 +00006995 // Update thisMBB to fall through to newMBB
6996 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006997
Mon P Wang63307c32008-05-05 19:05:59 +00006998 // newMBB jumps to itself and fall through to nextMBB
6999 newMBB->addSuccessor(nextMBB);
7000 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007001
Mon P Wang63307c32008-05-05 19:05:59 +00007002 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007003 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007004 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007005 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007006 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007007 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007008 int numArgs = bInstr->getNumOperands() - 1;
7009 for (int i=0; i < numArgs; ++i)
7010 argOpers[i] = &bInstr->getOperand(i+1);
7011
7012 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007013 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7014 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007015
Dale Johannesen140be2d2008-08-19 18:47:28 +00007016 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007017 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007018 for (int i=0; i <= lastAddrIndx; ++i)
7019 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007020
Dale Johannesen140be2d2008-08-19 18:47:28 +00007021 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007022 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007023 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007024 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007025 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007026 tt = t1;
7027
Dale Johannesen140be2d2008-08-19 18:47:28 +00007028 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007029 assert((argOpers[valArgIndx]->isReg() ||
7030 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007031 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007032 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007033 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007034 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007035 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007036 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007037 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007038
Dale Johannesene4d209d2009-02-03 20:21:25 +00007039 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007040 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007041
Dale Johannesene4d209d2009-02-03 20:21:25 +00007042 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007043 for (int i=0; i <= lastAddrIndx; ++i)
7044 (*MIB).addOperand(*argOpers[i]);
7045 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007046 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7047 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7048
Dale Johannesene4d209d2009-02-03 20:21:25 +00007049 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007050 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007051
Mon P Wang63307c32008-05-05 19:05:59 +00007052 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007053 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007054
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007055 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007056 return nextMBB;
7057}
7058
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007059// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007060MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007061X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7062 MachineBasicBlock *MBB,
7063 unsigned regOpcL,
7064 unsigned regOpcH,
7065 unsigned immOpcL,
7066 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007067 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007068 // For the atomic bitwise operator, we generate
7069 // thisMBB (instructions are in pairs, except cmpxchg8b)
7070 // ld t1,t2 = [bitinstr.addr]
7071 // newMBB:
7072 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7073 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007074 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007075 // mov ECX, EBX <- t5, t6
7076 // mov EAX, EDX <- t1, t2
7077 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7078 // mov t3, t4 <- EAX, EDX
7079 // bz newMBB
7080 // result in out1, out2
7081 // fallthrough -->nextMBB
7082
7083 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7084 const unsigned LoadOpc = X86::MOV32rm;
7085 const unsigned copyOpc = X86::MOV32rr;
7086 const unsigned NotOpc = X86::NOT32r;
7087 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7088 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7089 MachineFunction::iterator MBBIter = MBB;
7090 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007091
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007092 /// First build the CFG
7093 MachineFunction *F = MBB->getParent();
7094 MachineBasicBlock *thisMBB = MBB;
7095 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7096 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7097 F->insert(MBBIter, newMBB);
7098 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007099
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007100 // Move all successors to thisMBB to nextMBB
7101 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007102
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007103 // Update thisMBB to fall through to newMBB
7104 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007105
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007106 // newMBB jumps to itself and fall through to nextMBB
7107 newMBB->addSuccessor(nextMBB);
7108 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007109
Dale Johannesene4d209d2009-02-03 20:21:25 +00007110 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007111 // Insert instructions into newMBB based on incoming instruction
7112 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007113 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007114 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007115 MachineOperand& dest1Oper = bInstr->getOperand(0);
7116 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007117 MachineOperand* argOpers[2 + X86AddrNumOperands];
7118 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007119 argOpers[i] = &bInstr->getOperand(i+2);
7120
7121 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007122 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007123
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007124 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007125 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007126 for (int i=0; i <= lastAddrIndx; ++i)
7127 (*MIB).addOperand(*argOpers[i]);
7128 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007129 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007130 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007131 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007132 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007133 MachineOperand newOp3 = *(argOpers[3]);
7134 if (newOp3.isImm())
7135 newOp3.setImm(newOp3.getImm()+4);
7136 else
7137 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007138 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007139 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007140
7141 // t3/4 are defined later, at the bottom of the loop
7142 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7143 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007144 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007145 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007146 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007147 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7148
7149 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7150 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007151 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007152 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7153 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007154 } else {
7155 tt1 = t1;
7156 tt2 = t2;
7157 }
7158
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007159 int valArgIndx = lastAddrIndx + 1;
7160 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007161 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007162 "invalid operand");
7163 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7164 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007165 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007166 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007167 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007168 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007169 if (regOpcL != X86::MOV32rr)
7170 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007171 (*MIB).addOperand(*argOpers[valArgIndx]);
7172 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007173 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007174 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007175 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007176 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007177 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007178 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007179 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007180 if (regOpcH != X86::MOV32rr)
7181 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007182 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007183
Dale Johannesene4d209d2009-02-03 20:21:25 +00007184 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007185 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007186 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007187 MIB.addReg(t2);
7188
Dale Johannesene4d209d2009-02-03 20:21:25 +00007189 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007190 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007191 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007192 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007193
Dale Johannesene4d209d2009-02-03 20:21:25 +00007194 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007195 for (int i=0; i <= lastAddrIndx; ++i)
7196 (*MIB).addOperand(*argOpers[i]);
7197
7198 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7199 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7200
Dale Johannesene4d209d2009-02-03 20:21:25 +00007201 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007202 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007203 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007204 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007205
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007206 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007207 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007208
7209 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7210 return nextMBB;
7211}
7212
7213// private utility function
7214MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007215X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7216 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007217 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007218 // For the atomic min/max operator, we generate
7219 // thisMBB:
7220 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007221 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007222 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007223 // cmp t1, t2
7224 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007225 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007226 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7227 // bz newMBB
7228 // fallthrough -->nextMBB
7229 //
7230 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7231 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007232 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007233 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007234
Mon P Wang63307c32008-05-05 19:05:59 +00007235 /// First build the CFG
7236 MachineFunction *F = MBB->getParent();
7237 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007238 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7239 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7240 F->insert(MBBIter, newMBB);
7241 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Mon P Wang63307c32008-05-05 19:05:59 +00007243 // Move all successors to thisMBB to nextMBB
7244 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007245
Mon P Wang63307c32008-05-05 19:05:59 +00007246 // Update thisMBB to fall through to newMBB
7247 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007248
Mon P Wang63307c32008-05-05 19:05:59 +00007249 // newMBB jumps to newMBB and fall through to nextMBB
7250 newMBB->addSuccessor(nextMBB);
7251 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007252
Dale Johannesene4d209d2009-02-03 20:21:25 +00007253 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007254 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007255 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007256 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007257 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007258 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007259 int numArgs = mInstr->getNumOperands() - 1;
7260 for (int i=0; i < numArgs; ++i)
7261 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007262
Mon P Wang63307c32008-05-05 19:05:59 +00007263 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007264 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7265 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007266
Mon P Wangab3e7472008-05-05 22:56:23 +00007267 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007268 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007269 for (int i=0; i <= lastAddrIndx; ++i)
7270 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007271
Mon P Wang63307c32008-05-05 19:05:59 +00007272 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007273 assert((argOpers[valArgIndx]->isReg() ||
7274 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007275 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007276
7277 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007278 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007279 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007280 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007281 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007282 (*MIB).addOperand(*argOpers[valArgIndx]);
7283
Dale Johannesene4d209d2009-02-03 20:21:25 +00007284 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007285 MIB.addReg(t1);
7286
Dale Johannesene4d209d2009-02-03 20:21:25 +00007287 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007288 MIB.addReg(t1);
7289 MIB.addReg(t2);
7290
7291 // Generate movc
7292 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007293 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007294 MIB.addReg(t2);
7295 MIB.addReg(t1);
7296
7297 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007298 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007299 for (int i=0; i <= lastAddrIndx; ++i)
7300 (*MIB).addOperand(*argOpers[i]);
7301 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007302 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7303 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007304
Dale Johannesene4d209d2009-02-03 20:21:25 +00007305 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007306 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007307
Mon P Wang63307c32008-05-05 19:05:59 +00007308 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007309 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007310
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007311 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007312 return nextMBB;
7313}
7314
7315
Evan Cheng60c07e12006-07-05 22:17:51 +00007316MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007317X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007318 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007319 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007320 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007321 switch (MI->getOpcode()) {
7322 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007323 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007324 case X86::CMOV_FR32:
7325 case X86::CMOV_FR64:
7326 case X86::CMOV_V4F32:
7327 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007328 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007329 // To "insert" a SELECT_CC instruction, we actually have to insert the
7330 // diamond control-flow pattern. The incoming instruction knows the
7331 // destination vreg to set, the condition code register to branch on, the
7332 // true/false values to select between, and a branch opcode to use.
7333 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007334 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007335 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007336
Evan Cheng60c07e12006-07-05 22:17:51 +00007337 // thisMBB:
7338 // ...
7339 // TrueVal = ...
7340 // cmpTY ccX, r1, r2
7341 // bCC copy1MBB
7342 // fallthrough --> copy0MBB
7343 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007344 MachineFunction *F = BB->getParent();
7345 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7346 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007347 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007348 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007349 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007350 F->insert(It, copy0MBB);
7351 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007352 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007353 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007354 sinkMBB->transferSuccessors(BB);
7355
7356 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007357 BB->addSuccessor(copy0MBB);
7358 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007359
Evan Cheng60c07e12006-07-05 22:17:51 +00007360 // copy0MBB:
7361 // %FalseValue = ...
7362 // # fallthrough to sinkMBB
7363 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007364
Evan Cheng60c07e12006-07-05 22:17:51 +00007365 // Update machine-CFG edges
7366 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007367
Evan Cheng60c07e12006-07-05 22:17:51 +00007368 // sinkMBB:
7369 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7370 // ...
7371 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007372 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007373 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7374 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7375
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007376 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007377 return BB;
7378 }
7379
Dale Johannesen849f2142007-07-03 00:53:03 +00007380 case X86::FP32_TO_INT16_IN_MEM:
7381 case X86::FP32_TO_INT32_IN_MEM:
7382 case X86::FP32_TO_INT64_IN_MEM:
7383 case X86::FP64_TO_INT16_IN_MEM:
7384 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007385 case X86::FP64_TO_INT64_IN_MEM:
7386 case X86::FP80_TO_INT16_IN_MEM:
7387 case X86::FP80_TO_INT32_IN_MEM:
7388 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007389 // Change the floating point control register to use "round towards zero"
7390 // mode when truncating to an integer value.
7391 MachineFunction *F = BB->getParent();
7392 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007393 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007394
7395 // Load the old value of the high byte of the control word...
7396 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007397 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007398 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007399 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007400
7401 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007402 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007403 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007404
7405 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007406 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007407
7408 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007409 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007410 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007411
7412 // Get the X86 opcode to use.
7413 unsigned Opc;
7414 switch (MI->getOpcode()) {
7415 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007416 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7417 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7418 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7419 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7420 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7421 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007422 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7423 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7424 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007425 }
7426
7427 X86AddressMode AM;
7428 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007429 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007430 AM.BaseType = X86AddressMode::RegBase;
7431 AM.Base.Reg = Op.getReg();
7432 } else {
7433 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007434 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007435 }
7436 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007437 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007438 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007439 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007440 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007441 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007442 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007443 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007444 AM.GV = Op.getGlobal();
7445 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007446 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007447 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007448 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007449 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007450
7451 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007452 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007453
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007454 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007455 return BB;
7456 }
Mon P Wang63307c32008-05-05 19:05:59 +00007457 case X86::ATOMAND32:
7458 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007459 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007460 X86::LCMPXCHG32, X86::MOV32rr,
7461 X86::NOT32r, X86::EAX,
7462 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007463 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007464 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7465 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007466 X86::LCMPXCHG32, X86::MOV32rr,
7467 X86::NOT32r, X86::EAX,
7468 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007469 case X86::ATOMXOR32:
7470 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007471 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007472 X86::LCMPXCHG32, X86::MOV32rr,
7473 X86::NOT32r, X86::EAX,
7474 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007475 case X86::ATOMNAND32:
7476 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007477 X86::AND32ri, X86::MOV32rm,
7478 X86::LCMPXCHG32, X86::MOV32rr,
7479 X86::NOT32r, X86::EAX,
7480 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007481 case X86::ATOMMIN32:
7482 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7483 case X86::ATOMMAX32:
7484 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7485 case X86::ATOMUMIN32:
7486 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7487 case X86::ATOMUMAX32:
7488 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007489
7490 case X86::ATOMAND16:
7491 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7492 X86::AND16ri, X86::MOV16rm,
7493 X86::LCMPXCHG16, X86::MOV16rr,
7494 X86::NOT16r, X86::AX,
7495 X86::GR16RegisterClass);
7496 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007497 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007498 X86::OR16ri, X86::MOV16rm,
7499 X86::LCMPXCHG16, X86::MOV16rr,
7500 X86::NOT16r, X86::AX,
7501 X86::GR16RegisterClass);
7502 case X86::ATOMXOR16:
7503 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7504 X86::XOR16ri, X86::MOV16rm,
7505 X86::LCMPXCHG16, X86::MOV16rr,
7506 X86::NOT16r, X86::AX,
7507 X86::GR16RegisterClass);
7508 case X86::ATOMNAND16:
7509 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7510 X86::AND16ri, X86::MOV16rm,
7511 X86::LCMPXCHG16, X86::MOV16rr,
7512 X86::NOT16r, X86::AX,
7513 X86::GR16RegisterClass, true);
7514 case X86::ATOMMIN16:
7515 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7516 case X86::ATOMMAX16:
7517 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7518 case X86::ATOMUMIN16:
7519 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7520 case X86::ATOMUMAX16:
7521 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7522
7523 case X86::ATOMAND8:
7524 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7525 X86::AND8ri, X86::MOV8rm,
7526 X86::LCMPXCHG8, X86::MOV8rr,
7527 X86::NOT8r, X86::AL,
7528 X86::GR8RegisterClass);
7529 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007530 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007531 X86::OR8ri, X86::MOV8rm,
7532 X86::LCMPXCHG8, X86::MOV8rr,
7533 X86::NOT8r, X86::AL,
7534 X86::GR8RegisterClass);
7535 case X86::ATOMXOR8:
7536 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7537 X86::XOR8ri, X86::MOV8rm,
7538 X86::LCMPXCHG8, X86::MOV8rr,
7539 X86::NOT8r, X86::AL,
7540 X86::GR8RegisterClass);
7541 case X86::ATOMNAND8:
7542 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7543 X86::AND8ri, X86::MOV8rm,
7544 X86::LCMPXCHG8, X86::MOV8rr,
7545 X86::NOT8r, X86::AL,
7546 X86::GR8RegisterClass, true);
7547 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007548 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007549 case X86::ATOMAND64:
7550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007551 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007552 X86::LCMPXCHG64, X86::MOV64rr,
7553 X86::NOT64r, X86::RAX,
7554 X86::GR64RegisterClass);
7555 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007556 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7557 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007558 X86::LCMPXCHG64, X86::MOV64rr,
7559 X86::NOT64r, X86::RAX,
7560 X86::GR64RegisterClass);
7561 case X86::ATOMXOR64:
7562 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007563 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007564 X86::LCMPXCHG64, X86::MOV64rr,
7565 X86::NOT64r, X86::RAX,
7566 X86::GR64RegisterClass);
7567 case X86::ATOMNAND64:
7568 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7569 X86::AND64ri32, X86::MOV64rm,
7570 X86::LCMPXCHG64, X86::MOV64rr,
7571 X86::NOT64r, X86::RAX,
7572 X86::GR64RegisterClass, true);
7573 case X86::ATOMMIN64:
7574 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7575 case X86::ATOMMAX64:
7576 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7577 case X86::ATOMUMIN64:
7578 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7579 case X86::ATOMUMAX64:
7580 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007581
7582 // This group does 64-bit operations on a 32-bit host.
7583 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007584 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007585 X86::AND32rr, X86::AND32rr,
7586 X86::AND32ri, X86::AND32ri,
7587 false);
7588 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007589 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007590 X86::OR32rr, X86::OR32rr,
7591 X86::OR32ri, X86::OR32ri,
7592 false);
7593 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007594 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007595 X86::XOR32rr, X86::XOR32rr,
7596 X86::XOR32ri, X86::XOR32ri,
7597 false);
7598 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007599 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007600 X86::AND32rr, X86::AND32rr,
7601 X86::AND32ri, X86::AND32ri,
7602 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007603 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007604 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007605 X86::ADD32rr, X86::ADC32rr,
7606 X86::ADD32ri, X86::ADC32ri,
7607 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007608 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007609 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007610 X86::SUB32rr, X86::SBB32rr,
7611 X86::SUB32ri, X86::SBB32ri,
7612 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007613 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007614 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007615 X86::MOV32rr, X86::MOV32rr,
7616 X86::MOV32ri, X86::MOV32ri,
7617 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007618 }
7619}
7620
7621//===----------------------------------------------------------------------===//
7622// X86 Optimization Hooks
7623//===----------------------------------------------------------------------===//
7624
Dan Gohman475871a2008-07-27 21:46:04 +00007625void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007626 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007627 APInt &KnownZero,
7628 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007629 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007630 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007631 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007632 assert((Opc >= ISD::BUILTIN_OP_END ||
7633 Opc == ISD::INTRINSIC_WO_CHAIN ||
7634 Opc == ISD::INTRINSIC_W_CHAIN ||
7635 Opc == ISD::INTRINSIC_VOID) &&
7636 "Should use MaskedValueIsZero if you don't know whether Op"
7637 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007638
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007639 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007640 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007641 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007642 case X86ISD::ADD:
7643 case X86ISD::SUB:
7644 case X86ISD::SMUL:
7645 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007646 case X86ISD::INC:
7647 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007648 // These nodes' second result is a boolean.
7649 if (Op.getResNo() == 0)
7650 break;
7651 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007652 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007653 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7654 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007655 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007656 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007657}
Chris Lattner259e97c2006-01-31 19:43:35 +00007658
Evan Cheng206ee9d2006-07-07 08:33:52 +00007659/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007660/// node is a GlobalAddress + offset.
7661bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7662 GlobalValue* &GA, int64_t &Offset) const{
7663 if (N->getOpcode() == X86ISD::Wrapper) {
7664 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007665 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007666 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007667 return true;
7668 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007669 }
Evan Chengad4196b2008-05-12 19:56:52 +00007670 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007671}
7672
Evan Chengad4196b2008-05-12 19:56:52 +00007673static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7674 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007675 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007676 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007677 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007678 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007679 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007680 return false;
7681}
7682
Nate Begeman9008ca62009-04-27 18:41:29 +00007683static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7684 MVT EVT, SDNode *&Base,
Evan Chengad4196b2008-05-12 19:56:52 +00007685 SelectionDAG &DAG, MachineFrameInfo *MFI,
7686 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007687 Base = NULL;
7688 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007689 if (N->getMaskElt(i) < 0) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007690 if (!Base)
7691 return false;
7692 continue;
7693 }
7694
Dan Gohman475871a2008-07-27 21:46:04 +00007695 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007696 if (!Elt.getNode() ||
7697 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007698 return false;
7699 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007700 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007701 if (Base->getOpcode() == ISD::UNDEF)
7702 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007703 continue;
7704 }
7705 if (Elt.getOpcode() == ISD::UNDEF)
7706 continue;
7707
Nate Begemanabc01992009-06-05 21:37:30 +00007708 LoadSDNode *LD = cast<LoadSDNode>(Elt);
7709 LoadSDNode *LDBase = cast<LoadSDNode>(Base);
7710 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007711 return false;
7712 }
7713 return true;
7714}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007715
7716/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7717/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7718/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007719/// order. In the case of v2i64, it will see if it can rewrite the
7720/// shuffle to be an appropriate build vector so it can take advantage of
7721// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007722static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007723 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007724 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007725 MVT VT = N->getValueType(0);
7726 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007727 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7728 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007729
7730 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7731 // where the upper half is 0, it is advantageous to rewrite it as a build
7732 // vector of (0, val) so it can use movq.
7733 if (VT == MVT::v2i64) {
7734 SDValue In[2];
7735 In[0] = N->getOperand(0);
7736 In[1] = N->getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00007737 int Idx0 = SVN->getMaskElt(0);
7738 int Idx1 = SVN->getMaskElt(1);
7739 // FIXME: can we take advantage of undef index?
7740 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang1e955802009-04-03 02:43:30 +00007741 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7742 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7743 ConstantSDNode* InsertVecIdx =
7744 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7745 if (InsertVecIdx &&
Nate Begeman9008ca62009-04-27 18:41:29 +00007746 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang1e955802009-04-03 02:43:30 +00007747 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7748 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7749 In[Idx0/2].getOperand(1),
7750 In[Idx1/2].getOperand(Idx1 % 2));
7751 }
7752 }
7753 }
7754
7755 // Try to combine a vector_shuffle into a 128-bit load.
7756 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007757 SDNode *Base = NULL;
Nate Begeman9008ca62009-04-27 18:41:29 +00007758 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007759 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007760
Dan Gohmand3006222007-07-27 17:16:43 +00007761 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007762 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007763 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007764 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007765 LD->isVolatile());
7766 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7767 LD->getSrcValue(), LD->getSrcValueOffset(),
7768 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007769}
7770
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007771/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007772static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007773 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007774 const X86Subtarget *Subtarget,
7775 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007776 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007777 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007778
Evan Chengd880b972008-05-09 21:53:03 +00007779 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007780 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007781 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007782
Duncan Sands83ec4b62008-06-06 12:08:01 +00007783 MVT VT = N->getValueType(0);
7784 MVT EVT = VT.getVectorElementType();
Nate Begemanabc01992009-06-05 21:37:30 +00007785
7786 // Before or during type legalization, we want to try and convert a
7787 // build_vector of an i64 load and a zero value into vzext_movl before the
7788 // legalizer can break it up.
7789 // FIXME: does the case below remove the need to do this?
7790 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) {
7791 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00007792 return SDValue();
Nate Begemanabc01992009-06-05 21:37:30 +00007793
7794 // This must be an insertion into a zero vector.
7795 SDValue HighElt = N->getOperand(1);
7796 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007797 return SDValue();
Nate Begemanabc01992009-06-05 21:37:30 +00007798
7799 // Value must be a load.
7800 SDNode *Base = N->getOperand(0).getNode();
7801 if (!isa<LoadSDNode>(Base)) {
7802 if (Base->getOpcode() != ISD::BIT_CONVERT)
7803 return SDValue();
7804 Base = Base->getOperand(0).getNode();
7805 if (!isa<LoadSDNode>(Base))
7806 return SDValue();
7807 }
7808
7809 // Transform it into VZEXT_LOAD addr.
7810 LoadSDNode *LD = cast<LoadSDNode>(Base);
7811
7812 // Load must not be an extload.
7813 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
7814 return SDValue();
7815
7816 // Load type should legal type so we don't have to legalize it.
7817 if (!TLI.isTypeLegal(VT))
7818 return SDValue();
7819
7820 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7821 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7822 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
7823 TargetLowering::TargetLoweringOpt TLO(DAG);
7824 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7825 DCI.CommitTargetLoweringOpt(TLO);
7826 return ResNode;
Evan Chengd880b972008-05-09 21:53:03 +00007827 }
Evan Chengd880b972008-05-09 21:53:03 +00007828
Nate Begemanabc01992009-06-05 21:37:30 +00007829 // The type legalizer will have broken apart v2i64 build_vector created during
7830 // widening before the code which handles that case is run. Look for build
7831 // vector (load, load + 4, 0/undef, 0/undef)
7832 if (VT == MVT::v4i32 || VT == MVT::v4f32) {
7833 LoadSDNode *LD0 = dyn_cast<LoadSDNode>(N->getOperand(0));
7834 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(N->getOperand(1));
7835 if (!LD0 || !LD1)
7836 return SDValue();
7837 if (LD0->getExtensionType() != ISD::NON_EXTLOAD ||
7838 LD1->getExtensionType() != ISD::NON_EXTLOAD)
7839 return SDValue();
7840 // Make sure the second elt is a consecutive load.
7841 if (!TLI.isConsecutiveLoad(LD1, LD0, EVT.getSizeInBits()/8, 1,
7842 DAG.getMachineFunction().getFrameInfo()))
7843 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007844
Nate Begemanabc01992009-06-05 21:37:30 +00007845 SDValue N2 = N->getOperand(2);
7846 SDValue N3 = N->getOperand(3);
7847 if (!isZeroNode(N2) && N2.getOpcode() != ISD::UNDEF)
7848 return SDValue();
7849 if (!isZeroNode(N3) && N3.getOpcode() != ISD::UNDEF)
7850 return SDValue();
7851
7852 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
7853 SDValue Ops[] = { LD0->getChain(), LD0->getBasePtr() };
7854 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
7855 TargetLowering::TargetLoweringOpt TLO(DAG);
7856 TLO.CombineTo(SDValue(LD0, 1), ResNode.getValue(1));
7857 DCI.CommitTargetLoweringOpt(TLO);
7858 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7859 }
7860 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007861}
Evan Chengd880b972008-05-09 21:53:03 +00007862
Chris Lattner83e6c992006-10-04 06:57:07 +00007863/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007864static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007865 const X86Subtarget *Subtarget) {
7866 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007867 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007868 // Get the LHS/RHS of the select.
7869 SDValue LHS = N->getOperand(1);
7870 SDValue RHS = N->getOperand(2);
7871
Chris Lattner83e6c992006-10-04 06:57:07 +00007872 // If we have SSE[12] support, try to form min/max nodes.
7873 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007874 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7875 Cond.getOpcode() == ISD::SETCC) {
7876 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007877
Chris Lattner47b4ce82009-03-11 05:48:52 +00007878 unsigned Opcode = 0;
7879 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7880 switch (CC) {
7881 default: break;
7882 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7883 case ISD::SETULE:
7884 case ISD::SETLE:
7885 if (!UnsafeFPMath) break;
7886 // FALL THROUGH.
7887 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7888 case ISD::SETLT:
7889 Opcode = X86ISD::FMIN;
7890 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007891
Chris Lattner47b4ce82009-03-11 05:48:52 +00007892 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7893 case ISD::SETUGT:
7894 case ISD::SETGT:
7895 if (!UnsafeFPMath) break;
7896 // FALL THROUGH.
7897 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7898 case ISD::SETGE:
7899 Opcode = X86ISD::FMAX;
7900 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007901 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007902 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7903 switch (CC) {
7904 default: break;
7905 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7906 case ISD::SETUGT:
7907 case ISD::SETGT:
7908 if (!UnsafeFPMath) break;
7909 // FALL THROUGH.
7910 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7911 case ISD::SETGE:
7912 Opcode = X86ISD::FMIN;
7913 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007914
Chris Lattner47b4ce82009-03-11 05:48:52 +00007915 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7916 case ISD::SETULE:
7917 case ISD::SETLE:
7918 if (!UnsafeFPMath) break;
7919 // FALL THROUGH.
7920 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7921 case ISD::SETLT:
7922 Opcode = X86ISD::FMAX;
7923 break;
7924 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007925 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007926
Chris Lattner47b4ce82009-03-11 05:48:52 +00007927 if (Opcode)
7928 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007929 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007930
Chris Lattnerd1980a52009-03-12 06:52:53 +00007931 // If this is a select between two integer constants, try to do some
7932 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007933 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7934 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007935 // Don't do this for crazy integer types.
7936 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7937 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007938 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007939 bool NeedsCondInvert = false;
7940
Chris Lattnercee56e72009-03-13 05:53:31 +00007941 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007942 // Efficiently invertible.
7943 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7944 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7945 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7946 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007947 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007948 }
7949
7950 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007951 if (FalseC->getAPIntValue() == 0 &&
7952 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007953 if (NeedsCondInvert) // Invert the condition if needed.
7954 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7955 DAG.getConstant(1, Cond.getValueType()));
7956
7957 // Zero extend the condition if needed.
7958 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7959
Chris Lattnercee56e72009-03-13 05:53:31 +00007960 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007961 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7962 DAG.getConstant(ShAmt, MVT::i8));
7963 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007964
7965 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007966 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007967 if (NeedsCondInvert) // Invert the condition if needed.
7968 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7969 DAG.getConstant(1, Cond.getValueType()));
7970
7971 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007972 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7973 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007974 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007975 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007976 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007977
7978 // Optimize cases that will turn into an LEA instruction. This requires
7979 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7980 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7981 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7982 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7983
7984 bool isFastMultiplier = false;
7985 if (Diff < 10) {
7986 switch ((unsigned char)Diff) {
7987 default: break;
7988 case 1: // result = add base, cond
7989 case 2: // result = lea base( , cond*2)
7990 case 3: // result = lea base(cond, cond*2)
7991 case 4: // result = lea base( , cond*4)
7992 case 5: // result = lea base(cond, cond*4)
7993 case 8: // result = lea base( , cond*8)
7994 case 9: // result = lea base(cond, cond*8)
7995 isFastMultiplier = true;
7996 break;
7997 }
7998 }
7999
8000 if (isFastMultiplier) {
8001 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8002 if (NeedsCondInvert) // Invert the condition if needed.
8003 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8004 DAG.getConstant(1, Cond.getValueType()));
8005
8006 // Zero extend the condition if needed.
8007 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8008 Cond);
8009 // Scale the condition by the difference.
8010 if (Diff != 1)
8011 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8012 DAG.getConstant(Diff, Cond.getValueType()));
8013
8014 // Add the base if non-zero.
8015 if (FalseC->getAPIntValue() != 0)
8016 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8017 SDValue(FalseC, 0));
8018 return Cond;
8019 }
8020 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008021 }
8022 }
8023
Dan Gohman475871a2008-07-27 21:46:04 +00008024 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008025}
8026
Chris Lattnerd1980a52009-03-12 06:52:53 +00008027/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8028static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8029 TargetLowering::DAGCombinerInfo &DCI) {
8030 DebugLoc DL = N->getDebugLoc();
8031
8032 // If the flag operand isn't dead, don't touch this CMOV.
8033 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8034 return SDValue();
8035
8036 // If this is a select between two integer constants, try to do some
8037 // optimizations. Note that the operands are ordered the opposite of SELECT
8038 // operands.
8039 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8040 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8041 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8042 // larger than FalseC (the false value).
8043 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8044
8045 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8046 CC = X86::GetOppositeBranchCondition(CC);
8047 std::swap(TrueC, FalseC);
8048 }
8049
8050 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008051 // This is efficient for any integer data type (including i8/i16) and
8052 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008053 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8054 SDValue Cond = N->getOperand(3);
8055 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8056 DAG.getConstant(CC, MVT::i8), Cond);
8057
8058 // Zero extend the condition if needed.
8059 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8060
8061 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8062 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8063 DAG.getConstant(ShAmt, MVT::i8));
8064 if (N->getNumValues() == 2) // Dead flag value?
8065 return DCI.CombineTo(N, Cond, SDValue());
8066 return Cond;
8067 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008068
8069 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8070 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008071 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8072 SDValue Cond = N->getOperand(3);
8073 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8074 DAG.getConstant(CC, MVT::i8), Cond);
8075
8076 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008077 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8078 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008079 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8080 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008081
Chris Lattner97a29a52009-03-13 05:22:11 +00008082 if (N->getNumValues() == 2) // Dead flag value?
8083 return DCI.CombineTo(N, Cond, SDValue());
8084 return Cond;
8085 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008086
8087 // Optimize cases that will turn into an LEA instruction. This requires
8088 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8089 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8090 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8091 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8092
8093 bool isFastMultiplier = false;
8094 if (Diff < 10) {
8095 switch ((unsigned char)Diff) {
8096 default: break;
8097 case 1: // result = add base, cond
8098 case 2: // result = lea base( , cond*2)
8099 case 3: // result = lea base(cond, cond*2)
8100 case 4: // result = lea base( , cond*4)
8101 case 5: // result = lea base(cond, cond*4)
8102 case 8: // result = lea base( , cond*8)
8103 case 9: // result = lea base(cond, cond*8)
8104 isFastMultiplier = true;
8105 break;
8106 }
8107 }
8108
8109 if (isFastMultiplier) {
8110 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8111 SDValue Cond = N->getOperand(3);
8112 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8113 DAG.getConstant(CC, MVT::i8), Cond);
8114 // Zero extend the condition if needed.
8115 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8116 Cond);
8117 // Scale the condition by the difference.
8118 if (Diff != 1)
8119 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8120 DAG.getConstant(Diff, Cond.getValueType()));
8121
8122 // Add the base if non-zero.
8123 if (FalseC->getAPIntValue() != 0)
8124 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8125 SDValue(FalseC, 0));
8126 if (N->getNumValues() == 2) // Dead flag value?
8127 return DCI.CombineTo(N, Cond, SDValue());
8128 return Cond;
8129 }
8130 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008131 }
8132 }
8133 return SDValue();
8134}
8135
8136
Evan Cheng0b0cd912009-03-28 05:57:29 +00008137/// PerformMulCombine - Optimize a single multiply with constant into two
8138/// in order to implement it with two cheaper instructions, e.g.
8139/// LEA + SHL, LEA + LEA.
8140static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8141 TargetLowering::DAGCombinerInfo &DCI) {
8142 if (DAG.getMachineFunction().
8143 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8144 return SDValue();
8145
8146 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8147 return SDValue();
8148
8149 MVT VT = N->getValueType(0);
8150 if (VT != MVT::i64)
8151 return SDValue();
8152
8153 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8154 if (!C)
8155 return SDValue();
8156 uint64_t MulAmt = C->getZExtValue();
8157 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8158 return SDValue();
8159
8160 uint64_t MulAmt1 = 0;
8161 uint64_t MulAmt2 = 0;
8162 if ((MulAmt % 9) == 0) {
8163 MulAmt1 = 9;
8164 MulAmt2 = MulAmt / 9;
8165 } else if ((MulAmt % 5) == 0) {
8166 MulAmt1 = 5;
8167 MulAmt2 = MulAmt / 5;
8168 } else if ((MulAmt % 3) == 0) {
8169 MulAmt1 = 3;
8170 MulAmt2 = MulAmt / 3;
8171 }
8172 if (MulAmt2 &&
8173 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8174 DebugLoc DL = N->getDebugLoc();
8175
8176 if (isPowerOf2_64(MulAmt2) &&
8177 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8178 // If second multiplifer is pow2, issue it first. We want the multiply by
8179 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8180 // is an add.
8181 std::swap(MulAmt1, MulAmt2);
8182
8183 SDValue NewMul;
8184 if (isPowerOf2_64(MulAmt1))
8185 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8186 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8187 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008188 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008189 DAG.getConstant(MulAmt1, VT));
8190
8191 if (isPowerOf2_64(MulAmt2))
8192 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8193 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8194 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008195 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008196 DAG.getConstant(MulAmt2, VT));
8197
8198 // Do not add new nodes to DAG combiner worklist.
8199 DCI.CombineTo(N, NewMul, false);
8200 }
8201 return SDValue();
8202}
8203
8204
Nate Begeman740ab032009-01-26 00:52:55 +00008205/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8206/// when possible.
8207static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8208 const X86Subtarget *Subtarget) {
8209 // On X86 with SSE2 support, we can transform this to a vector shift if
8210 // all elements are shifted by the same amount. We can't do this in legalize
8211 // because the a constant vector is typically transformed to a constant pool
8212 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008213 if (!Subtarget->hasSSE2())
8214 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008215
Nate Begeman740ab032009-01-26 00:52:55 +00008216 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008217 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8218 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008219
Mon P Wang3becd092009-01-28 08:12:05 +00008220 SDValue ShAmtOp = N->getOperand(1);
8221 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008222 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008223 SDValue BaseShAmt;
8224 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8225 unsigned NumElts = VT.getVectorNumElements();
8226 unsigned i = 0;
8227 for (; i != NumElts; ++i) {
8228 SDValue Arg = ShAmtOp.getOperand(i);
8229 if (Arg.getOpcode() == ISD::UNDEF) continue;
8230 BaseShAmt = Arg;
8231 break;
8232 }
8233 for (; i != NumElts; ++i) {
8234 SDValue Arg = ShAmtOp.getOperand(i);
8235 if (Arg.getOpcode() == ISD::UNDEF) continue;
8236 if (Arg != BaseShAmt) {
8237 return SDValue();
8238 }
8239 }
8240 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008241 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8242 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8243 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008244 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008245 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008246
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008247 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008248 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008249 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008250 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008251
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008252 // The shift amount is identical so we can do a vector shift.
8253 SDValue ValOp = N->getOperand(0);
8254 switch (N->getOpcode()) {
8255 default:
8256 assert(0 && "Unknown shift opcode!");
8257 break;
8258 case ISD::SHL:
8259 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008260 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008261 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8262 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008263 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008264 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008265 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8266 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008267 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008268 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008269 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8270 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008271 break;
8272 case ISD::SRA:
8273 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008274 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008275 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8276 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008277 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008278 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008279 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8280 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008281 break;
8282 case ISD::SRL:
8283 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008284 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008285 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8286 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008287 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008288 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008289 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8290 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008291 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008292 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008293 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8294 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008295 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008296 }
8297 return SDValue();
8298}
8299
Chris Lattner149a4e52008-02-22 02:09:43 +00008300/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008301static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008302 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008303 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8304 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008305 // A preferable solution to the general problem is to figure out the right
8306 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008307
8308 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008309 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008310 MVT VT = St->getValue().getValueType();
8311 if (VT.getSizeInBits() != 64)
8312 return SDValue();
8313
Devang Patel578efa92009-06-05 21:57:13 +00008314 const Function *F = DAG.getMachineFunction().getFunction();
8315 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8316 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8317 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008318 if ((VT.isVector() ||
8319 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008320 isa<LoadSDNode>(St->getValue()) &&
8321 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8322 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008323 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008324 LoadSDNode *Ld = 0;
8325 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008326 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008327 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008328 // Must be a store of a load. We currently handle two cases: the load
8329 // is a direct child, and it's under an intervening TokenFactor. It is
8330 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008331 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008332 Ld = cast<LoadSDNode>(St->getChain());
8333 else if (St->getValue().hasOneUse() &&
8334 ChainVal->getOpcode() == ISD::TokenFactor) {
8335 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008336 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008337 TokenFactorIndex = i;
8338 Ld = cast<LoadSDNode>(St->getValue());
8339 } else
8340 Ops.push_back(ChainVal->getOperand(i));
8341 }
8342 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008343
Evan Cheng536e6672009-03-12 05:59:15 +00008344 if (!Ld || !ISD::isNormalLoad(Ld))
8345 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008346
Evan Cheng536e6672009-03-12 05:59:15 +00008347 // If this is not the MMX case, i.e. we are just turning i64 load/store
8348 // into f64 load/store, avoid the transformation if there are multiple
8349 // uses of the loaded value.
8350 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8351 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008352
Evan Cheng536e6672009-03-12 05:59:15 +00008353 DebugLoc LdDL = Ld->getDebugLoc();
8354 DebugLoc StDL = N->getDebugLoc();
8355 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8356 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8357 // pair instead.
8358 if (Subtarget->is64Bit() || F64IsLegal) {
8359 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8360 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8361 Ld->getBasePtr(), Ld->getSrcValue(),
8362 Ld->getSrcValueOffset(), Ld->isVolatile(),
8363 Ld->getAlignment());
8364 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008365 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008366 Ops.push_back(NewChain);
8367 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008368 Ops.size());
8369 }
Evan Cheng536e6672009-03-12 05:59:15 +00008370 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008371 St->getSrcValue(), St->getSrcValueOffset(),
8372 St->isVolatile(), St->getAlignment());
8373 }
Evan Cheng536e6672009-03-12 05:59:15 +00008374
8375 // Otherwise, lower to two pairs of 32-bit loads / stores.
8376 SDValue LoAddr = Ld->getBasePtr();
8377 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8378 DAG.getConstant(4, MVT::i32));
8379
8380 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8381 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8382 Ld->isVolatile(), Ld->getAlignment());
8383 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8384 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8385 Ld->isVolatile(),
8386 MinAlign(Ld->getAlignment(), 4));
8387
8388 SDValue NewChain = LoLd.getValue(1);
8389 if (TokenFactorIndex != -1) {
8390 Ops.push_back(LoLd);
8391 Ops.push_back(HiLd);
8392 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8393 Ops.size());
8394 }
8395
8396 LoAddr = St->getBasePtr();
8397 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8398 DAG.getConstant(4, MVT::i32));
8399
8400 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8401 St->getSrcValue(), St->getSrcValueOffset(),
8402 St->isVolatile(), St->getAlignment());
8403 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8404 St->getSrcValue(),
8405 St->getSrcValueOffset() + 4,
8406 St->isVolatile(),
8407 MinAlign(St->getAlignment(), 4));
8408 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008409 }
Dan Gohman475871a2008-07-27 21:46:04 +00008410 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008411}
8412
Chris Lattner6cf73262008-01-25 06:14:17 +00008413/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8414/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008415static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008416 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8417 // F[X]OR(0.0, x) -> x
8418 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008419 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8420 if (C->getValueAPF().isPosZero())
8421 return N->getOperand(1);
8422 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8423 if (C->getValueAPF().isPosZero())
8424 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008425 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008426}
8427
8428/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008429static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008430 // FAND(0.0, x) -> 0.0
8431 // FAND(x, 0.0) -> 0.0
8432 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8433 if (C->getValueAPF().isPosZero())
8434 return N->getOperand(0);
8435 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8436 if (C->getValueAPF().isPosZero())
8437 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008438 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008439}
8440
Dan Gohmane5af2d32009-01-29 01:59:02 +00008441static SDValue PerformBTCombine(SDNode *N,
8442 SelectionDAG &DAG,
8443 TargetLowering::DAGCombinerInfo &DCI) {
8444 // BT ignores high bits in the bit index operand.
8445 SDValue Op1 = N->getOperand(1);
8446 if (Op1.hasOneUse()) {
8447 unsigned BitWidth = Op1.getValueSizeInBits();
8448 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8449 APInt KnownZero, KnownOne;
8450 TargetLowering::TargetLoweringOpt TLO(DAG);
8451 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8452 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8453 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8454 DCI.CommitTargetLoweringOpt(TLO);
8455 }
8456 return SDValue();
8457}
Chris Lattner83e6c992006-10-04 06:57:07 +00008458
Dan Gohman475871a2008-07-27 21:46:04 +00008459SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008460 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008461 SelectionDAG &DAG = DCI.DAG;
8462 switch (N->getOpcode()) {
8463 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008464 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8465 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008466 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008467 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008468 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008469 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008470 case ISD::SHL:
8471 case ISD::SRA:
8472 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008473 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008474 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008475 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8476 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008477 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008478 }
8479
Dan Gohman475871a2008-07-27 21:46:04 +00008480 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008481}
8482
Evan Cheng60c07e12006-07-05 22:17:51 +00008483//===----------------------------------------------------------------------===//
8484// X86 Inline Assembly Support
8485//===----------------------------------------------------------------------===//
8486
Chris Lattnerf4dff842006-07-11 02:54:03 +00008487/// getConstraintType - Given a constraint letter, return the type of
8488/// constraint it is for this target.
8489X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008490X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8491 if (Constraint.size() == 1) {
8492 switch (Constraint[0]) {
8493 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008494 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008495 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008496 case 'r':
8497 case 'R':
8498 case 'l':
8499 case 'q':
8500 case 'Q':
8501 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008502 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008503 case 'Y':
8504 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008505 case 'e':
8506 case 'Z':
8507 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008508 default:
8509 break;
8510 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008511 }
Chris Lattner4234f572007-03-25 02:14:49 +00008512 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008513}
8514
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008515/// LowerXConstraint - try to replace an X constraint, which matches anything,
8516/// with another that has more specific requirements based on the type of the
8517/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008518const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008519LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008520 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8521 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008522 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008523 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008524 return "Y";
8525 if (Subtarget->hasSSE1())
8526 return "x";
8527 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008528
Chris Lattner5e764232008-04-26 23:02:14 +00008529 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008530}
8531
Chris Lattner48884cd2007-08-25 00:47:38 +00008532/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8533/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008534void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008535 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008536 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008537 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008538 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008539 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008540
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008541 switch (Constraint) {
8542 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008543 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008544 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008545 if (C->getZExtValue() <= 31) {
8546 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008547 break;
8548 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008549 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008550 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008551 case 'J':
8552 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8553 if (C->getZExtValue() <= 63) {
8554 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8555 break;
8556 }
8557 }
8558 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008559 case 'N':
8560 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008561 if (C->getZExtValue() <= 255) {
8562 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008563 break;
8564 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008565 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008566 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008567 case 'e': {
8568 // 32-bit signed value
8569 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8570 const ConstantInt *CI = C->getConstantIntValue();
8571 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8572 // Widen to 64 bits here to get it sign extended.
8573 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8574 break;
8575 }
8576 // FIXME gcc accepts some relocatable values here too, but only in certain
8577 // memory models; it's complicated.
8578 }
8579 return;
8580 }
8581 case 'Z': {
8582 // 32-bit unsigned value
8583 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8584 const ConstantInt *CI = C->getConstantIntValue();
8585 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8586 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8587 break;
8588 }
8589 }
8590 // FIXME gcc accepts some relocatable values here too, but only in certain
8591 // memory models; it's complicated.
8592 return;
8593 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008594 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008595 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008596 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008597 // Widen to 64 bits here to get it sign extended.
8598 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008599 break;
8600 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008601
Chris Lattnerdc43a882007-05-03 16:52:29 +00008602 // If we are in non-pic codegen mode, we allow the address of a global (with
8603 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008604 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008605 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008606
Chris Lattner49921962009-05-08 18:23:14 +00008607 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8608 while (1) {
8609 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8610 Offset += GA->getOffset();
8611 break;
8612 } else if (Op.getOpcode() == ISD::ADD) {
8613 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8614 Offset += C->getZExtValue();
8615 Op = Op.getOperand(0);
8616 continue;
8617 }
8618 } else if (Op.getOpcode() == ISD::SUB) {
8619 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8620 Offset += -C->getZExtValue();
8621 Op = Op.getOperand(0);
8622 continue;
8623 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008624 }
Chris Lattner49921962009-05-08 18:23:14 +00008625
8626 // Otherwise, this isn't something we can handle, reject it.
8627 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008629
Chris Lattner49921962009-05-08 18:23:14 +00008630 if (hasMemory)
8631 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8632 else
8633 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8634 Offset);
8635 Result = Op;
8636 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008637 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008638 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008639
Gabor Greifba36cb52008-08-28 21:40:38 +00008640 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008641 Ops.push_back(Result);
8642 return;
8643 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008644 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8645 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008646}
8647
Chris Lattner259e97c2006-01-31 19:43:35 +00008648std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008649getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008650 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008651 if (Constraint.size() == 1) {
8652 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008653 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008654 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008655 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8656 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008657 if (VT == MVT::i32)
8658 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8659 else if (VT == MVT::i16)
8660 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8661 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008662 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008663 else if (VT == MVT::i64)
8664 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8665 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008666 }
8667 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008668
Chris Lattner1efa40f2006-02-22 00:56:39 +00008669 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008670}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008671
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008672std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008673X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008674 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008675 // First, see if this is a constraint that directly corresponds to an LLVM
8676 // register class.
8677 if (Constraint.size() == 1) {
8678 // GCC Constraint Letters
8679 switch (Constraint[0]) {
8680 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008681 case 'r': // GENERAL_REGS
8682 case 'R': // LEGACY_REGS
8683 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008684 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008685 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008686 if (VT == MVT::i16)
8687 return std::make_pair(0U, X86::GR16RegisterClass);
8688 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008689 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008690 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008691 case 'f': // FP Stack registers.
8692 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8693 // value to the correct fpstack register class.
8694 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8695 return std::make_pair(0U, X86::RFP32RegisterClass);
8696 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8697 return std::make_pair(0U, X86::RFP64RegisterClass);
8698 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008699 case 'y': // MMX_REGS if MMX allowed.
8700 if (!Subtarget->hasMMX()) break;
8701 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008702 case 'Y': // SSE_REGS if SSE2 allowed
8703 if (!Subtarget->hasSSE2()) break;
8704 // FALL THROUGH.
8705 case 'x': // SSE_REGS if SSE1 allowed
8706 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008707
8708 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008709 default: break;
8710 // Scalar SSE types.
8711 case MVT::f32:
8712 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008713 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008714 case MVT::f64:
8715 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008716 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008717 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008718 case MVT::v16i8:
8719 case MVT::v8i16:
8720 case MVT::v4i32:
8721 case MVT::v2i64:
8722 case MVT::v4f32:
8723 case MVT::v2f64:
8724 return std::make_pair(0U, X86::VR128RegisterClass);
8725 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008726 break;
8727 }
8728 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008729
Chris Lattnerf76d1802006-07-31 23:26:50 +00008730 // Use the default implementation in TargetLowering to convert the register
8731 // constraint into a member of a register class.
8732 std::pair<unsigned, const TargetRegisterClass*> Res;
8733 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008734
8735 // Not found as a standard register?
8736 if (Res.second == 0) {
8737 // GCC calls "st(0)" just plain "st".
8738 if (StringsEqualNoCase("{st}", Constraint)) {
8739 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008740 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008741 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008742 // 'A' means EAX + EDX.
8743 if (Constraint == "A") {
8744 Res.first = X86::EAX;
8745 Res.second = X86::GRADRegisterClass;
8746 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008747 return Res;
8748 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008749
Chris Lattnerf76d1802006-07-31 23:26:50 +00008750 // Otherwise, check to see if this is a register class of the wrong value
8751 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8752 // turn into {ax},{dx}.
8753 if (Res.second->hasType(VT))
8754 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008755
Chris Lattnerf76d1802006-07-31 23:26:50 +00008756 // All of the single-register GCC register classes map their values onto
8757 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8758 // really want an 8-bit or 32-bit register, map to the appropriate register
8759 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008760 if (Res.second == X86::GR16RegisterClass) {
8761 if (VT == MVT::i8) {
8762 unsigned DestReg = 0;
8763 switch (Res.first) {
8764 default: break;
8765 case X86::AX: DestReg = X86::AL; break;
8766 case X86::DX: DestReg = X86::DL; break;
8767 case X86::CX: DestReg = X86::CL; break;
8768 case X86::BX: DestReg = X86::BL; break;
8769 }
8770 if (DestReg) {
8771 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008772 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008773 }
8774 } else if (VT == MVT::i32) {
8775 unsigned DestReg = 0;
8776 switch (Res.first) {
8777 default: break;
8778 case X86::AX: DestReg = X86::EAX; break;
8779 case X86::DX: DestReg = X86::EDX; break;
8780 case X86::CX: DestReg = X86::ECX; break;
8781 case X86::BX: DestReg = X86::EBX; break;
8782 case X86::SI: DestReg = X86::ESI; break;
8783 case X86::DI: DestReg = X86::EDI; break;
8784 case X86::BP: DestReg = X86::EBP; break;
8785 case X86::SP: DestReg = X86::ESP; break;
8786 }
8787 if (DestReg) {
8788 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008789 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008790 }
8791 } else if (VT == MVT::i64) {
8792 unsigned DestReg = 0;
8793 switch (Res.first) {
8794 default: break;
8795 case X86::AX: DestReg = X86::RAX; break;
8796 case X86::DX: DestReg = X86::RDX; break;
8797 case X86::CX: DestReg = X86::RCX; break;
8798 case X86::BX: DestReg = X86::RBX; break;
8799 case X86::SI: DestReg = X86::RSI; break;
8800 case X86::DI: DestReg = X86::RDI; break;
8801 case X86::BP: DestReg = X86::RBP; break;
8802 case X86::SP: DestReg = X86::RSP; break;
8803 }
8804 if (DestReg) {
8805 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008806 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008807 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008808 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008809 } else if (Res.second == X86::FR32RegisterClass ||
8810 Res.second == X86::FR64RegisterClass ||
8811 Res.second == X86::VR128RegisterClass) {
8812 // Handle references to XMM physical registers that got mapped into the
8813 // wrong class. This can happen with constraints like {xmm0} where the
8814 // target independent register mapper will just pick the first match it can
8815 // find, ignoring the required type.
8816 if (VT == MVT::f32)
8817 Res.second = X86::FR32RegisterClass;
8818 else if (VT == MVT::f64)
8819 Res.second = X86::FR64RegisterClass;
8820 else if (X86::VR128RegisterClass->hasType(VT))
8821 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008822 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008823
Chris Lattnerf76d1802006-07-31 23:26:50 +00008824 return Res;
8825}
Mon P Wang0c397192008-10-30 08:01:45 +00008826
8827//===----------------------------------------------------------------------===//
8828// X86 Widen vector type
8829//===----------------------------------------------------------------------===//
8830
8831/// getWidenVectorType: given a vector type, returns the type to widen
8832/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8833/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008834/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008835/// scalarizing vs using the wider vector type.
8836
Dan Gohmanc13cf132009-01-15 17:34:08 +00008837MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008838 assert(VT.isVector());
8839 if (isTypeLegal(VT))
8840 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008841
Mon P Wang0c397192008-10-30 08:01:45 +00008842 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8843 // type based on element type. This would speed up our search (though
8844 // it may not be worth it since the size of the list is relatively
8845 // small).
8846 MVT EltVT = VT.getVectorElementType();
8847 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008848
Mon P Wang0c397192008-10-30 08:01:45 +00008849 // On X86, it make sense to widen any vector wider than 1
8850 if (NElts <= 1)
8851 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008852
8853 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008854 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8855 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008856
8857 if (isTypeLegal(SVT) &&
8858 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008859 SVT.getVectorNumElements() > NElts)
8860 return SVT;
8861 }
8862 return MVT::Other;
8863}