blob: 8d0ea662dc0eaacd2b8d1f9843b7190e2a1ae18b [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);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000791
Evan Chengd54f2d52009-03-31 19:38:51 +0000792 if (!Subtarget->is64Bit()) {
793 // These libcalls are not available in 32-bit.
794 setLibcallName(RTLIB::SHL_I128, 0);
795 setLibcallName(RTLIB::SRL_I128, 0);
796 setLibcallName(RTLIB::SRA_I128, 0);
797 }
798
Evan Cheng206ee9d2006-07-07 08:33:52 +0000799 // We have target-specific dag combine patterns for the following nodes:
800 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000801 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000802 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000803 setTargetDAGCombine(ISD::SHL);
804 setTargetDAGCombine(ISD::SRA);
805 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000806 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000807 if (Subtarget->is64Bit())
808 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000809
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000810 computeRegisterProperties();
811
Evan Cheng87ed7162006-02-14 08:25:08 +0000812 // FIXME: These should be based on subtarget info. Plus, the values should
813 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000814 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
815 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
816 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000817 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000818 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000819 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000820}
821
Scott Michel5b8f82e2008-03-10 15:42:14 +0000822
Duncan Sands5480c042009-01-01 15:52:00 +0000823MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000824 return MVT::i8;
825}
826
827
Evan Cheng29286502008-01-23 23:17:41 +0000828/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
829/// the desired ByVal argument alignment.
830static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
831 if (MaxAlign == 16)
832 return;
833 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
834 if (VTy->getBitWidth() == 128)
835 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000836 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
837 unsigned EltAlign = 0;
838 getMaxByValAlign(ATy->getElementType(), EltAlign);
839 if (EltAlign > MaxAlign)
840 MaxAlign = EltAlign;
841 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
842 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
843 unsigned EltAlign = 0;
844 getMaxByValAlign(STy->getElementType(i), EltAlign);
845 if (EltAlign > MaxAlign)
846 MaxAlign = EltAlign;
847 if (MaxAlign == 16)
848 break;
849 }
850 }
851 return;
852}
853
854/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
855/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000856/// that contain SSE vectors are placed at 16-byte boundaries while the rest
857/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000858unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000859 if (Subtarget->is64Bit()) {
860 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000861 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000862 if (TyAlign > 8)
863 return TyAlign;
864 return 8;
865 }
866
Evan Cheng29286502008-01-23 23:17:41 +0000867 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000868 if (Subtarget->hasSSE1())
869 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000870 return Align;
871}
Chris Lattner2b02a442007-02-25 08:29:00 +0000872
Evan Chengf0df0312008-05-15 08:39:06 +0000873/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000874/// and store operations as a result of memset, memcpy, and memmove
875/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000876/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000878X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +0000879 bool isSrcConst, bool isSrcStr,
880 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000881 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
882 // linux. This is because the stack realignment code can't handle certain
883 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +0000884 const Function *F = DAG.getMachineFunction().getFunction();
885 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
886 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000887 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
888 return MVT::v4i32;
889 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
890 return MVT::v4f32;
891 }
Evan Chengf0df0312008-05-15 08:39:06 +0000892 if (Subtarget->is64Bit() && Size >= 8)
893 return MVT::i64;
894 return MVT::i32;
895}
896
Evan Chengcc415862007-11-09 01:32:10 +0000897/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
898/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000899SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000900 SelectionDAG &DAG) const {
901 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000902 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000903 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000904 // This doesn't have DebugLoc associated with it, but is not really the
905 // same as a Register.
906 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
907 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000908 return Table;
909}
910
Chris Lattner2b02a442007-02-25 08:29:00 +0000911//===----------------------------------------------------------------------===//
912// Return Value Calling Convention Implementation
913//===----------------------------------------------------------------------===//
914
Chris Lattner59ed56b2007-02-28 04:55:35 +0000915#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000916
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000917/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000918SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000919 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000920 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000921
Chris Lattner9774c912007-02-27 05:28:59 +0000922 SmallVector<CCValAssign, 16> RVLocs;
923 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000924 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
925 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000926 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000927
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000928 // If this is the first return lowered for this function, add the regs to the
929 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000930 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000931 for (unsigned i = 0; i != RVLocs.size(); ++i)
932 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000933 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000934 }
Dan Gohman475871a2008-07-27 21:46:04 +0000935 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000936
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000937 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000938 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000939 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SDValue TailCall = Chain;
941 SDValue TargetAddress = TailCall.getOperand(1);
942 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000943 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000944 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +0000945 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R11)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000946 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000947 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000948 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000949 assert(StackAdjustment.getOpcode() == ISD::Constant &&
950 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000951
Dan Gohman475871a2008-07-27 21:46:04 +0000952 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000953 Operands.push_back(Chain.getOperand(0));
954 Operands.push_back(TargetAddress);
955 Operands.push_back(StackAdjustment);
956 // Copy registers used by the call. Last operand is a flag so it is not
957 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000958 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000959 Operands.push_back(Chain.getOperand(i));
960 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000961 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000962 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000963 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000965 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000966 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000967
Dan Gohman475871a2008-07-27 21:46:04 +0000968 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000969 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
970 // Operand #1 = Bytes To Pop
971 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000972
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000973 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000974 for (unsigned i = 0; i != RVLocs.size(); ++i) {
975 CCValAssign &VA = RVLocs[i];
976 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000977 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000978
Chris Lattner447ff682008-03-11 03:23:40 +0000979 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
980 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000981 if (VA.getLocReg() == X86::ST0 ||
982 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000983 // If this is a copy from an xmm register to ST(0), use an FPExtend to
984 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000985 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000986 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000987 RetOps.push_back(ValToCopy);
988 // Don't emit a copytoreg.
989 continue;
990 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000991
Evan Cheng242b38b2009-02-23 09:03:22 +0000992 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
993 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +0000994 if (Subtarget->is64Bit()) {
995 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +0000996 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +0000997 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +0000998 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
999 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1000 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001001 }
1002
Dale Johannesendd64c412009-02-04 00:33:20 +00001003 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001004 Flag = Chain.getValue(1);
1005 }
Dan Gohman61a92132008-04-21 23:59:07 +00001006
1007 // The x86-64 ABI for returning structs by value requires that we copy
1008 // the sret argument into %rax for the return. We saved the argument into
1009 // a virtual register in the entry block, so now we copy the value out
1010 // and into %rax.
1011 if (Subtarget->is64Bit() &&
1012 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1013 MachineFunction &MF = DAG.getMachineFunction();
1014 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1015 unsigned Reg = FuncInfo->getSRetReturnReg();
1016 if (!Reg) {
1017 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1018 FuncInfo->setSRetReturnReg(Reg);
1019 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001020 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001021
Dale Johannesendd64c412009-02-04 00:33:20 +00001022 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001023 Flag = Chain.getValue(1);
1024 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Chris Lattner447ff682008-03-11 03:23:40 +00001026 RetOps[0] = Chain; // Update chain.
1027
1028 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001029 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001030 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
1032 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001033 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001034}
1035
1036
Chris Lattner3085e152007-02-25 08:59:22 +00001037/// LowerCallResult - Lower the result values of an ISD::CALL into the
1038/// appropriate copies out of appropriate physical registers. This assumes that
1039/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1040/// being lowered. The returns a SDNode with the same number of values as the
1041/// ISD::CALL.
1042SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001043LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001044 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001045
Scott Michelfdc40a02009-02-17 22:15:04 +00001046 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001047 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001048 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001049 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001050 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001051 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001052 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1053
Dan Gohman475871a2008-07-27 21:46:04 +00001054 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001055
Chris Lattner3085e152007-02-25 08:59:22 +00001056 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001057 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001058 CCValAssign &VA = RVLocs[i];
1059 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Torok Edwin3f142c32009-02-01 18:15:56 +00001061 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001062 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001063 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1064 cerr << "SSE register return with SSE disabled\n";
1065 exit(1);
1066 }
1067
Chris Lattner8e6da152008-03-10 21:08:41 +00001068 // If this is a call to a function that returns an fp value on the floating
1069 // point stack, but where we prefer to use the value in xmm registers, copy
1070 // 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 +00001071 if ((VA.getLocReg() == X86::ST0 ||
1072 VA.getLocReg() == X86::ST1) &&
1073 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001074 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001076
Evan Cheng79fb3b42009-02-20 20:43:02 +00001077 SDValue Val;
1078 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001079 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1080 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1081 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1082 MVT::v2i64, InFlag).getValue(1);
1083 Val = Chain.getValue(0);
1084 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1085 Val, DAG.getConstant(0, MVT::i64));
1086 } else {
1087 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1088 MVT::i64, InFlag).getValue(1);
1089 Val = Chain.getValue(0);
1090 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001091 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1092 } else {
1093 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1094 CopyVT, InFlag).getValue(1);
1095 Val = Chain.getValue(0);
1096 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001097 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001098
Dan Gohman37eed792009-02-04 17:28:58 +00001099 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001100 // Round the F80 the right size, which also moves to the appropriate xmm
1101 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001102 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001103 // This truncation won't change the value.
1104 DAG.getIntPtrConstant(1));
1105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001106
Chris Lattner8e6da152008-03-10 21:08:41 +00001107 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001108 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001109
Chris Lattner3085e152007-02-25 08:59:22 +00001110 // Merge everything together with a MERGE_VALUES node.
1111 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001112 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1113 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001114}
1115
1116
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001117//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001118// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001119//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001120// StdCall calling convention seems to be standard for many Windows' API
1121// routines and around. It differs from C calling convention just a little:
1122// callee should clean up the stack, not caller. Symbols should be also
1123// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001124// For info on fast calling convention see Fast Calling Convention (tail call)
1125// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001126
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001127/// CallIsStructReturn - Determines whether a CALL node uses struct return
1128/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001129static bool CallIsStructReturn(CallSDNode *TheCall) {
1130 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001131 if (!NumOps)
1132 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001133
Dan Gohman095cc292008-09-13 01:54:27 +00001134 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001135}
1136
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001137/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1138/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001139static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001140 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001141 if (!NumArgs)
1142 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001143
1144 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001145}
1146
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001147/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1148/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001149/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001150bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001151 if (IsVarArg)
1152 return false;
1153
Dan Gohman095cc292008-09-13 01:54:27 +00001154 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001155 default:
1156 return false;
1157 case CallingConv::X86_StdCall:
1158 return !Subtarget->is64Bit();
1159 case CallingConv::X86_FastCall:
1160 return !Subtarget->is64Bit();
1161 case CallingConv::Fast:
1162 return PerformTailCallOpt;
1163 }
1164}
1165
Dan Gohman095cc292008-09-13 01:54:27 +00001166/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1167/// given CallingConvention value.
1168CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001169 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001170 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001171 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001172 else
1173 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001174 }
1175
Gordon Henriksen86737662008-01-05 16:56:59 +00001176 if (CC == CallingConv::X86_FastCall)
1177 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001178 else if (CC == CallingConv::Fast)
1179 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001180 else
1181 return CC_X86_32_C;
1182}
1183
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001184/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1185/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001186NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001187X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001188 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001189 if (CC == CallingConv::X86_FastCall)
1190 return FastCall;
1191 else if (CC == CallingConv::X86_StdCall)
1192 return StdCall;
1193 return None;
1194}
1195
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001196
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001197/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1198/// in a register before calling.
1199bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1200 return !IsTailCall && !Is64Bit &&
1201 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1202 Subtarget->isPICStyleGOT();
1203}
1204
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001205/// CallRequiresFnAddressInReg - Check whether the call requires the function
1206/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001207bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001208X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001209 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001210 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1211 Subtarget->isPICStyleGOT();
1212}
1213
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001214/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1215/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001216/// the specific parameter attribute. The copy will be passed as a byval
1217/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001218static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001219CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001220 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1221 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001223 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001224 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001225}
1226
Dan Gohman475871a2008-07-27 21:46:04 +00001227SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001228 const CCValAssign &VA,
1229 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001230 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001231 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001232 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001233 ISD::ArgFlagsTy Flags =
1234 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001235 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001236 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001237
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001238 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001239 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001240 // In case of tail call optimization mark all arguments mutable. Since they
1241 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001242 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001243 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001244 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001245 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001246 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001247 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001248 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001249}
1250
Dan Gohman475871a2008-07-27 21:46:04 +00001251SDValue
1252X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001253 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001254 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001255 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001256
Gordon Henriksen86737662008-01-05 16:56:59 +00001257 const Function* Fn = MF.getFunction();
1258 if (Fn->hasExternalLinkage() &&
1259 Subtarget->isTargetCygMing() &&
1260 Fn->getName() == "main")
1261 FuncInfo->setForceFramePointer(true);
1262
1263 // Decorate the function name.
1264 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
Evan Cheng1bc78042006-04-26 01:20:17 +00001266 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001267 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001268 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001269 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001270 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001271 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001272
1273 assert(!(isVarArg && CC == CallingConv::Fast) &&
1274 "Var args not supported with calling convention fastcc");
1275
Chris Lattner638402b2007-02-28 07:00:42 +00001276 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001277 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001278 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001279 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Dan Gohman475871a2008-07-27 21:46:04 +00001281 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001282 unsigned LastVal = ~0U;
1283 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1284 CCValAssign &VA = ArgLocs[i];
1285 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1286 // places.
1287 assert(VA.getValNo() != LastVal &&
1288 "Don't support value assigned to multiple locs yet");
1289 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001290
Chris Lattnerf39f7712007-02-28 05:46:49 +00001291 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001292 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001293 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001294 if (RegVT == MVT::i32)
1295 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001296 else if (Is64Bit && RegVT == MVT::i64)
1297 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001298 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001299 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001300 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001301 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001302 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001303 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001304 else if (RegVT.isVector()) {
1305 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001306 if (!Is64Bit)
1307 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1308 else {
1309 // Darwin calling convention passes MMX values in either GPRs or
1310 // XMMs in x86-64. Other targets pass them in memory.
1311 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1312 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1313 RegVT = MVT::v2i64;
1314 } else {
1315 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1316 RegVT = MVT::i64;
1317 }
1318 }
1319 } else {
1320 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001321 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001322
Bob Wilson998e1252009-04-20 18:36:57 +00001323 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001324 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001325
Chris Lattnerf39f7712007-02-28 05:46:49 +00001326 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1327 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1328 // right size.
1329 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001330 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001331 DAG.getValueType(VA.getValVT()));
1332 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001333 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001334 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001335
Chris Lattnerf39f7712007-02-28 05:46:49 +00001336 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001337 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001338
Gordon Henriksen86737662008-01-05 16:56:59 +00001339 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001340 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001341 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001342 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001343 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001344 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1345 ArgValue, DAG.getConstant(0, MVT::i64));
1346 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001347 }
1348 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001349
Chris Lattnerf39f7712007-02-28 05:46:49 +00001350 ArgValues.push_back(ArgValue);
1351 } else {
1352 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001353 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001354 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001355 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001356
Dan Gohman61a92132008-04-21 23:59:07 +00001357 // The x86-64 ABI for returning structs by value requires that we copy
1358 // the sret argument into %rax for the return. Save the argument into
1359 // a virtual register so that we can access it from the return points.
1360 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1361 MachineFunction &MF = DAG.getMachineFunction();
1362 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1363 unsigned Reg = FuncInfo->getSRetReturnReg();
1364 if (!Reg) {
1365 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1366 FuncInfo->setSRetReturnReg(Reg);
1367 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001368 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001369 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001370 }
1371
Chris Lattnerf39f7712007-02-28 05:46:49 +00001372 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001373 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001374 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001375 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001376
Evan Cheng1bc78042006-04-26 01:20:17 +00001377 // If the function takes variable number of arguments, make a frame index for
1378 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001379 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1381 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1382 }
1383 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001384 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1385
1386 // FIXME: We should really autogenerate these arrays
1387 static const unsigned GPR64ArgRegsWin64[] = {
1388 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001389 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001390 static const unsigned XMMArgRegsWin64[] = {
1391 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1392 };
1393 static const unsigned GPR64ArgRegs64Bit[] = {
1394 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1395 };
1396 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001397 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1398 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1399 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001400 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1401
1402 if (IsWin64) {
1403 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1404 GPR64ArgRegs = GPR64ArgRegsWin64;
1405 XMMArgRegs = XMMArgRegsWin64;
1406 } else {
1407 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1408 GPR64ArgRegs = GPR64ArgRegs64Bit;
1409 XMMArgRegs = XMMArgRegs64Bit;
1410 }
1411 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1412 TotalNumIntRegs);
1413 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1414 TotalNumXMMRegs);
1415
Devang Patel578efa92009-06-05 21:57:13 +00001416 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001417 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001418 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001419 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001420 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001421 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001422 // Kernel mode asks for SSE to be disabled, so don't push them
1423 // on the stack.
1424 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001425
Gordon Henriksen86737662008-01-05 16:56:59 +00001426 // For X86-64, if there are vararg parameters that are passed via
1427 // registers, then we must store them to their spots on the stack so they
1428 // may be loaded by deferencing the result of va_next.
1429 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001430 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1431 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1432 TotalNumXMMRegs * 16, 16);
1433
Gordon Henriksen86737662008-01-05 16:56:59 +00001434 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001435 SmallVector<SDValue, 8> MemOps;
1436 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001437 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001438 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001439 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001440 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1441 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001442 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001444 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001445 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001446 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001447 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001448 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001449 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001450
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001452 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001453 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001454 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001455 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1456 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001457 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001458 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001459 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001460 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001461 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001462 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001463 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001464 }
1465 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001466 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001467 &MemOps[0], MemOps.size());
1468 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001470
Gordon Henriksenae636f82008-01-03 16:47:34 +00001471 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001472
Gordon Henriksen86737662008-01-05 16:56:59 +00001473 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001474 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001475 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001476 BytesCallerReserves = 0;
1477 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001478 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001479 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001480 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001481 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001482 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001483 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001484
Gordon Henriksen86737662008-01-05 16:56:59 +00001485 if (!Is64Bit) {
1486 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1487 if (CC == CallingConv::X86_FastCall)
1488 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1489 }
Evan Cheng25caf632006-05-23 21:06:34 +00001490
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001491 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001492
Evan Cheng25caf632006-05-23 21:06:34 +00001493 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001494 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001495 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001496}
1497
Dan Gohman475871a2008-07-27 21:46:04 +00001498SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001499X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001500 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001501 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001502 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001503 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001504 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001505 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001506 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001507 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001508 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001509 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001510 }
Dale Johannesenace16102009-02-03 19:33:06 +00001511 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001512 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001513}
1514
Bill Wendling64e87322009-01-16 19:25:27 +00001515/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001516/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001517SDValue
1518X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001519 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001520 SDValue Chain,
1521 bool IsTailCall,
1522 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001523 int FPDiff,
1524 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001525 if (!IsTailCall || FPDiff==0) return Chain;
1526
1527 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001528 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001529 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001530
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001531 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001532 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001533 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001534}
1535
1536/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1537/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001538static SDValue
1539EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001541 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001542 // Store the return address to the appropriate stack slot.
1543 if (!FPDiff) return Chain;
1544 // Calculate the new stack slot for the return address.
1545 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001546 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001547 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001548 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001549 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001550 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001551 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001552 return Chain;
1553}
1554
Dan Gohman475871a2008-07-27 21:46:04 +00001555SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001556 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001557 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1558 SDValue Chain = TheCall->getChain();
1559 unsigned CC = TheCall->getCallingConv();
1560 bool isVarArg = TheCall->isVarArg();
1561 bool IsTailCall = TheCall->isTailCall() &&
1562 CC == CallingConv::Fast && PerformTailCallOpt;
1563 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001565 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001566 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001567
1568 assert(!(isVarArg && CC == CallingConv::Fast) &&
1569 "Var args not supported with calling convention fastcc");
1570
Chris Lattner638402b2007-02-28 07:00:42 +00001571 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001572 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001573 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001574 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001575
Chris Lattner423c5f42007-02-28 05:31:48 +00001576 // Get a count of how many bytes are to be pushed on the stack.
1577 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001578 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001579 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001580
Gordon Henriksen86737662008-01-05 16:56:59 +00001581 int FPDiff = 0;
1582 if (IsTailCall) {
1583 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001584 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001585 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1586 FPDiff = NumBytesCallerPushed - NumBytes;
1587
1588 // Set the delta of movement of the returnaddr stackslot.
1589 // But only set if delta is greater than previous delta.
1590 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1591 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1592 }
1593
Chris Lattnere563bbc2008-10-11 22:08:30 +00001594 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001595
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001597 // Load return adress for tail calls.
1598 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001599 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001600
Dan Gohman475871a2008-07-27 21:46:04 +00001601 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1602 SmallVector<SDValue, 8> MemOpChains;
1603 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001604
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001605 // Walk the register/memloc assignments, inserting copies/loads. In the case
1606 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001607 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1608 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001609 SDValue Arg = TheCall->getArg(i);
1610 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1611 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001612
Chris Lattner423c5f42007-02-28 05:31:48 +00001613 // Promote the value if needed.
1614 switch (VA.getLocInfo()) {
1615 default: assert(0 && "Unknown loc info!");
1616 case CCValAssign::Full: break;
1617 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001618 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001619 break;
1620 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001621 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001622 break;
1623 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001624 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001625 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001627
Chris Lattner423c5f42007-02-28 05:31:48 +00001628 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001629 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001630 MVT RegVT = VA.getLocVT();
1631 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001632 switch (VA.getLocReg()) {
1633 default:
1634 break;
1635 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1636 case X86::R8: {
1637 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001638 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001639 break;
1640 }
1641 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1642 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1643 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001644 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1645 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001646 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001647 break;
1648 }
1649 }
1650 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001651 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1652 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001653 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001654 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001655 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001656 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001657
Dan Gohman095cc292008-09-13 01:54:27 +00001658 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1659 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001660 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001661 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001663
Evan Cheng32fe1032006-05-25 00:59:30 +00001664 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001665 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001666 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001667
Evan Cheng347d5f72006-04-28 21:29:37 +00001668 // Build a sequence of copy-to-reg nodes chained together with token chain
1669 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001670 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001671 // Tail call byval lowering might overwrite argument registers so in case of
1672 // tail call optimization the copies to registers are lowered later.
1673 if (!IsTailCall)
1674 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001675 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001676 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001677 InFlag = Chain.getValue(1);
1678 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001679
Evan Chengf4684712007-02-21 21:18:14 +00001680 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001681 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001682 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001683 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001684 DAG.getNode(X86ISD::GlobalBaseReg,
1685 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001686 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001687 InFlag);
1688 InFlag = Chain.getValue(1);
1689 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001690 // If we are tail calling and generating PIC/GOT style code load the address
1691 // of the callee into ecx. The value in ecx is used as target of the tail
1692 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1693 // calls on PIC/GOT architectures. Normally we would just put the address of
1694 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1695 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001696 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001697 // Note: The actual moving to ecx is done further down.
1698 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001699 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001700 !G->getGlobal()->hasProtectedVisibility())
1701 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001702 else if (isa<ExternalSymbolSDNode>(Callee))
1703 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001704 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001705
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 if (Is64Bit && isVarArg) {
1707 // From AMD64 ABI document:
1708 // For calls that may call functions that use varargs or stdargs
1709 // (prototype-less calls or calls to functions containing ellipsis (...) in
1710 // the declaration) %al is used as hidden argument to specify the number
1711 // of SSE registers used. The contents of %al do not need to match exactly
1712 // the number of registers, but must be an ubound on the number of SSE
1713 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001714
1715 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001716 // Count the number of XMM registers allocated.
1717 static const unsigned XMMArgRegs[] = {
1718 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1719 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1720 };
1721 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001722 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001723 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001724
Dale Johannesendd64c412009-02-04 00:33:20 +00001725 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001726 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1727 InFlag = Chain.getValue(1);
1728 }
1729
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001730
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001731 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001732 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001733 SmallVector<SDValue, 8> MemOpChains2;
1734 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001735 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001736 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001737 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001738 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1739 CCValAssign &VA = ArgLocs[i];
1740 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001741 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001742 SDValue Arg = TheCall->getArg(i);
1743 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 // Create frame index.
1745 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001746 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001747 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001748 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001749
Duncan Sands276dcbd2008-03-21 09:14:45 +00001750 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001751 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001752 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001753 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001754 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001755 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001756 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001757
1758 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001759 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001760 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001761 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001762 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001763 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001764 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001765 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 }
1767 }
1768
1769 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001770 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001771 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001772
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001773 // Copy arguments to their registers.
1774 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001775 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001776 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001777 InFlag = Chain.getValue(1);
1778 }
Dan Gohman475871a2008-07-27 21:46:04 +00001779 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001780
Gordon Henriksen86737662008-01-05 16:56:59 +00001781 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001782 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001783 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001784 }
1785
Evan Cheng32fe1032006-05-25 00:59:30 +00001786 // If the callee is a GlobalAddress node (quite common, every direct call is)
1787 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001788 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001789 // We should use extra load for direct calls to dllimported functions in
1790 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001791 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1792 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001793 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1794 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001795 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1796 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001797 } else if (IsTailCall) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001798 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001799
Dale Johannesendd64c412009-02-04 00:33:20 +00001800 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001801 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001802 Callee,InFlag);
1803 Callee = DAG.getRegister(Opc, getPointerTy());
1804 // Add register as live out.
1805 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001806 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001807
Chris Lattnerd96d0722007-02-25 06:40:16 +00001808 // Returns a chain & a flag for retval copy to use.
1809 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001810 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001811
1812 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001813 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1814 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001815 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Gordon Henriksen86737662008-01-05 16:56:59 +00001817 // Returns a chain & a flag for retval copy to use.
1818 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1819 Ops.clear();
1820 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001821
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001822 Ops.push_back(Chain);
1823 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001824
Gordon Henriksen86737662008-01-05 16:56:59 +00001825 if (IsTailCall)
1826 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001827
Gordon Henriksen86737662008-01-05 16:56:59 +00001828 // Add argument registers to the end of the list so that they are known live
1829 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001830 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1831 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1832 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001833
Evan Cheng586ccac2008-03-18 23:36:35 +00001834 // Add an implicit use GOT pointer in EBX.
1835 if (!IsTailCall && !Is64Bit &&
1836 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1837 Subtarget->isPICStyleGOT())
1838 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1839
1840 // Add an implicit use of AL for x86 vararg functions.
1841 if (Is64Bit && isVarArg)
1842 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1843
Gabor Greifba36cb52008-08-28 21:40:38 +00001844 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001845 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001846
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001848 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001850 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001851 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001852
Gabor Greifba36cb52008-08-28 21:40:38 +00001853 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 }
1855
Dale Johannesenace16102009-02-03 19:33:06 +00001856 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001857 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001858
Chris Lattner2d297092006-05-23 18:50:38 +00001859 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001861 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001862 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001863 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001864 // If this is is a call to a struct-return function, the callee
1865 // pops the hidden struct pointer, so we have to push it back.
1866 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001867 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001868 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001869 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001870
Gordon Henriksenae636f82008-01-03 16:47:34 +00001871 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001872 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001873 DAG.getIntPtrConstant(NumBytes, true),
1874 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1875 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001876 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001877 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001878
Chris Lattner3085e152007-02-25 08:59:22 +00001879 // Handle result values, copying them out of physregs into vregs that we
1880 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001881 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001882 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001883}
1884
Evan Cheng25ab6902006-09-08 06:48:29 +00001885
1886//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001887// Fast Calling Convention (tail call) implementation
1888//===----------------------------------------------------------------------===//
1889
1890// Like std call, callee cleans arguments, convention except that ECX is
1891// reserved for storing the tail called function address. Only 2 registers are
1892// free for argument passing (inreg). Tail call optimization is performed
1893// provided:
1894// * tailcallopt is enabled
1895// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001896// On X86_64 architecture with GOT-style position independent code only local
1897// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001898// To keep the stack aligned according to platform abi the function
1899// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1900// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001901// If a tail called function callee has more arguments than the caller the
1902// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001903// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001904// original REtADDR, but before the saved framepointer or the spilled registers
1905// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1906// stack layout:
1907// arg1
1908// arg2
1909// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001910// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001911// move area ]
1912// (possible EBP)
1913// ESI
1914// EDI
1915// local1 ..
1916
1917/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1918/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001919unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001920 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001921 MachineFunction &MF = DAG.getMachineFunction();
1922 const TargetMachine &TM = MF.getTarget();
1923 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1924 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001925 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001926 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001927 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001928 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1929 // Number smaller than 12 so just add the difference.
1930 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1931 } else {
1932 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001933 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001934 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001935 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001936 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001937}
1938
1939/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001940/// following the call is a return. A function is eligible if caller/callee
1941/// calling conventions match, currently only fastcc supports tail calls, and
1942/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001943bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001945 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001946 if (!PerformTailCallOpt)
1947 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001948
Dan Gohman095cc292008-09-13 01:54:27 +00001949 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001950 MachineFunction &MF = DAG.getMachineFunction();
1951 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001952 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001953 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001954 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001955 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001956 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001957 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001958 return true;
1959
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001960 // Can only do local tail calls (in same module, hidden or protected) on
1961 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001962 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1963 return G->getGlobal()->hasHiddenVisibility()
1964 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001965 }
1966 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001967
1968 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001969}
1970
Dan Gohman3df24e62008-09-03 23:12:08 +00001971FastISel *
1972X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001973 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001974 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001975 DenseMap<const Value *, unsigned> &vm,
1976 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001977 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001978 DenseMap<const AllocaInst *, int> &am
1979#ifndef NDEBUG
1980 , SmallSet<Instruction*, 8> &cil
1981#endif
1982 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001983 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001984#ifndef NDEBUG
1985 , cil
1986#endif
1987 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001988}
1989
1990
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001991//===----------------------------------------------------------------------===//
1992// Other Lowering Hooks
1993//===----------------------------------------------------------------------===//
1994
1995
Dan Gohman475871a2008-07-27 21:46:04 +00001996SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001997 MachineFunction &MF = DAG.getMachineFunction();
1998 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1999 int ReturnAddrIndex = FuncInfo->getRAIndex();
2000
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002001 if (ReturnAddrIndex == 0) {
2002 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002003 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002004 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002005 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002006 }
2007
Evan Cheng25ab6902006-09-08 06:48:29 +00002008 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002009}
2010
2011
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002012/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2013/// specific condition code, returning the condition code and the LHS/RHS of the
2014/// comparison to make.
2015static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2016 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002017 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002018 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2019 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2020 // X > -1 -> X == 0, jump !sign.
2021 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002022 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002023 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2024 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002025 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002026 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002027 // X < 1 -> X <= 0
2028 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002029 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002030 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002031 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002032
Evan Chengd9558e02006-01-06 00:43:03 +00002033 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002034 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002035 case ISD::SETEQ: return X86::COND_E;
2036 case ISD::SETGT: return X86::COND_G;
2037 case ISD::SETGE: return X86::COND_GE;
2038 case ISD::SETLT: return X86::COND_L;
2039 case ISD::SETLE: return X86::COND_LE;
2040 case ISD::SETNE: return X86::COND_NE;
2041 case ISD::SETULT: return X86::COND_B;
2042 case ISD::SETUGT: return X86::COND_A;
2043 case ISD::SETULE: return X86::COND_BE;
2044 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002045 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002046 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002047
Chris Lattner4c78e022008-12-23 23:42:27 +00002048 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002049
Chris Lattner4c78e022008-12-23 23:42:27 +00002050 // If LHS is a foldable load, but RHS is not, flip the condition.
2051 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2052 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2053 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2054 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002055 }
2056
Chris Lattner4c78e022008-12-23 23:42:27 +00002057 switch (SetCCOpcode) {
2058 default: break;
2059 case ISD::SETOLT:
2060 case ISD::SETOLE:
2061 case ISD::SETUGT:
2062 case ISD::SETUGE:
2063 std::swap(LHS, RHS);
2064 break;
2065 }
2066
2067 // On a floating point condition, the flags are set as follows:
2068 // ZF PF CF op
2069 // 0 | 0 | 0 | X > Y
2070 // 0 | 0 | 1 | X < Y
2071 // 1 | 0 | 0 | X == Y
2072 // 1 | 1 | 1 | unordered
2073 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002074 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002075 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002076 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002077 case ISD::SETOLT: // flipped
2078 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002079 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002080 case ISD::SETOLE: // flipped
2081 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002082 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002083 case ISD::SETUGT: // flipped
2084 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002085 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002086 case ISD::SETUGE: // flipped
2087 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002088 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002089 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002090 case ISD::SETNE: return X86::COND_NE;
2091 case ISD::SETUO: return X86::COND_P;
2092 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002093 }
Evan Chengd9558e02006-01-06 00:43:03 +00002094}
2095
Evan Cheng4a460802006-01-11 00:33:36 +00002096/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2097/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002098/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002099static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002100 switch (X86CC) {
2101 default:
2102 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002103 case X86::COND_B:
2104 case X86::COND_BE:
2105 case X86::COND_E:
2106 case X86::COND_P:
2107 case X86::COND_A:
2108 case X86::COND_AE:
2109 case X86::COND_NE:
2110 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002111 return true;
2112 }
2113}
2114
Nate Begeman9008ca62009-04-27 18:41:29 +00002115/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2116/// the specified range (L, H].
2117static bool isUndefOrInRange(int Val, int Low, int Hi) {
2118 return (Val < 0) || (Val >= Low && Val < Hi);
2119}
2120
2121/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2122/// specified value.
2123static bool isUndefOrEqual(int Val, int CmpVal) {
2124 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002125 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002126 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002127}
2128
Nate Begeman9008ca62009-04-27 18:41:29 +00002129/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2130/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2131/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002132static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002133 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2134 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2135 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2136 return (Mask[0] < 2 && Mask[1] < 2);
2137 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002138}
2139
Nate Begeman9008ca62009-04-27 18:41:29 +00002140bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2141 SmallVector<int, 8> M;
2142 N->getMask(M);
2143 return ::isPSHUFDMask(M, N->getValueType(0));
2144}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002145
Nate Begeman9008ca62009-04-27 18:41:29 +00002146/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2147/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002148static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002149 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002150 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002151
2152 // Lower quadword copied in order or undef.
2153 for (int i = 0; i != 4; ++i)
2154 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002155 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002156
Evan Cheng506d3df2006-03-29 23:07:14 +00002157 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002158 for (int i = 4; i != 8; ++i)
2159 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002160 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002161
Evan Cheng506d3df2006-03-29 23:07:14 +00002162 return true;
2163}
2164
Nate Begeman9008ca62009-04-27 18:41:29 +00002165bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2166 SmallVector<int, 8> M;
2167 N->getMask(M);
2168 return ::isPSHUFHWMask(M, N->getValueType(0));
2169}
Evan Cheng506d3df2006-03-29 23:07:14 +00002170
Nate Begeman9008ca62009-04-27 18:41:29 +00002171/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2172/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002173static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002174 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002175 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002176
Rafael Espindola15684b22009-04-24 12:40:33 +00002177 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002178 for (int i = 4; i != 8; ++i)
2179 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002180 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002181
Rafael Espindola15684b22009-04-24 12:40:33 +00002182 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002183 for (int i = 0; i != 4; ++i)
2184 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002185 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002186
Rafael Espindola15684b22009-04-24 12:40:33 +00002187 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002188}
2189
Nate Begeman9008ca62009-04-27 18:41:29 +00002190bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2191 SmallVector<int, 8> M;
2192 N->getMask(M);
2193 return ::isPSHUFLWMask(M, N->getValueType(0));
2194}
2195
Evan Cheng14aed5e2006-03-24 01:18:28 +00002196/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2197/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002198static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002199 int NumElems = VT.getVectorNumElements();
2200 if (NumElems != 2 && NumElems != 4)
2201 return false;
2202
2203 int Half = NumElems / 2;
2204 for (int i = 0; i < Half; ++i)
2205 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002206 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002207 for (int i = Half; i < NumElems; ++i)
2208 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002209 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002210
Evan Cheng14aed5e2006-03-24 01:18:28 +00002211 return true;
2212}
2213
Nate Begeman9008ca62009-04-27 18:41:29 +00002214bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2215 SmallVector<int, 8> M;
2216 N->getMask(M);
2217 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002218}
2219
Evan Cheng213d2cf2007-05-17 18:45:50 +00002220/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002221/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2222/// half elements to come from vector 1 (which would equal the dest.) and
2223/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002224static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002225 int NumElems = VT.getVectorNumElements();
2226
2227 if (NumElems != 2 && NumElems != 4)
2228 return false;
2229
2230 int Half = NumElems / 2;
2231 for (int i = 0; i < Half; ++i)
2232 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002233 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002234 for (int i = Half; i < NumElems; ++i)
2235 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002236 return false;
2237 return true;
2238}
2239
Nate Begeman9008ca62009-04-27 18:41:29 +00002240static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2241 SmallVector<int, 8> M;
2242 N->getMask(M);
2243 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002244}
2245
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002246/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2247/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002248bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2249 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002250 return false;
2251
Evan Cheng2064a2b2006-03-28 06:50:32 +00002252 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002253 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2254 isUndefOrEqual(N->getMaskElt(1), 7) &&
2255 isUndefOrEqual(N->getMaskElt(2), 2) &&
2256 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002257}
2258
Evan Cheng5ced1d82006-04-06 23:23:56 +00002259/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2260/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002261bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2262 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002263
Evan Cheng5ced1d82006-04-06 23:23:56 +00002264 if (NumElems != 2 && NumElems != 4)
2265 return false;
2266
Evan Chengc5cdff22006-04-07 21:53:05 +00002267 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002268 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002269 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002270
Evan Chengc5cdff22006-04-07 21:53:05 +00002271 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002272 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002273 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002274
2275 return true;
2276}
2277
2278/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002279/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2280/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002281bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2282 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002283
Evan Cheng5ced1d82006-04-06 23:23:56 +00002284 if (NumElems != 2 && NumElems != 4)
2285 return false;
2286
Evan Chengc5cdff22006-04-07 21:53:05 +00002287 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002288 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002289 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002290
Nate Begeman9008ca62009-04-27 18:41:29 +00002291 for (unsigned i = 0; i < NumElems/2; ++i)
2292 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002293 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002294
2295 return true;
2296}
2297
Nate Begeman9008ca62009-04-27 18:41:29 +00002298/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2299/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2300/// <2, 3, 2, 3>
2301bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2302 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2303
2304 if (NumElems != 4)
2305 return false;
2306
2307 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2308 isUndefOrEqual(N->getMaskElt(1), 3) &&
2309 isUndefOrEqual(N->getMaskElt(2), 2) &&
2310 isUndefOrEqual(N->getMaskElt(3), 3);
2311}
2312
Evan Cheng0038e592006-03-28 00:39:58 +00002313/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2314/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002315static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002316 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002317 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002318 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002319 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002320
2321 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2322 int BitI = Mask[i];
2323 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002324 if (!isUndefOrEqual(BitI, j))
2325 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002326 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002327 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002328 return false;
2329 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002330 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002331 return false;
2332 }
Evan Cheng0038e592006-03-28 00:39:58 +00002333 }
Evan Cheng0038e592006-03-28 00:39:58 +00002334 return true;
2335}
2336
Nate Begeman9008ca62009-04-27 18:41:29 +00002337bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2338 SmallVector<int, 8> M;
2339 N->getMask(M);
2340 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002341}
2342
Evan Cheng4fcb9222006-03-28 02:43:26 +00002343/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2344/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002345static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002346 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002347 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002348 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002349 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002350
2351 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2352 int BitI = Mask[i];
2353 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002354 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002355 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002356 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002357 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002358 return false;
2359 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002360 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002361 return false;
2362 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002363 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002364 return true;
2365}
2366
Nate Begeman9008ca62009-04-27 18:41:29 +00002367bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2368 SmallVector<int, 8> M;
2369 N->getMask(M);
2370 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002371}
2372
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002373/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2374/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2375/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002376static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002377 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002378 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002379 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002380
2381 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2382 int BitI = Mask[i];
2383 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002384 if (!isUndefOrEqual(BitI, j))
2385 return false;
2386 if (!isUndefOrEqual(BitI1, j))
2387 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002388 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002389 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002390}
2391
Nate Begeman9008ca62009-04-27 18:41:29 +00002392bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2393 SmallVector<int, 8> M;
2394 N->getMask(M);
2395 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2396}
2397
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002398/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2399/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2400/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002401static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002402 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002403 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2404 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002405
2406 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2407 int BitI = Mask[i];
2408 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002409 if (!isUndefOrEqual(BitI, j))
2410 return false;
2411 if (!isUndefOrEqual(BitI1, j))
2412 return false;
2413 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002414 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002415}
2416
Nate Begeman9008ca62009-04-27 18:41:29 +00002417bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2418 SmallVector<int, 8> M;
2419 N->getMask(M);
2420 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2421}
2422
Evan Cheng017dcc62006-04-21 01:05:10 +00002423/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2424/// specifies a shuffle of elements that is suitable for input to MOVSS,
2425/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002426static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002427 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002428 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002429
2430 int NumElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002431
2432 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002433 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002434
2435 for (int i = 1; i < NumElts; ++i)
2436 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002437 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002438
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002439 return true;
2440}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002441
Nate Begeman9008ca62009-04-27 18:41:29 +00002442bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2443 SmallVector<int, 8> M;
2444 N->getMask(M);
2445 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002446}
2447
Evan Cheng017dcc62006-04-21 01:05:10 +00002448/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2449/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002450/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002451static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002452 bool V2IsSplat = false, bool V2IsUndef = false) {
2453 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002454 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002455 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002456
2457 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002458 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002459
2460 for (int i = 1; i < NumOps; ++i)
2461 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2462 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2463 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002464 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002465
Evan Cheng39623da2006-04-20 08:58:49 +00002466 return true;
2467}
2468
Nate Begeman9008ca62009-04-27 18:41:29 +00002469static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002470 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002471 SmallVector<int, 8> M;
2472 N->getMask(M);
2473 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002474}
2475
Evan Chengd9539472006-04-14 21:59:03 +00002476/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2477/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002478bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2479 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002480 return false;
2481
2482 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002483 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002484 int Elt = N->getMaskElt(i);
2485 if (Elt >= 0 && Elt != 1)
2486 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002487 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002488
2489 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002490 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002491 int Elt = N->getMaskElt(i);
2492 if (Elt >= 0 && Elt != 3)
2493 return false;
2494 if (Elt == 3)
2495 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002496 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002497 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002498 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002499 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002500}
2501
2502/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2503/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002504bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2505 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002506 return false;
2507
2508 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002509 for (unsigned i = 0; i < 2; ++i)
2510 if (N->getMaskElt(i) > 0)
2511 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002512
2513 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002514 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 int Elt = N->getMaskElt(i);
2516 if (Elt >= 0 && Elt != 2)
2517 return false;
2518 if (Elt == 2)
2519 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002520 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002521 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002522 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002523}
2524
Evan Cheng0b457f02008-09-25 20:50:48 +00002525/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2526/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002527bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2528 int e = N->getValueType(0).getVectorNumElements() / 2;
2529
2530 for (int i = 0; i < e; ++i)
2531 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002532 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002533 for (int i = 0; i < e; ++i)
2534 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002535 return false;
2536 return true;
2537}
2538
Evan Cheng63d33002006-03-22 08:01:21 +00002539/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2540/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2541/// instructions.
2542unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002543 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2544 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2545
Evan Chengb9df0ca2006-03-22 02:53:00 +00002546 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2547 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002548 for (int i = 0; i < NumOperands; ++i) {
2549 int Val = SVOp->getMaskElt(NumOperands-i-1);
2550 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002551 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002552 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002553 if (i != NumOperands - 1)
2554 Mask <<= Shift;
2555 }
Evan Cheng63d33002006-03-22 08:01:21 +00002556 return Mask;
2557}
2558
Evan Cheng506d3df2006-03-29 23:07:14 +00002559/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2560/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2561/// instructions.
2562unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002563 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002564 unsigned Mask = 0;
2565 // 8 nodes, but we only care about the last 4.
2566 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002567 int Val = SVOp->getMaskElt(i);
2568 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002569 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002570 if (i != 4)
2571 Mask <<= 2;
2572 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002573 return Mask;
2574}
2575
2576/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2577/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2578/// instructions.
2579unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002580 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002581 unsigned Mask = 0;
2582 // 8 nodes, but we only care about the first 4.
2583 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002584 int Val = SVOp->getMaskElt(i);
2585 if (Val >= 0)
2586 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002587 if (i != 0)
2588 Mask <<= 2;
2589 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002590 return Mask;
2591}
2592
Nate Begeman9008ca62009-04-27 18:41:29 +00002593/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2594/// their permute mask.
2595static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2596 SelectionDAG &DAG) {
2597 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002598 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 SmallVector<int, 8> MaskVec;
2600
Nate Begeman5a5ca152009-04-29 05:20:52 +00002601 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002602 int idx = SVOp->getMaskElt(i);
2603 if (idx < 0)
2604 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002605 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002606 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002607 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002609 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002610 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2611 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002612}
2613
Evan Cheng779ccea2007-12-07 21:30:01 +00002614/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2615/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002616static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002617 unsigned NumElems = VT.getVectorNumElements();
2618 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 int idx = Mask[i];
2620 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002621 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002622 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002623 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002624 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002626 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002627}
2628
Evan Cheng533a0aa2006-04-19 20:35:22 +00002629/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2630/// match movhlps. The lower half elements should come from upper half of
2631/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002632/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002633static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2634 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002635 return false;
2636 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002637 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002638 return false;
2639 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002641 return false;
2642 return true;
2643}
2644
Evan Cheng5ced1d82006-04-06 23:23:56 +00002645/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002646/// is promoted to a vector. It also returns the LoadSDNode by reference if
2647/// required.
2648static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002649 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2650 return false;
2651 N = N->getOperand(0).getNode();
2652 if (!ISD::isNON_EXTLoad(N))
2653 return false;
2654 if (LD)
2655 *LD = cast<LoadSDNode>(N);
2656 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002657}
2658
Evan Cheng533a0aa2006-04-19 20:35:22 +00002659/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2660/// match movlp{s|d}. The lower half elements should come from lower half of
2661/// V1 (and in order), and the upper half elements should come from the upper
2662/// half of V2 (and in order). And since V1 will become the source of the
2663/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002664static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2665 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002666 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002667 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002668 // Is V2 is a vector load, don't do this transformation. We will try to use
2669 // load folding shufps op.
2670 if (ISD::isNON_EXTLoad(V2))
2671 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002672
Nate Begeman5a5ca152009-04-29 05:20:52 +00002673 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002674
Evan Cheng533a0aa2006-04-19 20:35:22 +00002675 if (NumElems != 2 && NumElems != 4)
2676 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002677 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002678 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002679 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002680 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002681 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002682 return false;
2683 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002684}
2685
Evan Cheng39623da2006-04-20 08:58:49 +00002686/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2687/// all the same.
2688static bool isSplatVector(SDNode *N) {
2689 if (N->getOpcode() != ISD::BUILD_VECTOR)
2690 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002691
Dan Gohman475871a2008-07-27 21:46:04 +00002692 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002693 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2694 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002695 return false;
2696 return true;
2697}
2698
Evan Cheng213d2cf2007-05-17 18:45:50 +00002699/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2700/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002701static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002702 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002703 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002704 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002705 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002706}
2707
2708/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002709/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002710/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002711static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002712 SDValue V1 = N->getOperand(0);
2713 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002714 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2715 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002716 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002717 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002718 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002719 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2720 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002721 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2722 return false;
2723 } else if (Idx >= 0) {
2724 unsigned Opc = V1.getOpcode();
2725 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2726 continue;
2727 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002728 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002729 }
2730 }
2731 return true;
2732}
2733
2734/// getZeroVector - Returns a vector of specified type with all zero elements.
2735///
Dale Johannesenace16102009-02-03 19:33:06 +00002736static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2737 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002738 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002739
Chris Lattner8a594482007-11-25 00:24:49 +00002740 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2741 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002743 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002744 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002745 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002746 } else if (HasSSE2) { // SSE2
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::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002749 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002750 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002751 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002752 }
Dale Johannesenace16102009-02-03 19:33:06 +00002753 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002754}
2755
Chris Lattner8a594482007-11-25 00:24:49 +00002756/// getOnesVector - Returns a vector of specified type with all bits set.
2757///
Dale Johannesenace16102009-02-03 19:33:06 +00002758static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002759 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002760
Chris Lattner8a594482007-11-25 00:24:49 +00002761 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2762 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002763 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2764 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002765 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002766 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002767 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002768 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002769 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002770}
2771
2772
Evan Cheng39623da2006-04-20 08:58:49 +00002773/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2774/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002775static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2776 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002777 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002778
Evan Cheng39623da2006-04-20 08:58:49 +00002779 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002780 SmallVector<int, 8> MaskVec;
2781 SVOp->getMask(MaskVec);
2782
Nate Begeman5a5ca152009-04-29 05:20:52 +00002783 for (unsigned i = 0; i != NumElems; ++i) {
2784 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002785 MaskVec[i] = NumElems;
2786 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002787 }
Evan Cheng39623da2006-04-20 08:58:49 +00002788 }
Evan Cheng39623da2006-04-20 08:58:49 +00002789 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002790 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2791 SVOp->getOperand(1), &MaskVec[0]);
2792 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002793}
2794
Evan Cheng017dcc62006-04-21 01:05:10 +00002795/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2796/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002797static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2798 SDValue V2) {
2799 unsigned NumElems = VT.getVectorNumElements();
2800 SmallVector<int, 8> Mask;
2801 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002802 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 Mask.push_back(i);
2804 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002805}
2806
Nate Begeman9008ca62009-04-27 18:41:29 +00002807/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2808static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2809 SDValue V2) {
2810 unsigned NumElems = VT.getVectorNumElements();
2811 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002812 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 Mask.push_back(i);
2814 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002815 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002816 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002817}
2818
Nate Begeman9008ca62009-04-27 18:41:29 +00002819/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2820static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2821 SDValue V2) {
2822 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002823 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002824 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002825 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002826 Mask.push_back(i + Half);
2827 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002828 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002830}
2831
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002832/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002833static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2834 bool HasSSE2) {
2835 if (SV->getValueType(0).getVectorNumElements() <= 4)
2836 return SDValue(SV, 0);
2837
2838 MVT PVT = MVT::v4f32;
2839 MVT VT = SV->getValueType(0);
2840 DebugLoc dl = SV->getDebugLoc();
2841 SDValue V1 = SV->getOperand(0);
2842 int NumElems = VT.getVectorNumElements();
2843 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002844
Nate Begeman9008ca62009-04-27 18:41:29 +00002845 // unpack elements to the correct location
2846 while (NumElems > 4) {
2847 if (EltNo < NumElems/2) {
2848 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2849 } else {
2850 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2851 EltNo -= NumElems/2;
2852 }
2853 NumElems >>= 1;
2854 }
2855
2856 // Perform the splat.
2857 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002858 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2860 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002861}
2862
Evan Chengba05f722006-04-21 23:03:30 +00002863/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002864/// vector of zero or undef vector. This produces a shuffle where the low
2865/// element of V2 is swizzled into the zero/undef vector, landing at element
2866/// 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 +00002867static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002868 bool isZero, bool HasSSE2,
2869 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002870 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002871 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002872 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2873 unsigned NumElems = VT.getVectorNumElements();
2874 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002875 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 // If this is the insertion idx, put the low elt of V2 here.
2877 MaskVec.push_back(i == Idx ? NumElems : i);
2878 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002879}
2880
Evan Chengf26ffe92008-05-29 08:22:04 +00002881/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2882/// a shuffle that is zero.
2883static
Nate Begeman9008ca62009-04-27 18:41:29 +00002884unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2885 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002886 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002888 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 int Idx = SVOp->getMaskElt(Index);
2890 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002891 ++NumZeros;
2892 continue;
2893 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002894 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002895 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002896 ++NumZeros;
2897 else
2898 break;
2899 }
2900 return NumZeros;
2901}
2902
2903/// isVectorShift - Returns true if the shuffle can be implemented as a
2904/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002905/// FIXME: split into pslldqi, psrldqi, palignr variants.
2906static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002907 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002908 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002909
2910 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002911 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002912 if (!NumZeros) {
2913 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002915 if (!NumZeros)
2916 return false;
2917 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002918 bool SeenV1 = false;
2919 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002920 for (int i = NumZeros; i < NumElems; ++i) {
2921 int Val = isLeft ? (i - NumZeros) : i;
2922 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2923 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002924 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002925 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002926 SeenV1 = true;
2927 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002928 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002929 SeenV2 = true;
2930 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002932 return false;
2933 }
2934 if (SeenV1 && SeenV2)
2935 return false;
2936
Nate Begeman9008ca62009-04-27 18:41:29 +00002937 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002938 ShAmt = NumZeros;
2939 return true;
2940}
2941
2942
Evan Chengc78d3b42006-04-24 18:01:45 +00002943/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2944///
Dan Gohman475871a2008-07-27 21:46:04 +00002945static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002946 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002947 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002948 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002949 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002950
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002951 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002952 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002953 bool First = true;
2954 for (unsigned i = 0; i < 16; ++i) {
2955 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2956 if (ThisIsNonZero && First) {
2957 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002958 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002959 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002960 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002961 First = false;
2962 }
2963
2964 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002965 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002966 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2967 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002968 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002969 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002970 }
2971 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002972 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2973 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002974 ThisElt, DAG.getConstant(8, MVT::i8));
2975 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002976 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00002977 } else
2978 ThisElt = LastElt;
2979
Gabor Greifba36cb52008-08-28 21:40:38 +00002980 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00002981 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002982 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002983 }
2984 }
2985
Dale Johannesenace16102009-02-03 19:33:06 +00002986 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00002987}
2988
Bill Wendlinga348c562007-03-22 18:42:45 +00002989/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002990///
Dan Gohman475871a2008-07-27 21:46:04 +00002991static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002992 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002993 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002994 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00002995 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002996
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002997 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002998 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002999 bool First = true;
3000 for (unsigned i = 0; i < 8; ++i) {
3001 bool isNonZero = (NonZeros & (1 << i)) != 0;
3002 if (isNonZero) {
3003 if (First) {
3004 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003005 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003006 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003007 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003008 First = false;
3009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003010 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003011 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003012 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003013 }
3014 }
3015
3016 return V;
3017}
3018
Evan Chengf26ffe92008-05-29 08:22:04 +00003019/// getVShift - Return a vector logical shift node.
3020///
Dan Gohman475871a2008-07-27 21:46:04 +00003021static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003022 unsigned NumBits, SelectionDAG &DAG,
3023 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003024 bool isMMX = VT.getSizeInBits() == 64;
3025 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003026 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003027 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3028 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3029 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003030 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003031}
3032
Dan Gohman475871a2008-07-27 21:46:04 +00003033SDValue
3034X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003035 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003036 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003037 if (ISD::isBuildVectorAllZeros(Op.getNode())
3038 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003039 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3040 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3041 // eliminated on x86-32 hosts.
3042 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3043 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003044
Gabor Greifba36cb52008-08-28 21:40:38 +00003045 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003046 return getOnesVector(Op.getValueType(), DAG, dl);
3047 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003048 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003049
Duncan Sands83ec4b62008-06-06 12:08:01 +00003050 MVT VT = Op.getValueType();
3051 MVT EVT = VT.getVectorElementType();
3052 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003053
3054 unsigned NumElems = Op.getNumOperands();
3055 unsigned NumZero = 0;
3056 unsigned NumNonZero = 0;
3057 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003058 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003059 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003060 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003061 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003062 if (Elt.getOpcode() == ISD::UNDEF)
3063 continue;
3064 Values.insert(Elt);
3065 if (Elt.getOpcode() != ISD::Constant &&
3066 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003067 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003068 if (isZeroNode(Elt))
3069 NumZero++;
3070 else {
3071 NonZeros |= (1 << i);
3072 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003073 }
3074 }
3075
Dan Gohman7f321562007-06-25 16:23:39 +00003076 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003077 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003078 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003079 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003080
Chris Lattner67f453a2008-03-09 05:42:06 +00003081 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003082 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003083 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003085
Chris Lattner62098042008-03-09 01:05:04 +00003086 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3087 // the value are obviously zero, truncate the value to i32 and do the
3088 // insertion that way. Only do this if the value is non-constant or if the
3089 // value is a constant being inserted into element 0. It is cheaper to do
3090 // a constant pool load than it is to do a movd + shuffle.
3091 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3092 (!IsAllConstants || Idx == 0)) {
3093 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3094 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3096 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003097
Chris Lattner62098042008-03-09 01:05:04 +00003098 // Truncate the value (which may itself be a constant) to i32, and
3099 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003100 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3101 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003102 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3103 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003104
Chris Lattner62098042008-03-09 01:05:04 +00003105 // Now we have our 32-bit value zero extended in the low element of
3106 // a vector. If Idx != 0, swizzle it into place.
3107 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 SmallVector<int, 4> Mask;
3109 Mask.push_back(Idx);
3110 for (unsigned i = 1; i != VecElts; ++i)
3111 Mask.push_back(i);
3112 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3113 DAG.getUNDEF(Item.getValueType()),
3114 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003115 }
Dale Johannesenace16102009-02-03 19:33:06 +00003116 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003117 }
3118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003119
Chris Lattner19f79692008-03-08 22:59:52 +00003120 // If we have a constant or non-constant insertion into the low element of
3121 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3122 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003123 // depending on what the source datatype is.
3124 if (Idx == 0) {
3125 if (NumZero == 0) {
3126 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3127 } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 ||
3128 (EVT == MVT::i64 && Subtarget->is64Bit())) {
3129 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3130 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3131 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3132 DAG);
3133 } else if (EVT == MVT::i16 || EVT == MVT::i8) {
3134 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3135 MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
3136 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3137 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3138 Subtarget->hasSSE2(), DAG);
3139 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3140 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003141 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003142
3143 // Is it a vector logical left shift?
3144 if (NumElems == 2 && Idx == 1 &&
3145 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003146 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003147 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003148 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003149 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003150 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003151 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003152
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003153 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003154 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003155
Chris Lattner19f79692008-03-08 22:59:52 +00003156 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3157 // is a non-constant being inserted into an element other than the low one,
3158 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3159 // movd/movss) to move this into the low element, then shuffle it into
3160 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003161 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003162 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003163
Evan Cheng0db9fe62006-04-25 20:13:52 +00003164 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003165 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3166 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003168 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 MaskVec.push_back(i == Idx ? 0 : 1);
3170 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003171 }
3172 }
3173
Chris Lattner67f453a2008-03-09 05:42:06 +00003174 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3175 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003176 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003177
Dan Gohmana3941172007-07-24 22:55:08 +00003178 // A vector full of immediates; various special cases are already
3179 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003180 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003181 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003182
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003183 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003184 if (EVTBits == 64) {
3185 if (NumNonZero == 1) {
3186 // One half is zero or undef.
3187 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003188 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003189 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003190 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3191 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003192 }
Dan Gohman475871a2008-07-27 21:46:04 +00003193 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003194 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003195
3196 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003197 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003198 SDValue V = LowerBuildVectorv16i8(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
Bill Wendling826f36f2007-03-28 00:57:11 +00003203 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003205 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003206 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003207 }
3208
3209 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003210 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003211 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003212 if (NumElems == 4 && NumZero > 0) {
3213 for (unsigned i = 0; i < 4; ++i) {
3214 bool isZero = !(NonZeros & (1 << i));
3215 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003216 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003217 else
Dale Johannesenace16102009-02-03 19:33:06 +00003218 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003219 }
3220
3221 for (unsigned i = 0; i < 2; ++i) {
3222 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3223 default: break;
3224 case 0:
3225 V[i] = V[i*2]; // Must be a zero vector.
3226 break;
3227 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003228 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003229 break;
3230 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003232 break;
3233 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003234 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003235 break;
3236 }
3237 }
3238
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003240 bool Reverse = (NonZeros & 0x3) == 2;
3241 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003243 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3244 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003245 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3246 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003247 }
3248
3249 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3251 // values to be inserted is equal to the number of elements, in which case
3252 // use the unpack code below in the hopes of matching the consecutive elts
3253 // load merge pattern for shuffles.
3254 // FIXME: We could probably just check that here directly.
3255 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3256 getSubtarget()->hasSSE41()) {
3257 V[0] = DAG.getUNDEF(VT);
3258 for (unsigned i = 0; i < NumElems; ++i)
3259 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3260 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3261 Op.getOperand(i), DAG.getIntPtrConstant(i));
3262 return V[0];
3263 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003264 // Expand into a number of unpckl*.
3265 // e.g. for v4f32
3266 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3267 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3268 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003269 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003270 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003271 NumElems >>= 1;
3272 while (NumElems != 0) {
3273 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003275 NumElems >>= 1;
3276 }
3277 return V[0];
3278 }
3279
Dan Gohman475871a2008-07-27 21:46:04 +00003280 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003281}
3282
Nate Begemanb9a47b82009-02-23 08:49:38 +00003283// v8i16 shuffles - Prefer shuffles in the following order:
3284// 1. [all] pshuflw, pshufhw, optional move
3285// 2. [ssse3] 1 x pshufb
3286// 3. [ssse3] 2 x pshufb + 1 x por
3287// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003288static
Nate Begeman9008ca62009-04-27 18:41:29 +00003289SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3290 SelectionDAG &DAG, X86TargetLowering &TLI) {
3291 SDValue V1 = SVOp->getOperand(0);
3292 SDValue V2 = SVOp->getOperand(1);
3293 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003294 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003295
Nate Begemanb9a47b82009-02-23 08:49:38 +00003296 // Determine if more than 1 of the words in each of the low and high quadwords
3297 // of the result come from the same quadword of one of the two inputs. Undef
3298 // mask values count as coming from any quadword, for better codegen.
3299 SmallVector<unsigned, 4> LoQuad(4);
3300 SmallVector<unsigned, 4> HiQuad(4);
3301 BitVector InputQuads(4);
3302 for (unsigned i = 0; i < 8; ++i) {
3303 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003304 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003305 MaskVals.push_back(EltIdx);
3306 if (EltIdx < 0) {
3307 ++Quad[0];
3308 ++Quad[1];
3309 ++Quad[2];
3310 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003311 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003312 }
3313 ++Quad[EltIdx / 4];
3314 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003315 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003316
Nate Begemanb9a47b82009-02-23 08:49:38 +00003317 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003318 unsigned MaxQuad = 1;
3319 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003320 if (LoQuad[i] > MaxQuad) {
3321 BestLoQuad = i;
3322 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003323 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003324 }
3325
Nate Begemanb9a47b82009-02-23 08:49:38 +00003326 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003327 MaxQuad = 1;
3328 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003329 if (HiQuad[i] > MaxQuad) {
3330 BestHiQuad = i;
3331 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003332 }
3333 }
3334
Nate Begemanb9a47b82009-02-23 08:49:38 +00003335 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3336 // of the two input vectors, shuffle them into one input vector so only a
3337 // single pshufb instruction is necessary. If There are more than 2 input
3338 // quads, disable the next transformation since it does not help SSSE3.
3339 bool V1Used = InputQuads[0] || InputQuads[1];
3340 bool V2Used = InputQuads[2] || InputQuads[3];
3341 if (TLI.getSubtarget()->hasSSSE3()) {
3342 if (InputQuads.count() == 2 && V1Used && V2Used) {
3343 BestLoQuad = InputQuads.find_first();
3344 BestHiQuad = InputQuads.find_next(BestLoQuad);
3345 }
3346 if (InputQuads.count() > 2) {
3347 BestLoQuad = -1;
3348 BestHiQuad = -1;
3349 }
3350 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003351
Nate Begemanb9a47b82009-02-23 08:49:38 +00003352 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3353 // the shuffle mask. If a quad is scored as -1, that means that it contains
3354 // words from all 4 input quadwords.
3355 SDValue NewV;
3356 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 SmallVector<int, 8> MaskV;
3358 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3359 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3360 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3361 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3362 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003363 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003364
Nate Begemanb9a47b82009-02-23 08:49:38 +00003365 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3366 // source words for the shuffle, to aid later transformations.
3367 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003368 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003369 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003370 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003371 if (idx != (int)i)
3372 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003373 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003374 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003375 AllWordsInNewV = false;
3376 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003377 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003378
Nate Begemanb9a47b82009-02-23 08:49:38 +00003379 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3380 if (AllWordsInNewV) {
3381 for (int i = 0; i != 8; ++i) {
3382 int idx = MaskVals[i];
3383 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003384 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003385 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3386 if ((idx != i) && idx < 4)
3387 pshufhw = false;
3388 if ((idx != i) && idx > 3)
3389 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003390 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003391 V1 = NewV;
3392 V2Used = false;
3393 BestLoQuad = 0;
3394 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003395 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003396
Nate Begemanb9a47b82009-02-23 08:49:38 +00003397 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3398 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003399 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003400 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3401 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003402 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003403 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003404
3405 // If we have SSSE3, and all words of the result are from 1 input vector,
3406 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3407 // is present, fall back to case 4.
3408 if (TLI.getSubtarget()->hasSSSE3()) {
3409 SmallVector<SDValue,16> pshufbMask;
3410
3411 // If we have elements from both input vectors, set the high bit of the
3412 // shuffle mask element to zero out elements that come from V2 in the V1
3413 // mask, and elements that come from V1 in the V2 mask, so that the two
3414 // results can be OR'd together.
3415 bool TwoInputs = V1Used && V2Used;
3416 for (unsigned i = 0; i != 8; ++i) {
3417 int EltIdx = MaskVals[i] * 2;
3418 if (TwoInputs && (EltIdx >= 16)) {
3419 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3420 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3421 continue;
3422 }
3423 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3424 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3425 }
3426 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3427 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003428 DAG.getNode(ISD::BUILD_VECTOR, dl,
3429 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003430 if (!TwoInputs)
3431 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3432
3433 // Calculate the shuffle mask for the second input, shuffle it, and
3434 // OR it with the first shuffled input.
3435 pshufbMask.clear();
3436 for (unsigned i = 0; i != 8; ++i) {
3437 int EltIdx = MaskVals[i] * 2;
3438 if (EltIdx < 16) {
3439 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3440 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3441 continue;
3442 }
3443 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3444 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3445 }
3446 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3447 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003448 DAG.getNode(ISD::BUILD_VECTOR, dl,
3449 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003450 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3451 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3452 }
3453
3454 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3455 // and update MaskVals with new element order.
3456 BitVector InOrder(8);
3457 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003459 for (int i = 0; i != 4; ++i) {
3460 int idx = MaskVals[i];
3461 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003463 InOrder.set(i);
3464 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003466 InOrder.set(i);
3467 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003468 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003469 }
3470 }
3471 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 MaskV.push_back(i);
3473 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3474 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003475 }
3476
3477 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3478 // and update MaskVals with the new element order.
3479 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003481 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003483 for (unsigned i = 4; i != 8; ++i) {
3484 int idx = MaskVals[i];
3485 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003486 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003487 InOrder.set(i);
3488 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003489 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003490 InOrder.set(i);
3491 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003492 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003493 }
3494 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3496 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003497 }
3498
3499 // In case BestHi & BestLo were both -1, which means each quadword has a word
3500 // from each of the four input quadwords, calculate the InOrder bitvector now
3501 // before falling through to the insert/extract cleanup.
3502 if (BestLoQuad == -1 && BestHiQuad == -1) {
3503 NewV = V1;
3504 for (int i = 0; i != 8; ++i)
3505 if (MaskVals[i] < 0 || MaskVals[i] == i)
3506 InOrder.set(i);
3507 }
3508
3509 // The other elements are put in the right place using pextrw and pinsrw.
3510 for (unsigned i = 0; i != 8; ++i) {
3511 if (InOrder[i])
3512 continue;
3513 int EltIdx = MaskVals[i];
3514 if (EltIdx < 0)
3515 continue;
3516 SDValue ExtOp = (EltIdx < 8)
3517 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3518 DAG.getIntPtrConstant(EltIdx))
3519 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3520 DAG.getIntPtrConstant(EltIdx - 8));
3521 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3522 DAG.getIntPtrConstant(i));
3523 }
3524 return NewV;
3525}
3526
3527// v16i8 shuffles - Prefer shuffles in the following order:
3528// 1. [ssse3] 1 x pshufb
3529// 2. [ssse3] 2 x pshufb + 1 x por
3530// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3531static
Nate Begeman9008ca62009-04-27 18:41:29 +00003532SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3533 SelectionDAG &DAG, X86TargetLowering &TLI) {
3534 SDValue V1 = SVOp->getOperand(0);
3535 SDValue V2 = SVOp->getOperand(1);
3536 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003537 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003538 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003539
3540 // If we have SSSE3, case 1 is generated when all result bytes come from
3541 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3542 // present, fall back to case 3.
3543 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3544 bool V1Only = true;
3545 bool V2Only = true;
3546 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003547 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003548 if (EltIdx < 0)
3549 continue;
3550 if (EltIdx < 16)
3551 V2Only = false;
3552 else
3553 V1Only = false;
3554 }
3555
3556 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3557 if (TLI.getSubtarget()->hasSSSE3()) {
3558 SmallVector<SDValue,16> pshufbMask;
3559
3560 // If all result elements are from one input vector, then only translate
3561 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3562 //
3563 // Otherwise, we have elements from both input vectors, and must zero out
3564 // elements that come from V2 in the first mask, and V1 in the second mask
3565 // so that we can OR them together.
3566 bool TwoInputs = !(V1Only || V2Only);
3567 for (unsigned i = 0; i != 16; ++i) {
3568 int EltIdx = MaskVals[i];
3569 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3570 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3571 continue;
3572 }
3573 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3574 }
3575 // If all the elements are from V2, assign it to V1 and return after
3576 // building the first pshufb.
3577 if (V2Only)
3578 V1 = V2;
3579 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003580 DAG.getNode(ISD::BUILD_VECTOR, dl,
3581 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003582 if (!TwoInputs)
3583 return V1;
3584
3585 // Calculate the shuffle mask for the second input, shuffle it, and
3586 // OR it with the first shuffled input.
3587 pshufbMask.clear();
3588 for (unsigned i = 0; i != 16; ++i) {
3589 int EltIdx = MaskVals[i];
3590 if (EltIdx < 16) {
3591 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3592 continue;
3593 }
3594 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3595 }
3596 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003597 DAG.getNode(ISD::BUILD_VECTOR, dl,
3598 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003599 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3600 }
3601
3602 // No SSSE3 - Calculate in place words and then fix all out of place words
3603 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3604 // the 16 different words that comprise the two doublequadword input vectors.
3605 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3606 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3607 SDValue NewV = V2Only ? V2 : V1;
3608 for (int i = 0; i != 8; ++i) {
3609 int Elt0 = MaskVals[i*2];
3610 int Elt1 = MaskVals[i*2+1];
3611
3612 // This word of the result is all undef, skip it.
3613 if (Elt0 < 0 && Elt1 < 0)
3614 continue;
3615
3616 // This word of the result is already in the correct place, skip it.
3617 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3618 continue;
3619 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3620 continue;
3621
3622 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3623 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3624 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003625
3626 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3627 // using a single extract together, load it and store it.
3628 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3629 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3630 DAG.getIntPtrConstant(Elt1 / 2));
3631 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3632 DAG.getIntPtrConstant(i));
3633 continue;
3634 }
3635
Nate Begemanb9a47b82009-02-23 08:49:38 +00003636 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003637 // source byte is not also odd, shift the extracted word left 8 bits
3638 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003639 if (Elt1 >= 0) {
3640 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3641 DAG.getIntPtrConstant(Elt1 / 2));
3642 if ((Elt1 & 1) == 0)
3643 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3644 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003645 else if (Elt0 >= 0)
3646 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3647 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003648 }
3649 // If Elt0 is defined, extract it from the appropriate source. If the
3650 // source byte is not also even, shift the extracted word right 8 bits. If
3651 // Elt1 was also defined, OR the extracted values together before
3652 // inserting them in the result.
3653 if (Elt0 >= 0) {
3654 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3655 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3656 if ((Elt0 & 1) != 0)
3657 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3658 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003659 else if (Elt1 >= 0)
3660 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3661 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003662 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3663 : InsElt0;
3664 }
3665 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3666 DAG.getIntPtrConstant(i));
3667 }
3668 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003669}
3670
Evan Cheng7a831ce2007-12-15 03:00:47 +00003671/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3672/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3673/// done when every pair / quad of shuffle mask elements point to elements in
3674/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003675/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3676static
Nate Begeman9008ca62009-04-27 18:41:29 +00003677SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3678 SelectionDAG &DAG,
3679 TargetLowering &TLI, DebugLoc dl) {
3680 MVT VT = SVOp->getValueType(0);
3681 SDValue V1 = SVOp->getOperand(0);
3682 SDValue V2 = SVOp->getOperand(1);
3683 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003684 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003685 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003686 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003687 MVT NewVT = MaskVT;
3688 switch (VT.getSimpleVT()) {
3689 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003690 case MVT::v4f32: NewVT = MVT::v2f64; break;
3691 case MVT::v4i32: NewVT = MVT::v2i64; break;
3692 case MVT::v8i16: NewVT = MVT::v4i32; break;
3693 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003694 }
3695
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003696 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003697 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003698 NewVT = MVT::v2i64;
3699 else
3700 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003701 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003702 int Scale = NumElems / NewWidth;
3703 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003704 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003705 int StartIdx = -1;
3706 for (int j = 0; j < Scale; ++j) {
3707 int EltIdx = SVOp->getMaskElt(i+j);
3708 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003709 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003711 StartIdx = EltIdx - (EltIdx % Scale);
3712 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003713 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003714 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 if (StartIdx == -1)
3716 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003717 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003718 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003719 }
3720
Dale Johannesenace16102009-02-03 19:33:06 +00003721 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3722 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003723 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003724}
3725
Evan Chengd880b972008-05-09 21:53:03 +00003726/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003727///
Dan Gohman475871a2008-07-27 21:46:04 +00003728static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003729 SDValue SrcOp, SelectionDAG &DAG,
3730 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003731 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3732 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003733 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003734 LD = dyn_cast<LoadSDNode>(SrcOp);
3735 if (!LD) {
3736 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3737 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003738 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003739 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3740 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3741 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3742 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3743 // PR2108
3744 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003745 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3746 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3747 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3748 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003749 SrcOp.getOperand(0)
3750 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003751 }
3752 }
3753 }
3754
Dale Johannesenace16102009-02-03 19:33:06 +00003755 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3756 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003757 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003758 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003759}
3760
Evan Chengace3c172008-07-22 21:13:36 +00003761/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3762/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003763static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003764LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3765 SDValue V1 = SVOp->getOperand(0);
3766 SDValue V2 = SVOp->getOperand(1);
3767 DebugLoc dl = SVOp->getDebugLoc();
3768 MVT VT = SVOp->getValueType(0);
3769
Evan Chengace3c172008-07-22 21:13:36 +00003770 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003771 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003772 SmallVector<int, 8> Mask1(4U, -1);
3773 SmallVector<int, 8> PermMask;
3774 SVOp->getMask(PermMask);
3775
Evan Chengace3c172008-07-22 21:13:36 +00003776 unsigned NumHi = 0;
3777 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003778 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003779 int Idx = PermMask[i];
3780 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003781 Locs[i] = std::make_pair(-1, -1);
3782 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003783 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3784 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003785 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003787 NumLo++;
3788 } else {
3789 Locs[i] = std::make_pair(1, NumHi);
3790 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003791 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003792 NumHi++;
3793 }
3794 }
3795 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003796
Evan Chengace3c172008-07-22 21:13:36 +00003797 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003798 // If no more than two elements come from either vector. This can be
3799 // implemented with two shuffles. First shuffle gather the elements.
3800 // The second shuffle, which takes the first shuffle as both of its
3801 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003802 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003803
Nate Begeman9008ca62009-04-27 18:41:29 +00003804 SmallVector<int, 8> Mask2(4U, -1);
3805
Evan Chengace3c172008-07-22 21:13:36 +00003806 for (unsigned i = 0; i != 4; ++i) {
3807 if (Locs[i].first == -1)
3808 continue;
3809 else {
3810 unsigned Idx = (i < 2) ? 0 : 4;
3811 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003812 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003813 }
3814 }
3815
Nate Begeman9008ca62009-04-27 18:41:29 +00003816 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003817 } else if (NumLo == 3 || NumHi == 3) {
3818 // Otherwise, we must have three elements from one vector, call it X, and
3819 // one element from the other, call it Y. First, use a shufps to build an
3820 // intermediate vector with the one element from Y and the element from X
3821 // that will be in the same half in the final destination (the indexes don't
3822 // matter). Then, use a shufps to build the final vector, taking the half
3823 // containing the element from Y from the intermediate, and the other half
3824 // from X.
3825 if (NumHi == 3) {
3826 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003828 std::swap(V1, V2);
3829 }
3830
3831 // Find the element from V2.
3832 unsigned HiIndex;
3833 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003834 int Val = PermMask[HiIndex];
3835 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003836 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003837 if (Val >= 4)
3838 break;
3839 }
3840
Nate Begeman9008ca62009-04-27 18:41:29 +00003841 Mask1[0] = PermMask[HiIndex];
3842 Mask1[1] = -1;
3843 Mask1[2] = PermMask[HiIndex^1];
3844 Mask1[3] = -1;
3845 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003846
3847 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 Mask1[0] = PermMask[0];
3849 Mask1[1] = PermMask[1];
3850 Mask1[2] = HiIndex & 1 ? 6 : 4;
3851 Mask1[3] = HiIndex & 1 ? 4 : 6;
3852 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003853 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003854 Mask1[0] = HiIndex & 1 ? 2 : 0;
3855 Mask1[1] = HiIndex & 1 ? 0 : 2;
3856 Mask1[2] = PermMask[2];
3857 Mask1[3] = PermMask[3];
3858 if (Mask1[2] >= 0)
3859 Mask1[2] += 4;
3860 if (Mask1[3] >= 0)
3861 Mask1[3] += 4;
3862 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003863 }
Evan Chengace3c172008-07-22 21:13:36 +00003864 }
3865
3866 // Break it into (shuffle shuffle_hi, shuffle_lo).
3867 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003868 SmallVector<int,8> LoMask(4U, -1);
3869 SmallVector<int,8> HiMask(4U, -1);
3870
3871 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003872 unsigned MaskIdx = 0;
3873 unsigned LoIdx = 0;
3874 unsigned HiIdx = 2;
3875 for (unsigned i = 0; i != 4; ++i) {
3876 if (i == 2) {
3877 MaskPtr = &HiMask;
3878 MaskIdx = 1;
3879 LoIdx = 0;
3880 HiIdx = 2;
3881 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003882 int Idx = PermMask[i];
3883 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003884 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003885 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003886 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003888 LoIdx++;
3889 } else {
3890 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003891 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003892 HiIdx++;
3893 }
3894 }
3895
Nate Begeman9008ca62009-04-27 18:41:29 +00003896 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3897 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3898 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003899 for (unsigned i = 0; i != 4; ++i) {
3900 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003902 } else {
3903 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003905 }
3906 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003908}
3909
Dan Gohman475871a2008-07-27 21:46:04 +00003910SDValue
3911X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003913 SDValue V1 = Op.getOperand(0);
3914 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003915 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003916 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003917 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003918 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003919 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3920 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003921 bool V1IsSplat = false;
3922 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003923
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003925 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003926
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 // Promote splats to v4f32.
3928 if (SVOp->isSplat()) {
3929 if (isMMX || NumElems < 4)
3930 return Op;
3931 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003932 }
3933
Evan Cheng7a831ce2007-12-15 03:00:47 +00003934 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3935 // do it!
3936 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003938 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003939 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003940 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003941 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3942 // FIXME: Figure out a cleaner way to do this.
3943 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003944 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003945 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003946 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3948 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3949 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003950 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003951 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3953 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003954 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003956 }
3957 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003958
3959 if (X86::isPSHUFDMask(SVOp))
3960 return Op;
3961
Evan Chengf26ffe92008-05-29 08:22:04 +00003962 // Check if this can be converted into a logical shift.
3963 bool isLeft = false;
3964 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003965 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 bool isShift = getSubtarget()->hasSSE2() &&
3967 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003968 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003969 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003970 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003971 MVT EVT = VT.getVectorElementType();
3972 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003973 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003974 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003975
3976 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003977 if (V1IsUndef)
3978 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003979 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003980 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003981 if (!isMMX)
3982 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003983 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003984
3985 // FIXME: fold these into legal mask.
3986 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
3987 X86::isMOVSLDUPMask(SVOp) ||
3988 X86::isMOVHLPSMask(SVOp) ||
3989 X86::isMOVHPMask(SVOp) ||
3990 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003991 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003992
Nate Begeman9008ca62009-04-27 18:41:29 +00003993 if (ShouldXformToMOVHLPS(SVOp) ||
3994 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
3995 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003996
Evan Chengf26ffe92008-05-29 08:22:04 +00003997 if (isShift) {
3998 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003999 MVT EVT = VT.getVectorElementType();
4000 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004001 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004002 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004003
Evan Cheng9eca5e82006-10-25 21:49:50 +00004004 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004005 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4006 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004007 V1IsSplat = isSplatVector(V1.getNode());
4008 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004009
Chris Lattner8a594482007-11-25 00:24:49 +00004010 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004011 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004012 Op = CommuteVectorShuffle(SVOp, DAG);
4013 SVOp = cast<ShuffleVectorSDNode>(Op);
4014 V1 = SVOp->getOperand(0);
4015 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004016 std::swap(V1IsSplat, V2IsSplat);
4017 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004018 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004019 }
4020
Nate Begeman9008ca62009-04-27 18:41:29 +00004021 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4022 // Shuffling low element of v1 into undef, just return v1.
4023 if (V2IsUndef)
4024 return V1;
4025 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4026 // the instruction selector will not match, so get a canonical MOVL with
4027 // swapped operands to undo the commute.
4028 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004029 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004030
Nate Begeman9008ca62009-04-27 18:41:29 +00004031 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4032 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4033 X86::isUNPCKLMask(SVOp) ||
4034 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004035 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004036
Evan Cheng9bbbb982006-10-25 20:48:19 +00004037 if (V2IsSplat) {
4038 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004039 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004040 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004041 SDValue NewMask = NormalizeMask(SVOp, DAG);
4042 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4043 if (NSVOp != SVOp) {
4044 if (X86::isUNPCKLMask(NSVOp, true)) {
4045 return NewMask;
4046 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4047 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004048 }
4049 }
4050 }
4051
Evan Cheng9eca5e82006-10-25 21:49:50 +00004052 if (Commuted) {
4053 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004054 // FIXME: this seems wrong.
4055 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4056 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4057 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4058 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4059 X86::isUNPCKLMask(NewSVOp) ||
4060 X86::isUNPCKHMask(NewSVOp))
4061 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004062 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063
Nate Begemanb9a47b82009-02-23 08:49:38 +00004064 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004065
4066 // Normalize the node to match x86 shuffle ops if needed
4067 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4068 return CommuteVectorShuffle(SVOp, DAG);
4069
4070 // Check for legal shuffle and return?
4071 SmallVector<int, 16> PermMask;
4072 SVOp->getMask(PermMask);
4073 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004074 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004075
Evan Cheng14b32e12007-12-11 01:46:18 +00004076 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4077 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004078 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004079 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004080 return NewOp;
4081 }
4082
Nate Begemanb9a47b82009-02-23 08:49:38 +00004083 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004084 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 if (NewOp.getNode())
4086 return NewOp;
4087 }
4088
Evan Chengace3c172008-07-22 21:13:36 +00004089 // Handle all 4 wide cases with a number of shuffles except for MMX.
4090 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004091 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092
Dan Gohman475871a2008-07-27 21:46:04 +00004093 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004094}
4095
Dan Gohman475871a2008-07-27 21:46:04 +00004096SDValue
4097X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004098 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004099 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004100 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004102 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004103 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004104 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004105 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004106 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004107 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004108 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4109 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4110 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004111 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4112 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4113 DAG.getNode(ISD::BIT_CONVERT, dl,
4114 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004115 Op.getOperand(0)),
4116 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004117 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004118 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004119 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004120 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004121 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004122 } else if (VT == MVT::f32) {
4123 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4124 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004125 // result has a single use which is a store or a bitcast to i32. And in
4126 // the case of a store, it's not worth it if the index is a constant 0,
4127 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004128 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004129 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004130 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004131 if ((User->getOpcode() != ISD::STORE ||
4132 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4133 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004134 (User->getOpcode() != ISD::BIT_CONVERT ||
4135 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004136 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004137 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004138 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004139 Op.getOperand(0)),
4140 Op.getOperand(1));
4141 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004142 } else if (VT == MVT::i32) {
4143 // ExtractPS works with constant index.
4144 if (isa<ConstantSDNode>(Op.getOperand(1)))
4145 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004146 }
Dan Gohman475871a2008-07-27 21:46:04 +00004147 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004148}
4149
4150
Dan Gohman475871a2008-07-27 21:46:04 +00004151SDValue
4152X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004153 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004154 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155
Evan Cheng62a3f152008-03-24 21:52:23 +00004156 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004157 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004158 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004159 return Res;
4160 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004161
Duncan Sands83ec4b62008-06-06 12:08:01 +00004162 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004163 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004165 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004167 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004168 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004169 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4170 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004171 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004172 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004173 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004174 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004175 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004176 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004177 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004178 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004179 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004180 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004182 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004183 if (Idx == 0)
4184 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004185
Evan Cheng0db9fe62006-04-25 20:13:52 +00004186 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 int Mask[4] = { Idx, -1, -1, -1 };
4188 MVT VVT = Op.getOperand(0).getValueType();
4189 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4190 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004191 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004192 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004193 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004194 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4195 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4196 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004197 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004198 if (Idx == 0)
4199 return Op;
4200
4201 // UNPCKHPD the element to the lowest double word, then movsd.
4202 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4203 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 int Mask[2] = { 1, -1 };
4205 MVT VVT = Op.getOperand(0).getValueType();
4206 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4207 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004208 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004209 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004210 }
4211
Dan Gohman475871a2008-07-27 21:46:04 +00004212 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004213}
4214
Dan Gohman475871a2008-07-27 21:46:04 +00004215SDValue
4216X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004217 MVT VT = Op.getValueType();
4218 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004219 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004220
Dan Gohman475871a2008-07-27 21:46:04 +00004221 SDValue N0 = Op.getOperand(0);
4222 SDValue N1 = Op.getOperand(1);
4223 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004224
Dan Gohmanef521f12008-08-14 22:53:18 +00004225 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4226 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004227 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004228 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004229 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4230 // argument.
4231 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004232 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004233 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004234 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004235 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004236 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004237 // Bits [7:6] of the constant are the source select. This will always be
4238 // zero here. The DAG Combiner may combine an extract_elt index into these
4239 // bits. For example (insert (extract, 3), 2) could be matched by putting
4240 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004241 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004242 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004243 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004244 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004245 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004246 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004247 } else if (EVT == MVT::i32) {
4248 // InsertPS works with constant index.
4249 if (isa<ConstantSDNode>(N2))
4250 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004251 }
Dan Gohman475871a2008-07-27 21:46:04 +00004252 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004253}
4254
Dan Gohman475871a2008-07-27 21:46:04 +00004255SDValue
4256X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004257 MVT VT = Op.getValueType();
4258 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004259
4260 if (Subtarget->hasSSE41())
4261 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4262
Evan Cheng794405e2007-12-12 07:55:34 +00004263 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004264 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004265
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004266 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004267 SDValue N0 = Op.getOperand(0);
4268 SDValue N1 = Op.getOperand(1);
4269 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004270
Eli Friedman30e71eb2009-06-06 06:32:50 +00004271 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004272 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4273 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004274 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004275 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004276 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004277 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004278 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004279 }
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004281}
4282
Dan Gohman475871a2008-07-27 21:46:04 +00004283SDValue
4284X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004285 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004286 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004287 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4288 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4289 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004290 Op.getOperand(0))));
4291
Dale Johannesenace16102009-02-03 19:33:06 +00004292 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004293 MVT VT = MVT::v2i32;
4294 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004295 default: break;
4296 case MVT::v16i8:
4297 case MVT::v8i16:
4298 VT = MVT::v4i32;
4299 break;
4300 }
Dale Johannesenace16102009-02-03 19:33:06 +00004301 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4302 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004303}
4304
Bill Wendling056292f2008-09-16 21:48:12 +00004305// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4306// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4307// one of the above mentioned nodes. It has to be wrapped because otherwise
4308// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4309// be used to form addressing mode. These wrapped nodes will be selected
4310// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004311SDValue
4312X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004313 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004314 // FIXME there isn't really any debug info here, should come from the parent
4315 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004316 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4317 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004318 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004319 // With PIC, the address is actually $g + Offset.
4320 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4321 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004322 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004323 DAG.getNode(X86ISD::GlobalBaseReg,
4324 DebugLoc::getUnknownLoc(),
4325 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004326 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004327 }
4328
4329 return Result;
4330}
4331
Dan Gohman475871a2008-07-27 21:46:04 +00004332SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004333X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004334 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004335 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004336 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4337 bool ExtraLoadRequired =
4338 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4339
4340 // Create the TargetGlobalAddress node, folding in the constant
4341 // offset if it is legal.
4342 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004343 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004344 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4345 Offset = 0;
4346 } else
4347 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004348 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004349
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004350 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004351 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004352 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4353 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004354 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004355 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004356
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004357 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4358 // load the value at address GV, not the value of GV itself. This means that
4359 // the GlobalAddress must be in the base or index register of the address, not
4360 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004361 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004362 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004363 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004364 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004365
Dan Gohman6520e202008-10-18 02:06:02 +00004366 // If there was a non-zero offset that we didn't fold, create an explicit
4367 // addition for it.
4368 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004369 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004370 DAG.getConstant(Offset, getPointerTy()));
4371
Evan Cheng0db9fe62006-04-25 20:13:52 +00004372 return Result;
4373}
4374
Evan Chengda43bcf2008-09-24 00:05:32 +00004375SDValue
4376X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4377 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004378 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004379 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004380}
4381
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004382static SDValue
4383GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004384 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004385 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4386 DebugLoc dl = GA->getDebugLoc();
4387 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4388 GA->getValueType(0),
4389 GA->getOffset());
4390 if (InFlag) {
4391 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004392 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004393 } else {
4394 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004395 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004396 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004397 SDValue Flag = Chain.getValue(1);
4398 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004399}
4400
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004401// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004402static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004403LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004404 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004405 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004406 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4407 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004408 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004409 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004410 PtrVT), InFlag);
4411 InFlag = Chain.getValue(1);
4412
Rafael Espindola15f1b662009-04-24 12:59:40 +00004413 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004414}
4415
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004416// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004417static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004418LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004419 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004420 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004421}
4422
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004423// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4424// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004425static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004426 const MVT PtrVT, TLSModel::Model model,
4427 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004428 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004429 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004430 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4431 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004432 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4433 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004434
4435 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4436 NULL, 0);
4437
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004438 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4439 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00004440 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4441 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004442 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004443
Rafael Espindola9a580232009-02-27 13:37:18 +00004444 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004445 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004446 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004447
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004448 // The address of the thread local variable is the add of the thread
4449 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004450 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004451}
4452
Dan Gohman475871a2008-07-27 21:46:04 +00004453SDValue
4454X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004455 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004456 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004457 assert(Subtarget->isTargetELF() &&
4458 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004459 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004460 GlobalValue *GV = GA->getGlobal();
4461 TLSModel::Model model =
4462 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004463 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004464 switch (model) {
4465 case TLSModel::GeneralDynamic:
4466 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004467 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004468
4469 case TLSModel::InitialExec:
4470 case TLSModel::LocalExec:
4471 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004472 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004473 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004474 switch (model) {
4475 case TLSModel::GeneralDynamic:
4476 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004477 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004478
4479 case TLSModel::InitialExec:
4480 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004481 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004482 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004483 }
Chris Lattner5867de12009-04-01 22:14:45 +00004484 assert(0 && "Unreachable");
4485 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004486}
4487
Dan Gohman475871a2008-07-27 21:46:04 +00004488SDValue
4489X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004490 // FIXME there isn't really any debug info here
4491 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004492 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4493 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004494 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004495 // With PIC, the address is actually $g + Offset.
4496 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4497 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004498 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004499 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004500 DebugLoc::getUnknownLoc(),
4501 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004502 Result);
4503 }
4504
4505 return Result;
4506}
4507
Dan Gohman475871a2008-07-27 21:46:04 +00004508SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004509 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004510 // FIXME there isn't really any debug into here
4511 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004512 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004513 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004514 // With PIC, the address is actually $g + Offset.
4515 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4516 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004517 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004518 DAG.getNode(X86ISD::GlobalBaseReg,
4519 DebugLoc::getUnknownLoc(),
4520 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004521 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522 }
4523
4524 return Result;
4525}
4526
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004527/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004528/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004529SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004530 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004531 MVT VT = Op.getValueType();
4532 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004533 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004534 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004535 SDValue ShOpLo = Op.getOperand(0);
4536 SDValue ShOpHi = Op.getOperand(1);
4537 SDValue ShAmt = Op.getOperand(2);
4538 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004539 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004540 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004541 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004542
Dan Gohman475871a2008-07-27 21:46:04 +00004543 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004544 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004545 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4546 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004547 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004548 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4549 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004550 }
Evan Chenge3413162006-01-09 18:33:28 +00004551
Dale Johannesenace16102009-02-03 19:33:06 +00004552 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004553 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004554 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004555 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004556
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue Hi, Lo;
4558 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4559 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4560 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004561
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004562 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004563 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4564 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004565 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004566 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4567 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004568 }
4569
Dan Gohman475871a2008-07-27 21:46:04 +00004570 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004571 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004572}
Evan Chenga3195e82006-01-12 22:54:21 +00004573
Dan Gohman475871a2008-07-27 21:46:04 +00004574SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004575 MVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004576
4577 if (SrcVT.isVector()) {
4578 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
4579 return Op;
4580 }
4581 return SDValue();
4582 }
4583
Duncan Sands8e4eb092008-06-08 20:54:56 +00004584 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004585 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004586
Eli Friedman36df4992009-05-27 00:47:34 +00004587 // These are really Legal; return the operand so the caller accepts it as
4588 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004589 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004590 return Op;
4591 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4592 Subtarget->is64Bit()) {
4593 return Op;
4594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004595
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004596 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004597 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 MachineFunction &MF = DAG.getMachineFunction();
4599 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004600 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004601 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004602 StackSlot,
4603 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004604 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4605}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004606
Eli Friedman948e95a2009-05-23 09:59:16 +00004607SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4608 SDValue StackSlot,
4609 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004610 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004611 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004612 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004613 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004614 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004615 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4616 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004617 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004618 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004619 Ops.push_back(Chain);
4620 Ops.push_back(StackSlot);
4621 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004622 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004623 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004624
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004625 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004626 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004627 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004628
4629 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4630 // shouldn't be necessary except that RFP cannot be live across
4631 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004632 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004634 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004635 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004636 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004637 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004638 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004639 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004640 Ops.push_back(DAG.getValueType(Op.getValueType()));
4641 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004642 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4643 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004644 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004645 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004646
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647 return Result;
4648}
4649
Bill Wendling8b8a6362009-01-17 03:56:04 +00004650// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4651SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4652 // This algorithm is not obvious. Here it is in C code, more or less:
4653 /*
4654 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4655 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4656 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004657
Bill Wendling8b8a6362009-01-17 03:56:04 +00004658 // Copy ints to xmm registers.
4659 __m128i xh = _mm_cvtsi32_si128( hi );
4660 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004661
Bill Wendling8b8a6362009-01-17 03:56:04 +00004662 // Combine into low half of a single xmm register.
4663 __m128i x = _mm_unpacklo_epi32( xh, xl );
4664 __m128d d;
4665 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004666
Bill Wendling8b8a6362009-01-17 03:56:04 +00004667 // Merge in appropriate exponents to give the integer bits the right
4668 // magnitude.
4669 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004670
Bill Wendling8b8a6362009-01-17 03:56:04 +00004671 // Subtract away the biases to deal with the IEEE-754 double precision
4672 // implicit 1.
4673 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004674
Bill Wendling8b8a6362009-01-17 03:56:04 +00004675 // All conversions up to here are exact. The correctly rounded result is
4676 // calculated using the current rounding mode using the following
4677 // horizontal add.
4678 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4679 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4680 // store doesn't really need to be here (except
4681 // maybe to zero the other double)
4682 return sd;
4683 }
4684 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004685
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004686 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004687
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004688 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004689 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004690 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4691 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4692 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4693 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4694 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004695 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004696
Bill Wendling8b8a6362009-01-17 03:56:04 +00004697 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004698 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4699 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4700 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004701 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004702
Dale Johannesenace16102009-02-03 19:33:06 +00004703 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4704 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004705 Op.getOperand(0),
4706 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004707 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4708 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004709 Op.getOperand(0),
4710 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004712 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004713 PseudoSourceValue::getConstantPool(), 0,
4714 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004716 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4717 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004718 PseudoSourceValue::getConstantPool(), 0,
4719 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004720 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004721
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004722 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004723 int ShufMask[2] = { 1, -1 };
4724 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4725 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004726 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4727 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004728 DAG.getIntPtrConstant(0));
4729}
4730
Bill Wendling8b8a6362009-01-17 03:56:04 +00004731// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4732SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004733 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004734 // FP constant to bias correct the final result.
4735 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4736 MVT::f64);
4737
4738 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004739 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4740 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004741 Op.getOperand(0),
4742 DAG.getIntPtrConstant(0)));
4743
Dale Johannesenace16102009-02-03 19:33:06 +00004744 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4745 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004746 DAG.getIntPtrConstant(0));
4747
4748 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004749 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4750 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4751 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004752 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004753 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4754 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004755 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004756 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4757 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004758 DAG.getIntPtrConstant(0));
4759
4760 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004761 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004762
4763 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004764 MVT DestVT = Op.getValueType();
4765
4766 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004767 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004768 DAG.getIntPtrConstant(0));
4769 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004770 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004771 }
4772
4773 // Handle final rounding.
4774 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004775}
4776
4777SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004778 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004779 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004780
Evan Chenga06ec9e2009-01-19 08:08:22 +00004781 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4782 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4783 // the optimization here.
4784 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004785 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004786
4787 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004788 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004789 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004790 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004791 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004792
Bill Wendling8b8a6362009-01-17 03:56:04 +00004793 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004794 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004795 return LowerUINT_TO_FP_i32(Op, DAG);
4796 }
4797
Eli Friedman948e95a2009-05-23 09:59:16 +00004798 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4799
4800 // Make a 64-bit buffer, and use it to build an FILD.
4801 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4802 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4803 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4804 getPointerTy(), StackSlot, WordOff);
4805 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4806 StackSlot, NULL, 0);
4807 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4808 OffsetSlot, NULL, 0);
4809 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004810}
4811
Dan Gohman475871a2008-07-27 21:46:04 +00004812std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004813FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004814 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004815
4816 MVT DstTy = Op.getValueType();
4817
4818 if (!IsSigned) {
4819 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4820 DstTy = MVT::i64;
4821 }
4822
4823 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4824 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004825 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004827 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004828 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004829 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004830 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004831 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004832 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00004833 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004834 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004835
Evan Cheng87c89352007-10-15 20:11:21 +00004836 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4837 // stack slot.
4838 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004839 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004840 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004841 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004842
Evan Cheng0db9fe62006-04-25 20:13:52 +00004843 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004844 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004845 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4846 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4847 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4848 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004849 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004850
Dan Gohman475871a2008-07-27 21:46:04 +00004851 SDValue Chain = DAG.getEntryNode();
4852 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004853 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004854 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004855 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004856 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004857 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004858 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004859 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4860 };
Dale Johannesenace16102009-02-03 19:33:06 +00004861 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004862 Chain = Value.getValue(1);
4863 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4864 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4865 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004866
Evan Cheng0db9fe62006-04-25 20:13:52 +00004867 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004868 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004869 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004870
Chris Lattner27a6c732007-11-24 07:07:01 +00004871 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004872}
4873
Dan Gohman475871a2008-07-27 21:46:04 +00004874SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00004875 if (Op.getValueType().isVector()) {
4876 if (Op.getValueType() == MVT::v2i32 &&
4877 Op.getOperand(0).getValueType() == MVT::v2f64) {
4878 return Op;
4879 }
4880 return SDValue();
4881 }
4882
Eli Friedman948e95a2009-05-23 09:59:16 +00004883 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004884 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00004885 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
4886 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004887
Chris Lattner27a6c732007-11-24 07:07:01 +00004888 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004889 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004890 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004891}
4892
Eli Friedman948e95a2009-05-23 09:59:16 +00004893SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4894 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4895 SDValue FIST = Vals.first, StackSlot = Vals.second;
4896 assert(FIST.getNode() && "Unexpected failure");
4897
4898 // Load the result.
4899 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4900 FIST, StackSlot, NULL, 0);
4901}
4902
Dan Gohman475871a2008-07-27 21:46:04 +00004903SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004904 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004905 MVT VT = Op.getValueType();
4906 MVT EltVT = VT;
4907 if (VT.isVector())
4908 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004909 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004910 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004911 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004912 CV.push_back(C);
4913 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004914 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004915 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004916 CV.push_back(C);
4917 CV.push_back(C);
4918 CV.push_back(C);
4919 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004920 }
Dan Gohmand3006222007-07-27 17:16:43 +00004921 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004922 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004923 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004924 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004925 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004926 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004927}
4928
Dan Gohman475871a2008-07-27 21:46:04 +00004929SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004930 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004931 MVT VT = Op.getValueType();
4932 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004933 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004934 if (VT.isVector()) {
4935 EltVT = VT.getVectorElementType();
4936 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004937 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004938 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004939 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004940 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004941 CV.push_back(C);
4942 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004943 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004944 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004945 CV.push_back(C);
4946 CV.push_back(C);
4947 CV.push_back(C);
4948 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004949 }
Dan Gohmand3006222007-07-27 17:16:43 +00004950 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004951 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004952 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004953 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004954 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004955 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004956 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4957 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004958 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004959 Op.getOperand(0)),
4960 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004961 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004962 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004963 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004964}
4965
Dan Gohman475871a2008-07-27 21:46:04 +00004966SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4967 SDValue Op0 = Op.getOperand(0);
4968 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004969 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004970 MVT VT = Op.getValueType();
4971 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004972
4973 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004974 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004975 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004976 SrcVT = VT;
4977 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004978 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004979 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004980 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004981 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004982 }
4983
4984 // At this point the operands and the result should have the same
4985 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004986
Evan Cheng68c47cb2007-01-05 07:55:56 +00004987 // First get the sign bit of second operand.
4988 std::vector<Constant*> CV;
4989 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004990 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004992 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004993 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4994 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4995 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4996 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004997 }
Dan Gohmand3006222007-07-27 17:16:43 +00004998 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004999 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005000 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005001 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005002 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005003 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005004
5005 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005006 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005007 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005008 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5009 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005010 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005011 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5012 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005013 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005014 }
5015
Evan Cheng73d6cf12007-01-05 21:37:56 +00005016 // Clear first operand sign bit.
5017 CV.clear();
5018 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005019 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5020 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005021 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005022 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5023 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5024 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5025 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005026 }
Dan Gohmand3006222007-07-27 17:16:43 +00005027 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005028 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005029 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005030 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005031 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005032 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005033
5034 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005035 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005036}
5037
Dan Gohman076aee32009-03-04 19:44:21 +00005038/// Emit nodes that will be selected as "test Op0,Op0", or something
5039/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005040SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5041 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005042 DebugLoc dl = Op.getDebugLoc();
5043
Dan Gohman31125812009-03-07 01:58:32 +00005044 // CF and OF aren't always set the way we want. Determine which
5045 // of these we need.
5046 bool NeedCF = false;
5047 bool NeedOF = false;
5048 switch (X86CC) {
5049 case X86::COND_A: case X86::COND_AE:
5050 case X86::COND_B: case X86::COND_BE:
5051 NeedCF = true;
5052 break;
5053 case X86::COND_G: case X86::COND_GE:
5054 case X86::COND_L: case X86::COND_LE:
5055 case X86::COND_O: case X86::COND_NO:
5056 NeedOF = true;
5057 break;
5058 default: break;
5059 }
5060
Dan Gohman076aee32009-03-04 19:44:21 +00005061 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005062 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5063 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5064 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005065 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005066 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005067 switch (Op.getNode()->getOpcode()) {
5068 case ISD::ADD:
5069 // Due to an isel shortcoming, be conservative if this add is likely to
5070 // be selected as part of a load-modify-store instruction. When the root
5071 // node in a match is a store, isel doesn't know how to remap non-chain
5072 // non-flag uses of other nodes in the match, such as the ADD in this
5073 // case. This leads to the ADD being left around and reselected, with
5074 // the result being two adds in the output.
5075 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5076 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5077 if (UI->getOpcode() == ISD::STORE)
5078 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005079 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005080 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5081 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005082 if (C->getAPIntValue() == 1) {
5083 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005084 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005085 break;
5086 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005087 // An add of negative one (subtract of one) will be selected as a DEC.
5088 if (C->getAPIntValue().isAllOnesValue()) {
5089 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005090 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005091 break;
5092 }
5093 }
Dan Gohman076aee32009-03-04 19:44:21 +00005094 // Otherwise use a regular EFLAGS-setting add.
5095 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005096 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005097 break;
5098 case ISD::SUB:
5099 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5100 // likely to be selected as part of a load-modify-store instruction.
5101 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5102 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5103 if (UI->getOpcode() == ISD::STORE)
5104 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005105 // Otherwise use a regular EFLAGS-setting sub.
5106 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005107 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005108 break;
5109 case X86ISD::ADD:
5110 case X86ISD::SUB:
5111 case X86ISD::INC:
5112 case X86ISD::DEC:
5113 return SDValue(Op.getNode(), 1);
5114 default:
5115 default_case:
5116 break;
5117 }
5118 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005119 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005120 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005121 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005122 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005123 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005124 DAG.ReplaceAllUsesWith(Op, New);
5125 return SDValue(New.getNode(), 1);
5126 }
5127 }
5128
5129 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5130 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5131 DAG.getConstant(0, Op.getValueType()));
5132}
5133
5134/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5135/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005136SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5137 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005138 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5139 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005140 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005141
5142 DebugLoc dl = Op0.getDebugLoc();
5143 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5144}
5145
Dan Gohman475871a2008-07-27 21:46:04 +00005146SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005147 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005148 SDValue Op0 = Op.getOperand(0);
5149 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005150 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005151 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005152
Dan Gohmane5af2d32009-01-29 01:59:02 +00005153 // Lower (X & (1 << N)) == 0 to BT(X, N).
5154 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5155 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005156 if (Op0.getOpcode() == ISD::AND &&
5157 Op0.hasOneUse() &&
5158 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005159 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005160 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005161 SDValue LHS, RHS;
5162 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5163 if (ConstantSDNode *Op010C =
5164 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5165 if (Op010C->getZExtValue() == 1) {
5166 LHS = Op0.getOperand(0);
5167 RHS = Op0.getOperand(1).getOperand(1);
5168 }
5169 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5170 if (ConstantSDNode *Op000C =
5171 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5172 if (Op000C->getZExtValue() == 1) {
5173 LHS = Op0.getOperand(1);
5174 RHS = Op0.getOperand(0).getOperand(1);
5175 }
5176 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5177 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5178 SDValue AndLHS = Op0.getOperand(0);
5179 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5180 LHS = AndLHS.getOperand(0);
5181 RHS = AndLHS.getOperand(1);
5182 }
5183 }
Evan Cheng0488db92007-09-25 01:57:46 +00005184
Dan Gohmane5af2d32009-01-29 01:59:02 +00005185 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005186 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5187 // instruction. Since the shift amount is in-range-or-undefined, we know
5188 // that doing a bittest on the i16 value is ok. We extend to i32 because
5189 // the encoding for the i16 version is larger than the i32 version.
5190 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005191 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005192
5193 // If the operand types disagree, extend the shift amount to match. Since
5194 // BT ignores high bits (like shifts) we can use anyextend.
5195 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005196 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005197
Dale Johannesenace16102009-02-03 19:33:06 +00005198 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005199 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005200 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005201 DAG.getConstant(Cond, MVT::i8), BT);
5202 }
5203 }
5204
5205 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5206 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005207
Dan Gohman31125812009-03-07 01:58:32 +00005208 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005209 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005210 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005211}
5212
Dan Gohman475871a2008-07-27 21:46:04 +00005213SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5214 SDValue Cond;
5215 SDValue Op0 = Op.getOperand(0);
5216 SDValue Op1 = Op.getOperand(1);
5217 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005218 MVT VT = Op.getValueType();
5219 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5220 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005221 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005222
5223 if (isFP) {
5224 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005225 MVT VT0 = Op0.getValueType();
5226 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5227 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005228 bool Swap = false;
5229
5230 switch (SetCCOpcode) {
5231 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005232 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005233 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005234 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005235 case ISD::SETGT: Swap = true; // Fallthrough
5236 case ISD::SETLT:
5237 case ISD::SETOLT: SSECC = 1; break;
5238 case ISD::SETOGE:
5239 case ISD::SETGE: Swap = true; // Fallthrough
5240 case ISD::SETLE:
5241 case ISD::SETOLE: SSECC = 2; break;
5242 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005243 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005244 case ISD::SETNE: SSECC = 4; break;
5245 case ISD::SETULE: Swap = true;
5246 case ISD::SETUGE: SSECC = 5; break;
5247 case ISD::SETULT: Swap = true;
5248 case ISD::SETUGT: SSECC = 6; break;
5249 case ISD::SETO: SSECC = 7; break;
5250 }
5251 if (Swap)
5252 std::swap(Op0, Op1);
5253
Nate Begemanfb8ead02008-07-25 19:05:58 +00005254 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005255 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005256 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005257 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005258 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5259 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5260 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005261 }
5262 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005263 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005264 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5265 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5266 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005267 }
5268 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005269 }
5270 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005271 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005272 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005273
Nate Begeman30a0de92008-07-17 16:51:19 +00005274 // We are handling one of the integer comparisons here. Since SSE only has
5275 // GT and EQ comparisons for integer, swapping operands and multiple
5276 // operations may be required for some comparisons.
5277 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5278 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005279
Nate Begeman30a0de92008-07-17 16:51:19 +00005280 switch (VT.getSimpleVT()) {
5281 default: break;
5282 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5283 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5284 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5285 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5286 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005287
Nate Begeman30a0de92008-07-17 16:51:19 +00005288 switch (SetCCOpcode) {
5289 default: break;
5290 case ISD::SETNE: Invert = true;
5291 case ISD::SETEQ: Opc = EQOpc; break;
5292 case ISD::SETLT: Swap = true;
5293 case ISD::SETGT: Opc = GTOpc; break;
5294 case ISD::SETGE: Swap = true;
5295 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5296 case ISD::SETULT: Swap = true;
5297 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5298 case ISD::SETUGE: Swap = true;
5299 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5300 }
5301 if (Swap)
5302 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005303
Nate Begeman30a0de92008-07-17 16:51:19 +00005304 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5305 // bits of the inputs before performing those operations.
5306 if (FlipSigns) {
5307 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005308 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5309 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005310 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005311 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5312 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005313 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5314 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005315 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005316
Dale Johannesenace16102009-02-03 19:33:06 +00005317 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005318
5319 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005320 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005321 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005322
Nate Begeman30a0de92008-07-17 16:51:19 +00005323 return Result;
5324}
Evan Cheng0488db92007-09-25 01:57:46 +00005325
Evan Cheng370e5342008-12-03 08:38:43 +00005326// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005327static bool isX86LogicalCmp(SDValue Op) {
5328 unsigned Opc = Op.getNode()->getOpcode();
5329 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5330 return true;
5331 if (Op.getResNo() == 1 &&
5332 (Opc == X86ISD::ADD ||
5333 Opc == X86ISD::SUB ||
5334 Opc == X86ISD::SMUL ||
5335 Opc == X86ISD::UMUL ||
5336 Opc == X86ISD::INC ||
5337 Opc == X86ISD::DEC))
5338 return true;
5339
5340 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005341}
5342
Dan Gohman475871a2008-07-27 21:46:04 +00005343SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005344 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005345 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005346 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005347 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005348
Evan Cheng734503b2006-09-11 02:19:56 +00005349 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005350 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005351
Evan Cheng3f41d662007-10-08 22:16:29 +00005352 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5353 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005354 if (Cond.getOpcode() == X86ISD::SETCC) {
5355 CC = Cond.getOperand(0);
5356
Dan Gohman475871a2008-07-27 21:46:04 +00005357 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005358 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005359 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005360
Evan Cheng3f41d662007-10-08 22:16:29 +00005361 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005362 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005363 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005364 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005365
Chris Lattnerd1980a52009-03-12 06:52:53 +00005366 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5367 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005368 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005369 addTest = false;
5370 }
5371 }
5372
5373 if (addTest) {
5374 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005375 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005376 }
5377
Dan Gohmanfc166572009-04-09 23:54:40 +00005378 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005379 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005380 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5381 // condition is true.
5382 Ops.push_back(Op.getOperand(2));
5383 Ops.push_back(Op.getOperand(1));
5384 Ops.push_back(CC);
5385 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005386 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005387}
5388
Evan Cheng370e5342008-12-03 08:38:43 +00005389// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5390// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5391// from the AND / OR.
5392static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5393 Opc = Op.getOpcode();
5394 if (Opc != ISD::OR && Opc != ISD::AND)
5395 return false;
5396 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5397 Op.getOperand(0).hasOneUse() &&
5398 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5399 Op.getOperand(1).hasOneUse());
5400}
5401
Evan Cheng961d6d42009-02-02 08:19:07 +00005402// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5403// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005404static bool isXor1OfSetCC(SDValue Op) {
5405 if (Op.getOpcode() != ISD::XOR)
5406 return false;
5407 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5408 if (N1C && N1C->getAPIntValue() == 1) {
5409 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5410 Op.getOperand(0).hasOneUse();
5411 }
5412 return false;
5413}
5414
Dan Gohman475871a2008-07-27 21:46:04 +00005415SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005416 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue Chain = Op.getOperand(0);
5418 SDValue Cond = Op.getOperand(1);
5419 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005420 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005421 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005422
Evan Cheng0db9fe62006-04-25 20:13:52 +00005423 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005424 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005425#if 0
5426 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005427 else if (Cond.getOpcode() == X86ISD::ADD ||
5428 Cond.getOpcode() == X86ISD::SUB ||
5429 Cond.getOpcode() == X86ISD::SMUL ||
5430 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005431 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005432#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005433
Evan Cheng3f41d662007-10-08 22:16:29 +00005434 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5435 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005436 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005437 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005438
Dan Gohman475871a2008-07-27 21:46:04 +00005439 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005440 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005441 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005442 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005443 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005444 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005445 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005446 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005447 default: break;
5448 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005449 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005450 // These can only come from an arithmetic instruction with overflow,
5451 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005452 Cond = Cond.getNode()->getOperand(1);
5453 addTest = false;
5454 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005455 }
Evan Cheng0488db92007-09-25 01:57:46 +00005456 }
Evan Cheng370e5342008-12-03 08:38:43 +00005457 } else {
5458 unsigned CondOpc;
5459 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5460 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005461 if (CondOpc == ISD::OR) {
5462 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5463 // two branches instead of an explicit OR instruction with a
5464 // separate test.
5465 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005466 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005467 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005468 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005469 Chain, Dest, CC, Cmp);
5470 CC = Cond.getOperand(1).getOperand(0);
5471 Cond = Cmp;
5472 addTest = false;
5473 }
5474 } else { // ISD::AND
5475 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5476 // two branches instead of an explicit AND instruction with a
5477 // separate test. However, we only do this if this block doesn't
5478 // have a fall-through edge, because this requires an explicit
5479 // jmp when the condition is false.
5480 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005481 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005482 Op.getNode()->hasOneUse()) {
5483 X86::CondCode CCode =
5484 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5485 CCode = X86::GetOppositeBranchCondition(CCode);
5486 CC = DAG.getConstant(CCode, MVT::i8);
5487 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5488 // Look for an unconditional branch following this conditional branch.
5489 // We need this because we need to reverse the successors in order
5490 // to implement FCMP_OEQ.
5491 if (User.getOpcode() == ISD::BR) {
5492 SDValue FalseBB = User.getOperand(1);
5493 SDValue NewBR =
5494 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5495 assert(NewBR == User);
5496 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005497
Dale Johannesene4d209d2009-02-03 20:21:25 +00005498 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005499 Chain, Dest, CC, Cmp);
5500 X86::CondCode CCode =
5501 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5502 CCode = X86::GetOppositeBranchCondition(CCode);
5503 CC = DAG.getConstant(CCode, MVT::i8);
5504 Cond = Cmp;
5505 addTest = false;
5506 }
5507 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005508 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005509 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5510 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5511 // It should be transformed during dag combiner except when the condition
5512 // is set by a arithmetics with overflow node.
5513 X86::CondCode CCode =
5514 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5515 CCode = X86::GetOppositeBranchCondition(CCode);
5516 CC = DAG.getConstant(CCode, MVT::i8);
5517 Cond = Cond.getOperand(0).getOperand(1);
5518 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005519 }
Evan Cheng0488db92007-09-25 01:57:46 +00005520 }
5521
5522 if (addTest) {
5523 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005524 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005525 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005526 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005527 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005528}
5529
Anton Korobeynikove060b532007-04-17 19:34:00 +00005530
5531// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5532// Calls to _alloca is needed to probe the stack when allocating more than 4k
5533// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5534// that the guard pages used by the OS virtual memory manager are allocated in
5535// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005536SDValue
5537X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005538 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005539 assert(Subtarget->isTargetCygMing() &&
5540 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005541 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005542
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005543 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005544 SDValue Chain = Op.getOperand(0);
5545 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005546 // FIXME: Ensure alignment here
5547
Dan Gohman475871a2008-07-27 21:46:04 +00005548 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005549
Duncan Sands83ec4b62008-06-06 12:08:01 +00005550 MVT IntPtr = getPointerTy();
5551 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005552
Chris Lattnere563bbc2008-10-11 22:08:30 +00005553 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005554
Dale Johannesendd64c412009-02-04 00:33:20 +00005555 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005556 Flag = Chain.getValue(1);
5557
5558 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005560 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005561 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005562 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005563 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005564 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005565 Flag = Chain.getValue(1);
5566
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005567 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005568 DAG.getIntPtrConstant(0, true),
5569 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005570 Flag);
5571
Dale Johannesendd64c412009-02-04 00:33:20 +00005572 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005573
Dan Gohman475871a2008-07-27 21:46:04 +00005574 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005575 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005576}
5577
Dan Gohman475871a2008-07-27 21:46:04 +00005578SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005579X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005580 SDValue Chain,
5581 SDValue Dst, SDValue Src,
5582 SDValue Size, unsigned Align,
5583 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005584 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005585 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005586
Bill Wendling6f287b22008-09-30 21:22:07 +00005587 // If not DWORD aligned or size is more than the threshold, call the library.
5588 // The libc version is likely to be faster for these cases. It can use the
5589 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005590 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005591 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005592 ConstantSize->getZExtValue() >
5593 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005594 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005595
5596 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005597 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005598
Bill Wendling6158d842008-10-01 00:59:58 +00005599 if (const char *bzeroEntry = V &&
5600 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5601 MVT IntPtr = getPointerTy();
5602 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005603 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005604 TargetLowering::ArgListEntry Entry;
5605 Entry.Node = Dst;
5606 Entry.Ty = IntPtrTy;
5607 Args.push_back(Entry);
5608 Entry.Node = Size;
5609 Args.push_back(Entry);
5610 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005611 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5612 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005613 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005614 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005615 }
5616
Dan Gohman707e0182008-04-12 04:36:06 +00005617 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005618 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005619 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005620
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005621 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005624 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005625 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005626 unsigned BytesLeft = 0;
5627 bool TwoRepStos = false;
5628 if (ValC) {
5629 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005630 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005631
Evan Cheng0db9fe62006-04-25 20:13:52 +00005632 // If the value is a constant, then we can potentially use larger sets.
5633 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005634 case 2: // WORD aligned
5635 AVT = MVT::i16;
5636 ValReg = X86::AX;
5637 Val = (Val << 8) | Val;
5638 break;
5639 case 0: // DWORD aligned
5640 AVT = MVT::i32;
5641 ValReg = X86::EAX;
5642 Val = (Val << 8) | Val;
5643 Val = (Val << 16) | Val;
5644 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5645 AVT = MVT::i64;
5646 ValReg = X86::RAX;
5647 Val = (Val << 32) | Val;
5648 }
5649 break;
5650 default: // Byte aligned
5651 AVT = MVT::i8;
5652 ValReg = X86::AL;
5653 Count = DAG.getIntPtrConstant(SizeVal);
5654 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005655 }
5656
Duncan Sands8e4eb092008-06-08 20:54:56 +00005657 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005658 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005659 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5660 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005661 }
5662
Dale Johannesen0f502f62009-02-03 22:26:09 +00005663 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005664 InFlag);
5665 InFlag = Chain.getValue(1);
5666 } else {
5667 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005668 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005669 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005670 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005671 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005672
Scott Michelfdc40a02009-02-17 22:15:04 +00005673 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005674 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005675 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005676 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005677 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005678 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005679 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005680 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005681
Chris Lattnerd96d0722007-02-25 06:40:16 +00005682 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005683 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005684 Ops.push_back(Chain);
5685 Ops.push_back(DAG.getValueType(AVT));
5686 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005687 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005688
Evan Cheng0db9fe62006-04-25 20:13:52 +00005689 if (TwoRepStos) {
5690 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005691 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005692 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005693 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005694 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005695 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005696 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005697 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005698 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005699 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700 Ops.clear();
5701 Ops.push_back(Chain);
5702 Ops.push_back(DAG.getValueType(MVT::i8));
5703 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005704 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005706 // Handle the last 1 - 7 bytes.
5707 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005708 MVT AddrVT = Dst.getValueType();
5709 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005710
Dale Johannesen0f502f62009-02-03 22:26:09 +00005711 Chain = DAG.getMemset(Chain, dl,
5712 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005713 DAG.getConstant(Offset, AddrVT)),
5714 Src,
5715 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005716 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005717 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005718
Dan Gohman707e0182008-04-12 04:36:06 +00005719 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720 return Chain;
5721}
Evan Cheng11e15b32006-04-03 20:53:28 +00005722
Dan Gohman475871a2008-07-27 21:46:04 +00005723SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005724X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005725 SDValue Chain, SDValue Dst, SDValue Src,
5726 SDValue Size, unsigned Align,
5727 bool AlwaysInline,
5728 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005729 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005730 // This requires the copy size to be a constant, preferrably
5731 // within a subtarget-specific limit.
5732 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5733 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005734 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005735 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005736 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005737 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005738
Evan Cheng1887c1c2008-08-21 21:00:15 +00005739 /// If not DWORD aligned, call the library.
5740 if ((Align & 3) != 0)
5741 return SDValue();
5742
5743 // DWORD aligned
5744 MVT AVT = MVT::i32;
5745 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005746 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005747
Duncan Sands83ec4b62008-06-06 12:08:01 +00005748 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005749 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005750 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005751 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005752
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005754 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005755 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005756 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005757 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005758 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005759 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005760 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005761 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005762 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005763 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005764 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005765 InFlag = Chain.getValue(1);
5766
Chris Lattnerd96d0722007-02-25 06:40:16 +00005767 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005768 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005769 Ops.push_back(Chain);
5770 Ops.push_back(DAG.getValueType(AVT));
5771 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005772 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005773
Dan Gohman475871a2008-07-27 21:46:04 +00005774 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005775 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005776 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005777 // Handle the last 1 - 7 bytes.
5778 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005779 MVT DstVT = Dst.getValueType();
5780 MVT SrcVT = Src.getValueType();
5781 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005782 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005783 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005784 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005785 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005786 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005787 DAG.getConstant(BytesLeft, SizeVT),
5788 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005789 DstSV, DstSVOff + Offset,
5790 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005791 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005792
Scott Michelfdc40a02009-02-17 22:15:04 +00005793 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005794 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005795}
5796
Dan Gohman475871a2008-07-27 21:46:04 +00005797SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005798 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005799 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005800
Evan Cheng25ab6902006-09-08 06:48:29 +00005801 if (!Subtarget->is64Bit()) {
5802 // vastart just stores the address of the VarArgsFrameIndex slot into the
5803 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005804 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005805 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005806 }
5807
5808 // __va_list_tag:
5809 // gp_offset (0 - 6 * 8)
5810 // fp_offset (48 - 48 + 8 * 16)
5811 // overflow_arg_area (point to parameters coming in memory).
5812 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005813 SmallVector<SDValue, 8> MemOps;
5814 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005815 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005816 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005817 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005818 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005819 MemOps.push_back(Store);
5820
5821 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005822 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005823 FIN, DAG.getIntPtrConstant(4));
5824 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005825 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005826 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005827 MemOps.push_back(Store);
5828
5829 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005830 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005831 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005832 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005833 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005834 MemOps.push_back(Store);
5835
5836 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005837 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005838 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005839 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005840 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005841 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005842 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005843 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005844}
5845
Dan Gohman475871a2008-07-27 21:46:04 +00005846SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005847 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5848 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005849 SDValue Chain = Op.getOperand(0);
5850 SDValue SrcPtr = Op.getOperand(1);
5851 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005852
5853 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5854 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005855 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005856}
5857
Dan Gohman475871a2008-07-27 21:46:04 +00005858SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005859 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005860 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005861 SDValue Chain = Op.getOperand(0);
5862 SDValue DstPtr = Op.getOperand(1);
5863 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005864 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5865 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005866 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005867
Dale Johannesendd64c412009-02-04 00:33:20 +00005868 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005869 DAG.getIntPtrConstant(24), 8, false,
5870 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005871}
5872
Dan Gohman475871a2008-07-27 21:46:04 +00005873SDValue
5874X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005875 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005876 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005877 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005878 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005879 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880 case Intrinsic::x86_sse_comieq_ss:
5881 case Intrinsic::x86_sse_comilt_ss:
5882 case Intrinsic::x86_sse_comile_ss:
5883 case Intrinsic::x86_sse_comigt_ss:
5884 case Intrinsic::x86_sse_comige_ss:
5885 case Intrinsic::x86_sse_comineq_ss:
5886 case Intrinsic::x86_sse_ucomieq_ss:
5887 case Intrinsic::x86_sse_ucomilt_ss:
5888 case Intrinsic::x86_sse_ucomile_ss:
5889 case Intrinsic::x86_sse_ucomigt_ss:
5890 case Intrinsic::x86_sse_ucomige_ss:
5891 case Intrinsic::x86_sse_ucomineq_ss:
5892 case Intrinsic::x86_sse2_comieq_sd:
5893 case Intrinsic::x86_sse2_comilt_sd:
5894 case Intrinsic::x86_sse2_comile_sd:
5895 case Intrinsic::x86_sse2_comigt_sd:
5896 case Intrinsic::x86_sse2_comige_sd:
5897 case Intrinsic::x86_sse2_comineq_sd:
5898 case Intrinsic::x86_sse2_ucomieq_sd:
5899 case Intrinsic::x86_sse2_ucomilt_sd:
5900 case Intrinsic::x86_sse2_ucomile_sd:
5901 case Intrinsic::x86_sse2_ucomigt_sd:
5902 case Intrinsic::x86_sse2_ucomige_sd:
5903 case Intrinsic::x86_sse2_ucomineq_sd: {
5904 unsigned Opc = 0;
5905 ISD::CondCode CC = ISD::SETCC_INVALID;
5906 switch (IntNo) {
5907 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005908 case Intrinsic::x86_sse_comieq_ss:
5909 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910 Opc = X86ISD::COMI;
5911 CC = ISD::SETEQ;
5912 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005913 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005914 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005915 Opc = X86ISD::COMI;
5916 CC = ISD::SETLT;
5917 break;
5918 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005919 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005920 Opc = X86ISD::COMI;
5921 CC = ISD::SETLE;
5922 break;
5923 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005924 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925 Opc = X86ISD::COMI;
5926 CC = ISD::SETGT;
5927 break;
5928 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005929 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005930 Opc = X86ISD::COMI;
5931 CC = ISD::SETGE;
5932 break;
5933 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005934 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005935 Opc = X86ISD::COMI;
5936 CC = ISD::SETNE;
5937 break;
5938 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005939 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005940 Opc = X86ISD::UCOMI;
5941 CC = ISD::SETEQ;
5942 break;
5943 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005944 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005945 Opc = X86ISD::UCOMI;
5946 CC = ISD::SETLT;
5947 break;
5948 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005949 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005950 Opc = X86ISD::UCOMI;
5951 CC = ISD::SETLE;
5952 break;
5953 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005954 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005955 Opc = X86ISD::UCOMI;
5956 CC = ISD::SETGT;
5957 break;
5958 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005959 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005960 Opc = X86ISD::UCOMI;
5961 CC = ISD::SETGE;
5962 break;
5963 case Intrinsic::x86_sse_ucomineq_ss:
5964 case Intrinsic::x86_sse2_ucomineq_sd:
5965 Opc = X86ISD::UCOMI;
5966 CC = ISD::SETNE;
5967 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005968 }
Evan Cheng734503b2006-09-11 02:19:56 +00005969
Dan Gohman475871a2008-07-27 21:46:04 +00005970 SDValue LHS = Op.getOperand(1);
5971 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005972 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005973 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5974 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005975 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005976 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005977 }
Evan Cheng5759f972008-05-04 09:15:50 +00005978
5979 // Fix vector shift instructions where the last operand is a non-immediate
5980 // i32 value.
5981 case Intrinsic::x86_sse2_pslli_w:
5982 case Intrinsic::x86_sse2_pslli_d:
5983 case Intrinsic::x86_sse2_pslli_q:
5984 case Intrinsic::x86_sse2_psrli_w:
5985 case Intrinsic::x86_sse2_psrli_d:
5986 case Intrinsic::x86_sse2_psrli_q:
5987 case Intrinsic::x86_sse2_psrai_w:
5988 case Intrinsic::x86_sse2_psrai_d:
5989 case Intrinsic::x86_mmx_pslli_w:
5990 case Intrinsic::x86_mmx_pslli_d:
5991 case Intrinsic::x86_mmx_pslli_q:
5992 case Intrinsic::x86_mmx_psrli_w:
5993 case Intrinsic::x86_mmx_psrli_d:
5994 case Intrinsic::x86_mmx_psrli_q:
5995 case Intrinsic::x86_mmx_psrai_w:
5996 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005997 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005998 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005999 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006000
6001 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006002 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006003 switch (IntNo) {
6004 case Intrinsic::x86_sse2_pslli_w:
6005 NewIntNo = Intrinsic::x86_sse2_psll_w;
6006 break;
6007 case Intrinsic::x86_sse2_pslli_d:
6008 NewIntNo = Intrinsic::x86_sse2_psll_d;
6009 break;
6010 case Intrinsic::x86_sse2_pslli_q:
6011 NewIntNo = Intrinsic::x86_sse2_psll_q;
6012 break;
6013 case Intrinsic::x86_sse2_psrli_w:
6014 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6015 break;
6016 case Intrinsic::x86_sse2_psrli_d:
6017 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6018 break;
6019 case Intrinsic::x86_sse2_psrli_q:
6020 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6021 break;
6022 case Intrinsic::x86_sse2_psrai_w:
6023 NewIntNo = Intrinsic::x86_sse2_psra_w;
6024 break;
6025 case Intrinsic::x86_sse2_psrai_d:
6026 NewIntNo = Intrinsic::x86_sse2_psra_d;
6027 break;
6028 default: {
6029 ShAmtVT = MVT::v2i32;
6030 switch (IntNo) {
6031 case Intrinsic::x86_mmx_pslli_w:
6032 NewIntNo = Intrinsic::x86_mmx_psll_w;
6033 break;
6034 case Intrinsic::x86_mmx_pslli_d:
6035 NewIntNo = Intrinsic::x86_mmx_psll_d;
6036 break;
6037 case Intrinsic::x86_mmx_pslli_q:
6038 NewIntNo = Intrinsic::x86_mmx_psll_q;
6039 break;
6040 case Intrinsic::x86_mmx_psrli_w:
6041 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6042 break;
6043 case Intrinsic::x86_mmx_psrli_d:
6044 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6045 break;
6046 case Intrinsic::x86_mmx_psrli_q:
6047 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6048 break;
6049 case Intrinsic::x86_mmx_psrai_w:
6050 NewIntNo = Intrinsic::x86_mmx_psra_w;
6051 break;
6052 case Intrinsic::x86_mmx_psrai_d:
6053 NewIntNo = Intrinsic::x86_mmx_psra_d;
6054 break;
6055 default: abort(); // Can't reach here.
6056 }
6057 break;
6058 }
6059 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006060 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006061 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6062 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6063 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006064 DAG.getConstant(NewIntNo, MVT::i32),
6065 Op.getOperand(1), ShAmt);
6066 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006067 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006068}
Evan Cheng72261582005-12-20 06:22:03 +00006069
Dan Gohman475871a2008-07-27 21:46:04 +00006070SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006071 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006072 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006073
6074 if (Depth > 0) {
6075 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6076 SDValue Offset =
6077 DAG.getConstant(TD->getPointerSize(),
6078 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006079 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006080 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006081 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006082 NULL, 0);
6083 }
6084
6085 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006086 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006087 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006088 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006089}
6090
Dan Gohman475871a2008-07-27 21:46:04 +00006091SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006092 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6093 MFI->setFrameAddressIsTaken(true);
6094 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006095 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006096 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6097 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006098 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006099 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006100 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006101 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006102}
6103
Dan Gohman475871a2008-07-27 21:46:04 +00006104SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006105 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006106 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006107}
6108
Dan Gohman475871a2008-07-27 21:46:04 +00006109SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006110{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006111 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006112 SDValue Chain = Op.getOperand(0);
6113 SDValue Offset = Op.getOperand(1);
6114 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006115 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006116
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006117 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6118 getPointerTy());
6119 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006120
Dale Johannesene4d209d2009-02-03 20:21:25 +00006121 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006122 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006123 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6124 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006125 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006126 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006127
Dale Johannesene4d209d2009-02-03 20:21:25 +00006128 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006129 MVT::Other,
6130 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006131}
6132
Dan Gohman475871a2008-07-27 21:46:04 +00006133SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006134 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006135 SDValue Root = Op.getOperand(0);
6136 SDValue Trmp = Op.getOperand(1); // trampoline
6137 SDValue FPtr = Op.getOperand(2); // nested function
6138 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006139 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006140
Dan Gohman69de1932008-02-06 22:27:42 +00006141 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006142
Duncan Sands339e14f2008-01-16 22:55:25 +00006143 const X86InstrInfo *TII =
6144 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6145
Duncan Sandsb116fac2007-07-27 20:02:49 +00006146 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006147 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006148
6149 // Large code-model.
6150
6151 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6152 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6153
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006154 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6155 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006156
6157 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6158
6159 // Load the pointer to the nested function into R11.
6160 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006161 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006162 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6163 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006164
Scott Michelfdc40a02009-02-17 22:15:04 +00006165 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006166 DAG.getConstant(2, MVT::i64));
6167 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006168
6169 // Load the 'nest' parameter value into R10.
6170 // R10 is specified in X86CallingConv.td
6171 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
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(10, MVT::i64));
6174 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6175 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006176
Scott Michelfdc40a02009-02-17 22:15:04 +00006177 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006178 DAG.getConstant(12, MVT::i64));
6179 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006180
6181 // Jump to the nested function.
6182 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006183 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006184 DAG.getConstant(20, MVT::i64));
6185 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6186 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006187
6188 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006189 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006190 DAG.getConstant(22, MVT::i64));
6191 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006192 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006193
Dan Gohman475871a2008-07-27 21:46:04 +00006194 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006195 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6196 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006197 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006198 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006199 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6200 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006201 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006202
6203 switch (CC) {
6204 default:
6205 assert(0 && "Unsupported calling convention");
6206 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006207 case CallingConv::X86_StdCall: {
6208 // Pass 'nest' parameter in ECX.
6209 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006210 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006211
6212 // Check that ECX wasn't needed by an 'inreg' parameter.
6213 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006214 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006215
Chris Lattner58d74912008-03-12 17:45:29 +00006216 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006217 unsigned InRegCount = 0;
6218 unsigned Idx = 1;
6219
6220 for (FunctionType::param_iterator I = FTy->param_begin(),
6221 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006222 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006223 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006224 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006225
6226 if (InRegCount > 2) {
6227 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6228 abort();
6229 }
6230 }
6231 break;
6232 }
6233 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006234 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006235 // Pass 'nest' parameter in EAX.
6236 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006237 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006238 break;
6239 }
6240
Dan Gohman475871a2008-07-27 21:46:04 +00006241 SDValue OutChains[4];
6242 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006243
Scott Michelfdc40a02009-02-17 22:15:04 +00006244 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006245 DAG.getConstant(10, MVT::i32));
6246 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006247
Duncan Sands339e14f2008-01-16 22:55:25 +00006248 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006249 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006250 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006251 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006252 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006253
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(1, MVT::i32));
6256 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006257
Duncan Sands339e14f2008-01-16 22:55:25 +00006258 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
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(5, MVT::i32));
6261 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006262 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006263
Scott Michelfdc40a02009-02-17 22:15:04 +00006264 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006265 DAG.getConstant(6, MVT::i32));
6266 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006267
Dan Gohman475871a2008-07-27 21:46:04 +00006268 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006269 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6270 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006271 }
6272}
6273
Dan Gohman475871a2008-07-27 21:46:04 +00006274SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006275 /*
6276 The rounding mode is in bits 11:10 of FPSR, and has the following
6277 settings:
6278 00 Round to nearest
6279 01 Round to -inf
6280 10 Round to +inf
6281 11 Round to 0
6282
6283 FLT_ROUNDS, on the other hand, expects the following:
6284 -1 Undefined
6285 0 Round to 0
6286 1 Round to nearest
6287 2 Round to +inf
6288 3 Round to -inf
6289
6290 To perform the conversion, we do:
6291 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6292 */
6293
6294 MachineFunction &MF = DAG.getMachineFunction();
6295 const TargetMachine &TM = MF.getTarget();
6296 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6297 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006298 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006299 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006300
6301 // Save FP Control Word to stack slot
6302 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006303 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006304
Dale Johannesene4d209d2009-02-03 20:21:25 +00006305 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006306 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006307
6308 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006309 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006310
6311 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006312 SDValue CWD1 =
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(0x800, MVT::i16)),
6316 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006317 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006318 DAG.getNode(ISD::SRL, dl, MVT::i16,
6319 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006320 CWD, DAG.getConstant(0x400, MVT::i16)),
6321 DAG.getConstant(9, MVT::i8));
6322
Dan Gohman475871a2008-07-27 21:46:04 +00006323 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006324 DAG.getNode(ISD::AND, dl, MVT::i16,
6325 DAG.getNode(ISD::ADD, dl, MVT::i16,
6326 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006327 DAG.getConstant(1, MVT::i16)),
6328 DAG.getConstant(3, MVT::i16));
6329
6330
Duncan Sands83ec4b62008-06-06 12:08:01 +00006331 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006332 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006333}
6334
Dan Gohman475871a2008-07-27 21:46:04 +00006335SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006336 MVT VT = Op.getValueType();
6337 MVT OpVT = VT;
6338 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006339 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006340
6341 Op = Op.getOperand(0);
6342 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006343 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006344 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006345 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006346 }
Evan Cheng18efe262007-12-14 02:13:44 +00006347
Evan Cheng152804e2007-12-14 08:30:15 +00006348 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6349 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006350 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006351
6352 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006353 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006354 Ops.push_back(Op);
6355 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6356 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6357 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006358 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006359
6360 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006361 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006362
Evan Cheng18efe262007-12-14 02:13:44 +00006363 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006364 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006365 return Op;
6366}
6367
Dan Gohman475871a2008-07-27 21:46:04 +00006368SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006369 MVT VT = Op.getValueType();
6370 MVT OpVT = VT;
6371 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006372 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006373
6374 Op = Op.getOperand(0);
6375 if (VT == MVT::i8) {
6376 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006377 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006378 }
Evan Cheng152804e2007-12-14 08:30:15 +00006379
6380 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6381 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006382 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006383
6384 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006385 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006386 Ops.push_back(Op);
6387 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6388 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6389 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006390 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006391
Evan Cheng18efe262007-12-14 02:13:44 +00006392 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006393 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006394 return Op;
6395}
6396
Mon P Wangaf9b9522008-12-18 21:42:19 +00006397SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6398 MVT VT = Op.getValueType();
6399 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006400 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006401
Mon P Wangaf9b9522008-12-18 21:42:19 +00006402 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6403 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6404 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6405 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6406 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6407 //
6408 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6409 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6410 // return AloBlo + AloBhi + AhiBlo;
6411
6412 SDValue A = Op.getOperand(0);
6413 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006414
Dale Johannesene4d209d2009-02-03 20:21:25 +00006415 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006416 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6417 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006418 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006419 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6420 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006421 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006422 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6423 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006424 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006425 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6426 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006427 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006428 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6429 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006430 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006431 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6432 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006433 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006434 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6435 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006436 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6437 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006438 return Res;
6439}
6440
6441
Bill Wendling74c37652008-12-09 22:08:41 +00006442SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6443 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6444 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006445 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6446 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006447 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006448 SDValue LHS = N->getOperand(0);
6449 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006450 unsigned BaseOp = 0;
6451 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006452 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006453
6454 switch (Op.getOpcode()) {
6455 default: assert(0 && "Unknown ovf instruction!");
6456 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006457 // A subtract of one will be selected as a INC. Note that INC doesn't
6458 // set CF, so we can't do this for UADDO.
6459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6460 if (C->getAPIntValue() == 1) {
6461 BaseOp = X86ISD::INC;
6462 Cond = X86::COND_O;
6463 break;
6464 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006465 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006466 Cond = X86::COND_O;
6467 break;
6468 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006469 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006470 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006471 break;
6472 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006473 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6474 // set CF, so we can't do this for USUBO.
6475 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6476 if (C->getAPIntValue() == 1) {
6477 BaseOp = X86ISD::DEC;
6478 Cond = X86::COND_O;
6479 break;
6480 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006481 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006482 Cond = X86::COND_O;
6483 break;
6484 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006485 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006486 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006487 break;
6488 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006489 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006490 Cond = X86::COND_O;
6491 break;
6492 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006493 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006494 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006495 break;
6496 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006497
Bill Wendling61edeb52008-12-02 01:06:39 +00006498 // Also sets EFLAGS.
6499 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006500 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006501
Bill Wendling61edeb52008-12-02 01:06:39 +00006502 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006503 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006504 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006505
Bill Wendling61edeb52008-12-02 01:06:39 +00006506 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6507 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006508}
6509
Dan Gohman475871a2008-07-27 21:46:04 +00006510SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006511 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006512 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006513 unsigned Reg = 0;
6514 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006515 switch(T.getSimpleVT()) {
6516 default:
6517 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006518 case MVT::i8: Reg = X86::AL; size = 1; break;
6519 case MVT::i16: Reg = X86::AX; size = 2; break;
6520 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006521 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006522 assert(Subtarget->is64Bit() && "Node not type legal!");
6523 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006524 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006525 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006526 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006527 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006528 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006529 Op.getOperand(1),
6530 Op.getOperand(3),
6531 DAG.getTargetConstant(size, MVT::i8),
6532 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006533 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006534 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006535 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006536 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006537 return cpOut;
6538}
6539
Duncan Sands1607f052008-12-01 11:39:25 +00006540SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006541 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006542 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006543 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006544 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006545 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006546 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006547 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6548 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006549 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006550 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006551 DAG.getConstant(32, MVT::i8));
6552 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006553 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006554 rdx.getValue(1)
6555 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006556 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006557}
6558
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006559SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6560 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006561 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006562 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006563 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006564 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006565 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006566 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006567 Node->getOperand(0),
6568 Node->getOperand(1), negOp,
6569 cast<AtomicSDNode>(Node)->getSrcValue(),
6570 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006571}
6572
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573/// LowerOperation - Provide custom lowering hooks for some operations.
6574///
Dan Gohman475871a2008-07-27 21:46:04 +00006575SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006576 switch (Op.getOpcode()) {
6577 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006578 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6579 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6581 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6582 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6583 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6584 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6585 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6586 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006587 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006588 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006589 case ISD::SHL_PARTS:
6590 case ISD::SRA_PARTS:
6591 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6592 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006593 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006594 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006595 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006596 case ISD::FABS: return LowerFABS(Op, DAG);
6597 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006598 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006599 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006600 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006601 case ISD::SELECT: return LowerSELECT(Op, DAG);
6602 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006604 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006606 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006608 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006609 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006611 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6612 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006613 case ISD::FRAME_TO_ARGS_OFFSET:
6614 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006615 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006616 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006617 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006618 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006619 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6620 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006621 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006622 case ISD::SADDO:
6623 case ISD::UADDO:
6624 case ISD::SSUBO:
6625 case ISD::USUBO:
6626 case ISD::SMULO:
6627 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006628 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006629 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006630}
6631
Duncan Sands1607f052008-12-01 11:39:25 +00006632void X86TargetLowering::
6633ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6634 SelectionDAG &DAG, unsigned NewOp) {
6635 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006636 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006637 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6638
6639 SDValue Chain = Node->getOperand(0);
6640 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006641 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006642 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006643 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006644 Node->getOperand(2), DAG.getIntPtrConstant(1));
6645 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6646 // have a MemOperand. Pass the info through as a normal operand.
6647 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6648 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6649 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006650 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006651 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006652 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006653 Results.push_back(Result.getValue(2));
6654}
6655
Duncan Sands126d9072008-07-04 11:47:58 +00006656/// ReplaceNodeResults - Replace a node with an illegal result type
6657/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006658void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6659 SmallVectorImpl<SDValue>&Results,
6660 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006661 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006662 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006663 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006664 assert(false && "Do not know how to custom type legalize this operation!");
6665 return;
6666 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006667 std::pair<SDValue,SDValue> Vals =
6668 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006669 SDValue FIST = Vals.first, StackSlot = Vals.second;
6670 if (FIST.getNode() != 0) {
6671 MVT VT = N->getValueType(0);
6672 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006673 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006674 }
6675 return;
6676 }
6677 case ISD::READCYCLECOUNTER: {
6678 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6679 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006680 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006681 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006682 rd.getValue(1));
6683 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006684 eax.getValue(2));
6685 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6686 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006687 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006688 Results.push_back(edx.getValue(1));
6689 return;
6690 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006691 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006692 MVT T = N->getValueType(0);
6693 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6694 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006695 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006696 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006697 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006698 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006699 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6700 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006701 cpInL.getValue(1));
6702 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006703 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006704 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006705 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006706 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006707 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006708 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006709 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006710 swapInL.getValue(1));
6711 SDValue Ops[] = { swapInH.getValue(0),
6712 N->getOperand(1),
6713 swapInH.getValue(1) };
6714 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006715 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006716 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6717 MVT::i32, Result.getValue(1));
6718 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6719 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006720 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006721 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006722 Results.push_back(cpOutH.getValue(1));
6723 return;
6724 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006725 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006726 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6727 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006728 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006729 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6730 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006731 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006732 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6733 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006734 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006735 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6736 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006737 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006738 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6739 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006740 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006741 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6742 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006743 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006744 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6745 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006746 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006747}
6748
Evan Cheng72261582005-12-20 06:22:03 +00006749const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6750 switch (Opcode) {
6751 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006752 case X86ISD::BSF: return "X86ISD::BSF";
6753 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006754 case X86ISD::SHLD: return "X86ISD::SHLD";
6755 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006756 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006757 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006758 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006759 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006760 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006761 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006762 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6763 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6764 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006765 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006766 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006767 case X86ISD::CALL: return "X86ISD::CALL";
6768 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6769 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006770 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006771 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006772 case X86ISD::COMI: return "X86ISD::COMI";
6773 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006774 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006775 case X86ISD::CMOV: return "X86ISD::CMOV";
6776 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006777 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006778 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6779 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006780 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006781 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006782 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006783 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006784 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6785 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006786 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006787 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006788 case X86ISD::FMAX: return "X86ISD::FMAX";
6789 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006790 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6791 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006792 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006793 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006794 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006795 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006796 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006797 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6798 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006799 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6800 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6801 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6802 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6803 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6804 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006805 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6806 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006807 case X86ISD::VSHL: return "X86ISD::VSHL";
6808 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006809 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6810 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6811 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6812 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6813 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6814 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6815 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6816 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6817 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6818 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006819 case X86ISD::ADD: return "X86ISD::ADD";
6820 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006821 case X86ISD::SMUL: return "X86ISD::SMUL";
6822 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006823 case X86ISD::INC: return "X86ISD::INC";
6824 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006825 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006826 }
6827}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006828
Chris Lattnerc9addb72007-03-30 23:15:24 +00006829// isLegalAddressingMode - Return true if the addressing mode represented
6830// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006831bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006832 const Type *Ty) const {
6833 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006834
Chris Lattnerc9addb72007-03-30 23:15:24 +00006835 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6836 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6837 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006838
Chris Lattnerc9addb72007-03-30 23:15:24 +00006839 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006840 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006841 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6842 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006843 // If BaseGV requires a register, we cannot also have a BaseReg.
6844 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6845 AM.HasBaseReg)
6846 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006847
6848 // X86-64 only supports addr of globals in small code model.
6849 if (Subtarget->is64Bit()) {
6850 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6851 return false;
6852 // If lower 4G is not available, then we must use rip-relative addressing.
6853 if (AM.BaseOffs || AM.Scale > 1)
6854 return false;
6855 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006856 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006857
Chris Lattnerc9addb72007-03-30 23:15:24 +00006858 switch (AM.Scale) {
6859 case 0:
6860 case 1:
6861 case 2:
6862 case 4:
6863 case 8:
6864 // These scales always work.
6865 break;
6866 case 3:
6867 case 5:
6868 case 9:
6869 // These scales are formed with basereg+scalereg. Only accept if there is
6870 // no basereg yet.
6871 if (AM.HasBaseReg)
6872 return false;
6873 break;
6874 default: // Other stuff never works.
6875 return false;
6876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006877
Chris Lattnerc9addb72007-03-30 23:15:24 +00006878 return true;
6879}
6880
6881
Evan Cheng2bd122c2007-10-26 01:56:11 +00006882bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6883 if (!Ty1->isInteger() || !Ty2->isInteger())
6884 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006885 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6886 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006887 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006888 return false;
6889 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006890}
6891
Duncan Sands83ec4b62008-06-06 12:08:01 +00006892bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6893 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006894 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006895 unsigned NumBits1 = VT1.getSizeInBits();
6896 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006897 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006898 return false;
6899 return Subtarget->is64Bit() || NumBits1 < 64;
6900}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006901
Dan Gohman97121ba2009-04-08 00:15:30 +00006902bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) 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 Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6905}
6906
6907bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006908 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006909 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6910}
6911
Evan Cheng8b944d32009-05-28 00:35:15 +00006912bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
6913 // i16 instructions are longer (0x66 prefix) and potentially slower.
6914 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
6915}
6916
Evan Cheng60c07e12006-07-05 22:17:51 +00006917/// isShuffleMaskLegal - Targets can use this to indicate that they only
6918/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6919/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6920/// are assumed to be legal.
6921bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006922X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6923 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006924 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006925 if (VT.getSizeInBits() == 64)
6926 return false;
6927
6928 // FIXME: pshufb, blends, palignr, shifts.
6929 return (VT.getVectorNumElements() == 2 ||
6930 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6931 isMOVLMask(M, VT) ||
6932 isSHUFPMask(M, VT) ||
6933 isPSHUFDMask(M, VT) ||
6934 isPSHUFHWMask(M, VT) ||
6935 isPSHUFLWMask(M, VT) ||
6936 isUNPCKLMask(M, VT) ||
6937 isUNPCKHMask(M, VT) ||
6938 isUNPCKL_v_undef_Mask(M, VT) ||
6939 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006940}
6941
Dan Gohman7d8143f2008-04-09 20:09:42 +00006942bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006943X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006944 MVT VT) const {
6945 unsigned NumElts = VT.getVectorNumElements();
6946 // FIXME: This collection of masks seems suspect.
6947 if (NumElts == 2)
6948 return true;
6949 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6950 return (isMOVLMask(Mask, VT) ||
6951 isCommutedMOVLMask(Mask, VT, true) ||
6952 isSHUFPMask(Mask, VT) ||
6953 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006954 }
6955 return false;
6956}
6957
6958//===----------------------------------------------------------------------===//
6959// X86 Scheduler Hooks
6960//===----------------------------------------------------------------------===//
6961
Mon P Wang63307c32008-05-05 19:05:59 +00006962// private utility function
6963MachineBasicBlock *
6964X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6965 MachineBasicBlock *MBB,
6966 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006967 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006968 unsigned LoadOpc,
6969 unsigned CXchgOpc,
6970 unsigned copyOpc,
6971 unsigned notOpc,
6972 unsigned EAXreg,
6973 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006974 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006975 // For the atomic bitwise operator, we generate
6976 // thisMBB:
6977 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006978 // ld t1 = [bitinstr.addr]
6979 // op t2 = t1, [bitinstr.val]
6980 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006981 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6982 // bz newMBB
6983 // fallthrough -->nextMBB
6984 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6985 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006986 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006987 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006988
Mon P Wang63307c32008-05-05 19:05:59 +00006989 /// First build the CFG
6990 MachineFunction *F = MBB->getParent();
6991 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006992 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6993 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6994 F->insert(MBBIter, newMBB);
6995 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006996
Mon P Wang63307c32008-05-05 19:05:59 +00006997 // Move all successors to thisMBB to nextMBB
6998 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006999
Mon P Wang63307c32008-05-05 19:05:59 +00007000 // Update thisMBB to fall through to newMBB
7001 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007002
Mon P Wang63307c32008-05-05 19:05:59 +00007003 // newMBB jumps to itself and fall through to nextMBB
7004 newMBB->addSuccessor(nextMBB);
7005 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007006
Mon P Wang63307c32008-05-05 19:05:59 +00007007 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007008 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007009 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007010 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007011 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007012 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007013 int numArgs = bInstr->getNumOperands() - 1;
7014 for (int i=0; i < numArgs; ++i)
7015 argOpers[i] = &bInstr->getOperand(i+1);
7016
7017 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007018 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7019 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007020
Dale Johannesen140be2d2008-08-19 18:47:28 +00007021 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007022 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007023 for (int i=0; i <= lastAddrIndx; ++i)
7024 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007025
Dale Johannesen140be2d2008-08-19 18:47:28 +00007026 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007027 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007028 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007030 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007031 tt = t1;
7032
Dale Johannesen140be2d2008-08-19 18:47:28 +00007033 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007034 assert((argOpers[valArgIndx]->isReg() ||
7035 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007036 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007037 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007038 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007039 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007040 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007041 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007042 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007043
Dale Johannesene4d209d2009-02-03 20:21:25 +00007044 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007045 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007046
Dale Johannesene4d209d2009-02-03 20:21:25 +00007047 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007048 for (int i=0; i <= lastAddrIndx; ++i)
7049 (*MIB).addOperand(*argOpers[i]);
7050 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007051 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7052 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7053
Dale Johannesene4d209d2009-02-03 20:21:25 +00007054 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007055 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007056
Mon P Wang63307c32008-05-05 19:05:59 +00007057 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007058 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007059
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007060 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007061 return nextMBB;
7062}
7063
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007064// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007065MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007066X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7067 MachineBasicBlock *MBB,
7068 unsigned regOpcL,
7069 unsigned regOpcH,
7070 unsigned immOpcL,
7071 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007072 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007073 // For the atomic bitwise operator, we generate
7074 // thisMBB (instructions are in pairs, except cmpxchg8b)
7075 // ld t1,t2 = [bitinstr.addr]
7076 // newMBB:
7077 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7078 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007079 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007080 // mov ECX, EBX <- t5, t6
7081 // mov EAX, EDX <- t1, t2
7082 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7083 // mov t3, t4 <- EAX, EDX
7084 // bz newMBB
7085 // result in out1, out2
7086 // fallthrough -->nextMBB
7087
7088 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7089 const unsigned LoadOpc = X86::MOV32rm;
7090 const unsigned copyOpc = X86::MOV32rr;
7091 const unsigned NotOpc = X86::NOT32r;
7092 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7093 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7094 MachineFunction::iterator MBBIter = MBB;
7095 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007096
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007097 /// First build the CFG
7098 MachineFunction *F = MBB->getParent();
7099 MachineBasicBlock *thisMBB = MBB;
7100 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7101 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7102 F->insert(MBBIter, newMBB);
7103 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007104
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007105 // Move all successors to thisMBB to nextMBB
7106 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007107
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007108 // Update thisMBB to fall through to newMBB
7109 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007110
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007111 // newMBB jumps to itself and fall through to nextMBB
7112 newMBB->addSuccessor(nextMBB);
7113 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007114
Dale Johannesene4d209d2009-02-03 20:21:25 +00007115 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007116 // Insert instructions into newMBB based on incoming instruction
7117 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007118 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007119 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007120 MachineOperand& dest1Oper = bInstr->getOperand(0);
7121 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007122 MachineOperand* argOpers[2 + X86AddrNumOperands];
7123 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007124 argOpers[i] = &bInstr->getOperand(i+2);
7125
7126 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007127 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007128
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007129 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007130 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007131 for (int i=0; i <= lastAddrIndx; ++i)
7132 (*MIB).addOperand(*argOpers[i]);
7133 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007134 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007135 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007136 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007137 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007138 MachineOperand newOp3 = *(argOpers[3]);
7139 if (newOp3.isImm())
7140 newOp3.setImm(newOp3.getImm()+4);
7141 else
7142 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007143 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007144 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007145
7146 // t3/4 are defined later, at the bottom of the loop
7147 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7148 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007149 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007150 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007151 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007152 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7153
7154 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7155 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007156 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007157 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7158 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007159 } else {
7160 tt1 = t1;
7161 tt2 = t2;
7162 }
7163
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007164 int valArgIndx = lastAddrIndx + 1;
7165 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007166 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007167 "invalid operand");
7168 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7169 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007170 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007171 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007172 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007173 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007174 if (regOpcL != X86::MOV32rr)
7175 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007176 (*MIB).addOperand(*argOpers[valArgIndx]);
7177 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007178 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007179 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007180 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007181 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007182 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007183 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007184 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007185 if (regOpcH != X86::MOV32rr)
7186 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007187 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007188
Dale Johannesene4d209d2009-02-03 20:21:25 +00007189 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007190 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007191 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007192 MIB.addReg(t2);
7193
Dale Johannesene4d209d2009-02-03 20:21:25 +00007194 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007195 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007196 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007197 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007198
Dale Johannesene4d209d2009-02-03 20:21:25 +00007199 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007200 for (int i=0; i <= lastAddrIndx; ++i)
7201 (*MIB).addOperand(*argOpers[i]);
7202
7203 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7204 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7205
Dale Johannesene4d209d2009-02-03 20:21:25 +00007206 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007207 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007208 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007209 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007210
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007211 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007212 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007213
7214 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7215 return nextMBB;
7216}
7217
7218// private utility function
7219MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007220X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7221 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007222 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007223 // For the atomic min/max operator, we generate
7224 // thisMBB:
7225 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007226 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007227 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007228 // cmp t1, t2
7229 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007230 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007231 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7232 // bz newMBB
7233 // fallthrough -->nextMBB
7234 //
7235 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7236 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007237 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007238 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007239
Mon P Wang63307c32008-05-05 19:05:59 +00007240 /// First build the CFG
7241 MachineFunction *F = MBB->getParent();
7242 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007243 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7244 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7245 F->insert(MBBIter, newMBB);
7246 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007247
Mon P Wang63307c32008-05-05 19:05:59 +00007248 // Move all successors to thisMBB to nextMBB
7249 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007250
Mon P Wang63307c32008-05-05 19:05:59 +00007251 // Update thisMBB to fall through to newMBB
7252 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007253
Mon P Wang63307c32008-05-05 19:05:59 +00007254 // newMBB jumps to newMBB and fall through to nextMBB
7255 newMBB->addSuccessor(nextMBB);
7256 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007257
Dale Johannesene4d209d2009-02-03 20:21:25 +00007258 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007259 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007260 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007261 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007262 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007263 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007264 int numArgs = mInstr->getNumOperands() - 1;
7265 for (int i=0; i < numArgs; ++i)
7266 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007267
Mon P Wang63307c32008-05-05 19:05:59 +00007268 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007269 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7270 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007271
Mon P Wangab3e7472008-05-05 22:56:23 +00007272 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007273 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007274 for (int i=0; i <= lastAddrIndx; ++i)
7275 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007276
Mon P Wang63307c32008-05-05 19:05:59 +00007277 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007278 assert((argOpers[valArgIndx]->isReg() ||
7279 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007280 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007281
7282 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007283 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007284 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007285 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007286 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007287 (*MIB).addOperand(*argOpers[valArgIndx]);
7288
Dale Johannesene4d209d2009-02-03 20:21:25 +00007289 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007290 MIB.addReg(t1);
7291
Dale Johannesene4d209d2009-02-03 20:21:25 +00007292 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007293 MIB.addReg(t1);
7294 MIB.addReg(t2);
7295
7296 // Generate movc
7297 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007298 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007299 MIB.addReg(t2);
7300 MIB.addReg(t1);
7301
7302 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007303 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007304 for (int i=0; i <= lastAddrIndx; ++i)
7305 (*MIB).addOperand(*argOpers[i]);
7306 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007307 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7308 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007309
Dale Johannesene4d209d2009-02-03 20:21:25 +00007310 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007311 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007312
Mon P Wang63307c32008-05-05 19:05:59 +00007313 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007314 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007315
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007316 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007317 return nextMBB;
7318}
7319
7320
Evan Cheng60c07e12006-07-05 22:17:51 +00007321MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007322X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007323 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007324 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007325 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007326 switch (MI->getOpcode()) {
7327 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007328 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007329 case X86::CMOV_FR32:
7330 case X86::CMOV_FR64:
7331 case X86::CMOV_V4F32:
7332 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007333 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007334 // To "insert" a SELECT_CC instruction, we actually have to insert the
7335 // diamond control-flow pattern. The incoming instruction knows the
7336 // destination vreg to set, the condition code register to branch on, the
7337 // true/false values to select between, and a branch opcode to use.
7338 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007339 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007340 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007341
Evan Cheng60c07e12006-07-05 22:17:51 +00007342 // thisMBB:
7343 // ...
7344 // TrueVal = ...
7345 // cmpTY ccX, r1, r2
7346 // bCC copy1MBB
7347 // fallthrough --> copy0MBB
7348 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007349 MachineFunction *F = BB->getParent();
7350 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7351 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007352 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007353 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007354 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007355 F->insert(It, copy0MBB);
7356 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007357 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007358 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007359 sinkMBB->transferSuccessors(BB);
7360
7361 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007362 BB->addSuccessor(copy0MBB);
7363 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007364
Evan Cheng60c07e12006-07-05 22:17:51 +00007365 // copy0MBB:
7366 // %FalseValue = ...
7367 // # fallthrough to sinkMBB
7368 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007369
Evan Cheng60c07e12006-07-05 22:17:51 +00007370 // Update machine-CFG edges
7371 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007372
Evan Cheng60c07e12006-07-05 22:17:51 +00007373 // sinkMBB:
7374 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7375 // ...
7376 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007377 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007378 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7379 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7380
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007381 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007382 return BB;
7383 }
7384
Dale Johannesen849f2142007-07-03 00:53:03 +00007385 case X86::FP32_TO_INT16_IN_MEM:
7386 case X86::FP32_TO_INT32_IN_MEM:
7387 case X86::FP32_TO_INT64_IN_MEM:
7388 case X86::FP64_TO_INT16_IN_MEM:
7389 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007390 case X86::FP64_TO_INT64_IN_MEM:
7391 case X86::FP80_TO_INT16_IN_MEM:
7392 case X86::FP80_TO_INT32_IN_MEM:
7393 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007394 // Change the floating point control register to use "round towards zero"
7395 // mode when truncating to an integer value.
7396 MachineFunction *F = BB->getParent();
7397 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007398 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007399
7400 // Load the old value of the high byte of the control word...
7401 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007402 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007403 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007404 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007405
7406 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007407 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007408 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007409
7410 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007411 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007412
7413 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007414 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007415 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007416
7417 // Get the X86 opcode to use.
7418 unsigned Opc;
7419 switch (MI->getOpcode()) {
7420 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007421 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7422 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7423 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7424 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7425 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7426 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007427 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7428 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7429 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007430 }
7431
7432 X86AddressMode AM;
7433 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007434 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007435 AM.BaseType = X86AddressMode::RegBase;
7436 AM.Base.Reg = Op.getReg();
7437 } else {
7438 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007439 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007440 }
7441 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007442 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007443 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007444 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007445 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007446 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007447 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007448 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007449 AM.GV = Op.getGlobal();
7450 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007451 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007452 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007453 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007454 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007455
7456 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007457 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007458
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007459 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007460 return BB;
7461 }
Mon P Wang63307c32008-05-05 19:05:59 +00007462 case X86::ATOMAND32:
7463 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007464 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007465 X86::LCMPXCHG32, X86::MOV32rr,
7466 X86::NOT32r, X86::EAX,
7467 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007468 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007469 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7470 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007471 X86::LCMPXCHG32, X86::MOV32rr,
7472 X86::NOT32r, X86::EAX,
7473 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007474 case X86::ATOMXOR32:
7475 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007476 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007477 X86::LCMPXCHG32, X86::MOV32rr,
7478 X86::NOT32r, X86::EAX,
7479 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007480 case X86::ATOMNAND32:
7481 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007482 X86::AND32ri, X86::MOV32rm,
7483 X86::LCMPXCHG32, X86::MOV32rr,
7484 X86::NOT32r, X86::EAX,
7485 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007486 case X86::ATOMMIN32:
7487 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7488 case X86::ATOMMAX32:
7489 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7490 case X86::ATOMUMIN32:
7491 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7492 case X86::ATOMUMAX32:
7493 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007494
7495 case X86::ATOMAND16:
7496 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7497 X86::AND16ri, X86::MOV16rm,
7498 X86::LCMPXCHG16, X86::MOV16rr,
7499 X86::NOT16r, X86::AX,
7500 X86::GR16RegisterClass);
7501 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007502 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007503 X86::OR16ri, X86::MOV16rm,
7504 X86::LCMPXCHG16, X86::MOV16rr,
7505 X86::NOT16r, X86::AX,
7506 X86::GR16RegisterClass);
7507 case X86::ATOMXOR16:
7508 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7509 X86::XOR16ri, X86::MOV16rm,
7510 X86::LCMPXCHG16, X86::MOV16rr,
7511 X86::NOT16r, X86::AX,
7512 X86::GR16RegisterClass);
7513 case X86::ATOMNAND16:
7514 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7515 X86::AND16ri, X86::MOV16rm,
7516 X86::LCMPXCHG16, X86::MOV16rr,
7517 X86::NOT16r, X86::AX,
7518 X86::GR16RegisterClass, true);
7519 case X86::ATOMMIN16:
7520 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7521 case X86::ATOMMAX16:
7522 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7523 case X86::ATOMUMIN16:
7524 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7525 case X86::ATOMUMAX16:
7526 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7527
7528 case X86::ATOMAND8:
7529 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7530 X86::AND8ri, X86::MOV8rm,
7531 X86::LCMPXCHG8, X86::MOV8rr,
7532 X86::NOT8r, X86::AL,
7533 X86::GR8RegisterClass);
7534 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007535 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007536 X86::OR8ri, X86::MOV8rm,
7537 X86::LCMPXCHG8, X86::MOV8rr,
7538 X86::NOT8r, X86::AL,
7539 X86::GR8RegisterClass);
7540 case X86::ATOMXOR8:
7541 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7542 X86::XOR8ri, X86::MOV8rm,
7543 X86::LCMPXCHG8, X86::MOV8rr,
7544 X86::NOT8r, X86::AL,
7545 X86::GR8RegisterClass);
7546 case X86::ATOMNAND8:
7547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7548 X86::AND8ri, X86::MOV8rm,
7549 X86::LCMPXCHG8, X86::MOV8rr,
7550 X86::NOT8r, X86::AL,
7551 X86::GR8RegisterClass, true);
7552 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007553 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007554 case X86::ATOMAND64:
7555 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007556 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007557 X86::LCMPXCHG64, X86::MOV64rr,
7558 X86::NOT64r, X86::RAX,
7559 X86::GR64RegisterClass);
7560 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007561 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7562 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007563 X86::LCMPXCHG64, X86::MOV64rr,
7564 X86::NOT64r, X86::RAX,
7565 X86::GR64RegisterClass);
7566 case X86::ATOMXOR64:
7567 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007568 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007569 X86::LCMPXCHG64, X86::MOV64rr,
7570 X86::NOT64r, X86::RAX,
7571 X86::GR64RegisterClass);
7572 case X86::ATOMNAND64:
7573 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7574 X86::AND64ri32, X86::MOV64rm,
7575 X86::LCMPXCHG64, X86::MOV64rr,
7576 X86::NOT64r, X86::RAX,
7577 X86::GR64RegisterClass, true);
7578 case X86::ATOMMIN64:
7579 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7580 case X86::ATOMMAX64:
7581 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7582 case X86::ATOMUMIN64:
7583 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7584 case X86::ATOMUMAX64:
7585 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007586
7587 // This group does 64-bit operations on a 32-bit host.
7588 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007589 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007590 X86::AND32rr, X86::AND32rr,
7591 X86::AND32ri, X86::AND32ri,
7592 false);
7593 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007594 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007595 X86::OR32rr, X86::OR32rr,
7596 X86::OR32ri, X86::OR32ri,
7597 false);
7598 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007599 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007600 X86::XOR32rr, X86::XOR32rr,
7601 X86::XOR32ri, X86::XOR32ri,
7602 false);
7603 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007604 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007605 X86::AND32rr, X86::AND32rr,
7606 X86::AND32ri, X86::AND32ri,
7607 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007608 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007609 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007610 X86::ADD32rr, X86::ADC32rr,
7611 X86::ADD32ri, X86::ADC32ri,
7612 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007613 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007614 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007615 X86::SUB32rr, X86::SBB32rr,
7616 X86::SUB32ri, X86::SBB32ri,
7617 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007618 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007619 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007620 X86::MOV32rr, X86::MOV32rr,
7621 X86::MOV32ri, X86::MOV32ri,
7622 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007623 }
7624}
7625
7626//===----------------------------------------------------------------------===//
7627// X86 Optimization Hooks
7628//===----------------------------------------------------------------------===//
7629
Dan Gohman475871a2008-07-27 21:46:04 +00007630void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007631 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007632 APInt &KnownZero,
7633 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007634 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007635 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007636 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007637 assert((Opc >= ISD::BUILTIN_OP_END ||
7638 Opc == ISD::INTRINSIC_WO_CHAIN ||
7639 Opc == ISD::INTRINSIC_W_CHAIN ||
7640 Opc == ISD::INTRINSIC_VOID) &&
7641 "Should use MaskedValueIsZero if you don't know whether Op"
7642 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007643
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007644 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007645 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007646 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007647 case X86ISD::ADD:
7648 case X86ISD::SUB:
7649 case X86ISD::SMUL:
7650 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007651 case X86ISD::INC:
7652 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007653 // These nodes' second result is a boolean.
7654 if (Op.getResNo() == 0)
7655 break;
7656 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007657 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007658 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7659 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007660 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007661 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007662}
Chris Lattner259e97c2006-01-31 19:43:35 +00007663
Evan Cheng206ee9d2006-07-07 08:33:52 +00007664/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007665/// node is a GlobalAddress + offset.
7666bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7667 GlobalValue* &GA, int64_t &Offset) const{
7668 if (N->getOpcode() == X86ISD::Wrapper) {
7669 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007670 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007671 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007672 return true;
7673 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007674 }
Evan Chengad4196b2008-05-12 19:56:52 +00007675 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007676}
7677
Evan Chengad4196b2008-05-12 19:56:52 +00007678static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7679 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007680 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007681 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007682 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007683 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007684 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007685 return false;
7686}
7687
Nate Begeman9008ca62009-04-27 18:41:29 +00007688static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Eli Friedman7a5e5552009-06-07 06:52:44 +00007689 MVT EVT, LoadSDNode *&LDBase,
7690 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00007691 SelectionDAG &DAG, MachineFrameInfo *MFI,
7692 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007693 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00007694 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007695 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007696 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007697 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00007698 return false;
7699 continue;
7700 }
7701
Dan Gohman475871a2008-07-27 21:46:04 +00007702 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007703 if (!Elt.getNode() ||
7704 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007705 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007706 if (!LDBase) {
7707 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00007708 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007709 LDBase = cast<LoadSDNode>(Elt.getNode());
7710 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007711 continue;
7712 }
7713 if (Elt.getOpcode() == ISD::UNDEF)
7714 continue;
7715
Nate Begemanabc01992009-06-05 21:37:30 +00007716 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Nate Begemanabc01992009-06-05 21:37:30 +00007717 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007718 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007719 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007720 }
7721 return true;
7722}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007723
7724/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7725/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7726/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007727/// order. In the case of v2i64, it will see if it can rewrite the
7728/// shuffle to be an appropriate build vector so it can take advantage of
7729// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007730static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007731 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007732 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007733 MVT VT = N->getValueType(0);
7734 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007735 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7736 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007737
Eli Friedman7a5e5552009-06-07 06:52:44 +00007738 if (VT.getSizeInBits() != 128)
7739 return SDValue();
7740
Mon P Wang1e955802009-04-03 02:43:30 +00007741 // Try to combine a vector_shuffle into a 128-bit load.
7742 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00007743 LoadSDNode *LD = NULL;
7744 unsigned LastLoadedElt;
7745 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG,
7746 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007747 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007748
Eli Friedman7a5e5552009-06-07 06:52:44 +00007749 if (LastLoadedElt == NumElems - 1) {
7750 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI))
7751 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7752 LD->getSrcValue(), LD->getSrcValueOffset(),
7753 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007754 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007755 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00007756 LD->isVolatile(), LD->getAlignment());
7757 } else if (NumElems == 4 && LastLoadedElt == 1) {
7758 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00007759 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7760 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00007761 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7762 }
7763 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007764}
Evan Chengd880b972008-05-09 21:53:03 +00007765
Chris Lattner83e6c992006-10-04 06:57:07 +00007766/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007767static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007768 const X86Subtarget *Subtarget) {
7769 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007770 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007771 // Get the LHS/RHS of the select.
7772 SDValue LHS = N->getOperand(1);
7773 SDValue RHS = N->getOperand(2);
7774
Chris Lattner83e6c992006-10-04 06:57:07 +00007775 // If we have SSE[12] support, try to form min/max nodes.
7776 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007777 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7778 Cond.getOpcode() == ISD::SETCC) {
7779 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007780
Chris Lattner47b4ce82009-03-11 05:48:52 +00007781 unsigned Opcode = 0;
7782 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7783 switch (CC) {
7784 default: break;
7785 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7786 case ISD::SETULE:
7787 case ISD::SETLE:
7788 if (!UnsafeFPMath) break;
7789 // FALL THROUGH.
7790 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7791 case ISD::SETLT:
7792 Opcode = X86ISD::FMIN;
7793 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007794
Chris Lattner47b4ce82009-03-11 05:48:52 +00007795 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7796 case ISD::SETUGT:
7797 case ISD::SETGT:
7798 if (!UnsafeFPMath) break;
7799 // FALL THROUGH.
7800 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7801 case ISD::SETGE:
7802 Opcode = X86ISD::FMAX;
7803 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007804 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007805 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7806 switch (CC) {
7807 default: break;
7808 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7809 case ISD::SETUGT:
7810 case ISD::SETGT:
7811 if (!UnsafeFPMath) break;
7812 // FALL THROUGH.
7813 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7814 case ISD::SETGE:
7815 Opcode = X86ISD::FMIN;
7816 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007817
Chris Lattner47b4ce82009-03-11 05:48:52 +00007818 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7819 case ISD::SETULE:
7820 case ISD::SETLE:
7821 if (!UnsafeFPMath) break;
7822 // FALL THROUGH.
7823 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7824 case ISD::SETLT:
7825 Opcode = X86ISD::FMAX;
7826 break;
7827 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007828 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007829
Chris Lattner47b4ce82009-03-11 05:48:52 +00007830 if (Opcode)
7831 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007832 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007833
Chris Lattnerd1980a52009-03-12 06:52:53 +00007834 // If this is a select between two integer constants, try to do some
7835 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007836 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7837 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007838 // Don't do this for crazy integer types.
7839 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7840 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007841 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007842 bool NeedsCondInvert = false;
7843
Chris Lattnercee56e72009-03-13 05:53:31 +00007844 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007845 // Efficiently invertible.
7846 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7847 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7848 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7849 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007850 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007851 }
7852
7853 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007854 if (FalseC->getAPIntValue() == 0 &&
7855 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007856 if (NeedsCondInvert) // Invert the condition if needed.
7857 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7858 DAG.getConstant(1, Cond.getValueType()));
7859
7860 // Zero extend the condition if needed.
7861 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7862
Chris Lattnercee56e72009-03-13 05:53:31 +00007863 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007864 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7865 DAG.getConstant(ShAmt, MVT::i8));
7866 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007867
7868 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007869 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007870 if (NeedsCondInvert) // Invert the condition if needed.
7871 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7872 DAG.getConstant(1, Cond.getValueType()));
7873
7874 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007875 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7876 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007877 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007878 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007879 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007880
7881 // Optimize cases that will turn into an LEA instruction. This requires
7882 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7883 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7884 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7885 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7886
7887 bool isFastMultiplier = false;
7888 if (Diff < 10) {
7889 switch ((unsigned char)Diff) {
7890 default: break;
7891 case 1: // result = add base, cond
7892 case 2: // result = lea base( , cond*2)
7893 case 3: // result = lea base(cond, cond*2)
7894 case 4: // result = lea base( , cond*4)
7895 case 5: // result = lea base(cond, cond*4)
7896 case 8: // result = lea base( , cond*8)
7897 case 9: // result = lea base(cond, cond*8)
7898 isFastMultiplier = true;
7899 break;
7900 }
7901 }
7902
7903 if (isFastMultiplier) {
7904 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7905 if (NeedsCondInvert) // Invert the condition if needed.
7906 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7907 DAG.getConstant(1, Cond.getValueType()));
7908
7909 // Zero extend the condition if needed.
7910 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7911 Cond);
7912 // Scale the condition by the difference.
7913 if (Diff != 1)
7914 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7915 DAG.getConstant(Diff, Cond.getValueType()));
7916
7917 // Add the base if non-zero.
7918 if (FalseC->getAPIntValue() != 0)
7919 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7920 SDValue(FalseC, 0));
7921 return Cond;
7922 }
7923 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007924 }
7925 }
7926
Dan Gohman475871a2008-07-27 21:46:04 +00007927 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007928}
7929
Chris Lattnerd1980a52009-03-12 06:52:53 +00007930/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7931static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7932 TargetLowering::DAGCombinerInfo &DCI) {
7933 DebugLoc DL = N->getDebugLoc();
7934
7935 // If the flag operand isn't dead, don't touch this CMOV.
7936 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
7937 return SDValue();
7938
7939 // If this is a select between two integer constants, try to do some
7940 // optimizations. Note that the operands are ordered the opposite of SELECT
7941 // operands.
7942 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
7943 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
7944 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
7945 // larger than FalseC (the false value).
7946 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
7947
7948 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
7949 CC = X86::GetOppositeBranchCondition(CC);
7950 std::swap(TrueC, FalseC);
7951 }
7952
7953 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007954 // This is efficient for any integer data type (including i8/i16) and
7955 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007956 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
7957 SDValue Cond = N->getOperand(3);
7958 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7959 DAG.getConstant(CC, MVT::i8), Cond);
7960
7961 // Zero extend the condition if needed.
7962 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
7963
7964 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
7965 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
7966 DAG.getConstant(ShAmt, MVT::i8));
7967 if (N->getNumValues() == 2) // Dead flag value?
7968 return DCI.CombineTo(N, Cond, SDValue());
7969 return Cond;
7970 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007971
7972 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
7973 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00007974 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
7975 SDValue Cond = N->getOperand(3);
7976 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7977 DAG.getConstant(CC, MVT::i8), Cond);
7978
7979 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007980 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7981 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007982 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7983 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00007984
Chris Lattner97a29a52009-03-13 05:22:11 +00007985 if (N->getNumValues() == 2) // Dead flag value?
7986 return DCI.CombineTo(N, Cond, SDValue());
7987 return Cond;
7988 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007989
7990 // Optimize cases that will turn into an LEA instruction. This requires
7991 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7992 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7993 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7994 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7995
7996 bool isFastMultiplier = false;
7997 if (Diff < 10) {
7998 switch ((unsigned char)Diff) {
7999 default: break;
8000 case 1: // result = add base, cond
8001 case 2: // result = lea base( , cond*2)
8002 case 3: // result = lea base(cond, cond*2)
8003 case 4: // result = lea base( , cond*4)
8004 case 5: // result = lea base(cond, cond*4)
8005 case 8: // result = lea base( , cond*8)
8006 case 9: // result = lea base(cond, cond*8)
8007 isFastMultiplier = true;
8008 break;
8009 }
8010 }
8011
8012 if (isFastMultiplier) {
8013 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8014 SDValue Cond = N->getOperand(3);
8015 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8016 DAG.getConstant(CC, MVT::i8), Cond);
8017 // Zero extend the condition if needed.
8018 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8019 Cond);
8020 // Scale the condition by the difference.
8021 if (Diff != 1)
8022 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8023 DAG.getConstant(Diff, Cond.getValueType()));
8024
8025 // Add the base if non-zero.
8026 if (FalseC->getAPIntValue() != 0)
8027 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8028 SDValue(FalseC, 0));
8029 if (N->getNumValues() == 2) // Dead flag value?
8030 return DCI.CombineTo(N, Cond, SDValue());
8031 return Cond;
8032 }
8033 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008034 }
8035 }
8036 return SDValue();
8037}
8038
8039
Evan Cheng0b0cd912009-03-28 05:57:29 +00008040/// PerformMulCombine - Optimize a single multiply with constant into two
8041/// in order to implement it with two cheaper instructions, e.g.
8042/// LEA + SHL, LEA + LEA.
8043static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8044 TargetLowering::DAGCombinerInfo &DCI) {
8045 if (DAG.getMachineFunction().
8046 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8047 return SDValue();
8048
8049 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8050 return SDValue();
8051
8052 MVT VT = N->getValueType(0);
8053 if (VT != MVT::i64)
8054 return SDValue();
8055
8056 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8057 if (!C)
8058 return SDValue();
8059 uint64_t MulAmt = C->getZExtValue();
8060 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8061 return SDValue();
8062
8063 uint64_t MulAmt1 = 0;
8064 uint64_t MulAmt2 = 0;
8065 if ((MulAmt % 9) == 0) {
8066 MulAmt1 = 9;
8067 MulAmt2 = MulAmt / 9;
8068 } else if ((MulAmt % 5) == 0) {
8069 MulAmt1 = 5;
8070 MulAmt2 = MulAmt / 5;
8071 } else if ((MulAmt % 3) == 0) {
8072 MulAmt1 = 3;
8073 MulAmt2 = MulAmt / 3;
8074 }
8075 if (MulAmt2 &&
8076 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8077 DebugLoc DL = N->getDebugLoc();
8078
8079 if (isPowerOf2_64(MulAmt2) &&
8080 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8081 // If second multiplifer is pow2, issue it first. We want the multiply by
8082 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8083 // is an add.
8084 std::swap(MulAmt1, MulAmt2);
8085
8086 SDValue NewMul;
8087 if (isPowerOf2_64(MulAmt1))
8088 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8089 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8090 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008091 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008092 DAG.getConstant(MulAmt1, VT));
8093
8094 if (isPowerOf2_64(MulAmt2))
8095 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8096 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8097 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008098 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008099 DAG.getConstant(MulAmt2, VT));
8100
8101 // Do not add new nodes to DAG combiner worklist.
8102 DCI.CombineTo(N, NewMul, false);
8103 }
8104 return SDValue();
8105}
8106
8107
Nate Begeman740ab032009-01-26 00:52:55 +00008108/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8109/// when possible.
8110static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8111 const X86Subtarget *Subtarget) {
8112 // On X86 with SSE2 support, we can transform this to a vector shift if
8113 // all elements are shifted by the same amount. We can't do this in legalize
8114 // because the a constant vector is typically transformed to a constant pool
8115 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008116 if (!Subtarget->hasSSE2())
8117 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008118
Nate Begeman740ab032009-01-26 00:52:55 +00008119 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008120 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8121 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008122
Mon P Wang3becd092009-01-28 08:12:05 +00008123 SDValue ShAmtOp = N->getOperand(1);
8124 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008125 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008126 SDValue BaseShAmt;
8127 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8128 unsigned NumElts = VT.getVectorNumElements();
8129 unsigned i = 0;
8130 for (; i != NumElts; ++i) {
8131 SDValue Arg = ShAmtOp.getOperand(i);
8132 if (Arg.getOpcode() == ISD::UNDEF) continue;
8133 BaseShAmt = Arg;
8134 break;
8135 }
8136 for (; i != NumElts; ++i) {
8137 SDValue Arg = ShAmtOp.getOperand(i);
8138 if (Arg.getOpcode() == ISD::UNDEF) continue;
8139 if (Arg != BaseShAmt) {
8140 return SDValue();
8141 }
8142 }
8143 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008144 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8145 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8146 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008147 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008148 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008149
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008150 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008151 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008152 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008153 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008154
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008155 // The shift amount is identical so we can do a vector shift.
8156 SDValue ValOp = N->getOperand(0);
8157 switch (N->getOpcode()) {
8158 default:
8159 assert(0 && "Unknown shift opcode!");
8160 break;
8161 case ISD::SHL:
8162 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008163 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008164 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8165 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008166 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008167 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008168 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8169 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008170 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008171 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008172 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8173 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008174 break;
8175 case ISD::SRA:
8176 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008177 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008178 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8179 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008180 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008181 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008182 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8183 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008184 break;
8185 case ISD::SRL:
8186 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008187 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008188 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8189 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008190 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008191 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008192 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8193 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008194 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008195 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008196 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8197 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008198 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008199 }
8200 return SDValue();
8201}
8202
Chris Lattner149a4e52008-02-22 02:09:43 +00008203/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008204static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008205 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008206 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8207 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008208 // A preferable solution to the general problem is to figure out the right
8209 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008210
8211 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008212 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008213 MVT VT = St->getValue().getValueType();
8214 if (VT.getSizeInBits() != 64)
8215 return SDValue();
8216
Devang Patel578efa92009-06-05 21:57:13 +00008217 const Function *F = DAG.getMachineFunction().getFunction();
8218 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8219 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8220 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008221 if ((VT.isVector() ||
8222 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008223 isa<LoadSDNode>(St->getValue()) &&
8224 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8225 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008226 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008227 LoadSDNode *Ld = 0;
8228 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008229 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008230 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008231 // Must be a store of a load. We currently handle two cases: the load
8232 // is a direct child, and it's under an intervening TokenFactor. It is
8233 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008234 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008235 Ld = cast<LoadSDNode>(St->getChain());
8236 else if (St->getValue().hasOneUse() &&
8237 ChainVal->getOpcode() == ISD::TokenFactor) {
8238 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008239 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008240 TokenFactorIndex = i;
8241 Ld = cast<LoadSDNode>(St->getValue());
8242 } else
8243 Ops.push_back(ChainVal->getOperand(i));
8244 }
8245 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008246
Evan Cheng536e6672009-03-12 05:59:15 +00008247 if (!Ld || !ISD::isNormalLoad(Ld))
8248 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008249
Evan Cheng536e6672009-03-12 05:59:15 +00008250 // If this is not the MMX case, i.e. we are just turning i64 load/store
8251 // into f64 load/store, avoid the transformation if there are multiple
8252 // uses of the loaded value.
8253 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8254 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008255
Evan Cheng536e6672009-03-12 05:59:15 +00008256 DebugLoc LdDL = Ld->getDebugLoc();
8257 DebugLoc StDL = N->getDebugLoc();
8258 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8259 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8260 // pair instead.
8261 if (Subtarget->is64Bit() || F64IsLegal) {
8262 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8263 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8264 Ld->getBasePtr(), Ld->getSrcValue(),
8265 Ld->getSrcValueOffset(), Ld->isVolatile(),
8266 Ld->getAlignment());
8267 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008268 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008269 Ops.push_back(NewChain);
8270 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008271 Ops.size());
8272 }
Evan Cheng536e6672009-03-12 05:59:15 +00008273 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008274 St->getSrcValue(), St->getSrcValueOffset(),
8275 St->isVolatile(), St->getAlignment());
8276 }
Evan Cheng536e6672009-03-12 05:59:15 +00008277
8278 // Otherwise, lower to two pairs of 32-bit loads / stores.
8279 SDValue LoAddr = Ld->getBasePtr();
8280 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8281 DAG.getConstant(4, MVT::i32));
8282
8283 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8284 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8285 Ld->isVolatile(), Ld->getAlignment());
8286 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8287 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8288 Ld->isVolatile(),
8289 MinAlign(Ld->getAlignment(), 4));
8290
8291 SDValue NewChain = LoLd.getValue(1);
8292 if (TokenFactorIndex != -1) {
8293 Ops.push_back(LoLd);
8294 Ops.push_back(HiLd);
8295 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8296 Ops.size());
8297 }
8298
8299 LoAddr = St->getBasePtr();
8300 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8301 DAG.getConstant(4, MVT::i32));
8302
8303 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8304 St->getSrcValue(), St->getSrcValueOffset(),
8305 St->isVolatile(), St->getAlignment());
8306 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8307 St->getSrcValue(),
8308 St->getSrcValueOffset() + 4,
8309 St->isVolatile(),
8310 MinAlign(St->getAlignment(), 4));
8311 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008312 }
Dan Gohman475871a2008-07-27 21:46:04 +00008313 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008314}
8315
Chris Lattner6cf73262008-01-25 06:14:17 +00008316/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8317/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008318static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008319 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8320 // F[X]OR(0.0, x) -> x
8321 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008322 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8323 if (C->getValueAPF().isPosZero())
8324 return N->getOperand(1);
8325 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8326 if (C->getValueAPF().isPosZero())
8327 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008328 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008329}
8330
8331/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008332static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008333 // FAND(0.0, x) -> 0.0
8334 // FAND(x, 0.0) -> 0.0
8335 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8336 if (C->getValueAPF().isPosZero())
8337 return N->getOperand(0);
8338 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8339 if (C->getValueAPF().isPosZero())
8340 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008341 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008342}
8343
Dan Gohmane5af2d32009-01-29 01:59:02 +00008344static SDValue PerformBTCombine(SDNode *N,
8345 SelectionDAG &DAG,
8346 TargetLowering::DAGCombinerInfo &DCI) {
8347 // BT ignores high bits in the bit index operand.
8348 SDValue Op1 = N->getOperand(1);
8349 if (Op1.hasOneUse()) {
8350 unsigned BitWidth = Op1.getValueSizeInBits();
8351 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8352 APInt KnownZero, KnownOne;
8353 TargetLowering::TargetLoweringOpt TLO(DAG);
8354 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8355 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8356 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8357 DCI.CommitTargetLoweringOpt(TLO);
8358 }
8359 return SDValue();
8360}
Chris Lattner83e6c992006-10-04 06:57:07 +00008361
Eli Friedman7a5e5552009-06-07 06:52:44 +00008362static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
8363 SDValue Op = N->getOperand(0);
8364 if (Op.getOpcode() == ISD::BIT_CONVERT)
8365 Op = Op.getOperand(0);
8366 MVT VT = N->getValueType(0), OpVT = Op.getValueType();
8367 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
8368 VT.getVectorElementType().getSizeInBits() ==
8369 OpVT.getVectorElementType().getSizeInBits()) {
8370 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
8371 }
8372 return SDValue();
8373}
8374
Dan Gohman475871a2008-07-27 21:46:04 +00008375SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008376 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008377 SelectionDAG &DAG = DCI.DAG;
8378 switch (N->getOpcode()) {
8379 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008380 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008381 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008382 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008383 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008384 case ISD::SHL:
8385 case ISD::SRA:
8386 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008387 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008388 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008389 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8390 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008391 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00008392 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008393 }
8394
Dan Gohman475871a2008-07-27 21:46:04 +00008395 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008396}
8397
Evan Cheng60c07e12006-07-05 22:17:51 +00008398//===----------------------------------------------------------------------===//
8399// X86 Inline Assembly Support
8400//===----------------------------------------------------------------------===//
8401
Chris Lattnerf4dff842006-07-11 02:54:03 +00008402/// getConstraintType - Given a constraint letter, return the type of
8403/// constraint it is for this target.
8404X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008405X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8406 if (Constraint.size() == 1) {
8407 switch (Constraint[0]) {
8408 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008409 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008410 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008411 case 'r':
8412 case 'R':
8413 case 'l':
8414 case 'q':
8415 case 'Q':
8416 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008417 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008418 case 'Y':
8419 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008420 case 'e':
8421 case 'Z':
8422 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008423 default:
8424 break;
8425 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008426 }
Chris Lattner4234f572007-03-25 02:14:49 +00008427 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008428}
8429
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008430/// LowerXConstraint - try to replace an X constraint, which matches anything,
8431/// with another that has more specific requirements based on the type of the
8432/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008433const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008434LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008435 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8436 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008437 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008438 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008439 return "Y";
8440 if (Subtarget->hasSSE1())
8441 return "x";
8442 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008443
Chris Lattner5e764232008-04-26 23:02:14 +00008444 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008445}
8446
Chris Lattner48884cd2007-08-25 00:47:38 +00008447/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8448/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008449void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008450 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008451 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008452 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008453 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008454 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008455
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008456 switch (Constraint) {
8457 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008458 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008460 if (C->getZExtValue() <= 31) {
8461 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008462 break;
8463 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008464 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008465 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008466 case 'J':
8467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008468 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00008469 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8470 break;
8471 }
8472 }
8473 return;
8474 case 'K':
8475 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008476 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00008477 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8478 break;
8479 }
8480 }
8481 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008482 case 'N':
8483 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008484 if (C->getZExtValue() <= 255) {
8485 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008486 break;
8487 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008488 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008489 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008490 case 'e': {
8491 // 32-bit signed value
8492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8493 const ConstantInt *CI = C->getConstantIntValue();
8494 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8495 // Widen to 64 bits here to get it sign extended.
8496 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8497 break;
8498 }
8499 // FIXME gcc accepts some relocatable values here too, but only in certain
8500 // memory models; it's complicated.
8501 }
8502 return;
8503 }
8504 case 'Z': {
8505 // 32-bit unsigned value
8506 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8507 const ConstantInt *CI = C->getConstantIntValue();
8508 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8509 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8510 break;
8511 }
8512 }
8513 // FIXME gcc accepts some relocatable values here too, but only in certain
8514 // memory models; it's complicated.
8515 return;
8516 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008517 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008518 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008519 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008520 // Widen to 64 bits here to get it sign extended.
8521 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008522 break;
8523 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008524
Chris Lattnerdc43a882007-05-03 16:52:29 +00008525 // If we are in non-pic codegen mode, we allow the address of a global (with
8526 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008527 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008528 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008529
Chris Lattner49921962009-05-08 18:23:14 +00008530 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8531 while (1) {
8532 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8533 Offset += GA->getOffset();
8534 break;
8535 } else if (Op.getOpcode() == ISD::ADD) {
8536 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8537 Offset += C->getZExtValue();
8538 Op = Op.getOperand(0);
8539 continue;
8540 }
8541 } else if (Op.getOpcode() == ISD::SUB) {
8542 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8543 Offset += -C->getZExtValue();
8544 Op = Op.getOperand(0);
8545 continue;
8546 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008547 }
Chris Lattner49921962009-05-08 18:23:14 +00008548
8549 // Otherwise, this isn't something we can handle, reject it.
8550 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008551 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008552
Chris Lattner49921962009-05-08 18:23:14 +00008553 if (hasMemory)
8554 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8555 else
8556 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8557 Offset);
8558 Result = Op;
8559 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008560 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008561 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008562
Gabor Greifba36cb52008-08-28 21:40:38 +00008563 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008564 Ops.push_back(Result);
8565 return;
8566 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008567 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8568 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008569}
8570
Chris Lattner259e97c2006-01-31 19:43:35 +00008571std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008572getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008573 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008574 if (Constraint.size() == 1) {
8575 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008576 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008577 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008578 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8579 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008580 if (VT == MVT::i32)
8581 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8582 else if (VT == MVT::i16)
8583 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8584 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008585 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008586 else if (VT == MVT::i64)
8587 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8588 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008589 }
8590 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008591
Chris Lattner1efa40f2006-02-22 00:56:39 +00008592 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008593}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008594
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008595std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008596X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008597 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008598 // First, see if this is a constraint that directly corresponds to an LLVM
8599 // register class.
8600 if (Constraint.size() == 1) {
8601 // GCC Constraint Letters
8602 switch (Constraint[0]) {
8603 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008604 case 'r': // GENERAL_REGS
8605 case 'R': // LEGACY_REGS
8606 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008607 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008608 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008609 if (VT == MVT::i16)
8610 return std::make_pair(0U, X86::GR16RegisterClass);
8611 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008612 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008613 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008614 case 'f': // FP Stack registers.
8615 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8616 // value to the correct fpstack register class.
8617 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8618 return std::make_pair(0U, X86::RFP32RegisterClass);
8619 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8620 return std::make_pair(0U, X86::RFP64RegisterClass);
8621 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008622 case 'y': // MMX_REGS if MMX allowed.
8623 if (!Subtarget->hasMMX()) break;
8624 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008625 case 'Y': // SSE_REGS if SSE2 allowed
8626 if (!Subtarget->hasSSE2()) break;
8627 // FALL THROUGH.
8628 case 'x': // SSE_REGS if SSE1 allowed
8629 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008630
8631 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008632 default: break;
8633 // Scalar SSE types.
8634 case MVT::f32:
8635 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008636 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008637 case MVT::f64:
8638 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008639 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008640 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008641 case MVT::v16i8:
8642 case MVT::v8i16:
8643 case MVT::v4i32:
8644 case MVT::v2i64:
8645 case MVT::v4f32:
8646 case MVT::v2f64:
8647 return std::make_pair(0U, X86::VR128RegisterClass);
8648 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008649 break;
8650 }
8651 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008652
Chris Lattnerf76d1802006-07-31 23:26:50 +00008653 // Use the default implementation in TargetLowering to convert the register
8654 // constraint into a member of a register class.
8655 std::pair<unsigned, const TargetRegisterClass*> Res;
8656 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008657
8658 // Not found as a standard register?
8659 if (Res.second == 0) {
8660 // GCC calls "st(0)" just plain "st".
8661 if (StringsEqualNoCase("{st}", Constraint)) {
8662 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008663 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008664 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008665 // 'A' means EAX + EDX.
8666 if (Constraint == "A") {
8667 Res.first = X86::EAX;
8668 Res.second = X86::GRADRegisterClass;
8669 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008670 return Res;
8671 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008672
Chris Lattnerf76d1802006-07-31 23:26:50 +00008673 // Otherwise, check to see if this is a register class of the wrong value
8674 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8675 // turn into {ax},{dx}.
8676 if (Res.second->hasType(VT))
8677 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008678
Chris Lattnerf76d1802006-07-31 23:26:50 +00008679 // All of the single-register GCC register classes map their values onto
8680 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8681 // really want an 8-bit or 32-bit register, map to the appropriate register
8682 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008683 if (Res.second == X86::GR16RegisterClass) {
8684 if (VT == MVT::i8) {
8685 unsigned DestReg = 0;
8686 switch (Res.first) {
8687 default: break;
8688 case X86::AX: DestReg = X86::AL; break;
8689 case X86::DX: DestReg = X86::DL; break;
8690 case X86::CX: DestReg = X86::CL; break;
8691 case X86::BX: DestReg = X86::BL; break;
8692 }
8693 if (DestReg) {
8694 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008695 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008696 }
8697 } else if (VT == MVT::i32) {
8698 unsigned DestReg = 0;
8699 switch (Res.first) {
8700 default: break;
8701 case X86::AX: DestReg = X86::EAX; break;
8702 case X86::DX: DestReg = X86::EDX; break;
8703 case X86::CX: DestReg = X86::ECX; break;
8704 case X86::BX: DestReg = X86::EBX; break;
8705 case X86::SI: DestReg = X86::ESI; break;
8706 case X86::DI: DestReg = X86::EDI; break;
8707 case X86::BP: DestReg = X86::EBP; break;
8708 case X86::SP: DestReg = X86::ESP; break;
8709 }
8710 if (DestReg) {
8711 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008712 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008713 }
8714 } else if (VT == MVT::i64) {
8715 unsigned DestReg = 0;
8716 switch (Res.first) {
8717 default: break;
8718 case X86::AX: DestReg = X86::RAX; break;
8719 case X86::DX: DestReg = X86::RDX; break;
8720 case X86::CX: DestReg = X86::RCX; break;
8721 case X86::BX: DestReg = X86::RBX; break;
8722 case X86::SI: DestReg = X86::RSI; break;
8723 case X86::DI: DestReg = X86::RDI; break;
8724 case X86::BP: DestReg = X86::RBP; break;
8725 case X86::SP: DestReg = X86::RSP; break;
8726 }
8727 if (DestReg) {
8728 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008729 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008730 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008731 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008732 } else if (Res.second == X86::FR32RegisterClass ||
8733 Res.second == X86::FR64RegisterClass ||
8734 Res.second == X86::VR128RegisterClass) {
8735 // Handle references to XMM physical registers that got mapped into the
8736 // wrong class. This can happen with constraints like {xmm0} where the
8737 // target independent register mapper will just pick the first match it can
8738 // find, ignoring the required type.
8739 if (VT == MVT::f32)
8740 Res.second = X86::FR32RegisterClass;
8741 else if (VT == MVT::f64)
8742 Res.second = X86::FR64RegisterClass;
8743 else if (X86::VR128RegisterClass->hasType(VT))
8744 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008745 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008746
Chris Lattnerf76d1802006-07-31 23:26:50 +00008747 return Res;
8748}
Mon P Wang0c397192008-10-30 08:01:45 +00008749
8750//===----------------------------------------------------------------------===//
8751// X86 Widen vector type
8752//===----------------------------------------------------------------------===//
8753
8754/// getWidenVectorType: given a vector type, returns the type to widen
8755/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8756/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008757/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008758/// scalarizing vs using the wider vector type.
8759
Dan Gohmanc13cf132009-01-15 17:34:08 +00008760MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008761 assert(VT.isVector());
8762 if (isTypeLegal(VT))
8763 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008764
Mon P Wang0c397192008-10-30 08:01:45 +00008765 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8766 // type based on element type. This would speed up our search (though
8767 // it may not be worth it since the size of the list is relatively
8768 // small).
8769 MVT EltVT = VT.getVectorElementType();
8770 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008771
Mon P Wang0c397192008-10-30 08:01:45 +00008772 // On X86, it make sense to widen any vector wider than 1
8773 if (NElts <= 1)
8774 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008775
8776 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008777 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8778 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008779
8780 if (isTypeLegal(SVT) &&
8781 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008782 SVT.getVectorNumElements() > NElts)
8783 return SVT;
8784 }
8785 return MVT::Other;
8786}