blob: 77c9f3d02a6fed9533bf429618a2e554d47d162b [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000022#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000025#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000026#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000030#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000032#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000033#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000034#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000037#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000038#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039using namespace llvm;
40
Mon P Wang3c81d352008-11-23 04:37:22 +000041static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000042DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000043
Evan Cheng10e86422008-04-25 19:11:04 +000044// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000045static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
46 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000047
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000048X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000050 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000051 X86ScalarSSEf64 = Subtarget->hasSSE2();
52 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000053 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000056 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000057
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000058 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000062 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000063 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000065 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000066
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000068 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000071 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Evan Cheng03294662008-10-14 21:26:46 +000087 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000088
Scott Michelfdc40a02009-02-17 22:15:04 +000089 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +000090 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000095 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
97 // SETOEQ and SETUNE require checking two conditions.
98 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
99 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
100 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
101 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
102 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
103 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000104
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
106 // operation.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
108 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
109 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000110
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000112 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000113 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000114 } else if (!UseSoftFloat) {
115 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000116 // We have an impenetrably clever algorithm for ui64->double only.
117 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000118 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000119 // We have an algorithm for SSE2, and we turn this into a 64-bit
120 // FILD for other targets.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000122 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000123
124 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
125 // this operation.
126 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
127 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000128
Devang Patel6a784892009-06-05 18:48:29 +0000129 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000130 // SSE has no i16 to fp conversion, only i32
131 if (X86ScalarSSEf32) {
132 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
133 // f32 and f64 cases are Legal, f80 case is not
134 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
135 } else {
136 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000139 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000140 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
141 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000142 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143
Dale Johannesen73328d12007-09-19 23:55:34 +0000144 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
145 // are Legal, f80 is custom lowered.
146 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
147 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000148
Evan Cheng02568ff2006-01-30 22:13:22 +0000149 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
150 // this operation.
151 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
152 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
153
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000154 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000155 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000156 // f32 and f64 cases are Legal, f80 case is not
157 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000158 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000159 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000160 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 }
162
163 // Handle FP_TO_UINT by promoting the destination to a larger signed
164 // conversion.
165 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
166 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
167 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
168
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 if (Subtarget->is64Bit()) {
170 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000172 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000173 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 // Expand FP_TO_UINT into a select.
175 // FIXME: We would like to use a Custom expander here eventually to do
176 // the optimal thing for SSE vs. the default expansion in the legalizer.
177 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
178 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000179 // With SSE3 we can use fisttpll to convert to a signed i64; without
180 // SSE, we're stuck with a fistpll.
181 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000182 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000183
Chris Lattner399610a2006-12-05 18:22:22 +0000184 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000185 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000186 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
187 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
188 }
Chris Lattner21f66852005-12-23 05:15:23 +0000189
Dan Gohmanb00ee212008-02-18 19:34:53 +0000190 // Scalar integer divide and remainder are lowered to use operations that
191 // produce two results, to match the available instructions. This exposes
192 // the two-result form to trivial CSE, which is able to combine x/y and x%y
193 // into a single instruction.
194 //
195 // Scalar integer multiply-high is also lowered to use two-result
196 // operations, to match the available instructions. However, plain multiply
197 // (low) operations are left as Legal, as there are single-result
198 // instructions for this in x86. Using the two-result multiply instructions
199 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
204 setOperationAction(ISD::SREM , MVT::i8 , Expand);
205 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000206 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
207 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
208 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
209 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
210 setOperationAction(ISD::SREM , MVT::i16 , Expand);
211 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000212 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
213 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
214 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
215 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
216 setOperationAction(ISD::SREM , MVT::i32 , Expand);
217 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000218 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
219 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
220 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
221 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
222 setOperationAction(ISD::SREM , MVT::i64 , Expand);
223 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000224
Evan Chengc35497f2006-10-30 08:02:39 +0000225 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000226 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000227 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
228 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
232 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
234 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000235 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000237 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000238 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000241 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
242 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000243 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000247 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
248 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit()) {
250 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000251 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
252 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 }
254
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000255 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000256 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000257
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000258 // These should be promoted to a larger select which is supported.
259 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
260 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000261 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000262 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
263 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
264 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
265 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000266 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
268 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
269 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
270 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
271 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000272 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (Subtarget->is64Bit()) {
274 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
275 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
276 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000277 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000278 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000279 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000280
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000281 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000282 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000283 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000285 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000286 if (Subtarget->is64Bit())
287 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000288 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
290 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
291 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
292 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000293 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000294 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000295 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000296 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
297 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
298 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000299 if (Subtarget->is64Bit()) {
300 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
301 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
302 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
303 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000304
Evan Chengd2cde682008-03-10 19:38:10 +0000305 if (Subtarget->hasSSE1())
306 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000307
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000308 if (!Subtarget->hasSSE2())
309 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
310
Mon P Wang63307c32008-05-05 19:05:59 +0000311 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
313 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
314 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
315 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000316
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
319 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000321
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000322 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000323 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
328 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000330 }
331
Dan Gohman7f460202008-06-30 20:59:49 +0000332 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
333 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000334 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000335 if (!Subtarget->isTargetDarwin() &&
336 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000337 !Subtarget->isTargetCygMing()) {
338 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
339 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
340 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000341
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000342 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
343 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
344 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
345 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
346 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000347 setExceptionPointerRegister(X86::RAX);
348 setExceptionSelectorRegister(X86::RDX);
349 } else {
350 setExceptionPointerRegister(X86::EAX);
351 setExceptionSelectorRegister(X86::EDX);
352 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000353 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000354 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
355
Duncan Sandsf7331b32007-09-11 14:10:23 +0000356 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000357
Chris Lattnerda68d302008-01-15 21:58:22 +0000358 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000359
Nate Begemanacc398c2006-01-25 18:21:52 +0000360 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
361 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000362 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000363 if (Subtarget->is64Bit()) {
364 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000365 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000366 } else {
367 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000368 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000369 }
Evan Chengae642192007-03-02 23:16:35 +0000370
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000371 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000372 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit())
374 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000375 if (Subtarget->isTargetCygMing())
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
377 else
378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000379
Evan Chengc7ce29b2009-02-13 22:36:38 +0000380 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000381 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000382 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000383 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
384 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000385
Evan Cheng223547a2006-01-31 22:28:30 +0000386 // Use ANDPD to simulate FABS.
387 setOperationAction(ISD::FABS , MVT::f64, Custom);
388 setOperationAction(ISD::FABS , MVT::f32, Custom);
389
390 // Use XORP to simulate FNEG.
391 setOperationAction(ISD::FNEG , MVT::f64, Custom);
392 setOperationAction(ISD::FNEG , MVT::f32, Custom);
393
Evan Cheng68c47cb2007-01-05 07:55:56 +0000394 // Use ANDPD and ORPD to simulate FCOPYSIGN.
395 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
396 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
397
Evan Chengd25e9e82006-02-02 00:28:23 +0000398 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000399 setOperationAction(ISD::FSIN , MVT::f64, Expand);
400 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000401 setOperationAction(ISD::FSIN , MVT::f32, Expand);
402 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403
Chris Lattnera54aa942006-01-29 06:26:08 +0000404 // Expand FP immediates into loads from the stack, except for the special
405 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000406 addLegalFPImmediate(APFloat(+0.0)); // xorpd
407 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000408 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000409 // Use SSE for f32, x87 for f64.
410 // Set up the FP register classes.
411 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
412 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
413
414 // Use ANDPS to simulate FABS.
415 setOperationAction(ISD::FABS , MVT::f32, Custom);
416
417 // Use XORP to simulate FNEG.
418 setOperationAction(ISD::FNEG , MVT::f32, Custom);
419
420 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
421
422 // Use ANDPS and ORPS to simulate FCOPYSIGN.
423 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
424 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
425
426 // We don't support sin/cos/fmod
427 setOperationAction(ISD::FSIN , MVT::f32, Expand);
428 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429
Nate Begemane1795842008-02-14 08:57:00 +0000430 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000431 addLegalFPImmediate(APFloat(+0.0f)); // xorps
432 addLegalFPImmediate(APFloat(+0.0)); // FLD0
433 addLegalFPImmediate(APFloat(+1.0)); // FLD1
434 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
435 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
436
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000441 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000442 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000443 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000444 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
445 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000446
Evan Cheng68c47cb2007-01-05 07:55:56 +0000447 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000448 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
450 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000451
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000456 addLegalFPImmediate(APFloat(+0.0)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000464 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000465
Dale Johannesen59a58732007-08-05 18:49:15 +0000466 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000467 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000468 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
469 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
471 {
472 bool ignored;
473 APFloat TmpFlt(+0.0);
474 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
475 &ignored);
476 addLegalFPImmediate(TmpFlt); // FLD0
477 TmpFlt.changeSign();
478 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
479 APFloat TmpFlt2(+1.0);
480 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
481 &ignored);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000486
Evan Chengc7ce29b2009-02-13 22:36:38 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000491 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000492
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000493 // Always use a library call for pow.
494 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
496 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
497
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000498 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000499 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000500 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000502 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
503
Mon P Wangf007a8b2008-11-06 05:31:54 +0000504 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000505 // (for widening) or expand (for scalarization). Then we will selectively
506 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000507 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
508 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000509 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000522 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000524 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000525 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000526 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000548 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000553 }
554
Evan Chengc7ce29b2009-02-13 22:36:38 +0000555 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
556 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000557 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000558 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
559 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
560 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000561 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000562 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000563
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000564 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
565 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
566 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000567 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000568
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000569 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
570 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
571 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000572 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000573
Bill Wendling74027e92007-03-15 21:24:36 +0000574 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
575 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
576
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000577 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000578 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000579 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000580 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
581 setOperationAction(ISD::AND, MVT::v2i32, Promote);
582 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
583 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000584
585 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000586 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000587 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000588 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
589 setOperationAction(ISD::OR, MVT::v2i32, Promote);
590 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
591 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000592
593 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000594 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000595 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000596 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
597 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
598 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
599 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000600
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000601 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000602 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000603 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000604 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
605 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
606 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000607 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
608 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000609 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000610
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000611 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
612 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
613 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000614 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000615 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000616
617 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
618 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000621
Evan Cheng52672b82008-07-22 18:39:19 +0000622 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000623 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
624 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000625 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000626
627 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000628
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000629 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000630 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
631 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
632 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
633 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
634 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000635 }
636
Evan Cheng92722532009-03-26 23:06:32 +0000637 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000638 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
639
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000640 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
641 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
642 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
643 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000644 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
645 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000646 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000649 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000650 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000651 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000652 }
653
Evan Cheng92722532009-03-26 23:06:32 +0000654 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000655 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000656
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000657 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
658 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000659 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
660 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
661 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
662 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
663
Evan Chengf7c378e2006-04-10 07:23:14 +0000664 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
665 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
666 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000667 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000668 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000669 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
670 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
671 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000672 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000673 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000674 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
675 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
676 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
677 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000678 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
679 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000680
Nate Begeman30a0de92008-07-17 16:51:19 +0000681 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
682 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
683 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
684 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000685
Evan Chengf7c378e2006-04-10 07:23:14 +0000686 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
687 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000689 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000690 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000691
Evan Cheng2c3ae372006-04-12 21:21:57 +0000692 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000693 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
694 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000695 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000696 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000697 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000698 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
699 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000701 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000702
Evan Cheng2c3ae372006-04-12 21:21:57 +0000703 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
704 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
705 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
706 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000707 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000708 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000709
Nate Begemancdd1eec2008-02-12 22:51:28 +0000710 if (Subtarget->is64Bit()) {
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000713 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000714
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000715 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000716 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000717 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
718 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
719 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
720 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
721 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
722 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
723 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
724 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
725 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
726 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000727 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000728
Chris Lattnerddf89562008-01-17 19:59:44 +0000729 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000730
Evan Cheng2c3ae372006-04-12 21:21:57 +0000731 // Custom lower v2i64 and v2f64 selects.
732 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000733 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000734 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000735 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000736
Evan Cheng470a6ad2006-02-22 02:26:30 +0000737 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000738
Nate Begeman14d12ca2008-02-11 04:19:36 +0000739 if (Subtarget->hasSSE41()) {
740 // FIXME: Do we need to handle scalar-to-vector here?
741 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
742
743 // i8 and i16 vectors are custom , because the source register and source
744 // source memory operand types are not the same width. f32 vectors are
745 // custom since the immediate controlling the insert encodes additional
746 // information.
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000749 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000750 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
751
752 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
753 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000754 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000755 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000756
757 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
759 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000760 }
761 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000762
Nate Begeman30a0de92008-07-17 16:51:19 +0000763 if (Subtarget->hasSSE42()) {
764 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
765 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000766
Evan Cheng6be2c582006-04-05 23:38:46 +0000767 // We want to custom lower some of our intrinsics.
768 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
769
Bill Wendling74c37652008-12-09 22:08:41 +0000770 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000771 setOperationAction(ISD::SADDO, MVT::i32, Custom);
772 setOperationAction(ISD::SADDO, MVT::i64, Custom);
773 setOperationAction(ISD::UADDO, MVT::i32, Custom);
774 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000775 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
776 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
777 setOperationAction(ISD::USUBO, MVT::i32, Custom);
778 setOperationAction(ISD::USUBO, MVT::i64, Custom);
779 setOperationAction(ISD::SMULO, MVT::i32, Custom);
780 setOperationAction(ISD::SMULO, MVT::i64, Custom);
781 setOperationAction(ISD::UMULO, MVT::i32, Custom);
782 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000783
Evan Chengd54f2d52009-03-31 19:38:51 +0000784 if (!Subtarget->is64Bit()) {
785 // These libcalls are not available in 32-bit.
786 setLibcallName(RTLIB::SHL_I128, 0);
787 setLibcallName(RTLIB::SRL_I128, 0);
788 setLibcallName(RTLIB::SRA_I128, 0);
789 }
790
Evan Cheng206ee9d2006-07-07 08:33:52 +0000791 // We have target-specific dag combine patterns for the following nodes:
792 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000793 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000794 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000795 setTargetDAGCombine(ISD::SHL);
796 setTargetDAGCombine(ISD::SRA);
797 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000798 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000799 if (Subtarget->is64Bit())
800 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000801
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000802 computeRegisterProperties();
803
Evan Cheng87ed7162006-02-14 08:25:08 +0000804 // FIXME: These should be based on subtarget info. Plus, the values should
805 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000806 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
807 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
808 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000809 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000810 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000811 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000812}
813
Scott Michel5b8f82e2008-03-10 15:42:14 +0000814
Duncan Sands5480c042009-01-01 15:52:00 +0000815MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000816 return MVT::i8;
817}
818
819
Evan Cheng29286502008-01-23 23:17:41 +0000820/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
821/// the desired ByVal argument alignment.
822static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
823 if (MaxAlign == 16)
824 return;
825 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
826 if (VTy->getBitWidth() == 128)
827 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000828 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
829 unsigned EltAlign = 0;
830 getMaxByValAlign(ATy->getElementType(), EltAlign);
831 if (EltAlign > MaxAlign)
832 MaxAlign = EltAlign;
833 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
834 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
835 unsigned EltAlign = 0;
836 getMaxByValAlign(STy->getElementType(i), EltAlign);
837 if (EltAlign > MaxAlign)
838 MaxAlign = EltAlign;
839 if (MaxAlign == 16)
840 break;
841 }
842 }
843 return;
844}
845
846/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
847/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000848/// that contain SSE vectors are placed at 16-byte boundaries while the rest
849/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000850unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000851 if (Subtarget->is64Bit()) {
852 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000853 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000854 if (TyAlign > 8)
855 return TyAlign;
856 return 8;
857 }
858
Evan Cheng29286502008-01-23 23:17:41 +0000859 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000860 if (Subtarget->hasSSE1())
861 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000862 return Align;
863}
Chris Lattner2b02a442007-02-25 08:29:00 +0000864
Evan Chengf0df0312008-05-15 08:39:06 +0000865/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000866/// and store operations as a result of memset, memcpy, and memmove
867/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000868/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000869MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000870X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
871 bool isSrcConst, bool isSrcStr) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000872 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
873 // linux. This is because the stack realignment code can't handle certain
874 // cases like PR2962. This should be removed when PR2962 is fixed.
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000875 if (!NoImplicitFloat && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000876 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
877 return MVT::v4i32;
878 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
879 return MVT::v4f32;
880 }
Evan Chengf0df0312008-05-15 08:39:06 +0000881 if (Subtarget->is64Bit() && Size >= 8)
882 return MVT::i64;
883 return MVT::i32;
884}
885
Evan Chengcc415862007-11-09 01:32:10 +0000886/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
887/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000888SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000889 SelectionDAG &DAG) const {
890 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000891 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000892 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000893 // This doesn't have DebugLoc associated with it, but is not really the
894 // same as a Register.
895 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
896 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000897 return Table;
898}
899
Chris Lattner2b02a442007-02-25 08:29:00 +0000900//===----------------------------------------------------------------------===//
901// Return Value Calling Convention Implementation
902//===----------------------------------------------------------------------===//
903
Chris Lattner59ed56b2007-02-28 04:55:35 +0000904#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000905
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000906/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000907SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000908 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000909 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000910
Chris Lattner9774c912007-02-27 05:28:59 +0000911 SmallVector<CCValAssign, 16> RVLocs;
912 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000913 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
914 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000915 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000916
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000917 // If this is the first return lowered for this function, add the regs to the
918 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000919 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000920 for (unsigned i = 0; i != RVLocs.size(); ++i)
921 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000922 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000923 }
Dan Gohman475871a2008-07-27 21:46:04 +0000924 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000925
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000926 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000927 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000928 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000929 SDValue TailCall = Chain;
930 SDValue TargetAddress = TailCall.getOperand(1);
931 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000932 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000933 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000934 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000935 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000936 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000937 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000938 assert(StackAdjustment.getOpcode() == ISD::Constant &&
939 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000940
Dan Gohman475871a2008-07-27 21:46:04 +0000941 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000942 Operands.push_back(Chain.getOperand(0));
943 Operands.push_back(TargetAddress);
944 Operands.push_back(StackAdjustment);
945 // Copy registers used by the call. Last operand is a flag so it is not
946 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000947 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000948 Operands.push_back(Chain.getOperand(i));
949 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000950 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000951 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000952 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000954 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000956
Dan Gohman475871a2008-07-27 21:46:04 +0000957 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000958 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
959 // Operand #1 = Bytes To Pop
960 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000961
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000962 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000963 for (unsigned i = 0; i != RVLocs.size(); ++i) {
964 CCValAssign &VA = RVLocs[i];
965 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000966 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000967
Chris Lattner447ff682008-03-11 03:23:40 +0000968 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
969 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000970 if (VA.getLocReg() == X86::ST0 ||
971 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000972 // If this is a copy from an xmm register to ST(0), use an FPExtend to
973 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000974 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000975 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000976 RetOps.push_back(ValToCopy);
977 // Don't emit a copytoreg.
978 continue;
979 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000980
Evan Cheng242b38b2009-02-23 09:03:22 +0000981 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
982 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +0000983 if (Subtarget->is64Bit()) {
984 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +0000985 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +0000986 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +0000987 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
988 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
989 }
Evan Cheng6140a8b2009-02-22 08:05:12 +0000990 }
991
Dale Johannesendd64c412009-02-04 00:33:20 +0000992 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000993 Flag = Chain.getValue(1);
994 }
Dan Gohman61a92132008-04-21 23:59:07 +0000995
996 // The x86-64 ABI for returning structs by value requires that we copy
997 // the sret argument into %rax for the return. We saved the argument into
998 // a virtual register in the entry block, so now we copy the value out
999 // and into %rax.
1000 if (Subtarget->is64Bit() &&
1001 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1002 MachineFunction &MF = DAG.getMachineFunction();
1003 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1004 unsigned Reg = FuncInfo->getSRetReturnReg();
1005 if (!Reg) {
1006 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1007 FuncInfo->setSRetReturnReg(Reg);
1008 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001009 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001010
Dale Johannesendd64c412009-02-04 00:33:20 +00001011 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001012 Flag = Chain.getValue(1);
1013 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001014
Chris Lattner447ff682008-03-11 03:23:40 +00001015 RetOps[0] = Chain; // Update chain.
1016
1017 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001018 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001019 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001020
1021 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001022 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001023}
1024
1025
Chris Lattner3085e152007-02-25 08:59:22 +00001026/// LowerCallResult - Lower the result values of an ISD::CALL into the
1027/// appropriate copies out of appropriate physical registers. This assumes that
1028/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1029/// being lowered. The returns a SDNode with the same number of values as the
1030/// ISD::CALL.
1031SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001032LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001033 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001034
Scott Michelfdc40a02009-02-17 22:15:04 +00001035 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001036 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001037 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001038 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001039 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001040 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001041 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1042
Dan Gohman475871a2008-07-27 21:46:04 +00001043 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001044
Chris Lattner3085e152007-02-25 08:59:22 +00001045 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001046 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001047 CCValAssign &VA = RVLocs[i];
1048 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001049
Torok Edwin3f142c32009-02-01 18:15:56 +00001050 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001051 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001052 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1053 cerr << "SSE register return with SSE disabled\n";
1054 exit(1);
1055 }
1056
Chris Lattner8e6da152008-03-10 21:08:41 +00001057 // If this is a call to a function that returns an fp value on the floating
1058 // point stack, but where we prefer to use the value in xmm registers, copy
1059 // 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 +00001060 if ((VA.getLocReg() == X86::ST0 ||
1061 VA.getLocReg() == X86::ST1) &&
1062 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001063 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Evan Cheng79fb3b42009-02-20 20:43:02 +00001066 SDValue Val;
1067 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001068 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1069 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1070 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1071 MVT::v2i64, InFlag).getValue(1);
1072 Val = Chain.getValue(0);
1073 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1074 Val, DAG.getConstant(0, MVT::i64));
1075 } else {
1076 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1077 MVT::i64, InFlag).getValue(1);
1078 Val = Chain.getValue(0);
1079 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001080 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1081 } else {
1082 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1083 CopyVT, InFlag).getValue(1);
1084 Val = Chain.getValue(0);
1085 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001086 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001087
Dan Gohman37eed792009-02-04 17:28:58 +00001088 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001089 // Round the F80 the right size, which also moves to the appropriate xmm
1090 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001091 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001092 // This truncation won't change the value.
1093 DAG.getIntPtrConstant(1));
1094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001095
Chris Lattner8e6da152008-03-10 21:08:41 +00001096 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001097 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001098
Chris Lattner3085e152007-02-25 08:59:22 +00001099 // Merge everything together with a MERGE_VALUES node.
1100 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001101 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1102 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001103}
1104
1105
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001106//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001107// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001108//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001109// StdCall calling convention seems to be standard for many Windows' API
1110// routines and around. It differs from C calling convention just a little:
1111// callee should clean up the stack, not caller. Symbols should be also
1112// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001113// For info on fast calling convention see Fast Calling Convention (tail call)
1114// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001115
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001116/// CallIsStructReturn - Determines whether a CALL node uses struct return
1117/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001118static bool CallIsStructReturn(CallSDNode *TheCall) {
1119 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001120 if (!NumOps)
1121 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001122
Dan Gohman095cc292008-09-13 01:54:27 +00001123 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001124}
1125
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001126/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1127/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001128static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001129 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001130 if (!NumArgs)
1131 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001132
1133 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001134}
1135
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001136/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1137/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001138/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001139bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001140 if (IsVarArg)
1141 return false;
1142
Dan Gohman095cc292008-09-13 01:54:27 +00001143 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001144 default:
1145 return false;
1146 case CallingConv::X86_StdCall:
1147 return !Subtarget->is64Bit();
1148 case CallingConv::X86_FastCall:
1149 return !Subtarget->is64Bit();
1150 case CallingConv::Fast:
1151 return PerformTailCallOpt;
1152 }
1153}
1154
Dan Gohman095cc292008-09-13 01:54:27 +00001155/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1156/// given CallingConvention value.
1157CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001158 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001159 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001160 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001161 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1162 return CC_X86_64_TailCall;
1163 else
1164 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001165 }
1166
Gordon Henriksen86737662008-01-05 16:56:59 +00001167 if (CC == CallingConv::X86_FastCall)
1168 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001169 else if (CC == CallingConv::Fast)
1170 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001171 else
1172 return CC_X86_32_C;
1173}
1174
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001175/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1176/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001177NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001178X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001179 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001180 if (CC == CallingConv::X86_FastCall)
1181 return FastCall;
1182 else if (CC == CallingConv::X86_StdCall)
1183 return StdCall;
1184 return None;
1185}
1186
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001187
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001188/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1189/// in a register before calling.
1190bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1191 return !IsTailCall && !Is64Bit &&
1192 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1193 Subtarget->isPICStyleGOT();
1194}
1195
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001196/// CallRequiresFnAddressInReg - Check whether the call requires the function
1197/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001198bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001199X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001200 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001201 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1202 Subtarget->isPICStyleGOT();
1203}
1204
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001205/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1206/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001207/// the specific parameter attribute. The copy will be passed as a byval
1208/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001209static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001210CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001211 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1212 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001213 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001214 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001215 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001216}
1217
Dan Gohman475871a2008-07-27 21:46:04 +00001218SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001219 const CCValAssign &VA,
1220 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001221 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001223 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001224 ISD::ArgFlagsTy Flags =
1225 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001226 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001227 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001228
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001229 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001230 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001231 // In case of tail call optimization mark all arguments mutable. Since they
1232 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001233 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001234 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001236 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001237 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001238 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001239 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001240}
1241
Dan Gohman475871a2008-07-27 21:46:04 +00001242SDValue
1243X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001244 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001245 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001246 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001247
Gordon Henriksen86737662008-01-05 16:56:59 +00001248 const Function* Fn = MF.getFunction();
1249 if (Fn->hasExternalLinkage() &&
1250 Subtarget->isTargetCygMing() &&
1251 Fn->getName() == "main")
1252 FuncInfo->setForceFramePointer(true);
1253
1254 // Decorate the function name.
1255 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001256
Evan Cheng1bc78042006-04-26 01:20:17 +00001257 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001258 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001259 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001260 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001261 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001262 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001263
1264 assert(!(isVarArg && CC == CallingConv::Fast) &&
1265 "Var args not supported with calling convention fastcc");
1266
Chris Lattner638402b2007-02-28 07:00:42 +00001267 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001268 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001269 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001270 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001271
Dan Gohman475871a2008-07-27 21:46:04 +00001272 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001273 unsigned LastVal = ~0U;
1274 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1275 CCValAssign &VA = ArgLocs[i];
1276 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1277 // places.
1278 assert(VA.getValNo() != LastVal &&
1279 "Don't support value assigned to multiple locs yet");
1280 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Chris Lattnerf39f7712007-02-28 05:46:49 +00001282 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001283 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001284 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001285 if (RegVT == MVT::i32)
1286 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001287 else if (Is64Bit && RegVT == MVT::i64)
1288 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001289 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001290 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001291 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001292 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001293 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001294 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001295 else if (RegVT.isVector()) {
1296 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001297 if (!Is64Bit)
1298 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1299 else {
1300 // Darwin calling convention passes MMX values in either GPRs or
1301 // XMMs in x86-64. Other targets pass them in memory.
1302 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1303 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1304 RegVT = MVT::v2i64;
1305 } else {
1306 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1307 RegVT = MVT::i64;
1308 }
1309 }
1310 } else {
1311 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001312 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001313
Bob Wilson998e1252009-04-20 18:36:57 +00001314 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001315 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001316
Chris Lattnerf39f7712007-02-28 05:46:49 +00001317 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1318 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1319 // right size.
1320 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001321 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001322 DAG.getValueType(VA.getValVT()));
1323 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001324 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001325 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001326
Chris Lattnerf39f7712007-02-28 05:46:49 +00001327 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001328 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001329
Gordon Henriksen86737662008-01-05 16:56:59 +00001330 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001331 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001332 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001333 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001334 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001335 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1336 ArgValue, DAG.getConstant(0, MVT::i64));
1337 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001338 }
1339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Chris Lattnerf39f7712007-02-28 05:46:49 +00001341 ArgValues.push_back(ArgValue);
1342 } else {
1343 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001344 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001345 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001346 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001347
Dan Gohman61a92132008-04-21 23:59:07 +00001348 // The x86-64 ABI for returning structs by value requires that we copy
1349 // the sret argument into %rax for the return. Save the argument into
1350 // a virtual register so that we can access it from the return points.
1351 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1352 MachineFunction &MF = DAG.getMachineFunction();
1353 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1354 unsigned Reg = FuncInfo->getSRetReturnReg();
1355 if (!Reg) {
1356 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1357 FuncInfo->setSRetReturnReg(Reg);
1358 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001359 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001360 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001361 }
1362
Chris Lattnerf39f7712007-02-28 05:46:49 +00001363 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001364 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001365 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001366 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001367
Evan Cheng1bc78042006-04-26 01:20:17 +00001368 // If the function takes variable number of arguments, make a frame index for
1369 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001370 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001371 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1372 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1373 }
1374 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001375 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1376
1377 // FIXME: We should really autogenerate these arrays
1378 static const unsigned GPR64ArgRegsWin64[] = {
1379 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001381 static const unsigned XMMArgRegsWin64[] = {
1382 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1383 };
1384 static const unsigned GPR64ArgRegs64Bit[] = {
1385 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1386 };
1387 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001388 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1389 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1390 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001391 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1392
1393 if (IsWin64) {
1394 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1395 GPR64ArgRegs = GPR64ArgRegsWin64;
1396 XMMArgRegs = XMMArgRegsWin64;
1397 } else {
1398 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1399 GPR64ArgRegs = GPR64ArgRegs64Bit;
1400 XMMArgRegs = XMMArgRegs64Bit;
1401 }
1402 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1403 TotalNumIntRegs);
1404 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1405 TotalNumXMMRegs);
1406
Evan Chengc7ce29b2009-02-13 22:36:38 +00001407 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001408 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001409 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloat) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001410 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001411 if (UseSoftFloat || NoImplicitFloat || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001412 // Kernel mode asks for SSE to be disabled, so don't push them
1413 // on the stack.
1414 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001415
Gordon Henriksen86737662008-01-05 16:56:59 +00001416 // For X86-64, if there are vararg parameters that are passed via
1417 // registers, then we must store them to their spots on the stack so they
1418 // may be loaded by deferencing the result of va_next.
1419 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001420 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1421 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1422 TotalNumXMMRegs * 16, 16);
1423
Gordon Henriksen86737662008-01-05 16:56:59 +00001424 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001425 SmallVector<SDValue, 8> MemOps;
1426 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001427 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001428 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001429 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001430 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1431 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001432 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001433 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001434 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001435 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001436 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001437 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001438 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001439 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001440
Gordon Henriksen86737662008-01-05 16:56:59 +00001441 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001442 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001443 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001444 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001445 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1446 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001447 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001448 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001449 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001450 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001452 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001453 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001454 }
1455 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001456 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001457 &MemOps[0], MemOps.size());
1458 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
Gordon Henriksenae636f82008-01-03 16:47:34 +00001461 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001462
Gordon Henriksen86737662008-01-05 16:56:59 +00001463 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001464 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001465 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001466 BytesCallerReserves = 0;
1467 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001468 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001469 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001470 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001471 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001472 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001473 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001474
Gordon Henriksen86737662008-01-05 16:56:59 +00001475 if (!Is64Bit) {
1476 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1477 if (CC == CallingConv::X86_FastCall)
1478 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1479 }
Evan Cheng25caf632006-05-23 21:06:34 +00001480
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001481 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001482
Evan Cheng25caf632006-05-23 21:06:34 +00001483 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001484 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001485 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001486}
1487
Dan Gohman475871a2008-07-27 21:46:04 +00001488SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001489X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001490 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001491 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001492 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001493 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001494 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001495 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001496 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001497 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001498 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001499 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001500 }
Dale Johannesenace16102009-02-03 19:33:06 +00001501 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001502 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001503}
1504
Bill Wendling64e87322009-01-16 19:25:27 +00001505/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001506/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001507SDValue
1508X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001509 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001510 SDValue Chain,
1511 bool IsTailCall,
1512 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001513 int FPDiff,
1514 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001515 if (!IsTailCall || FPDiff==0) return Chain;
1516
1517 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001518 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001519 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001520
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001521 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001522 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001523 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001524}
1525
1526/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1527/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001528static SDValue
1529EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001530 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001531 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001532 // Store the return address to the appropriate stack slot.
1533 if (!FPDiff) return Chain;
1534 // Calculate the new stack slot for the return address.
1535 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001536 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001537 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001539 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001540 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001541 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001542 return Chain;
1543}
1544
Dan Gohman475871a2008-07-27 21:46:04 +00001545SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001546 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001547 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1548 SDValue Chain = TheCall->getChain();
1549 unsigned CC = TheCall->getCallingConv();
1550 bool isVarArg = TheCall->isVarArg();
1551 bool IsTailCall = TheCall->isTailCall() &&
1552 CC == CallingConv::Fast && PerformTailCallOpt;
1553 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001555 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001556 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001557
1558 assert(!(isVarArg && CC == CallingConv::Fast) &&
1559 "Var args not supported with calling convention fastcc");
1560
Chris Lattner638402b2007-02-28 07:00:42 +00001561 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001562 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001563 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001564 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattner423c5f42007-02-28 05:31:48 +00001566 // Get a count of how many bytes are to be pushed on the stack.
1567 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001568 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001569 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001570
Gordon Henriksen86737662008-01-05 16:56:59 +00001571 int FPDiff = 0;
1572 if (IsTailCall) {
1573 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001574 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001575 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1576 FPDiff = NumBytesCallerPushed - NumBytes;
1577
1578 // Set the delta of movement of the returnaddr stackslot.
1579 // But only set if delta is greater than previous delta.
1580 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1581 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1582 }
1583
Chris Lattnere563bbc2008-10-11 22:08:30 +00001584 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001585
Dan Gohman475871a2008-07-27 21:46:04 +00001586 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001587 // Load return adress for tail calls.
1588 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001589 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001590
Dan Gohman475871a2008-07-27 21:46:04 +00001591 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1592 SmallVector<SDValue, 8> MemOpChains;
1593 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001594
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001595 // Walk the register/memloc assignments, inserting copies/loads. In the case
1596 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001597 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1598 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001599 SDValue Arg = TheCall->getArg(i);
1600 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1601 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001602
Chris Lattner423c5f42007-02-28 05:31:48 +00001603 // Promote the value if needed.
1604 switch (VA.getLocInfo()) {
1605 default: assert(0 && "Unknown loc info!");
1606 case CCValAssign::Full: break;
1607 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001608 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001609 break;
1610 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001611 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001612 break;
1613 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001614 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001615 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001616 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001617
Chris Lattner423c5f42007-02-28 05:31:48 +00001618 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001619 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001620 MVT RegVT = VA.getLocVT();
1621 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001622 switch (VA.getLocReg()) {
1623 default:
1624 break;
1625 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1626 case X86::R8: {
1627 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001628 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001629 break;
1630 }
1631 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1632 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1633 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001634 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1635 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001636 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001637 break;
1638 }
1639 }
1640 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001641 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1642 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001643 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001644 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001645 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001646 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001647
Dan Gohman095cc292008-09-13 01:54:27 +00001648 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1649 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001650 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001651 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001653
Evan Cheng32fe1032006-05-25 00:59:30 +00001654 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001655 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001656 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657
Evan Cheng347d5f72006-04-28 21:29:37 +00001658 // Build a sequence of copy-to-reg nodes chained together with token chain
1659 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001660 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001661 // Tail call byval lowering might overwrite argument registers so in case of
1662 // tail call optimization the copies to registers are lowered later.
1663 if (!IsTailCall)
1664 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001665 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001666 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001667 InFlag = Chain.getValue(1);
1668 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001669
Evan Chengf4684712007-02-21 21:18:14 +00001670 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001671 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001672 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001673 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001674 DAG.getNode(X86ISD::GlobalBaseReg,
1675 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001676 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001677 InFlag);
1678 InFlag = Chain.getValue(1);
1679 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001680 // If we are tail calling and generating PIC/GOT style code load the address
1681 // of the callee into ecx. The value in ecx is used as target of the tail
1682 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1683 // calls on PIC/GOT architectures. Normally we would just put the address of
1684 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1685 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001686 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001687 // Note: The actual moving to ecx is done further down.
1688 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001689 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001690 !G->getGlobal()->hasProtectedVisibility())
1691 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001692 else if (isa<ExternalSymbolSDNode>(Callee))
1693 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001694 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001695
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 if (Is64Bit && isVarArg) {
1697 // From AMD64 ABI document:
1698 // For calls that may call functions that use varargs or stdargs
1699 // (prototype-less calls or calls to functions containing ellipsis (...) in
1700 // the declaration) %al is used as hidden argument to specify the number
1701 // of SSE registers used. The contents of %al do not need to match exactly
1702 // the number of registers, but must be an ubound on the number of SSE
1703 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001704
1705 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 // Count the number of XMM registers allocated.
1707 static const unsigned XMMArgRegs[] = {
1708 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1709 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1710 };
1711 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001713 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001714
Dale Johannesendd64c412009-02-04 00:33:20 +00001715 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001716 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1717 InFlag = Chain.getValue(1);
1718 }
1719
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001720
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001721 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001723 SmallVector<SDValue, 8> MemOpChains2;
1724 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001726 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001727 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001728 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1729 CCValAssign &VA = ArgLocs[i];
1730 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001731 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001732 SDValue Arg = TheCall->getArg(i);
1733 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001734 // Create frame index.
1735 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001736 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001737 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001738 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001739
Duncan Sands276dcbd2008-03-21 09:14:45 +00001740 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001741 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001742 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001743 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001744 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001745 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001746 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001747
1748 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001749 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001751 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001752 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001753 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001754 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001755 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001756 }
1757 }
1758
1759 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001760 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001761 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001762
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001763 // Copy arguments to their registers.
1764 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001765 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001766 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001767 InFlag = Chain.getValue(1);
1768 }
Dan Gohman475871a2008-07-27 21:46:04 +00001769 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001770
Gordon Henriksen86737662008-01-05 16:56:59 +00001771 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001772 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001773 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001774 }
1775
Evan Cheng32fe1032006-05-25 00:59:30 +00001776 // If the callee is a GlobalAddress node (quite common, every direct call is)
1777 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001778 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001779 // We should use extra load for direct calls to dllimported functions in
1780 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001781 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1782 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001783 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1784 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001785 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1786 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001788 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001789
Dale Johannesendd64c412009-02-04 00:33:20 +00001790 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001791 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001792 Callee,InFlag);
1793 Callee = DAG.getRegister(Opc, getPointerTy());
1794 // Add register as live out.
1795 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001796 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001797
Chris Lattnerd96d0722007-02-25 06:40:16 +00001798 // Returns a chain & a flag for retval copy to use.
1799 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001800 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001801
1802 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001803 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1804 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001805 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001806
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 // Returns a chain & a flag for retval copy to use.
1808 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1809 Ops.clear();
1810 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001811
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001812 Ops.push_back(Chain);
1813 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001814
Gordon Henriksen86737662008-01-05 16:56:59 +00001815 if (IsTailCall)
1816 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001817
Gordon Henriksen86737662008-01-05 16:56:59 +00001818 // Add argument registers to the end of the list so that they are known live
1819 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001820 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1821 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1822 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001823
Evan Cheng586ccac2008-03-18 23:36:35 +00001824 // Add an implicit use GOT pointer in EBX.
1825 if (!IsTailCall && !Is64Bit &&
1826 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1827 Subtarget->isPICStyleGOT())
1828 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1829
1830 // Add an implicit use of AL for x86 vararg functions.
1831 if (Is64Bit && isVarArg)
1832 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1833
Gabor Greifba36cb52008-08-28 21:40:38 +00001834 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001835 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001836
Gordon Henriksen86737662008-01-05 16:56:59 +00001837 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001838 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001839 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001840 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001841 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001842
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001844 }
1845
Dale Johannesenace16102009-02-03 19:33:06 +00001846 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001847 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001848
Chris Lattner2d297092006-05-23 18:50:38 +00001849 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001851 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001853 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001854 // If this is is a call to a struct-return function, the callee
1855 // pops the hidden struct pointer, so we have to push it back.
1856 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001857 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001858 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001859 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001860
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001862 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001863 DAG.getIntPtrConstant(NumBytes, true),
1864 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1865 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001866 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001867 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001868
Chris Lattner3085e152007-02-25 08:59:22 +00001869 // Handle result values, copying them out of physregs into vregs that we
1870 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001871 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001872 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001873}
1874
Evan Cheng25ab6902006-09-08 06:48:29 +00001875
1876//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001877// Fast Calling Convention (tail call) implementation
1878//===----------------------------------------------------------------------===//
1879
1880// Like std call, callee cleans arguments, convention except that ECX is
1881// reserved for storing the tail called function address. Only 2 registers are
1882// free for argument passing (inreg). Tail call optimization is performed
1883// provided:
1884// * tailcallopt is enabled
1885// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001886// On X86_64 architecture with GOT-style position independent code only local
1887// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001888// To keep the stack aligned according to platform abi the function
1889// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1890// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001891// If a tail called function callee has more arguments than the caller the
1892// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001893// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001894// original REtADDR, but before the saved framepointer or the spilled registers
1895// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1896// stack layout:
1897// arg1
1898// arg2
1899// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001900// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001901// move area ]
1902// (possible EBP)
1903// ESI
1904// EDI
1905// local1 ..
1906
1907/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1908/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001909unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001910 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001911 MachineFunction &MF = DAG.getMachineFunction();
1912 const TargetMachine &TM = MF.getTarget();
1913 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1914 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001915 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001916 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001917 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001918 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1919 // Number smaller than 12 so just add the difference.
1920 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1921 } else {
1922 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001923 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001924 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001925 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001926 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001927}
1928
1929/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001930/// following the call is a return. A function is eligible if caller/callee
1931/// calling conventions match, currently only fastcc supports tail calls, and
1932/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001933bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001934 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001935 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001936 if (!PerformTailCallOpt)
1937 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001938
Dan Gohman095cc292008-09-13 01:54:27 +00001939 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001940 MachineFunction &MF = DAG.getMachineFunction();
1941 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001942 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001943 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001944 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001945 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001946 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001947 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001948 return true;
1949
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001950 // Can only do local tail calls (in same module, hidden or protected) on
1951 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001952 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1953 return G->getGlobal()->hasHiddenVisibility()
1954 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001955 }
1956 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001957
1958 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001959}
1960
Dan Gohman3df24e62008-09-03 23:12:08 +00001961FastISel *
1962X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001963 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001964 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001965 DenseMap<const Value *, unsigned> &vm,
1966 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001967 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001968 DenseMap<const AllocaInst *, int> &am
1969#ifndef NDEBUG
1970 , SmallSet<Instruction*, 8> &cil
1971#endif
1972 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001973 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001974#ifndef NDEBUG
1975 , cil
1976#endif
1977 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001978}
1979
1980
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001981//===----------------------------------------------------------------------===//
1982// Other Lowering Hooks
1983//===----------------------------------------------------------------------===//
1984
1985
Dan Gohman475871a2008-07-27 21:46:04 +00001986SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001987 MachineFunction &MF = DAG.getMachineFunction();
1988 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1989 int ReturnAddrIndex = FuncInfo->getRAIndex();
1990
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001991 if (ReturnAddrIndex == 0) {
1992 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00001993 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001994 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001995 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001996 }
1997
Evan Cheng25ab6902006-09-08 06:48:29 +00001998 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001999}
2000
2001
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002002/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2003/// specific condition code, returning the condition code and the LHS/RHS of the
2004/// comparison to make.
2005static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2006 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002007 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002008 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2009 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2010 // X > -1 -> X == 0, jump !sign.
2011 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002012 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002013 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2014 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002015 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002016 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002017 // X < 1 -> X <= 0
2018 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002019 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002020 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002021 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002022
Evan Chengd9558e02006-01-06 00:43:03 +00002023 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002024 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002025 case ISD::SETEQ: return X86::COND_E;
2026 case ISD::SETGT: return X86::COND_G;
2027 case ISD::SETGE: return X86::COND_GE;
2028 case ISD::SETLT: return X86::COND_L;
2029 case ISD::SETLE: return X86::COND_LE;
2030 case ISD::SETNE: return X86::COND_NE;
2031 case ISD::SETULT: return X86::COND_B;
2032 case ISD::SETUGT: return X86::COND_A;
2033 case ISD::SETULE: return X86::COND_BE;
2034 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002035 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002036 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002037
Chris Lattner4c78e022008-12-23 23:42:27 +00002038 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002039
Chris Lattner4c78e022008-12-23 23:42:27 +00002040 // If LHS is a foldable load, but RHS is not, flip the condition.
2041 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2042 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2043 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2044 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002045 }
2046
Chris Lattner4c78e022008-12-23 23:42:27 +00002047 switch (SetCCOpcode) {
2048 default: break;
2049 case ISD::SETOLT:
2050 case ISD::SETOLE:
2051 case ISD::SETUGT:
2052 case ISD::SETUGE:
2053 std::swap(LHS, RHS);
2054 break;
2055 }
2056
2057 // On a floating point condition, the flags are set as follows:
2058 // ZF PF CF op
2059 // 0 | 0 | 0 | X > Y
2060 // 0 | 0 | 1 | X < Y
2061 // 1 | 0 | 0 | X == Y
2062 // 1 | 1 | 1 | unordered
2063 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002064 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002065 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002066 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002067 case ISD::SETOLT: // flipped
2068 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002069 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002070 case ISD::SETOLE: // flipped
2071 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002072 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002073 case ISD::SETUGT: // flipped
2074 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002075 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002076 case ISD::SETUGE: // flipped
2077 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002078 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002079 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002080 case ISD::SETNE: return X86::COND_NE;
2081 case ISD::SETUO: return X86::COND_P;
2082 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002083 }
Evan Chengd9558e02006-01-06 00:43:03 +00002084}
2085
Evan Cheng4a460802006-01-11 00:33:36 +00002086/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2087/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002088/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002089static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002090 switch (X86CC) {
2091 default:
2092 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002093 case X86::COND_B:
2094 case X86::COND_BE:
2095 case X86::COND_E:
2096 case X86::COND_P:
2097 case X86::COND_A:
2098 case X86::COND_AE:
2099 case X86::COND_NE:
2100 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002101 return true;
2102 }
2103}
2104
Nate Begeman9008ca62009-04-27 18:41:29 +00002105/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2106/// the specified range (L, H].
2107static bool isUndefOrInRange(int Val, int Low, int Hi) {
2108 return (Val < 0) || (Val >= Low && Val < Hi);
2109}
2110
2111/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2112/// specified value.
2113static bool isUndefOrEqual(int Val, int CmpVal) {
2114 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002115 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002116 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002117}
2118
Nate Begeman9008ca62009-04-27 18:41:29 +00002119/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2120/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2121/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002122static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002123 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2124 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2125 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2126 return (Mask[0] < 2 && Mask[1] < 2);
2127 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002128}
2129
Nate Begeman9008ca62009-04-27 18:41:29 +00002130bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2131 SmallVector<int, 8> M;
2132 N->getMask(M);
2133 return ::isPSHUFDMask(M, N->getValueType(0));
2134}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002135
Nate Begeman9008ca62009-04-27 18:41:29 +00002136/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2137/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002138static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002139 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002140 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002141
2142 // Lower quadword copied in order or undef.
2143 for (int i = 0; i != 4; ++i)
2144 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002145 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002146
Evan Cheng506d3df2006-03-29 23:07:14 +00002147 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002148 for (int i = 4; i != 8; ++i)
2149 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002150 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002151
Evan Cheng506d3df2006-03-29 23:07:14 +00002152 return true;
2153}
2154
Nate Begeman9008ca62009-04-27 18:41:29 +00002155bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2156 SmallVector<int, 8> M;
2157 N->getMask(M);
2158 return ::isPSHUFHWMask(M, N->getValueType(0));
2159}
Evan Cheng506d3df2006-03-29 23:07:14 +00002160
Nate Begeman9008ca62009-04-27 18:41:29 +00002161/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2162/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002163static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002164 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002165 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002166
Rafael Espindola15684b22009-04-24 12:40:33 +00002167 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002168 for (int i = 4; i != 8; ++i)
2169 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002170 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002171
Rafael Espindola15684b22009-04-24 12:40:33 +00002172 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002173 for (int i = 0; i != 4; ++i)
2174 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002175 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002176
Rafael Espindola15684b22009-04-24 12:40:33 +00002177 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002178}
2179
Nate Begeman9008ca62009-04-27 18:41:29 +00002180bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2181 SmallVector<int, 8> M;
2182 N->getMask(M);
2183 return ::isPSHUFLWMask(M, N->getValueType(0));
2184}
2185
Evan Cheng14aed5e2006-03-24 01:18:28 +00002186/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2187/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002188static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002189 int NumElems = VT.getVectorNumElements();
2190 if (NumElems != 2 && NumElems != 4)
2191 return false;
2192
2193 int Half = NumElems / 2;
2194 for (int i = 0; i < Half; ++i)
2195 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002196 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002197 for (int i = Half; i < NumElems; ++i)
2198 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002199 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002200
Evan Cheng14aed5e2006-03-24 01:18:28 +00002201 return true;
2202}
2203
Nate Begeman9008ca62009-04-27 18:41:29 +00002204bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2205 SmallVector<int, 8> M;
2206 N->getMask(M);
2207 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002208}
2209
Evan Cheng213d2cf2007-05-17 18:45:50 +00002210/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002211/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2212/// half elements to come from vector 1 (which would equal the dest.) and
2213/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002214static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002215 int NumElems = VT.getVectorNumElements();
2216
2217 if (NumElems != 2 && NumElems != 4)
2218 return false;
2219
2220 int Half = NumElems / 2;
2221 for (int i = 0; i < Half; ++i)
2222 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002223 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002224 for (int i = Half; i < NumElems; ++i)
2225 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002226 return false;
2227 return true;
2228}
2229
Nate Begeman9008ca62009-04-27 18:41:29 +00002230static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2231 SmallVector<int, 8> M;
2232 N->getMask(M);
2233 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002234}
2235
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002236/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2237/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002238bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2239 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002240 return false;
2241
Evan Cheng2064a2b2006-03-28 06:50:32 +00002242 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002243 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2244 isUndefOrEqual(N->getMaskElt(1), 7) &&
2245 isUndefOrEqual(N->getMaskElt(2), 2) &&
2246 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002247}
2248
Evan Cheng5ced1d82006-04-06 23:23:56 +00002249/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2250/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002251bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2252 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002253
Evan Cheng5ced1d82006-04-06 23:23:56 +00002254 if (NumElems != 2 && NumElems != 4)
2255 return false;
2256
Evan Chengc5cdff22006-04-07 21:53:05 +00002257 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002258 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002259 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002260
Evan Chengc5cdff22006-04-07 21:53:05 +00002261 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002262 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002263 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002264
2265 return true;
2266}
2267
2268/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002269/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2270/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002271bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2272 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002273
Evan Cheng5ced1d82006-04-06 23:23:56 +00002274 if (NumElems != 2 && NumElems != 4)
2275 return false;
2276
Evan Chengc5cdff22006-04-07 21:53:05 +00002277 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002278 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002279 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002280
Nate Begeman9008ca62009-04-27 18:41:29 +00002281 for (unsigned i = 0; i < NumElems/2; ++i)
2282 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002283 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002284
2285 return true;
2286}
2287
Nate Begeman9008ca62009-04-27 18:41:29 +00002288/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2289/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2290/// <2, 3, 2, 3>
2291bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2292 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2293
2294 if (NumElems != 4)
2295 return false;
2296
2297 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2298 isUndefOrEqual(N->getMaskElt(1), 3) &&
2299 isUndefOrEqual(N->getMaskElt(2), 2) &&
2300 isUndefOrEqual(N->getMaskElt(3), 3);
2301}
2302
Evan Cheng0038e592006-03-28 00:39:58 +00002303/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2304/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002305static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002306 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002307 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002308 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002309 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002310
2311 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2312 int BitI = Mask[i];
2313 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002314 if (!isUndefOrEqual(BitI, j))
2315 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002316 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002317 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002318 return false;
2319 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002320 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002321 return false;
2322 }
Evan Cheng0038e592006-03-28 00:39:58 +00002323 }
Evan Cheng0038e592006-03-28 00:39:58 +00002324 return true;
2325}
2326
Nate Begeman9008ca62009-04-27 18:41:29 +00002327bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2328 SmallVector<int, 8> M;
2329 N->getMask(M);
2330 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002331}
2332
Evan Cheng4fcb9222006-03-28 02:43:26 +00002333/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2334/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002335static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002336 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002337 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002338 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002339 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002340
2341 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2342 int BitI = Mask[i];
2343 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002344 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002345 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002346 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002347 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002348 return false;
2349 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002350 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002351 return false;
2352 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002353 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002354 return true;
2355}
2356
Nate Begeman9008ca62009-04-27 18:41:29 +00002357bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2358 SmallVector<int, 8> M;
2359 N->getMask(M);
2360 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002361}
2362
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002363/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2364/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2365/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002366static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002367 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002368 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002369 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002370
2371 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2372 int BitI = Mask[i];
2373 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002374 if (!isUndefOrEqual(BitI, j))
2375 return false;
2376 if (!isUndefOrEqual(BitI1, j))
2377 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002378 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002379 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002380}
2381
Nate Begeman9008ca62009-04-27 18:41:29 +00002382bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2383 SmallVector<int, 8> M;
2384 N->getMask(M);
2385 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2386}
2387
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002388/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2389/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2390/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002391static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002392 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002393 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2394 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002395
2396 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2397 int BitI = Mask[i];
2398 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002399 if (!isUndefOrEqual(BitI, j))
2400 return false;
2401 if (!isUndefOrEqual(BitI1, j))
2402 return false;
2403 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002404 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002405}
2406
Nate Begeman9008ca62009-04-27 18:41:29 +00002407bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2408 SmallVector<int, 8> M;
2409 N->getMask(M);
2410 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2411}
2412
Evan Cheng017dcc62006-04-21 01:05:10 +00002413/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2414/// specifies a shuffle of elements that is suitable for input to MOVSS,
2415/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002416static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002417 int NumElts = VT.getVectorNumElements();
Evan Cheng10762102007-12-06 22:14:22 +00002418 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002419 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002420
2421 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002422 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002423
2424 for (int i = 1; i < NumElts; ++i)
2425 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002426 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002427
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002428 return true;
2429}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002430
Nate Begeman9008ca62009-04-27 18:41:29 +00002431bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2432 SmallVector<int, 8> M;
2433 N->getMask(M);
2434 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002435}
2436
Evan Cheng017dcc62006-04-21 01:05:10 +00002437/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2438/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002439/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002440static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002441 bool V2IsSplat = false, bool V2IsUndef = false) {
2442 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002443 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002444 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002445
2446 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002447 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002448
2449 for (int i = 1; i < NumOps; ++i)
2450 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2451 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2452 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002453 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002454
Evan Cheng39623da2006-04-20 08:58:49 +00002455 return true;
2456}
2457
Nate Begeman9008ca62009-04-27 18:41:29 +00002458static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002459 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002460 SmallVector<int, 8> M;
2461 N->getMask(M);
2462 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002463}
2464
Evan Chengd9539472006-04-14 21:59:03 +00002465/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2466/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002467bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2468 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002469 return false;
2470
2471 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002472 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002473 int Elt = N->getMaskElt(i);
2474 if (Elt >= 0 && Elt != 1)
2475 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002476 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002477
2478 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002479 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002480 int Elt = N->getMaskElt(i);
2481 if (Elt >= 0 && Elt != 3)
2482 return false;
2483 if (Elt == 3)
2484 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002485 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002486 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002487 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002488 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002489}
2490
2491/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2492/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002493bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2494 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002495 return false;
2496
2497 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002498 for (unsigned i = 0; i < 2; ++i)
2499 if (N->getMaskElt(i) > 0)
2500 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002501
2502 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002503 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002504 int Elt = N->getMaskElt(i);
2505 if (Elt >= 0 && Elt != 2)
2506 return false;
2507 if (Elt == 2)
2508 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002509 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002510 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002511 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002512}
2513
Evan Cheng0b457f02008-09-25 20:50:48 +00002514/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2515/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002516bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2517 int e = N->getValueType(0).getVectorNumElements() / 2;
2518
2519 for (int i = 0; i < e; ++i)
2520 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002521 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002522 for (int i = 0; i < e; ++i)
2523 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002524 return false;
2525 return true;
2526}
2527
Evan Cheng63d33002006-03-22 08:01:21 +00002528/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2529/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2530/// instructions.
2531unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002532 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2533 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2534
Evan Chengb9df0ca2006-03-22 02:53:00 +00002535 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2536 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002537 for (int i = 0; i < NumOperands; ++i) {
2538 int Val = SVOp->getMaskElt(NumOperands-i-1);
2539 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002540 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002541 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002542 if (i != NumOperands - 1)
2543 Mask <<= Shift;
2544 }
Evan Cheng63d33002006-03-22 08:01:21 +00002545 return Mask;
2546}
2547
Evan Cheng506d3df2006-03-29 23:07:14 +00002548/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2549/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2550/// instructions.
2551unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002552 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002553 unsigned Mask = 0;
2554 // 8 nodes, but we only care about the last 4.
2555 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002556 int Val = SVOp->getMaskElt(i);
2557 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002558 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002559 if (i != 4)
2560 Mask <<= 2;
2561 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002562 return Mask;
2563}
2564
2565/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2566/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2567/// instructions.
2568unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002569 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002570 unsigned Mask = 0;
2571 // 8 nodes, but we only care about the first 4.
2572 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002573 int Val = SVOp->getMaskElt(i);
2574 if (Val >= 0)
2575 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002576 if (i != 0)
2577 Mask <<= 2;
2578 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002579 return Mask;
2580}
2581
Nate Begeman9008ca62009-04-27 18:41:29 +00002582/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2583/// their permute mask.
2584static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2585 SelectionDAG &DAG) {
2586 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002587 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002588 SmallVector<int, 8> MaskVec;
2589
Nate Begeman5a5ca152009-04-29 05:20:52 +00002590 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002591 int idx = SVOp->getMaskElt(i);
2592 if (idx < 0)
2593 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002594 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002595 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002596 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002597 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002598 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2600 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002601}
2602
Evan Cheng779ccea2007-12-07 21:30:01 +00002603/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2604/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002605static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002606 unsigned NumElems = VT.getVectorNumElements();
2607 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 int idx = Mask[i];
2609 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002610 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002611 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002613 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002614 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002615 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002616}
2617
Evan Cheng533a0aa2006-04-19 20:35:22 +00002618/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2619/// match movhlps. The lower half elements should come from upper half of
2620/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002621/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002622static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2623 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002624 return false;
2625 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002626 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002627 return false;
2628 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002629 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002630 return false;
2631 return true;
2632}
2633
Evan Cheng5ced1d82006-04-06 23:23:56 +00002634/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002635/// is promoted to a vector. It also returns the LoadSDNode by reference if
2636/// required.
2637static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002638 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2639 return false;
2640 N = N->getOperand(0).getNode();
2641 if (!ISD::isNON_EXTLoad(N))
2642 return false;
2643 if (LD)
2644 *LD = cast<LoadSDNode>(N);
2645 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002646}
2647
Evan Cheng533a0aa2006-04-19 20:35:22 +00002648/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2649/// match movlp{s|d}. The lower half elements should come from lower half of
2650/// V1 (and in order), and the upper half elements should come from the upper
2651/// half of V2 (and in order). And since V1 will become the source of the
2652/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002653static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2654 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002655 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002656 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002657 // Is V2 is a vector load, don't do this transformation. We will try to use
2658 // load folding shufps op.
2659 if (ISD::isNON_EXTLoad(V2))
2660 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002661
Nate Begeman5a5ca152009-04-29 05:20:52 +00002662 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002663
Evan Cheng533a0aa2006-04-19 20:35:22 +00002664 if (NumElems != 2 && NumElems != 4)
2665 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002666 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002667 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002668 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002669 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002670 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002671 return false;
2672 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002673}
2674
Evan Cheng39623da2006-04-20 08:58:49 +00002675/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2676/// all the same.
2677static bool isSplatVector(SDNode *N) {
2678 if (N->getOpcode() != ISD::BUILD_VECTOR)
2679 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002680
Dan Gohman475871a2008-07-27 21:46:04 +00002681 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002682 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2683 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002684 return false;
2685 return true;
2686}
2687
Evan Cheng213d2cf2007-05-17 18:45:50 +00002688/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2689/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002690static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002691 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002692 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002693 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002694 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002695}
2696
2697/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002698/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002699/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002700static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002701 SDValue V1 = N->getOperand(0);
2702 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002703 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2704 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002706 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002707 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002708 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2709 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002710 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2711 return false;
2712 } else if (Idx >= 0) {
2713 unsigned Opc = V1.getOpcode();
2714 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2715 continue;
2716 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002717 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002718 }
2719 }
2720 return true;
2721}
2722
2723/// getZeroVector - Returns a vector of specified type with all zero elements.
2724///
Dale Johannesenace16102009-02-03 19:33:06 +00002725static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2726 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002727 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002728
Chris Lattner8a594482007-11-25 00:24:49 +00002729 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2730 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002731 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002732 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002733 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002734 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002735 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002736 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002737 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002738 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002739 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002740 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002741 }
Dale Johannesenace16102009-02-03 19:33:06 +00002742 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002743}
2744
Chris Lattner8a594482007-11-25 00:24:49 +00002745/// getOnesVector - Returns a vector of specified type with all bits set.
2746///
Dale Johannesenace16102009-02-03 19:33:06 +00002747static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002748 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002749
Chris Lattner8a594482007-11-25 00:24:49 +00002750 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2751 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2753 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002754 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002755 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002756 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002757 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002758 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002759}
2760
2761
Evan Cheng39623da2006-04-20 08:58:49 +00002762/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2763/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002764static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2765 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002766 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002767
Evan Cheng39623da2006-04-20 08:58:49 +00002768 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002769 SmallVector<int, 8> MaskVec;
2770 SVOp->getMask(MaskVec);
2771
Nate Begeman5a5ca152009-04-29 05:20:52 +00002772 for (unsigned i = 0; i != NumElems; ++i) {
2773 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002774 MaskVec[i] = NumElems;
2775 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002776 }
Evan Cheng39623da2006-04-20 08:58:49 +00002777 }
Evan Cheng39623da2006-04-20 08:58:49 +00002778 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002779 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2780 SVOp->getOperand(1), &MaskVec[0]);
2781 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002782}
2783
Evan Cheng017dcc62006-04-21 01:05:10 +00002784/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2785/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002786static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2787 SDValue V2) {
2788 unsigned NumElems = VT.getVectorNumElements();
2789 SmallVector<int, 8> Mask;
2790 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002791 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002792 Mask.push_back(i);
2793 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002794}
2795
Nate Begeman9008ca62009-04-27 18:41:29 +00002796/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2797static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2798 SDValue V2) {
2799 unsigned NumElems = VT.getVectorNumElements();
2800 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002801 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002802 Mask.push_back(i);
2803 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002804 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002806}
2807
Nate Begeman9008ca62009-04-27 18:41:29 +00002808/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2809static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2810 SDValue V2) {
2811 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002812 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002814 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002815 Mask.push_back(i + Half);
2816 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002817 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002818 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002819}
2820
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002821/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002822static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2823 bool HasSSE2) {
2824 if (SV->getValueType(0).getVectorNumElements() <= 4)
2825 return SDValue(SV, 0);
2826
2827 MVT PVT = MVT::v4f32;
2828 MVT VT = SV->getValueType(0);
2829 DebugLoc dl = SV->getDebugLoc();
2830 SDValue V1 = SV->getOperand(0);
2831 int NumElems = VT.getVectorNumElements();
2832 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002833
Nate Begeman9008ca62009-04-27 18:41:29 +00002834 // unpack elements to the correct location
2835 while (NumElems > 4) {
2836 if (EltNo < NumElems/2) {
2837 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2838 } else {
2839 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2840 EltNo -= NumElems/2;
2841 }
2842 NumElems >>= 1;
2843 }
2844
2845 // Perform the splat.
2846 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002847 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002848 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2849 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002850}
2851
Evan Chengba05f722006-04-21 23:03:30 +00002852/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002853/// vector of zero or undef vector. This produces a shuffle where the low
2854/// element of V2 is swizzled into the zero/undef vector, landing at element
2855/// 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 +00002856static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002857 bool isZero, bool HasSSE2,
2858 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002859 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002860 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2862 unsigned NumElems = VT.getVectorNumElements();
2863 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002864 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002865 // If this is the insertion idx, put the low elt of V2 here.
2866 MaskVec.push_back(i == Idx ? NumElems : i);
2867 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002868}
2869
Evan Chengf26ffe92008-05-29 08:22:04 +00002870/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2871/// a shuffle that is zero.
2872static
Nate Begeman9008ca62009-04-27 18:41:29 +00002873unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2874 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002875 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002877 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 int Idx = SVOp->getMaskElt(Index);
2879 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002880 ++NumZeros;
2881 continue;
2882 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002884 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002885 ++NumZeros;
2886 else
2887 break;
2888 }
2889 return NumZeros;
2890}
2891
2892/// isVectorShift - Returns true if the shuffle can be implemented as a
2893/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002894/// FIXME: split into pslldqi, psrldqi, palignr variants.
2895static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002896 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002897 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002898
2899 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002900 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002901 if (!NumZeros) {
2902 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002903 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002904 if (!NumZeros)
2905 return false;
2906 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002907 bool SeenV1 = false;
2908 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002909 for (int i = NumZeros; i < NumElems; ++i) {
2910 int Val = isLeft ? (i - NumZeros) : i;
2911 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2912 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002913 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002915 SeenV1 = true;
2916 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002917 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002918 SeenV2 = true;
2919 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002920 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002921 return false;
2922 }
2923 if (SeenV1 && SeenV2)
2924 return false;
2925
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002927 ShAmt = NumZeros;
2928 return true;
2929}
2930
2931
Evan Chengc78d3b42006-04-24 18:01:45 +00002932/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2933///
Dan Gohman475871a2008-07-27 21:46:04 +00002934static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002935 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002936 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002937 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002938 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002939
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002940 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002941 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002942 bool First = true;
2943 for (unsigned i = 0; i < 16; ++i) {
2944 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2945 if (ThisIsNonZero && First) {
2946 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002947 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002948 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002949 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002950 First = false;
2951 }
2952
2953 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002954 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002955 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2956 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002957 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002958 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002959 }
2960 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002961 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2962 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002963 ThisElt, DAG.getConstant(8, MVT::i8));
2964 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002965 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00002966 } else
2967 ThisElt = LastElt;
2968
Gabor Greifba36cb52008-08-28 21:40:38 +00002969 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00002970 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002971 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002972 }
2973 }
2974
Dale Johannesenace16102009-02-03 19:33:06 +00002975 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00002976}
2977
Bill Wendlinga348c562007-03-22 18:42:45 +00002978/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002979///
Dan Gohman475871a2008-07-27 21:46:04 +00002980static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002981 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002982 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002983 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00002984 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002985
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002986 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002987 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002988 bool First = true;
2989 for (unsigned i = 0; i < 8; ++i) {
2990 bool isNonZero = (NonZeros & (1 << i)) != 0;
2991 if (isNonZero) {
2992 if (First) {
2993 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002994 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002995 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002996 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002997 First = false;
2998 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002999 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003000 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003001 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003002 }
3003 }
3004
3005 return V;
3006}
3007
Evan Chengf26ffe92008-05-29 08:22:04 +00003008/// getVShift - Return a vector logical shift node.
3009///
Dan Gohman475871a2008-07-27 21:46:04 +00003010static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 unsigned NumBits, SelectionDAG &DAG,
3012 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003013 bool isMMX = VT.getSizeInBits() == 64;
3014 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003015 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003016 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3017 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3018 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003019 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003020}
3021
Dan Gohman475871a2008-07-27 21:46:04 +00003022SDValue
3023X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003024 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003025 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003026 if (ISD::isBuildVectorAllZeros(Op.getNode())
3027 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003028 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3029 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3030 // eliminated on x86-32 hosts.
3031 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3032 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003033
Gabor Greifba36cb52008-08-28 21:40:38 +00003034 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003035 return getOnesVector(Op.getValueType(), DAG, dl);
3036 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003037 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003038
Duncan Sands83ec4b62008-06-06 12:08:01 +00003039 MVT VT = Op.getValueType();
3040 MVT EVT = VT.getVectorElementType();
3041 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003042
3043 unsigned NumElems = Op.getNumOperands();
3044 unsigned NumZero = 0;
3045 unsigned NumNonZero = 0;
3046 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003047 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003049 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003050 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003051 if (Elt.getOpcode() == ISD::UNDEF)
3052 continue;
3053 Values.insert(Elt);
3054 if (Elt.getOpcode() != ISD::Constant &&
3055 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003056 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003057 if (isZeroNode(Elt))
3058 NumZero++;
3059 else {
3060 NonZeros |= (1 << i);
3061 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003062 }
3063 }
3064
Dan Gohman7f321562007-06-25 16:23:39 +00003065 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003066 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003067 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003068 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003069
Chris Lattner67f453a2008-03-09 05:42:06 +00003070 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003071 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003072 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003073 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003074
Chris Lattner62098042008-03-09 01:05:04 +00003075 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3076 // the value are obviously zero, truncate the value to i32 and do the
3077 // insertion that way. Only do this if the value is non-constant or if the
3078 // value is a constant being inserted into element 0. It is cheaper to do
3079 // a constant pool load than it is to do a movd + shuffle.
3080 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3081 (!IsAllConstants || Idx == 0)) {
3082 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3083 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003084 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3085 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003086
Chris Lattner62098042008-03-09 01:05:04 +00003087 // Truncate the value (which may itself be a constant) to i32, and
3088 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003089 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3090 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003091 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3092 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003093
Chris Lattner62098042008-03-09 01:05:04 +00003094 // Now we have our 32-bit value zero extended in the low element of
3095 // a vector. If Idx != 0, swizzle it into place.
3096 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 SmallVector<int, 4> Mask;
3098 Mask.push_back(Idx);
3099 for (unsigned i = 1; i != VecElts; ++i)
3100 Mask.push_back(i);
3101 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3102 DAG.getUNDEF(Item.getValueType()),
3103 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003104 }
Dale Johannesenace16102009-02-03 19:33:06 +00003105 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003106 }
3107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003108
Chris Lattner19f79692008-03-08 22:59:52 +00003109 // If we have a constant or non-constant insertion into the low element of
3110 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3111 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3112 // depending on what the source datatype is. Because we can only get here
3113 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3114 if (Idx == 0 &&
3115 // Don't do this for i64 values on x86-32.
3116 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003117 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003118 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003119 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3120 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003121 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003122
3123 // Is it a vector logical left shift?
3124 if (NumElems == 2 && Idx == 1 &&
3125 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003126 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003127 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003128 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003129 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003130 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003132
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003133 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003134 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003135
Chris Lattner19f79692008-03-08 22:59:52 +00003136 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3137 // is a non-constant being inserted into an element other than the low one,
3138 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3139 // movd/movss) to move this into the low element, then shuffle it into
3140 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003141 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003142 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003143
Evan Cheng0db9fe62006-04-25 20:13:52 +00003144 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003145 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3146 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003148 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003149 MaskVec.push_back(i == Idx ? 0 : 1);
3150 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003151 }
3152 }
3153
Chris Lattner67f453a2008-03-09 05:42:06 +00003154 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3155 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003156 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003157
Dan Gohmana3941172007-07-24 22:55:08 +00003158 // A vector full of immediates; various special cases are already
3159 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003160 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003161 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003162
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003163 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003164 if (EVTBits == 64) {
3165 if (NumNonZero == 1) {
3166 // One half is zero or undef.
3167 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003168 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003169 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003170 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3171 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003172 }
Dan Gohman475871a2008-07-27 21:46:04 +00003173 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003174 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003175
3176 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003177 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003178 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003179 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003180 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003181 }
3182
Bill Wendling826f36f2007-03-28 00:57:11 +00003183 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003184 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003185 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003186 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003187 }
3188
3189 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003190 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003191 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003192 if (NumElems == 4 && NumZero > 0) {
3193 for (unsigned i = 0; i < 4; ++i) {
3194 bool isZero = !(NonZeros & (1 << i));
3195 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003196 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003197 else
Dale Johannesenace16102009-02-03 19:33:06 +00003198 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003199 }
3200
3201 for (unsigned i = 0; i < 2; ++i) {
3202 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3203 default: break;
3204 case 0:
3205 V[i] = V[i*2]; // Must be a zero vector.
3206 break;
3207 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003209 break;
3210 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003212 break;
3213 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003214 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003215 break;
3216 }
3217 }
3218
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003220 bool Reverse = (NonZeros & 0x3) == 2;
3221 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003222 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3224 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3226 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003227 }
3228
3229 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3231 // values to be inserted is equal to the number of elements, in which case
3232 // use the unpack code below in the hopes of matching the consecutive elts
3233 // load merge pattern for shuffles.
3234 // FIXME: We could probably just check that here directly.
3235 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3236 getSubtarget()->hasSSE41()) {
3237 V[0] = DAG.getUNDEF(VT);
3238 for (unsigned i = 0; i < NumElems; ++i)
3239 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3240 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3241 Op.getOperand(i), DAG.getIntPtrConstant(i));
3242 return V[0];
3243 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003244 // Expand into a number of unpckl*.
3245 // e.g. for v4f32
3246 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3247 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3248 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003249 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003250 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003251 NumElems >>= 1;
3252 while (NumElems != 0) {
3253 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003255 NumElems >>= 1;
3256 }
3257 return V[0];
3258 }
3259
Dan Gohman475871a2008-07-27 21:46:04 +00003260 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003261}
3262
Nate Begemanb9a47b82009-02-23 08:49:38 +00003263// v8i16 shuffles - Prefer shuffles in the following order:
3264// 1. [all] pshuflw, pshufhw, optional move
3265// 2. [ssse3] 1 x pshufb
3266// 3. [ssse3] 2 x pshufb + 1 x por
3267// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003268static
Nate Begeman9008ca62009-04-27 18:41:29 +00003269SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3270 SelectionDAG &DAG, X86TargetLowering &TLI) {
3271 SDValue V1 = SVOp->getOperand(0);
3272 SDValue V2 = SVOp->getOperand(1);
3273 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003274 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003275
Nate Begemanb9a47b82009-02-23 08:49:38 +00003276 // Determine if more than 1 of the words in each of the low and high quadwords
3277 // of the result come from the same quadword of one of the two inputs. Undef
3278 // mask values count as coming from any quadword, for better codegen.
3279 SmallVector<unsigned, 4> LoQuad(4);
3280 SmallVector<unsigned, 4> HiQuad(4);
3281 BitVector InputQuads(4);
3282 for (unsigned i = 0; i < 8; ++i) {
3283 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003284 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003285 MaskVals.push_back(EltIdx);
3286 if (EltIdx < 0) {
3287 ++Quad[0];
3288 ++Quad[1];
3289 ++Quad[2];
3290 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003291 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003292 }
3293 ++Quad[EltIdx / 4];
3294 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003295 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003296
Nate Begemanb9a47b82009-02-23 08:49:38 +00003297 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003298 unsigned MaxQuad = 1;
3299 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003300 if (LoQuad[i] > MaxQuad) {
3301 BestLoQuad = i;
3302 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003303 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003304 }
3305
Nate Begemanb9a47b82009-02-23 08:49:38 +00003306 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003307 MaxQuad = 1;
3308 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003309 if (HiQuad[i] > MaxQuad) {
3310 BestHiQuad = i;
3311 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003312 }
3313 }
3314
Nate Begemanb9a47b82009-02-23 08:49:38 +00003315 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3316 // of the two input vectors, shuffle them into one input vector so only a
3317 // single pshufb instruction is necessary. If There are more than 2 input
3318 // quads, disable the next transformation since it does not help SSSE3.
3319 bool V1Used = InputQuads[0] || InputQuads[1];
3320 bool V2Used = InputQuads[2] || InputQuads[3];
3321 if (TLI.getSubtarget()->hasSSSE3()) {
3322 if (InputQuads.count() == 2 && V1Used && V2Used) {
3323 BestLoQuad = InputQuads.find_first();
3324 BestHiQuad = InputQuads.find_next(BestLoQuad);
3325 }
3326 if (InputQuads.count() > 2) {
3327 BestLoQuad = -1;
3328 BestHiQuad = -1;
3329 }
3330 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003331
Nate Begemanb9a47b82009-02-23 08:49:38 +00003332 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3333 // the shuffle mask. If a quad is scored as -1, that means that it contains
3334 // words from all 4 input quadwords.
3335 SDValue NewV;
3336 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003337 SmallVector<int, 8> MaskV;
3338 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3339 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3340 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3341 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3342 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003343 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003344
Nate Begemanb9a47b82009-02-23 08:49:38 +00003345 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3346 // source words for the shuffle, to aid later transformations.
3347 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003348 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003349 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003350 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003351 if (idx != (int)i)
3352 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003353 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003354 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003355 AllWordsInNewV = false;
3356 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003357 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003358
Nate Begemanb9a47b82009-02-23 08:49:38 +00003359 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3360 if (AllWordsInNewV) {
3361 for (int i = 0; i != 8; ++i) {
3362 int idx = MaskVals[i];
3363 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003364 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003365 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3366 if ((idx != i) && idx < 4)
3367 pshufhw = false;
3368 if ((idx != i) && idx > 3)
3369 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003370 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003371 V1 = NewV;
3372 V2Used = false;
3373 BestLoQuad = 0;
3374 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003375 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003376
Nate Begemanb9a47b82009-02-23 08:49:38 +00003377 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3378 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003379 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3381 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003382 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003383 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003384
3385 // If we have SSSE3, and all words of the result are from 1 input vector,
3386 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3387 // is present, fall back to case 4.
3388 if (TLI.getSubtarget()->hasSSSE3()) {
3389 SmallVector<SDValue,16> pshufbMask;
3390
3391 // If we have elements from both input vectors, set the high bit of the
3392 // shuffle mask element to zero out elements that come from V2 in the V1
3393 // mask, and elements that come from V1 in the V2 mask, so that the two
3394 // results can be OR'd together.
3395 bool TwoInputs = V1Used && V2Used;
3396 for (unsigned i = 0; i != 8; ++i) {
3397 int EltIdx = MaskVals[i] * 2;
3398 if (TwoInputs && (EltIdx >= 16)) {
3399 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3400 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3401 continue;
3402 }
3403 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3404 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3405 }
3406 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3407 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003408 DAG.getNode(ISD::BUILD_VECTOR, dl,
3409 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003410 if (!TwoInputs)
3411 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3412
3413 // Calculate the shuffle mask for the second input, shuffle it, and
3414 // OR it with the first shuffled input.
3415 pshufbMask.clear();
3416 for (unsigned i = 0; i != 8; ++i) {
3417 int EltIdx = MaskVals[i] * 2;
3418 if (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 - 16, MVT::i8));
3424 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3425 }
3426 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3427 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
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 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3431 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3432 }
3433
3434 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3435 // and update MaskVals with new element order.
3436 BitVector InOrder(8);
3437 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003438 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003439 for (int i = 0; i != 4; ++i) {
3440 int idx = MaskVals[i];
3441 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003443 InOrder.set(i);
3444 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003446 InOrder.set(i);
3447 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003449 }
3450 }
3451 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003452 MaskV.push_back(i);
3453 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3454 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003455 }
3456
3457 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3458 // and update MaskVals with the new element order.
3459 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003461 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003463 for (unsigned i = 4; i != 8; ++i) {
3464 int idx = MaskVals[i];
3465 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003466 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003467 InOrder.set(i);
3468 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003470 InOrder.set(i);
3471 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003473 }
3474 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3476 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003477 }
3478
3479 // In case BestHi & BestLo were both -1, which means each quadword has a word
3480 // from each of the four input quadwords, calculate the InOrder bitvector now
3481 // before falling through to the insert/extract cleanup.
3482 if (BestLoQuad == -1 && BestHiQuad == -1) {
3483 NewV = V1;
3484 for (int i = 0; i != 8; ++i)
3485 if (MaskVals[i] < 0 || MaskVals[i] == i)
3486 InOrder.set(i);
3487 }
3488
3489 // The other elements are put in the right place using pextrw and pinsrw.
3490 for (unsigned i = 0; i != 8; ++i) {
3491 if (InOrder[i])
3492 continue;
3493 int EltIdx = MaskVals[i];
3494 if (EltIdx < 0)
3495 continue;
3496 SDValue ExtOp = (EltIdx < 8)
3497 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3498 DAG.getIntPtrConstant(EltIdx))
3499 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3500 DAG.getIntPtrConstant(EltIdx - 8));
3501 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3502 DAG.getIntPtrConstant(i));
3503 }
3504 return NewV;
3505}
3506
3507// v16i8 shuffles - Prefer shuffles in the following order:
3508// 1. [ssse3] 1 x pshufb
3509// 2. [ssse3] 2 x pshufb + 1 x por
3510// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3511static
Nate Begeman9008ca62009-04-27 18:41:29 +00003512SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3513 SelectionDAG &DAG, X86TargetLowering &TLI) {
3514 SDValue V1 = SVOp->getOperand(0);
3515 SDValue V2 = SVOp->getOperand(1);
3516 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003517 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003518 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003519
3520 // If we have SSSE3, case 1 is generated when all result bytes come from
3521 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3522 // present, fall back to case 3.
3523 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3524 bool V1Only = true;
3525 bool V2Only = true;
3526 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003527 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003528 if (EltIdx < 0)
3529 continue;
3530 if (EltIdx < 16)
3531 V2Only = false;
3532 else
3533 V1Only = false;
3534 }
3535
3536 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3537 if (TLI.getSubtarget()->hasSSSE3()) {
3538 SmallVector<SDValue,16> pshufbMask;
3539
3540 // If all result elements are from one input vector, then only translate
3541 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3542 //
3543 // Otherwise, we have elements from both input vectors, and must zero out
3544 // elements that come from V2 in the first mask, and V1 in the second mask
3545 // so that we can OR them together.
3546 bool TwoInputs = !(V1Only || V2Only);
3547 for (unsigned i = 0; i != 16; ++i) {
3548 int EltIdx = MaskVals[i];
3549 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3550 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3551 continue;
3552 }
3553 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3554 }
3555 // If all the elements are from V2, assign it to V1 and return after
3556 // building the first pshufb.
3557 if (V2Only)
3558 V1 = V2;
3559 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003560 DAG.getNode(ISD::BUILD_VECTOR, dl,
3561 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003562 if (!TwoInputs)
3563 return V1;
3564
3565 // Calculate the shuffle mask for the second input, shuffle it, and
3566 // OR it with the first shuffled input.
3567 pshufbMask.clear();
3568 for (unsigned i = 0; i != 16; ++i) {
3569 int EltIdx = MaskVals[i];
3570 if (EltIdx < 16) {
3571 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3572 continue;
3573 }
3574 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3575 }
3576 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003577 DAG.getNode(ISD::BUILD_VECTOR, dl,
3578 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003579 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3580 }
3581
3582 // No SSSE3 - Calculate in place words and then fix all out of place words
3583 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3584 // the 16 different words that comprise the two doublequadword input vectors.
3585 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3586 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3587 SDValue NewV = V2Only ? V2 : V1;
3588 for (int i = 0; i != 8; ++i) {
3589 int Elt0 = MaskVals[i*2];
3590 int Elt1 = MaskVals[i*2+1];
3591
3592 // This word of the result is all undef, skip it.
3593 if (Elt0 < 0 && Elt1 < 0)
3594 continue;
3595
3596 // This word of the result is already in the correct place, skip it.
3597 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3598 continue;
3599 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3600 continue;
3601
3602 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3603 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3604 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003605
3606 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3607 // using a single extract together, load it and store it.
3608 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3609 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3610 DAG.getIntPtrConstant(Elt1 / 2));
3611 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3612 DAG.getIntPtrConstant(i));
3613 continue;
3614 }
3615
Nate Begemanb9a47b82009-02-23 08:49:38 +00003616 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003617 // source byte is not also odd, shift the extracted word left 8 bits
3618 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003619 if (Elt1 >= 0) {
3620 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3621 DAG.getIntPtrConstant(Elt1 / 2));
3622 if ((Elt1 & 1) == 0)
3623 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3624 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003625 else if (Elt0 >= 0)
3626 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3627 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003628 }
3629 // If Elt0 is defined, extract it from the appropriate source. If the
3630 // source byte is not also even, shift the extracted word right 8 bits. If
3631 // Elt1 was also defined, OR the extracted values together before
3632 // inserting them in the result.
3633 if (Elt0 >= 0) {
3634 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3635 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3636 if ((Elt0 & 1) != 0)
3637 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3638 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003639 else if (Elt1 >= 0)
3640 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3641 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003642 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3643 : InsElt0;
3644 }
3645 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3646 DAG.getIntPtrConstant(i));
3647 }
3648 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003649}
3650
Evan Cheng7a831ce2007-12-15 03:00:47 +00003651/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3652/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3653/// done when every pair / quad of shuffle mask elements point to elements in
3654/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003655/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3656static
Nate Begeman9008ca62009-04-27 18:41:29 +00003657SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3658 SelectionDAG &DAG,
3659 TargetLowering &TLI, DebugLoc dl) {
3660 MVT VT = SVOp->getValueType(0);
3661 SDValue V1 = SVOp->getOperand(0);
3662 SDValue V2 = SVOp->getOperand(1);
3663 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003664 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003665 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003666 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003667 MVT NewVT = MaskVT;
3668 switch (VT.getSimpleVT()) {
3669 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003670 case MVT::v4f32: NewVT = MVT::v2f64; break;
3671 case MVT::v4i32: NewVT = MVT::v2i64; break;
3672 case MVT::v8i16: NewVT = MVT::v4i32; break;
3673 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003674 }
3675
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003676 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003677 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003678 NewVT = MVT::v2i64;
3679 else
3680 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003681 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003682 int Scale = NumElems / NewWidth;
3683 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003684 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003685 int StartIdx = -1;
3686 for (int j = 0; j < Scale; ++j) {
3687 int EltIdx = SVOp->getMaskElt(i+j);
3688 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003689 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003690 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003691 StartIdx = EltIdx - (EltIdx % Scale);
3692 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003693 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003694 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003695 if (StartIdx == -1)
3696 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003697 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003699 }
3700
Dale Johannesenace16102009-02-03 19:33:06 +00003701 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3702 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003703 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003704}
3705
Evan Chengd880b972008-05-09 21:53:03 +00003706/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003707///
Dan Gohman475871a2008-07-27 21:46:04 +00003708static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 SDValue SrcOp, SelectionDAG &DAG,
3710 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003711 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3712 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003713 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003714 LD = dyn_cast<LoadSDNode>(SrcOp);
3715 if (!LD) {
3716 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3717 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003719 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3720 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3721 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3722 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3723 // PR2108
3724 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003725 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3726 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3727 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3728 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003729 SrcOp.getOperand(0)
3730 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003731 }
3732 }
3733 }
3734
Dale Johannesenace16102009-02-03 19:33:06 +00003735 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3736 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003737 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003738 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003739}
3740
Evan Chengace3c172008-07-22 21:13:36 +00003741/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3742/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003743static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003744LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3745 SDValue V1 = SVOp->getOperand(0);
3746 SDValue V2 = SVOp->getOperand(1);
3747 DebugLoc dl = SVOp->getDebugLoc();
3748 MVT VT = SVOp->getValueType(0);
3749
Evan Chengace3c172008-07-22 21:13:36 +00003750 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003751 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003752 SmallVector<int, 8> Mask1(4U, -1);
3753 SmallVector<int, 8> PermMask;
3754 SVOp->getMask(PermMask);
3755
Evan Chengace3c172008-07-22 21:13:36 +00003756 unsigned NumHi = 0;
3757 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003758 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003759 int Idx = PermMask[i];
3760 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003761 Locs[i] = std::make_pair(-1, -1);
3762 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003763 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3764 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003765 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003767 NumLo++;
3768 } else {
3769 Locs[i] = std::make_pair(1, NumHi);
3770 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003771 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003772 NumHi++;
3773 }
3774 }
3775 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003776
Evan Chengace3c172008-07-22 21:13:36 +00003777 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003778 // If no more than two elements come from either vector. This can be
3779 // implemented with two shuffles. First shuffle gather the elements.
3780 // The second shuffle, which takes the first shuffle as both of its
3781 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003783
Nate Begeman9008ca62009-04-27 18:41:29 +00003784 SmallVector<int, 8> Mask2(4U, -1);
3785
Evan Chengace3c172008-07-22 21:13:36 +00003786 for (unsigned i = 0; i != 4; ++i) {
3787 if (Locs[i].first == -1)
3788 continue;
3789 else {
3790 unsigned Idx = (i < 2) ? 0 : 4;
3791 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003792 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003793 }
3794 }
3795
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003797 } else if (NumLo == 3 || NumHi == 3) {
3798 // Otherwise, we must have three elements from one vector, call it X, and
3799 // one element from the other, call it Y. First, use a shufps to build an
3800 // intermediate vector with the one element from Y and the element from X
3801 // that will be in the same half in the final destination (the indexes don't
3802 // matter). Then, use a shufps to build the final vector, taking the half
3803 // containing the element from Y from the intermediate, and the other half
3804 // from X.
3805 if (NumHi == 3) {
3806 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003807 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003808 std::swap(V1, V2);
3809 }
3810
3811 // Find the element from V2.
3812 unsigned HiIndex;
3813 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 int Val = PermMask[HiIndex];
3815 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003816 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003817 if (Val >= 4)
3818 break;
3819 }
3820
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 Mask1[0] = PermMask[HiIndex];
3822 Mask1[1] = -1;
3823 Mask1[2] = PermMask[HiIndex^1];
3824 Mask1[3] = -1;
3825 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003826
3827 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 Mask1[0] = PermMask[0];
3829 Mask1[1] = PermMask[1];
3830 Mask1[2] = HiIndex & 1 ? 6 : 4;
3831 Mask1[3] = HiIndex & 1 ? 4 : 6;
3832 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003833 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003834 Mask1[0] = HiIndex & 1 ? 2 : 0;
3835 Mask1[1] = HiIndex & 1 ? 0 : 2;
3836 Mask1[2] = PermMask[2];
3837 Mask1[3] = PermMask[3];
3838 if (Mask1[2] >= 0)
3839 Mask1[2] += 4;
3840 if (Mask1[3] >= 0)
3841 Mask1[3] += 4;
3842 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003843 }
Evan Chengace3c172008-07-22 21:13:36 +00003844 }
3845
3846 // Break it into (shuffle shuffle_hi, shuffle_lo).
3847 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 SmallVector<int,8> LoMask(4U, -1);
3849 SmallVector<int,8> HiMask(4U, -1);
3850
3851 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003852 unsigned MaskIdx = 0;
3853 unsigned LoIdx = 0;
3854 unsigned HiIdx = 2;
3855 for (unsigned i = 0; i != 4; ++i) {
3856 if (i == 2) {
3857 MaskPtr = &HiMask;
3858 MaskIdx = 1;
3859 LoIdx = 0;
3860 HiIdx = 2;
3861 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 int Idx = PermMask[i];
3863 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003864 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003865 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003866 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003868 LoIdx++;
3869 } else {
3870 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003872 HiIdx++;
3873 }
3874 }
3875
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3877 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3878 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003879 for (unsigned i = 0; i != 4; ++i) {
3880 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003881 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003882 } else {
3883 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003885 }
3886 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003888}
3889
Dan Gohman475871a2008-07-27 21:46:04 +00003890SDValue
3891X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003893 SDValue V1 = Op.getOperand(0);
3894 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003895 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003896 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003899 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3900 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003901 bool V1IsSplat = false;
3902 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003903
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003905 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003906
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 // Promote splats to v4f32.
3908 if (SVOp->isSplat()) {
3909 if (isMMX || NumElems < 4)
3910 return Op;
3911 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003912 }
3913
Evan Cheng7a831ce2007-12-15 03:00:47 +00003914 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3915 // do it!
3916 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003917 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003918 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003919 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003920 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003921 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3922 // FIXME: Figure out a cleaner way to do this.
3923 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003924 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003926 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3928 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3929 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003930 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003931 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3933 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003934 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003936 }
3937 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003938
3939 if (X86::isPSHUFDMask(SVOp))
3940 return Op;
3941
Evan Chengf26ffe92008-05-29 08:22:04 +00003942 // Check if this can be converted into a logical shift.
3943 bool isLeft = false;
3944 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003945 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 bool isShift = getSubtarget()->hasSSE2() &&
3947 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003948 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003949 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003950 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003951 MVT EVT = VT.getVectorElementType();
3952 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003953 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003954 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003955
3956 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003957 if (V1IsUndef)
3958 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003959 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003960 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003961 if (!isMMX)
3962 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003963 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003964
3965 // FIXME: fold these into legal mask.
3966 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
3967 X86::isMOVSLDUPMask(SVOp) ||
3968 X86::isMOVHLPSMask(SVOp) ||
3969 X86::isMOVHPMask(SVOp) ||
3970 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003971 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003972
Nate Begeman9008ca62009-04-27 18:41:29 +00003973 if (ShouldXformToMOVHLPS(SVOp) ||
3974 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
3975 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003976
Evan Chengf26ffe92008-05-29 08:22:04 +00003977 if (isShift) {
3978 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003979 MVT EVT = VT.getVectorElementType();
3980 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003981 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003982 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003983
Evan Cheng9eca5e82006-10-25 21:49:50 +00003984 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003985 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3986 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00003987 V1IsSplat = isSplatVector(V1.getNode());
3988 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00003989
Chris Lattner8a594482007-11-25 00:24:49 +00003990 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003991 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003992 Op = CommuteVectorShuffle(SVOp, DAG);
3993 SVOp = cast<ShuffleVectorSDNode>(Op);
3994 V1 = SVOp->getOperand(0);
3995 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003996 std::swap(V1IsSplat, V2IsSplat);
3997 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003998 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003999 }
4000
Nate Begeman9008ca62009-04-27 18:41:29 +00004001 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4002 // Shuffling low element of v1 into undef, just return v1.
4003 if (V2IsUndef)
4004 return V1;
4005 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4006 // the instruction selector will not match, so get a canonical MOVL with
4007 // swapped operands to undo the commute.
4008 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004009 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004010
Nate Begeman9008ca62009-04-27 18:41:29 +00004011 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4012 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4013 X86::isUNPCKLMask(SVOp) ||
4014 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004015 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004016
Evan Cheng9bbbb982006-10-25 20:48:19 +00004017 if (V2IsSplat) {
4018 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004019 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004020 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004021 SDValue NewMask = NormalizeMask(SVOp, DAG);
4022 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4023 if (NSVOp != SVOp) {
4024 if (X86::isUNPCKLMask(NSVOp, true)) {
4025 return NewMask;
4026 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4027 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004028 }
4029 }
4030 }
4031
Evan Cheng9eca5e82006-10-25 21:49:50 +00004032 if (Commuted) {
4033 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004034 // FIXME: this seems wrong.
4035 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4036 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4037 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4038 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4039 X86::isUNPCKLMask(NewSVOp) ||
4040 X86::isUNPCKHMask(NewSVOp))
4041 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004042 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004043
Nate Begemanb9a47b82009-02-23 08:49:38 +00004044 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004045
4046 // Normalize the node to match x86 shuffle ops if needed
4047 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4048 return CommuteVectorShuffle(SVOp, DAG);
4049
4050 // Check for legal shuffle and return?
4051 SmallVector<int, 16> PermMask;
4052 SVOp->getMask(PermMask);
4053 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004054 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004055
Evan Cheng14b32e12007-12-11 01:46:18 +00004056 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4057 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004058 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004059 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004060 return NewOp;
4061 }
4062
Nate Begemanb9a47b82009-02-23 08:49:38 +00004063 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004065 if (NewOp.getNode())
4066 return NewOp;
4067 }
4068
Evan Chengace3c172008-07-22 21:13:36 +00004069 // Handle all 4 wide cases with a number of shuffles except for MMX.
4070 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004071 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004072
Dan Gohman475871a2008-07-27 21:46:04 +00004073 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004074}
4075
Dan Gohman475871a2008-07-27 21:46:04 +00004076SDValue
4077X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004078 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004079 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004080 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004081 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004082 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004083 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004084 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004085 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004086 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004087 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004088 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4089 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4090 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004091 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4092 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4093 DAG.getNode(ISD::BIT_CONVERT, dl,
4094 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004095 Op.getOperand(0)),
4096 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004097 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004098 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004099 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004100 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004101 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004102 } else if (VT == MVT::f32) {
4103 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4104 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004105 // result has a single use which is a store or a bitcast to i32. And in
4106 // the case of a store, it's not worth it if the index is a constant 0,
4107 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004108 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004109 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004110 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004111 if ((User->getOpcode() != ISD::STORE ||
4112 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4113 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004114 (User->getOpcode() != ISD::BIT_CONVERT ||
4115 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004116 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004117 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004118 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004119 Op.getOperand(0)),
4120 Op.getOperand(1));
4121 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004122 } else if (VT == MVT::i32) {
4123 // ExtractPS works with constant index.
4124 if (isa<ConstantSDNode>(Op.getOperand(1)))
4125 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004126 }
Dan Gohman475871a2008-07-27 21:46:04 +00004127 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004128}
4129
4130
Dan Gohman475871a2008-07-27 21:46:04 +00004131SDValue
4132X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004133 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004134 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004135
Evan Cheng62a3f152008-03-24 21:52:23 +00004136 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004137 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004138 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004139 return Res;
4140 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004141
Duncan Sands83ec4b62008-06-06 12:08:01 +00004142 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004143 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004144 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004145 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004146 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004147 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004148 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004149 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4150 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004151 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004152 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004153 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004154 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004155 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004156 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004157 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004158 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004159 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004160 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004161 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004162 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004163 if (Idx == 0)
4164 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004165
Evan Cheng0db9fe62006-04-25 20:13:52 +00004166 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 int Mask[4] = { Idx, -1, -1, -1 };
4168 MVT VVT = Op.getOperand(0).getValueType();
4169 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4170 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004171 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004172 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004173 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004174 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4175 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4176 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004177 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004178 if (Idx == 0)
4179 return Op;
4180
4181 // UNPCKHPD the element to the lowest double word, then movsd.
4182 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4183 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 int Mask[2] = { 1, -1 };
4185 MVT VVT = Op.getOperand(0).getValueType();
4186 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4187 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004188 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004189 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004190 }
4191
Dan Gohman475871a2008-07-27 21:46:04 +00004192 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004193}
4194
Dan Gohman475871a2008-07-27 21:46:04 +00004195SDValue
4196X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004197 MVT VT = Op.getValueType();
4198 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004199 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004200
Dan Gohman475871a2008-07-27 21:46:04 +00004201 SDValue N0 = Op.getOperand(0);
4202 SDValue N1 = Op.getOperand(1);
4203 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004204
Dan Gohmanef521f12008-08-14 22:53:18 +00004205 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4206 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004207 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004209 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4210 // argument.
4211 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004212 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004213 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004214 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004215 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004216 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004217 // Bits [7:6] of the constant are the source select. This will always be
4218 // zero here. The DAG Combiner may combine an extract_elt index into these
4219 // bits. For example (insert (extract, 3), 2) could be matched by putting
4220 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004221 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004222 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004223 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004224 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004225 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004226 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004227 } else if (EVT == MVT::i32) {
4228 // InsertPS works with constant index.
4229 if (isa<ConstantSDNode>(N2))
4230 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004231 }
Dan Gohman475871a2008-07-27 21:46:04 +00004232 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004233}
4234
Dan Gohman475871a2008-07-27 21:46:04 +00004235SDValue
4236X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004237 MVT VT = Op.getValueType();
4238 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004239
4240 if (Subtarget->hasSSE41())
4241 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4242
Evan Cheng794405e2007-12-12 07:55:34 +00004243 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004244 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004245
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004246 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue N0 = Op.getOperand(0);
4248 SDValue N1 = Op.getOperand(1);
4249 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004250
Duncan Sands83ec4b62008-06-06 12:08:01 +00004251 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004252 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4253 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004254 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004255 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004256 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004257 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004258 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004259 }
Dan Gohman475871a2008-07-27 21:46:04 +00004260 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004261}
4262
Dan Gohman475871a2008-07-27 21:46:04 +00004263SDValue
4264X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004265 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004266 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004267 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4268 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4269 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004270 Op.getOperand(0))));
4271
Dale Johannesenace16102009-02-03 19:33:06 +00004272 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004273 MVT VT = MVT::v2i32;
4274 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004275 default: break;
4276 case MVT::v16i8:
4277 case MVT::v8i16:
4278 VT = MVT::v4i32;
4279 break;
4280 }
Dale Johannesenace16102009-02-03 19:33:06 +00004281 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4282 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004283}
4284
Bill Wendling056292f2008-09-16 21:48:12 +00004285// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4286// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4287// one of the above mentioned nodes. It has to be wrapped because otherwise
4288// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4289// be used to form addressing mode. These wrapped nodes will be selected
4290// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004291SDValue
4292X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004293 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004294 // FIXME there isn't really any debug info here, should come from the parent
4295 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004296 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4297 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004298 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004299 // With PIC, the address is actually $g + Offset.
4300 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4301 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004302 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004303 DAG.getNode(X86ISD::GlobalBaseReg,
4304 DebugLoc::getUnknownLoc(),
4305 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004306 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004307 }
4308
4309 return Result;
4310}
4311
Dan Gohman475871a2008-07-27 21:46:04 +00004312SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004313X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004314 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004315 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004316 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4317 bool ExtraLoadRequired =
4318 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4319
4320 // Create the TargetGlobalAddress node, folding in the constant
4321 // offset if it is legal.
4322 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004323 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004324 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4325 Offset = 0;
4326 } else
4327 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004328 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004329
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004330 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004331 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004332 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4333 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004334 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004336
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004337 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4338 // load the value at address GV, not the value of GV itself. This means that
4339 // the GlobalAddress must be in the base or index register of the address, not
4340 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004341 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004342 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004343 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004344 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004345
Dan Gohman6520e202008-10-18 02:06:02 +00004346 // If there was a non-zero offset that we didn't fold, create an explicit
4347 // addition for it.
4348 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004349 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004350 DAG.getConstant(Offset, getPointerTy()));
4351
Evan Cheng0db9fe62006-04-25 20:13:52 +00004352 return Result;
4353}
4354
Evan Chengda43bcf2008-09-24 00:05:32 +00004355SDValue
4356X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4357 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004358 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004359 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004360}
4361
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004362static SDValue
4363GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004364 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004365 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4366 DebugLoc dl = GA->getDebugLoc();
4367 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4368 GA->getValueType(0),
4369 GA->getOffset());
4370 if (InFlag) {
4371 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004372 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004373 } else {
4374 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004375 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004376 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004377 SDValue Flag = Chain.getValue(1);
4378 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004379}
4380
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004381// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004382static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004383LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004384 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004385 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004386 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4387 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004388 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004389 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004390 PtrVT), InFlag);
4391 InFlag = Chain.getValue(1);
4392
Rafael Espindola15f1b662009-04-24 12:59:40 +00004393 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004394}
4395
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004396// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004397static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004398LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004399 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004400 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004401}
4402
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004403// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4404// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004405static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004406 const MVT PtrVT, TLSModel::Model model,
4407 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004408 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004409 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004410 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4411 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004412 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4413 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004414
4415 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4416 NULL, 0);
4417
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004418 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4419 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004420 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004421 GA->getValueType(0),
4422 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004423 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004424
Rafael Espindola9a580232009-02-27 13:37:18 +00004425 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004426 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004427 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004428
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004429 // The address of the thread local variable is the add of the thread
4430 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004431 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004432}
4433
Dan Gohman475871a2008-07-27 21:46:04 +00004434SDValue
4435X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004436 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004437 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004438 assert(Subtarget->isTargetELF() &&
4439 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004440 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004441 GlobalValue *GV = GA->getGlobal();
4442 TLSModel::Model model =
4443 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004444 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004445 switch (model) {
4446 case TLSModel::GeneralDynamic:
4447 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004448 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004449
4450 case TLSModel::InitialExec:
4451 case TLSModel::LocalExec:
4452 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004453 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004454 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004455 switch (model) {
4456 case TLSModel::GeneralDynamic:
4457 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004458 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004459
4460 case TLSModel::InitialExec:
4461 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004462 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004463 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004464 }
Chris Lattner5867de12009-04-01 22:14:45 +00004465 assert(0 && "Unreachable");
4466 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004467}
4468
Dan Gohman475871a2008-07-27 21:46:04 +00004469SDValue
4470X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004471 // FIXME there isn't really any debug info here
4472 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004473 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4474 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004475 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004476 // With PIC, the address is actually $g + Offset.
4477 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4478 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004479 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004480 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004481 DebugLoc::getUnknownLoc(),
4482 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004483 Result);
4484 }
4485
4486 return Result;
4487}
4488
Dan Gohman475871a2008-07-27 21:46:04 +00004489SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004490 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004491 // FIXME there isn't really any debug into here
4492 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004493 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), 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(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004499 DAG.getNode(X86ISD::GlobalBaseReg,
4500 DebugLoc::getUnknownLoc(),
4501 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004502 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004503 }
4504
4505 return Result;
4506}
4507
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004508/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004509/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004510SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004511 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004512 MVT VT = Op.getValueType();
4513 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004514 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004515 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004516 SDValue ShOpLo = Op.getOperand(0);
4517 SDValue ShOpHi = Op.getOperand(1);
4518 SDValue ShAmt = Op.getOperand(2);
4519 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004520 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004521 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004522 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004523
Dan Gohman475871a2008-07-27 21:46:04 +00004524 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004525 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004526 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4527 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004528 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004529 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4530 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004531 }
Evan Chenge3413162006-01-09 18:33:28 +00004532
Dale Johannesenace16102009-02-03 19:33:06 +00004533 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004534 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004535 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004536 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004537
Dan Gohman475871a2008-07-27 21:46:04 +00004538 SDValue Hi, Lo;
4539 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4540 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4541 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004542
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004543 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004544 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4545 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004546 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004547 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4548 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004549 }
4550
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004552 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004553}
Evan Chenga3195e82006-01-12 22:54:21 +00004554
Dan Gohman475871a2008-07-27 21:46:04 +00004555SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004556 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004557 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004558 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004559
Eli Friedman36df4992009-05-27 00:47:34 +00004560 // These are really Legal; return the operand so the caller accepts it as
4561 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004562 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004563 return Op;
4564 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4565 Subtarget->is64Bit()) {
4566 return Op;
4567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004568
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004569 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004570 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 MachineFunction &MF = DAG.getMachineFunction();
4572 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004573 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004574 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004575 StackSlot,
4576 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004577 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4578}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579
Eli Friedman948e95a2009-05-23 09:59:16 +00004580SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4581 SDValue StackSlot,
4582 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004583 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004584 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004585 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004586 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004587 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004588 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4589 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004590 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004591 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592 Ops.push_back(Chain);
4593 Ops.push_back(StackSlot);
4594 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004595 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004596 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004597
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004598 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004599 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004600 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601
4602 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4603 // shouldn't be necessary except that RFP cannot be live across
4604 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004605 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004606 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004607 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004608 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004609 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004610 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004612 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 Ops.push_back(DAG.getValueType(Op.getValueType()));
4614 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004615 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4616 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004617 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004618 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004619
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 return Result;
4621}
4622
Bill Wendling8b8a6362009-01-17 03:56:04 +00004623// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4624SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4625 // This algorithm is not obvious. Here it is in C code, more or less:
4626 /*
4627 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4628 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4629 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004630
Bill Wendling8b8a6362009-01-17 03:56:04 +00004631 // Copy ints to xmm registers.
4632 __m128i xh = _mm_cvtsi32_si128( hi );
4633 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004634
Bill Wendling8b8a6362009-01-17 03:56:04 +00004635 // Combine into low half of a single xmm register.
4636 __m128i x = _mm_unpacklo_epi32( xh, xl );
4637 __m128d d;
4638 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004639
Bill Wendling8b8a6362009-01-17 03:56:04 +00004640 // Merge in appropriate exponents to give the integer bits the right
4641 // magnitude.
4642 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004643
Bill Wendling8b8a6362009-01-17 03:56:04 +00004644 // Subtract away the biases to deal with the IEEE-754 double precision
4645 // implicit 1.
4646 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004647
Bill Wendling8b8a6362009-01-17 03:56:04 +00004648 // All conversions up to here are exact. The correctly rounded result is
4649 // calculated using the current rounding mode using the following
4650 // horizontal add.
4651 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4652 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4653 // store doesn't really need to be here (except
4654 // maybe to zero the other double)
4655 return sd;
4656 }
4657 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004658
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004659 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004660
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004661 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004662 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004663 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4664 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4665 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4666 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4667 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004668 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004669
Bill Wendling8b8a6362009-01-17 03:56:04 +00004670 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004671 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4672 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4673 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004674 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004675
Dale Johannesenace16102009-02-03 19:33:06 +00004676 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4677 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004678 Op.getOperand(0),
4679 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004680 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4681 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004682 Op.getOperand(0),
4683 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004685 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004686 PseudoSourceValue::getConstantPool(), 0,
4687 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004689 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4690 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004691 PseudoSourceValue::getConstantPool(), 0,
4692 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004693 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004694
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004695 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 int ShufMask[2] = { 1, -1 };
4697 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4698 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004699 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4700 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004701 DAG.getIntPtrConstant(0));
4702}
4703
Bill Wendling8b8a6362009-01-17 03:56:04 +00004704// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4705SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004706 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004707 // FP constant to bias correct the final result.
4708 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4709 MVT::f64);
4710
4711 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004712 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4713 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004714 Op.getOperand(0),
4715 DAG.getIntPtrConstant(0)));
4716
Dale Johannesenace16102009-02-03 19:33:06 +00004717 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4718 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004719 DAG.getIntPtrConstant(0));
4720
4721 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004722 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4723 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4724 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004725 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004726 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4727 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004728 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004729 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4730 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004731 DAG.getIntPtrConstant(0));
4732
4733 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004734 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004735
4736 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004737 MVT DestVT = Op.getValueType();
4738
4739 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004740 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004741 DAG.getIntPtrConstant(0));
4742 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004743 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004744 }
4745
4746 // Handle final rounding.
4747 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004748}
4749
4750SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004751 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004752 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004753
Evan Chenga06ec9e2009-01-19 08:08:22 +00004754 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4755 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4756 // the optimization here.
4757 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004758 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004759
4760 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004761 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004762 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004763 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004764 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004765
Bill Wendling8b8a6362009-01-17 03:56:04 +00004766 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004767 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004768 return LowerUINT_TO_FP_i32(Op, DAG);
4769 }
4770
Eli Friedman948e95a2009-05-23 09:59:16 +00004771 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4772
4773 // Make a 64-bit buffer, and use it to build an FILD.
4774 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4775 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4776 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4777 getPointerTy(), StackSlot, WordOff);
4778 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4779 StackSlot, NULL, 0);
4780 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4781 OffsetSlot, NULL, 0);
4782 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004783}
4784
Dan Gohman475871a2008-07-27 21:46:04 +00004785std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004786FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004787 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004788
4789 MVT DstTy = Op.getValueType();
4790
4791 if (!IsSigned) {
4792 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4793 DstTy = MVT::i64;
4794 }
4795
4796 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4797 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004798 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004799
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004800 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004801 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004802 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004803 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004804 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004805 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00004806 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004807 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004808
Evan Cheng87c89352007-10-15 20:11:21 +00004809 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4810 // stack slot.
4811 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004812 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004813 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004814 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004815
Evan Cheng0db9fe62006-04-25 20:13:52 +00004816 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004817 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004818 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4819 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4820 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4821 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004822 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004823
Dan Gohman475871a2008-07-27 21:46:04 +00004824 SDValue Chain = DAG.getEntryNode();
4825 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004826 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004827 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004828 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004829 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004830 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004831 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004832 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4833 };
Dale Johannesenace16102009-02-03 19:33:06 +00004834 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004835 Chain = Value.getValue(1);
4836 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4837 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4838 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004839
Evan Cheng0db9fe62006-04-25 20:13:52 +00004840 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004841 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004842 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004843
Chris Lattner27a6c732007-11-24 07:07:01 +00004844 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004845}
4846
Dan Gohman475871a2008-07-27 21:46:04 +00004847SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004848 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004849 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00004850 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
4851 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004852
Chris Lattner27a6c732007-11-24 07:07:01 +00004853 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004854 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004855 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004856}
4857
Eli Friedman948e95a2009-05-23 09:59:16 +00004858SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4859 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4860 SDValue FIST = Vals.first, StackSlot = Vals.second;
4861 assert(FIST.getNode() && "Unexpected failure");
4862
4863 // Load the result.
4864 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4865 FIST, StackSlot, NULL, 0);
4866}
4867
Dan Gohman475871a2008-07-27 21:46:04 +00004868SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004869 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004870 MVT VT = Op.getValueType();
4871 MVT EltVT = VT;
4872 if (VT.isVector())
4873 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004874 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004875 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004876 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004877 CV.push_back(C);
4878 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004879 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004880 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004881 CV.push_back(C);
4882 CV.push_back(C);
4883 CV.push_back(C);
4884 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004885 }
Dan Gohmand3006222007-07-27 17:16:43 +00004886 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004887 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004888 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004889 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004890 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004891 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004892}
4893
Dan Gohman475871a2008-07-27 21:46:04 +00004894SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004895 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004896 MVT VT = Op.getValueType();
4897 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004898 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004899 if (VT.isVector()) {
4900 EltVT = VT.getVectorElementType();
4901 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004902 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004903 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004904 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004905 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004906 CV.push_back(C);
4907 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004908 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004909 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004910 CV.push_back(C);
4911 CV.push_back(C);
4912 CV.push_back(C);
4913 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004914 }
Dan Gohmand3006222007-07-27 17:16:43 +00004915 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004916 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004917 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004918 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004919 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004920 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004921 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4922 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004923 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004924 Op.getOperand(0)),
4925 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004926 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004927 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004928 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004929}
4930
Dan Gohman475871a2008-07-27 21:46:04 +00004931SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4932 SDValue Op0 = Op.getOperand(0);
4933 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004934 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004935 MVT VT = Op.getValueType();
4936 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004937
4938 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004939 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004940 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004941 SrcVT = VT;
4942 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004943 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004944 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004945 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004946 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004947 }
4948
4949 // At this point the operands and the result should have the same
4950 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004951
Evan Cheng68c47cb2007-01-05 07:55:56 +00004952 // First get the sign bit of second operand.
4953 std::vector<Constant*> CV;
4954 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004955 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4956 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004957 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004958 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4959 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4960 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4961 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004962 }
Dan Gohmand3006222007-07-27 17:16:43 +00004963 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004964 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004965 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004966 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004967 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004968 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004969
4970 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004971 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004972 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00004973 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
4974 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00004975 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004976 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
4977 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004978 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004979 }
4980
Evan Cheng73d6cf12007-01-05 21:37:56 +00004981 // Clear first operand sign bit.
4982 CV.clear();
4983 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004984 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4985 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004986 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004987 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4988 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4989 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004991 }
Dan Gohmand3006222007-07-27 17:16:43 +00004992 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004993 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004994 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004995 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004996 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004997 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004998
4999 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005000 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005001}
5002
Dan Gohman076aee32009-03-04 19:44:21 +00005003/// Emit nodes that will be selected as "test Op0,Op0", or something
5004/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005005SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5006 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005007 DebugLoc dl = Op.getDebugLoc();
5008
Dan Gohman31125812009-03-07 01:58:32 +00005009 // CF and OF aren't always set the way we want. Determine which
5010 // of these we need.
5011 bool NeedCF = false;
5012 bool NeedOF = false;
5013 switch (X86CC) {
5014 case X86::COND_A: case X86::COND_AE:
5015 case X86::COND_B: case X86::COND_BE:
5016 NeedCF = true;
5017 break;
5018 case X86::COND_G: case X86::COND_GE:
5019 case X86::COND_L: case X86::COND_LE:
5020 case X86::COND_O: case X86::COND_NO:
5021 NeedOF = true;
5022 break;
5023 default: break;
5024 }
5025
Dan Gohman076aee32009-03-04 19:44:21 +00005026 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005027 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5028 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5029 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005030 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005031 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005032 switch (Op.getNode()->getOpcode()) {
5033 case ISD::ADD:
5034 // Due to an isel shortcoming, be conservative if this add is likely to
5035 // be selected as part of a load-modify-store instruction. When the root
5036 // node in a match is a store, isel doesn't know how to remap non-chain
5037 // non-flag uses of other nodes in the match, such as the ADD in this
5038 // case. This leads to the ADD being left around and reselected, with
5039 // the result being two adds in the output.
5040 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5041 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5042 if (UI->getOpcode() == ISD::STORE)
5043 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005044 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005045 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5046 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005047 if (C->getAPIntValue() == 1) {
5048 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005049 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005050 break;
5051 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005052 // An add of negative one (subtract of one) will be selected as a DEC.
5053 if (C->getAPIntValue().isAllOnesValue()) {
5054 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005055 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005056 break;
5057 }
5058 }
Dan Gohman076aee32009-03-04 19:44:21 +00005059 // Otherwise use a regular EFLAGS-setting add.
5060 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005061 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005062 break;
5063 case ISD::SUB:
5064 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5065 // likely to be selected as part of a load-modify-store instruction.
5066 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5067 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5068 if (UI->getOpcode() == ISD::STORE)
5069 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005070 // Otherwise use a regular EFLAGS-setting sub.
5071 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005072 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005073 break;
5074 case X86ISD::ADD:
5075 case X86ISD::SUB:
5076 case X86ISD::INC:
5077 case X86ISD::DEC:
5078 return SDValue(Op.getNode(), 1);
5079 default:
5080 default_case:
5081 break;
5082 }
5083 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005084 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005085 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005086 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005087 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005088 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005089 DAG.ReplaceAllUsesWith(Op, New);
5090 return SDValue(New.getNode(), 1);
5091 }
5092 }
5093
5094 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5095 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5096 DAG.getConstant(0, Op.getValueType()));
5097}
5098
5099/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5100/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005101SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5102 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005103 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5104 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005105 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005106
5107 DebugLoc dl = Op0.getDebugLoc();
5108 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5109}
5110
Dan Gohman475871a2008-07-27 21:46:04 +00005111SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005112 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005113 SDValue Op0 = Op.getOperand(0);
5114 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005115 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005116 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005117
Dan Gohmane5af2d32009-01-29 01:59:02 +00005118 // Lower (X & (1 << N)) == 0 to BT(X, N).
5119 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5120 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005121 if (Op0.getOpcode() == ISD::AND &&
5122 Op0.hasOneUse() &&
5123 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005124 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005125 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005126 SDValue LHS, RHS;
5127 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5128 if (ConstantSDNode *Op010C =
5129 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5130 if (Op010C->getZExtValue() == 1) {
5131 LHS = Op0.getOperand(0);
5132 RHS = Op0.getOperand(1).getOperand(1);
5133 }
5134 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5135 if (ConstantSDNode *Op000C =
5136 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5137 if (Op000C->getZExtValue() == 1) {
5138 LHS = Op0.getOperand(1);
5139 RHS = Op0.getOperand(0).getOperand(1);
5140 }
5141 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5142 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5143 SDValue AndLHS = Op0.getOperand(0);
5144 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5145 LHS = AndLHS.getOperand(0);
5146 RHS = AndLHS.getOperand(1);
5147 }
5148 }
Evan Cheng0488db92007-09-25 01:57:46 +00005149
Dan Gohmane5af2d32009-01-29 01:59:02 +00005150 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005151 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5152 // instruction. Since the shift amount is in-range-or-undefined, we know
5153 // that doing a bittest on the i16 value is ok. We extend to i32 because
5154 // the encoding for the i16 version is larger than the i32 version.
5155 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005156 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005157
5158 // If the operand types disagree, extend the shift amount to match. Since
5159 // BT ignores high bits (like shifts) we can use anyextend.
5160 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005161 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005162
Dale Johannesenace16102009-02-03 19:33:06 +00005163 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005164 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005165 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005166 DAG.getConstant(Cond, MVT::i8), BT);
5167 }
5168 }
5169
5170 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5171 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005172
Dan Gohman31125812009-03-07 01:58:32 +00005173 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005174 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005175 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005176}
5177
Dan Gohman475871a2008-07-27 21:46:04 +00005178SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5179 SDValue Cond;
5180 SDValue Op0 = Op.getOperand(0);
5181 SDValue Op1 = Op.getOperand(1);
5182 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005183 MVT VT = Op.getValueType();
5184 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5185 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005186 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005187
5188 if (isFP) {
5189 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005190 MVT VT0 = Op0.getValueType();
5191 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5192 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005193 bool Swap = false;
5194
5195 switch (SetCCOpcode) {
5196 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005197 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005198 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005199 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005200 case ISD::SETGT: Swap = true; // Fallthrough
5201 case ISD::SETLT:
5202 case ISD::SETOLT: SSECC = 1; break;
5203 case ISD::SETOGE:
5204 case ISD::SETGE: Swap = true; // Fallthrough
5205 case ISD::SETLE:
5206 case ISD::SETOLE: SSECC = 2; break;
5207 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005208 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005209 case ISD::SETNE: SSECC = 4; break;
5210 case ISD::SETULE: Swap = true;
5211 case ISD::SETUGE: SSECC = 5; break;
5212 case ISD::SETULT: Swap = true;
5213 case ISD::SETUGT: SSECC = 6; break;
5214 case ISD::SETO: SSECC = 7; break;
5215 }
5216 if (Swap)
5217 std::swap(Op0, Op1);
5218
Nate Begemanfb8ead02008-07-25 19:05:58 +00005219 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005220 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005221 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005222 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005223 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5224 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5225 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005226 }
5227 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005228 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005229 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5230 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5231 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005232 }
5233 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005234 }
5235 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005236 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005238
Nate Begeman30a0de92008-07-17 16:51:19 +00005239 // We are handling one of the integer comparisons here. Since SSE only has
5240 // GT and EQ comparisons for integer, swapping operands and multiple
5241 // operations may be required for some comparisons.
5242 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5243 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005244
Nate Begeman30a0de92008-07-17 16:51:19 +00005245 switch (VT.getSimpleVT()) {
5246 default: break;
5247 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5248 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5249 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5250 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005252
Nate Begeman30a0de92008-07-17 16:51:19 +00005253 switch (SetCCOpcode) {
5254 default: break;
5255 case ISD::SETNE: Invert = true;
5256 case ISD::SETEQ: Opc = EQOpc; break;
5257 case ISD::SETLT: Swap = true;
5258 case ISD::SETGT: Opc = GTOpc; break;
5259 case ISD::SETGE: Swap = true;
5260 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5261 case ISD::SETULT: Swap = true;
5262 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5263 case ISD::SETUGE: Swap = true;
5264 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5265 }
5266 if (Swap)
5267 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005268
Nate Begeman30a0de92008-07-17 16:51:19 +00005269 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5270 // bits of the inputs before performing those operations.
5271 if (FlipSigns) {
5272 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005273 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5274 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005275 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005276 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5277 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005278 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5279 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005281
Dale Johannesenace16102009-02-03 19:33:06 +00005282 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005283
5284 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005285 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005286 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005287
Nate Begeman30a0de92008-07-17 16:51:19 +00005288 return Result;
5289}
Evan Cheng0488db92007-09-25 01:57:46 +00005290
Evan Cheng370e5342008-12-03 08:38:43 +00005291// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005292static bool isX86LogicalCmp(SDValue Op) {
5293 unsigned Opc = Op.getNode()->getOpcode();
5294 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5295 return true;
5296 if (Op.getResNo() == 1 &&
5297 (Opc == X86ISD::ADD ||
5298 Opc == X86ISD::SUB ||
5299 Opc == X86ISD::SMUL ||
5300 Opc == X86ISD::UMUL ||
5301 Opc == X86ISD::INC ||
5302 Opc == X86ISD::DEC))
5303 return true;
5304
5305 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005306}
5307
Dan Gohman475871a2008-07-27 21:46:04 +00005308SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005309 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005310 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005311 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005312 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005313
Evan Cheng734503b2006-09-11 02:19:56 +00005314 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005315 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005316
Evan Cheng3f41d662007-10-08 22:16:29 +00005317 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5318 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005319 if (Cond.getOpcode() == X86ISD::SETCC) {
5320 CC = Cond.getOperand(0);
5321
Dan Gohman475871a2008-07-27 21:46:04 +00005322 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005323 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005324 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005325
Evan Cheng3f41d662007-10-08 22:16:29 +00005326 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005327 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005328 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005329 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005330
Chris Lattnerd1980a52009-03-12 06:52:53 +00005331 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5332 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005333 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005334 addTest = false;
5335 }
5336 }
5337
5338 if (addTest) {
5339 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005340 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005341 }
5342
Dan Gohmanfc166572009-04-09 23:54:40 +00005343 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005344 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005345 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5346 // condition is true.
5347 Ops.push_back(Op.getOperand(2));
5348 Ops.push_back(Op.getOperand(1));
5349 Ops.push_back(CC);
5350 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005351 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005352}
5353
Evan Cheng370e5342008-12-03 08:38:43 +00005354// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5355// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5356// from the AND / OR.
5357static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5358 Opc = Op.getOpcode();
5359 if (Opc != ISD::OR && Opc != ISD::AND)
5360 return false;
5361 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5362 Op.getOperand(0).hasOneUse() &&
5363 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5364 Op.getOperand(1).hasOneUse());
5365}
5366
Evan Cheng961d6d42009-02-02 08:19:07 +00005367// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5368// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005369static bool isXor1OfSetCC(SDValue Op) {
5370 if (Op.getOpcode() != ISD::XOR)
5371 return false;
5372 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5373 if (N1C && N1C->getAPIntValue() == 1) {
5374 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5375 Op.getOperand(0).hasOneUse();
5376 }
5377 return false;
5378}
5379
Dan Gohman475871a2008-07-27 21:46:04 +00005380SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005381 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005382 SDValue Chain = Op.getOperand(0);
5383 SDValue Cond = Op.getOperand(1);
5384 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005385 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005387
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005389 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005390#if 0
5391 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005392 else if (Cond.getOpcode() == X86ISD::ADD ||
5393 Cond.getOpcode() == X86ISD::SUB ||
5394 Cond.getOpcode() == X86ISD::SMUL ||
5395 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005396 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005397#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005398
Evan Cheng3f41d662007-10-08 22:16:29 +00005399 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5400 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005401 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005402 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005403
Dan Gohman475871a2008-07-27 21:46:04 +00005404 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005405 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005406 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005407 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005408 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005409 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005410 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005411 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005412 default: break;
5413 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005414 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005415 // These can only come from an arithmetic instruction with overflow,
5416 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005417 Cond = Cond.getNode()->getOperand(1);
5418 addTest = false;
5419 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005420 }
Evan Cheng0488db92007-09-25 01:57:46 +00005421 }
Evan Cheng370e5342008-12-03 08:38:43 +00005422 } else {
5423 unsigned CondOpc;
5424 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5425 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005426 if (CondOpc == ISD::OR) {
5427 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5428 // two branches instead of an explicit OR instruction with a
5429 // separate test.
5430 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005431 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005432 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005433 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005434 Chain, Dest, CC, Cmp);
5435 CC = Cond.getOperand(1).getOperand(0);
5436 Cond = Cmp;
5437 addTest = false;
5438 }
5439 } else { // ISD::AND
5440 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5441 // two branches instead of an explicit AND instruction with a
5442 // separate test. However, we only do this if this block doesn't
5443 // have a fall-through edge, because this requires an explicit
5444 // jmp when the condition is false.
5445 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005446 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005447 Op.getNode()->hasOneUse()) {
5448 X86::CondCode CCode =
5449 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5450 CCode = X86::GetOppositeBranchCondition(CCode);
5451 CC = DAG.getConstant(CCode, MVT::i8);
5452 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5453 // Look for an unconditional branch following this conditional branch.
5454 // We need this because we need to reverse the successors in order
5455 // to implement FCMP_OEQ.
5456 if (User.getOpcode() == ISD::BR) {
5457 SDValue FalseBB = User.getOperand(1);
5458 SDValue NewBR =
5459 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5460 assert(NewBR == User);
5461 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005462
Dale Johannesene4d209d2009-02-03 20:21:25 +00005463 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005464 Chain, Dest, CC, Cmp);
5465 X86::CondCode CCode =
5466 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5467 CCode = X86::GetOppositeBranchCondition(CCode);
5468 CC = DAG.getConstant(CCode, MVT::i8);
5469 Cond = Cmp;
5470 addTest = false;
5471 }
5472 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005473 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005474 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5475 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5476 // It should be transformed during dag combiner except when the condition
5477 // is set by a arithmetics with overflow node.
5478 X86::CondCode CCode =
5479 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5480 CCode = X86::GetOppositeBranchCondition(CCode);
5481 CC = DAG.getConstant(CCode, MVT::i8);
5482 Cond = Cond.getOperand(0).getOperand(1);
5483 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005484 }
Evan Cheng0488db92007-09-25 01:57:46 +00005485 }
5486
5487 if (addTest) {
5488 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005489 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005490 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005491 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005492 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005493}
5494
Anton Korobeynikove060b532007-04-17 19:34:00 +00005495
5496// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5497// Calls to _alloca is needed to probe the stack when allocating more than 4k
5498// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5499// that the guard pages used by the OS virtual memory manager are allocated in
5500// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005501SDValue
5502X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005503 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005504 assert(Subtarget->isTargetCygMing() &&
5505 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005506 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005507
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005508 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005509 SDValue Chain = Op.getOperand(0);
5510 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005511 // FIXME: Ensure alignment here
5512
Dan Gohman475871a2008-07-27 21:46:04 +00005513 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005514
Duncan Sands83ec4b62008-06-06 12:08:01 +00005515 MVT IntPtr = getPointerTy();
5516 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005517
Chris Lattnere563bbc2008-10-11 22:08:30 +00005518 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005519
Dale Johannesendd64c412009-02-04 00:33:20 +00005520 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005521 Flag = Chain.getValue(1);
5522
5523 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005524 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005525 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005526 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005527 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005528 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005529 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005530 Flag = Chain.getValue(1);
5531
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005532 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005533 DAG.getIntPtrConstant(0, true),
5534 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005535 Flag);
5536
Dale Johannesendd64c412009-02-04 00:33:20 +00005537 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005538
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005540 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005541}
5542
Dan Gohman475871a2008-07-27 21:46:04 +00005543SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005544X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005545 SDValue Chain,
5546 SDValue Dst, SDValue Src,
5547 SDValue Size, unsigned Align,
5548 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005549 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005550 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005551
Bill Wendling6f287b22008-09-30 21:22:07 +00005552 // If not DWORD aligned or size is more than the threshold, call the library.
5553 // The libc version is likely to be faster for these cases. It can use the
5554 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005555 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005556 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005557 ConstantSize->getZExtValue() >
5558 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005560
5561 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005562 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005563
Bill Wendling6158d842008-10-01 00:59:58 +00005564 if (const char *bzeroEntry = V &&
5565 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5566 MVT IntPtr = getPointerTy();
5567 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005568 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005569 TargetLowering::ArgListEntry Entry;
5570 Entry.Node = Dst;
5571 Entry.Ty = IntPtrTy;
5572 Args.push_back(Entry);
5573 Entry.Node = Size;
5574 Args.push_back(Entry);
5575 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005576 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5577 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005578 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005579 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005580 }
5581
Dan Gohman707e0182008-04-12 04:36:06 +00005582 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005583 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005584 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005585
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005586 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005587 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005588 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005589 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005590 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005591 unsigned BytesLeft = 0;
5592 bool TwoRepStos = false;
5593 if (ValC) {
5594 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005595 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005596
Evan Cheng0db9fe62006-04-25 20:13:52 +00005597 // If the value is a constant, then we can potentially use larger sets.
5598 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005599 case 2: // WORD aligned
5600 AVT = MVT::i16;
5601 ValReg = X86::AX;
5602 Val = (Val << 8) | Val;
5603 break;
5604 case 0: // DWORD aligned
5605 AVT = MVT::i32;
5606 ValReg = X86::EAX;
5607 Val = (Val << 8) | Val;
5608 Val = (Val << 16) | Val;
5609 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5610 AVT = MVT::i64;
5611 ValReg = X86::RAX;
5612 Val = (Val << 32) | Val;
5613 }
5614 break;
5615 default: // Byte aligned
5616 AVT = MVT::i8;
5617 ValReg = X86::AL;
5618 Count = DAG.getIntPtrConstant(SizeVal);
5619 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005620 }
5621
Duncan Sands8e4eb092008-06-08 20:54:56 +00005622 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005624 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5625 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005626 }
5627
Dale Johannesen0f502f62009-02-03 22:26:09 +00005628 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005629 InFlag);
5630 InFlag = Chain.getValue(1);
5631 } else {
5632 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005633 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005634 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005635 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005636 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005637
Scott Michelfdc40a02009-02-17 22:15:04 +00005638 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005639 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005640 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005641 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005642 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005643 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005644 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005645 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005646
Chris Lattnerd96d0722007-02-25 06:40:16 +00005647 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005648 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005649 Ops.push_back(Chain);
5650 Ops.push_back(DAG.getValueType(AVT));
5651 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005652 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005653
Evan Cheng0db9fe62006-04-25 20:13:52 +00005654 if (TwoRepStos) {
5655 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005656 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005657 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005658 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005659 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005660 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005661 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005662 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005663 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005664 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005665 Ops.clear();
5666 Ops.push_back(Chain);
5667 Ops.push_back(DAG.getValueType(MVT::i8));
5668 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005669 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005670 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005671 // Handle the last 1 - 7 bytes.
5672 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005673 MVT AddrVT = Dst.getValueType();
5674 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005675
Dale Johannesen0f502f62009-02-03 22:26:09 +00005676 Chain = DAG.getMemset(Chain, dl,
5677 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005678 DAG.getConstant(Offset, AddrVT)),
5679 Src,
5680 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005681 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005682 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005683
Dan Gohman707e0182008-04-12 04:36:06 +00005684 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005685 return Chain;
5686}
Evan Cheng11e15b32006-04-03 20:53:28 +00005687
Dan Gohman475871a2008-07-27 21:46:04 +00005688SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005689X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005690 SDValue Chain, SDValue Dst, SDValue Src,
5691 SDValue Size, unsigned Align,
5692 bool AlwaysInline,
5693 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005694 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005695 // This requires the copy size to be a constant, preferrably
5696 // within a subtarget-specific limit.
5697 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5698 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005699 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005700 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005701 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005702 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005703
Evan Cheng1887c1c2008-08-21 21:00:15 +00005704 /// If not DWORD aligned, call the library.
5705 if ((Align & 3) != 0)
5706 return SDValue();
5707
5708 // DWORD aligned
5709 MVT AVT = MVT::i32;
5710 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005711 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005712
Duncan Sands83ec4b62008-06-06 12:08:01 +00005713 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005714 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005715 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005716 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005717
Dan Gohman475871a2008-07-27 21:46:04 +00005718 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005719 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005720 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005721 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005722 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005723 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005724 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005725 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005727 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005728 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005729 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005730 InFlag = Chain.getValue(1);
5731
Chris Lattnerd96d0722007-02-25 06:40:16 +00005732 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005734 Ops.push_back(Chain);
5735 Ops.push_back(DAG.getValueType(AVT));
5736 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005737 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005738
Dan Gohman475871a2008-07-27 21:46:04 +00005739 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005740 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005741 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005742 // Handle the last 1 - 7 bytes.
5743 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005744 MVT DstVT = Dst.getValueType();
5745 MVT SrcVT = Src.getValueType();
5746 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005747 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005748 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005749 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005750 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005751 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005752 DAG.getConstant(BytesLeft, SizeVT),
5753 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005754 DstSV, DstSVOff + Offset,
5755 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005756 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005757
Scott Michelfdc40a02009-02-17 22:15:04 +00005758 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005759 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005760}
5761
Dan Gohman475871a2008-07-27 21:46:04 +00005762SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005763 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005764 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005765
Evan Cheng25ab6902006-09-08 06:48:29 +00005766 if (!Subtarget->is64Bit()) {
5767 // vastart just stores the address of the VarArgsFrameIndex slot into the
5768 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005769 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005770 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005771 }
5772
5773 // __va_list_tag:
5774 // gp_offset (0 - 6 * 8)
5775 // fp_offset (48 - 48 + 8 * 16)
5776 // overflow_arg_area (point to parameters coming in memory).
5777 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005778 SmallVector<SDValue, 8> MemOps;
5779 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005780 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005781 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005782 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005783 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005784 MemOps.push_back(Store);
5785
5786 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005787 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005788 FIN, DAG.getIntPtrConstant(4));
5789 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005790 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005791 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005792 MemOps.push_back(Store);
5793
5794 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005795 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005796 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005797 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005798 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005799 MemOps.push_back(Store);
5800
5801 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005802 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005803 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005804 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005805 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005806 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005807 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005808 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005809}
5810
Dan Gohman475871a2008-07-27 21:46:04 +00005811SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005812 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5813 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005814 SDValue Chain = Op.getOperand(0);
5815 SDValue SrcPtr = Op.getOperand(1);
5816 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005817
5818 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5819 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005820 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005821}
5822
Dan Gohman475871a2008-07-27 21:46:04 +00005823SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005824 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005825 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005826 SDValue Chain = Op.getOperand(0);
5827 SDValue DstPtr = Op.getOperand(1);
5828 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005829 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5830 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005831 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005832
Dale Johannesendd64c412009-02-04 00:33:20 +00005833 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005834 DAG.getIntPtrConstant(24), 8, false,
5835 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005836}
5837
Dan Gohman475871a2008-07-27 21:46:04 +00005838SDValue
5839X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005840 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005841 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005842 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005843 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005844 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005845 case Intrinsic::x86_sse_comieq_ss:
5846 case Intrinsic::x86_sse_comilt_ss:
5847 case Intrinsic::x86_sse_comile_ss:
5848 case Intrinsic::x86_sse_comigt_ss:
5849 case Intrinsic::x86_sse_comige_ss:
5850 case Intrinsic::x86_sse_comineq_ss:
5851 case Intrinsic::x86_sse_ucomieq_ss:
5852 case Intrinsic::x86_sse_ucomilt_ss:
5853 case Intrinsic::x86_sse_ucomile_ss:
5854 case Intrinsic::x86_sse_ucomigt_ss:
5855 case Intrinsic::x86_sse_ucomige_ss:
5856 case Intrinsic::x86_sse_ucomineq_ss:
5857 case Intrinsic::x86_sse2_comieq_sd:
5858 case Intrinsic::x86_sse2_comilt_sd:
5859 case Intrinsic::x86_sse2_comile_sd:
5860 case Intrinsic::x86_sse2_comigt_sd:
5861 case Intrinsic::x86_sse2_comige_sd:
5862 case Intrinsic::x86_sse2_comineq_sd:
5863 case Intrinsic::x86_sse2_ucomieq_sd:
5864 case Intrinsic::x86_sse2_ucomilt_sd:
5865 case Intrinsic::x86_sse2_ucomile_sd:
5866 case Intrinsic::x86_sse2_ucomigt_sd:
5867 case Intrinsic::x86_sse2_ucomige_sd:
5868 case Intrinsic::x86_sse2_ucomineq_sd: {
5869 unsigned Opc = 0;
5870 ISD::CondCode CC = ISD::SETCC_INVALID;
5871 switch (IntNo) {
5872 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005873 case Intrinsic::x86_sse_comieq_ss:
5874 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875 Opc = X86ISD::COMI;
5876 CC = ISD::SETEQ;
5877 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005878 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005879 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005880 Opc = X86ISD::COMI;
5881 CC = ISD::SETLT;
5882 break;
5883 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005884 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005885 Opc = X86ISD::COMI;
5886 CC = ISD::SETLE;
5887 break;
5888 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005889 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005890 Opc = X86ISD::COMI;
5891 CC = ISD::SETGT;
5892 break;
5893 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005894 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005895 Opc = X86ISD::COMI;
5896 CC = ISD::SETGE;
5897 break;
5898 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005899 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005900 Opc = X86ISD::COMI;
5901 CC = ISD::SETNE;
5902 break;
5903 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005904 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005905 Opc = X86ISD::UCOMI;
5906 CC = ISD::SETEQ;
5907 break;
5908 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005909 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910 Opc = X86ISD::UCOMI;
5911 CC = ISD::SETLT;
5912 break;
5913 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005914 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005915 Opc = X86ISD::UCOMI;
5916 CC = ISD::SETLE;
5917 break;
5918 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005919 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005920 Opc = X86ISD::UCOMI;
5921 CC = ISD::SETGT;
5922 break;
5923 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005924 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925 Opc = X86ISD::UCOMI;
5926 CC = ISD::SETGE;
5927 break;
5928 case Intrinsic::x86_sse_ucomineq_ss:
5929 case Intrinsic::x86_sse2_ucomineq_sd:
5930 Opc = X86ISD::UCOMI;
5931 CC = ISD::SETNE;
5932 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005933 }
Evan Cheng734503b2006-09-11 02:19:56 +00005934
Dan Gohman475871a2008-07-27 21:46:04 +00005935 SDValue LHS = Op.getOperand(1);
5936 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005937 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005938 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5939 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005940 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005941 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005942 }
Evan Cheng5759f972008-05-04 09:15:50 +00005943
5944 // Fix vector shift instructions where the last operand is a non-immediate
5945 // i32 value.
5946 case Intrinsic::x86_sse2_pslli_w:
5947 case Intrinsic::x86_sse2_pslli_d:
5948 case Intrinsic::x86_sse2_pslli_q:
5949 case Intrinsic::x86_sse2_psrli_w:
5950 case Intrinsic::x86_sse2_psrli_d:
5951 case Intrinsic::x86_sse2_psrli_q:
5952 case Intrinsic::x86_sse2_psrai_w:
5953 case Intrinsic::x86_sse2_psrai_d:
5954 case Intrinsic::x86_mmx_pslli_w:
5955 case Intrinsic::x86_mmx_pslli_d:
5956 case Intrinsic::x86_mmx_pslli_q:
5957 case Intrinsic::x86_mmx_psrli_w:
5958 case Intrinsic::x86_mmx_psrli_d:
5959 case Intrinsic::x86_mmx_psrli_q:
5960 case Intrinsic::x86_mmx_psrai_w:
5961 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005962 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005963 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005964 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005965
5966 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005967 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005968 switch (IntNo) {
5969 case Intrinsic::x86_sse2_pslli_w:
5970 NewIntNo = Intrinsic::x86_sse2_psll_w;
5971 break;
5972 case Intrinsic::x86_sse2_pslli_d:
5973 NewIntNo = Intrinsic::x86_sse2_psll_d;
5974 break;
5975 case Intrinsic::x86_sse2_pslli_q:
5976 NewIntNo = Intrinsic::x86_sse2_psll_q;
5977 break;
5978 case Intrinsic::x86_sse2_psrli_w:
5979 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5980 break;
5981 case Intrinsic::x86_sse2_psrli_d:
5982 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5983 break;
5984 case Intrinsic::x86_sse2_psrli_q:
5985 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5986 break;
5987 case Intrinsic::x86_sse2_psrai_w:
5988 NewIntNo = Intrinsic::x86_sse2_psra_w;
5989 break;
5990 case Intrinsic::x86_sse2_psrai_d:
5991 NewIntNo = Intrinsic::x86_sse2_psra_d;
5992 break;
5993 default: {
5994 ShAmtVT = MVT::v2i32;
5995 switch (IntNo) {
5996 case Intrinsic::x86_mmx_pslli_w:
5997 NewIntNo = Intrinsic::x86_mmx_psll_w;
5998 break;
5999 case Intrinsic::x86_mmx_pslli_d:
6000 NewIntNo = Intrinsic::x86_mmx_psll_d;
6001 break;
6002 case Intrinsic::x86_mmx_pslli_q:
6003 NewIntNo = Intrinsic::x86_mmx_psll_q;
6004 break;
6005 case Intrinsic::x86_mmx_psrli_w:
6006 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6007 break;
6008 case Intrinsic::x86_mmx_psrli_d:
6009 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6010 break;
6011 case Intrinsic::x86_mmx_psrli_q:
6012 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6013 break;
6014 case Intrinsic::x86_mmx_psrai_w:
6015 NewIntNo = Intrinsic::x86_mmx_psra_w;
6016 break;
6017 case Intrinsic::x86_mmx_psrai_d:
6018 NewIntNo = Intrinsic::x86_mmx_psra_d;
6019 break;
6020 default: abort(); // Can't reach here.
6021 }
6022 break;
6023 }
6024 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006025 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006026 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6027 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6028 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006029 DAG.getConstant(NewIntNo, MVT::i32),
6030 Op.getOperand(1), ShAmt);
6031 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006032 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006033}
Evan Cheng72261582005-12-20 06:22:03 +00006034
Dan Gohman475871a2008-07-27 21:46:04 +00006035SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006036 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006037 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006038
6039 if (Depth > 0) {
6040 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6041 SDValue Offset =
6042 DAG.getConstant(TD->getPointerSize(),
6043 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006044 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006045 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006046 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006047 NULL, 0);
6048 }
6049
6050 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006051 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006052 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006053 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006054}
6055
Dan Gohman475871a2008-07-27 21:46:04 +00006056SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006057 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6058 MFI->setFrameAddressIsTaken(true);
6059 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006060 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006061 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6062 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006063 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006064 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006065 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006066 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006067}
6068
Dan Gohman475871a2008-07-27 21:46:04 +00006069SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006070 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006071 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006072}
6073
Dan Gohman475871a2008-07-27 21:46:04 +00006074SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006075{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006076 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006077 SDValue Chain = Op.getOperand(0);
6078 SDValue Offset = Op.getOperand(1);
6079 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006080 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006081
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006082 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6083 getPointerTy());
6084 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006085
Dale Johannesene4d209d2009-02-03 20:21:25 +00006086 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006087 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006088 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6089 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006090 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006091 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006092
Dale Johannesene4d209d2009-02-03 20:21:25 +00006093 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006094 MVT::Other,
6095 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006096}
6097
Dan Gohman475871a2008-07-27 21:46:04 +00006098SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006099 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006100 SDValue Root = Op.getOperand(0);
6101 SDValue Trmp = Op.getOperand(1); // trampoline
6102 SDValue FPtr = Op.getOperand(2); // nested function
6103 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006104 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006105
Dan Gohman69de1932008-02-06 22:27:42 +00006106 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006107
Duncan Sands339e14f2008-01-16 22:55:25 +00006108 const X86InstrInfo *TII =
6109 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6110
Duncan Sandsb116fac2007-07-27 20:02:49 +00006111 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006112 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006113
6114 // Large code-model.
6115
6116 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6117 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6118
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006119 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6120 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006121
6122 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6123
6124 // Load the pointer to the nested function into R11.
6125 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006126 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006127 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6128 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006129
Scott Michelfdc40a02009-02-17 22:15:04 +00006130 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006131 DAG.getConstant(2, MVT::i64));
6132 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006133
6134 // Load the 'nest' parameter value into R10.
6135 // R10 is specified in X86CallingConv.td
6136 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006137 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006138 DAG.getConstant(10, MVT::i64));
6139 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6140 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006141
Scott Michelfdc40a02009-02-17 22:15:04 +00006142 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006143 DAG.getConstant(12, MVT::i64));
6144 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006145
6146 // Jump to the nested function.
6147 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006148 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006149 DAG.getConstant(20, MVT::i64));
6150 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6151 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006152
6153 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006154 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006155 DAG.getConstant(22, MVT::i64));
6156 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006157 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006158
Dan Gohman475871a2008-07-27 21:46:04 +00006159 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006160 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6161 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006162 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006163 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006164 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6165 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006166 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006167
6168 switch (CC) {
6169 default:
6170 assert(0 && "Unsupported calling convention");
6171 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006172 case CallingConv::X86_StdCall: {
6173 // Pass 'nest' parameter in ECX.
6174 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006175 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006176
6177 // Check that ECX wasn't needed by an 'inreg' parameter.
6178 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006179 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006180
Chris Lattner58d74912008-03-12 17:45:29 +00006181 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006182 unsigned InRegCount = 0;
6183 unsigned Idx = 1;
6184
6185 for (FunctionType::param_iterator I = FTy->param_begin(),
6186 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006187 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006188 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006189 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006190
6191 if (InRegCount > 2) {
6192 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6193 abort();
6194 }
6195 }
6196 break;
6197 }
6198 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006199 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006200 // Pass 'nest' parameter in EAX.
6201 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006202 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006203 break;
6204 }
6205
Dan Gohman475871a2008-07-27 21:46:04 +00006206 SDValue OutChains[4];
6207 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006208
Scott Michelfdc40a02009-02-17 22:15:04 +00006209 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006210 DAG.getConstant(10, MVT::i32));
6211 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006212
Duncan Sands339e14f2008-01-16 22:55:25 +00006213 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006214 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006215 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006216 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006217 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006218
Scott Michelfdc40a02009-02-17 22:15:04 +00006219 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006220 DAG.getConstant(1, MVT::i32));
6221 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006222
Duncan Sands339e14f2008-01-16 22:55:25 +00006223 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006224 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006225 DAG.getConstant(5, MVT::i32));
6226 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006227 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006228
Scott Michelfdc40a02009-02-17 22:15:04 +00006229 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006230 DAG.getConstant(6, MVT::i32));
6231 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006232
Dan Gohman475871a2008-07-27 21:46:04 +00006233 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006234 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6235 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006236 }
6237}
6238
Dan Gohman475871a2008-07-27 21:46:04 +00006239SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006240 /*
6241 The rounding mode is in bits 11:10 of FPSR, and has the following
6242 settings:
6243 00 Round to nearest
6244 01 Round to -inf
6245 10 Round to +inf
6246 11 Round to 0
6247
6248 FLT_ROUNDS, on the other hand, expects the following:
6249 -1 Undefined
6250 0 Round to 0
6251 1 Round to nearest
6252 2 Round to +inf
6253 3 Round to -inf
6254
6255 To perform the conversion, we do:
6256 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6257 */
6258
6259 MachineFunction &MF = DAG.getMachineFunction();
6260 const TargetMachine &TM = MF.getTarget();
6261 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6262 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006263 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006264 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006265
6266 // Save FP Control Word to stack slot
6267 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006268 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006269
Dale Johannesene4d209d2009-02-03 20:21:25 +00006270 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006271 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006272
6273 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006274 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006275
6276 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006277 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006278 DAG.getNode(ISD::SRL, dl, MVT::i16,
6279 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006280 CWD, DAG.getConstant(0x800, MVT::i16)),
6281 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006282 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006283 DAG.getNode(ISD::SRL, dl, MVT::i16,
6284 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006285 CWD, DAG.getConstant(0x400, MVT::i16)),
6286 DAG.getConstant(9, MVT::i8));
6287
Dan Gohman475871a2008-07-27 21:46:04 +00006288 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006289 DAG.getNode(ISD::AND, dl, MVT::i16,
6290 DAG.getNode(ISD::ADD, dl, MVT::i16,
6291 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006292 DAG.getConstant(1, MVT::i16)),
6293 DAG.getConstant(3, MVT::i16));
6294
6295
Duncan Sands83ec4b62008-06-06 12:08:01 +00006296 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006297 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006298}
6299
Dan Gohman475871a2008-07-27 21:46:04 +00006300SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006301 MVT VT = Op.getValueType();
6302 MVT OpVT = VT;
6303 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006304 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006305
6306 Op = Op.getOperand(0);
6307 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006308 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006309 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006310 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006311 }
Evan Cheng18efe262007-12-14 02:13:44 +00006312
Evan Cheng152804e2007-12-14 08:30:15 +00006313 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6314 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006315 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006316
6317 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006318 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006319 Ops.push_back(Op);
6320 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6321 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6322 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006323 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006324
6325 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006326 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006327
Evan Cheng18efe262007-12-14 02:13:44 +00006328 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006329 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006330 return Op;
6331}
6332
Dan Gohman475871a2008-07-27 21:46:04 +00006333SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006334 MVT VT = Op.getValueType();
6335 MVT OpVT = VT;
6336 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006337 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006338
6339 Op = Op.getOperand(0);
6340 if (VT == MVT::i8) {
6341 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006342 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006343 }
Evan Cheng152804e2007-12-14 08:30:15 +00006344
6345 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6346 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006347 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006348
6349 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006350 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006351 Ops.push_back(Op);
6352 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6353 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6354 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006355 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006356
Evan Cheng18efe262007-12-14 02:13:44 +00006357 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006358 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006359 return Op;
6360}
6361
Mon P Wangaf9b9522008-12-18 21:42:19 +00006362SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6363 MVT VT = Op.getValueType();
6364 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006365 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006366
Mon P Wangaf9b9522008-12-18 21:42:19 +00006367 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6368 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6369 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6370 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6371 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6372 //
6373 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6374 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6375 // return AloBlo + AloBhi + AhiBlo;
6376
6377 SDValue A = Op.getOperand(0);
6378 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006379
Dale Johannesene4d209d2009-02-03 20:21:25 +00006380 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006381 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6382 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006383 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006384 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6385 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006386 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006387 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6388 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006389 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006390 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6391 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006392 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006393 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6394 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006395 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006396 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6397 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006398 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006399 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6400 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006401 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6402 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006403 return Res;
6404}
6405
6406
Bill Wendling74c37652008-12-09 22:08:41 +00006407SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6408 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6409 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006410 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6411 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006412 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006413 SDValue LHS = N->getOperand(0);
6414 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006415 unsigned BaseOp = 0;
6416 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006417 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006418
6419 switch (Op.getOpcode()) {
6420 default: assert(0 && "Unknown ovf instruction!");
6421 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006422 // A subtract of one will be selected as a INC. Note that INC doesn't
6423 // set CF, so we can't do this for UADDO.
6424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6425 if (C->getAPIntValue() == 1) {
6426 BaseOp = X86ISD::INC;
6427 Cond = X86::COND_O;
6428 break;
6429 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006430 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006431 Cond = X86::COND_O;
6432 break;
6433 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006434 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006435 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006436 break;
6437 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006438 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6439 // set CF, so we can't do this for USUBO.
6440 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6441 if (C->getAPIntValue() == 1) {
6442 BaseOp = X86ISD::DEC;
6443 Cond = X86::COND_O;
6444 break;
6445 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006446 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006447 Cond = X86::COND_O;
6448 break;
6449 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006450 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006451 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006452 break;
6453 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006454 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006455 Cond = X86::COND_O;
6456 break;
6457 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006458 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006459 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006460 break;
6461 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006462
Bill Wendling61edeb52008-12-02 01:06:39 +00006463 // Also sets EFLAGS.
6464 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006465 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006466
Bill Wendling61edeb52008-12-02 01:06:39 +00006467 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006468 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006469 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006470
Bill Wendling61edeb52008-12-02 01:06:39 +00006471 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6472 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006473}
6474
Dan Gohman475871a2008-07-27 21:46:04 +00006475SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006476 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006477 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006478 unsigned Reg = 0;
6479 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006480 switch(T.getSimpleVT()) {
6481 default:
6482 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006483 case MVT::i8: Reg = X86::AL; size = 1; break;
6484 case MVT::i16: Reg = X86::AX; size = 2; break;
6485 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006486 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006487 assert(Subtarget->is64Bit() && "Node not type legal!");
6488 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006489 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006490 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006491 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006492 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006493 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006494 Op.getOperand(1),
6495 Op.getOperand(3),
6496 DAG.getTargetConstant(size, MVT::i8),
6497 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006498 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006499 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006500 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006501 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006502 return cpOut;
6503}
6504
Duncan Sands1607f052008-12-01 11:39:25 +00006505SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006506 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006507 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006508 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006509 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006510 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006511 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006512 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6513 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006514 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006515 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006516 DAG.getConstant(32, MVT::i8));
6517 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006518 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006519 rdx.getValue(1)
6520 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006521 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006522}
6523
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006524SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6525 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006526 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006527 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006528 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006529 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006530 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006531 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006532 Node->getOperand(0),
6533 Node->getOperand(1), negOp,
6534 cast<AtomicSDNode>(Node)->getSrcValue(),
6535 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006536}
6537
Evan Cheng0db9fe62006-04-25 20:13:52 +00006538/// LowerOperation - Provide custom lowering hooks for some operations.
6539///
Dan Gohman475871a2008-07-27 21:46:04 +00006540SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006541 switch (Op.getOpcode()) {
6542 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006543 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6544 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006545 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6546 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6547 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6548 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6549 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6550 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6551 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006552 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006553 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006554 case ISD::SHL_PARTS:
6555 case ISD::SRA_PARTS:
6556 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6557 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006558 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006559 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006560 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006561 case ISD::FABS: return LowerFABS(Op, DAG);
6562 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006563 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006564 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006565 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006566 case ISD::SELECT: return LowerSELECT(Op, DAG);
6567 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006569 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006570 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006571 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006572 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006573 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006574 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006575 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006576 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6577 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006578 case ISD::FRAME_TO_ARGS_OFFSET:
6579 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006580 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006581 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006582 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006583 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006584 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6585 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006586 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006587 case ISD::SADDO:
6588 case ISD::UADDO:
6589 case ISD::SSUBO:
6590 case ISD::USUBO:
6591 case ISD::SMULO:
6592 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006593 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006594 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006595}
6596
Duncan Sands1607f052008-12-01 11:39:25 +00006597void X86TargetLowering::
6598ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6599 SelectionDAG &DAG, unsigned NewOp) {
6600 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006601 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006602 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6603
6604 SDValue Chain = Node->getOperand(0);
6605 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006606 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006607 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006608 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006609 Node->getOperand(2), DAG.getIntPtrConstant(1));
6610 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6611 // have a MemOperand. Pass the info through as a normal operand.
6612 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6613 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6614 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006615 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006616 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006617 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006618 Results.push_back(Result.getValue(2));
6619}
6620
Duncan Sands126d9072008-07-04 11:47:58 +00006621/// ReplaceNodeResults - Replace a node with an illegal result type
6622/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006623void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6624 SmallVectorImpl<SDValue>&Results,
6625 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006626 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006627 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006628 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006629 assert(false && "Do not know how to custom type legalize this operation!");
6630 return;
6631 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006632 std::pair<SDValue,SDValue> Vals =
6633 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006634 SDValue FIST = Vals.first, StackSlot = Vals.second;
6635 if (FIST.getNode() != 0) {
6636 MVT VT = N->getValueType(0);
6637 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006638 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006639 }
6640 return;
6641 }
6642 case ISD::READCYCLECOUNTER: {
6643 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6644 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006645 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006646 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006647 rd.getValue(1));
6648 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006649 eax.getValue(2));
6650 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6651 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006652 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006653 Results.push_back(edx.getValue(1));
6654 return;
6655 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006656 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006657 MVT T = N->getValueType(0);
6658 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6659 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006660 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006661 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006662 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006663 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006664 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6665 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006666 cpInL.getValue(1));
6667 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006668 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006669 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006670 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006671 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006672 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006673 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006674 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006675 swapInL.getValue(1));
6676 SDValue Ops[] = { swapInH.getValue(0),
6677 N->getOperand(1),
6678 swapInH.getValue(1) };
6679 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006680 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006681 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6682 MVT::i32, Result.getValue(1));
6683 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6684 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006685 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006686 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006687 Results.push_back(cpOutH.getValue(1));
6688 return;
6689 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006690 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006691 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6692 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006693 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006694 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6695 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006696 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006697 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6698 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006699 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006700 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6701 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006702 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006703 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6704 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006705 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006706 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6707 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006708 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006709 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6710 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006711 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006712}
6713
Evan Cheng72261582005-12-20 06:22:03 +00006714const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6715 switch (Opcode) {
6716 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006717 case X86ISD::BSF: return "X86ISD::BSF";
6718 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006719 case X86ISD::SHLD: return "X86ISD::SHLD";
6720 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006721 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006722 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006723 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006724 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006725 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006726 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006727 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6728 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6729 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006730 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006731 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006732 case X86ISD::CALL: return "X86ISD::CALL";
6733 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6734 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006735 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006736 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006737 case X86ISD::COMI: return "X86ISD::COMI";
6738 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006739 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006740 case X86ISD::CMOV: return "X86ISD::CMOV";
6741 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006742 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006743 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6744 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006745 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006746 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006747 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006748 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006749 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6750 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006751 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006752 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006753 case X86ISD::FMAX: return "X86ISD::FMAX";
6754 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006755 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6756 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006757 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006758 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006759 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006760 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006761 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006762 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6763 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006764 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6765 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6766 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6767 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6768 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6769 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006770 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6771 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006772 case X86ISD::VSHL: return "X86ISD::VSHL";
6773 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006774 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6775 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6776 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6777 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6778 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6779 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6780 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6781 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6782 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6783 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006784 case X86ISD::ADD: return "X86ISD::ADD";
6785 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006786 case X86ISD::SMUL: return "X86ISD::SMUL";
6787 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006788 case X86ISD::INC: return "X86ISD::INC";
6789 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006790 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006791 }
6792}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006793
Chris Lattnerc9addb72007-03-30 23:15:24 +00006794// isLegalAddressingMode - Return true if the addressing mode represented
6795// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006796bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006797 const Type *Ty) const {
6798 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006799
Chris Lattnerc9addb72007-03-30 23:15:24 +00006800 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6801 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6802 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006803
Chris Lattnerc9addb72007-03-30 23:15:24 +00006804 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006805 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006806 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6807 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006808 // If BaseGV requires a register, we cannot also have a BaseReg.
6809 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6810 AM.HasBaseReg)
6811 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006812
6813 // X86-64 only supports addr of globals in small code model.
6814 if (Subtarget->is64Bit()) {
6815 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6816 return false;
6817 // If lower 4G is not available, then we must use rip-relative addressing.
6818 if (AM.BaseOffs || AM.Scale > 1)
6819 return false;
6820 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006821 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006822
Chris Lattnerc9addb72007-03-30 23:15:24 +00006823 switch (AM.Scale) {
6824 case 0:
6825 case 1:
6826 case 2:
6827 case 4:
6828 case 8:
6829 // These scales always work.
6830 break;
6831 case 3:
6832 case 5:
6833 case 9:
6834 // These scales are formed with basereg+scalereg. Only accept if there is
6835 // no basereg yet.
6836 if (AM.HasBaseReg)
6837 return false;
6838 break;
6839 default: // Other stuff never works.
6840 return false;
6841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006842
Chris Lattnerc9addb72007-03-30 23:15:24 +00006843 return true;
6844}
6845
6846
Evan Cheng2bd122c2007-10-26 01:56:11 +00006847bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6848 if (!Ty1->isInteger() || !Ty2->isInteger())
6849 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006850 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6851 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006852 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006853 return false;
6854 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006855}
6856
Duncan Sands83ec4b62008-06-06 12:08:01 +00006857bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6858 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006859 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006860 unsigned NumBits1 = VT1.getSizeInBits();
6861 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006862 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006863 return false;
6864 return Subtarget->is64Bit() || NumBits1 < 64;
6865}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006866
Dan Gohman97121ba2009-04-08 00:15:30 +00006867bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006868 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006869 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6870}
6871
6872bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006873 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006874 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6875}
6876
Evan Cheng8b944d32009-05-28 00:35:15 +00006877bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
6878 // i16 instructions are longer (0x66 prefix) and potentially slower.
6879 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
6880}
6881
Evan Cheng60c07e12006-07-05 22:17:51 +00006882/// isShuffleMaskLegal - Targets can use this to indicate that they only
6883/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6884/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6885/// are assumed to be legal.
6886bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006887X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6888 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006889 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006890 if (VT.getSizeInBits() == 64)
6891 return false;
6892
6893 // FIXME: pshufb, blends, palignr, shifts.
6894 return (VT.getVectorNumElements() == 2 ||
6895 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6896 isMOVLMask(M, VT) ||
6897 isSHUFPMask(M, VT) ||
6898 isPSHUFDMask(M, VT) ||
6899 isPSHUFHWMask(M, VT) ||
6900 isPSHUFLWMask(M, VT) ||
6901 isUNPCKLMask(M, VT) ||
6902 isUNPCKHMask(M, VT) ||
6903 isUNPCKL_v_undef_Mask(M, VT) ||
6904 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006905}
6906
Dan Gohman7d8143f2008-04-09 20:09:42 +00006907bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006908X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006909 MVT VT) const {
6910 unsigned NumElts = VT.getVectorNumElements();
6911 // FIXME: This collection of masks seems suspect.
6912 if (NumElts == 2)
6913 return true;
6914 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6915 return (isMOVLMask(Mask, VT) ||
6916 isCommutedMOVLMask(Mask, VT, true) ||
6917 isSHUFPMask(Mask, VT) ||
6918 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006919 }
6920 return false;
6921}
6922
6923//===----------------------------------------------------------------------===//
6924// X86 Scheduler Hooks
6925//===----------------------------------------------------------------------===//
6926
Mon P Wang63307c32008-05-05 19:05:59 +00006927// private utility function
6928MachineBasicBlock *
6929X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6930 MachineBasicBlock *MBB,
6931 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006932 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006933 unsigned LoadOpc,
6934 unsigned CXchgOpc,
6935 unsigned copyOpc,
6936 unsigned notOpc,
6937 unsigned EAXreg,
6938 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006939 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006940 // For the atomic bitwise operator, we generate
6941 // thisMBB:
6942 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006943 // ld t1 = [bitinstr.addr]
6944 // op t2 = t1, [bitinstr.val]
6945 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006946 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6947 // bz newMBB
6948 // fallthrough -->nextMBB
6949 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6950 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006951 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006952 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006953
Mon P Wang63307c32008-05-05 19:05:59 +00006954 /// First build the CFG
6955 MachineFunction *F = MBB->getParent();
6956 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006957 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6958 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6959 F->insert(MBBIter, newMBB);
6960 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006961
Mon P Wang63307c32008-05-05 19:05:59 +00006962 // Move all successors to thisMBB to nextMBB
6963 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006964
Mon P Wang63307c32008-05-05 19:05:59 +00006965 // Update thisMBB to fall through to newMBB
6966 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006967
Mon P Wang63307c32008-05-05 19:05:59 +00006968 // newMBB jumps to itself and fall through to nextMBB
6969 newMBB->addSuccessor(nextMBB);
6970 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006971
Mon P Wang63307c32008-05-05 19:05:59 +00006972 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006973 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00006974 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00006975 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00006976 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006977 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00006978 int numArgs = bInstr->getNumOperands() - 1;
6979 for (int i=0; i < numArgs; ++i)
6980 argOpers[i] = &bInstr->getOperand(i+1);
6981
6982 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006983 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
6984 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00006985
Dale Johannesen140be2d2008-08-19 18:47:28 +00006986 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006987 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006988 for (int i=0; i <= lastAddrIndx; ++i)
6989 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006990
Dale Johannesen140be2d2008-08-19 18:47:28 +00006991 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006992 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006993 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006994 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006995 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006996 tt = t1;
6997
Dale Johannesen140be2d2008-08-19 18:47:28 +00006998 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00006999 assert((argOpers[valArgIndx]->isReg() ||
7000 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007001 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007002 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007003 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007004 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007005 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007006 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007007 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007008
Dale Johannesene4d209d2009-02-03 20:21:25 +00007009 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007010 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007011
Dale Johannesene4d209d2009-02-03 20:21:25 +00007012 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007013 for (int i=0; i <= lastAddrIndx; ++i)
7014 (*MIB).addOperand(*argOpers[i]);
7015 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007016 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7017 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7018
Dale Johannesene4d209d2009-02-03 20:21:25 +00007019 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007020 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007021
Mon P Wang63307c32008-05-05 19:05:59 +00007022 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007023 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007024
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007025 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007026 return nextMBB;
7027}
7028
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007029// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007030MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007031X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7032 MachineBasicBlock *MBB,
7033 unsigned regOpcL,
7034 unsigned regOpcH,
7035 unsigned immOpcL,
7036 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007037 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007038 // For the atomic bitwise operator, we generate
7039 // thisMBB (instructions are in pairs, except cmpxchg8b)
7040 // ld t1,t2 = [bitinstr.addr]
7041 // newMBB:
7042 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7043 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007044 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007045 // mov ECX, EBX <- t5, t6
7046 // mov EAX, EDX <- t1, t2
7047 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7048 // mov t3, t4 <- EAX, EDX
7049 // bz newMBB
7050 // result in out1, out2
7051 // fallthrough -->nextMBB
7052
7053 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7054 const unsigned LoadOpc = X86::MOV32rm;
7055 const unsigned copyOpc = X86::MOV32rr;
7056 const unsigned NotOpc = X86::NOT32r;
7057 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7058 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7059 MachineFunction::iterator MBBIter = MBB;
7060 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007061
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007062 /// First build the CFG
7063 MachineFunction *F = MBB->getParent();
7064 MachineBasicBlock *thisMBB = MBB;
7065 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7066 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7067 F->insert(MBBIter, newMBB);
7068 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007069
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007070 // Move all successors to thisMBB to nextMBB
7071 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007072
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007073 // Update thisMBB to fall through to newMBB
7074 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007075
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007076 // newMBB jumps to itself and fall through to nextMBB
7077 newMBB->addSuccessor(nextMBB);
7078 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007079
Dale Johannesene4d209d2009-02-03 20:21:25 +00007080 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007081 // Insert instructions into newMBB based on incoming instruction
7082 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007083 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007084 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007085 MachineOperand& dest1Oper = bInstr->getOperand(0);
7086 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007087 MachineOperand* argOpers[2 + X86AddrNumOperands];
7088 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007089 argOpers[i] = &bInstr->getOperand(i+2);
7090
7091 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007092 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007093
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007094 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007095 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007096 for (int i=0; i <= lastAddrIndx; ++i)
7097 (*MIB).addOperand(*argOpers[i]);
7098 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007099 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007100 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007101 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007102 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007103 MachineOperand newOp3 = *(argOpers[3]);
7104 if (newOp3.isImm())
7105 newOp3.setImm(newOp3.getImm()+4);
7106 else
7107 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007108 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007109 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007110
7111 // t3/4 are defined later, at the bottom of the loop
7112 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7113 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007114 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007115 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007116 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007117 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7118
7119 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7120 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007121 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007122 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7123 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007124 } else {
7125 tt1 = t1;
7126 tt2 = t2;
7127 }
7128
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007129 int valArgIndx = lastAddrIndx + 1;
7130 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007131 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007132 "invalid operand");
7133 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7134 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007135 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007136 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007137 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007138 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007139 if (regOpcL != X86::MOV32rr)
7140 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007141 (*MIB).addOperand(*argOpers[valArgIndx]);
7142 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007143 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007144 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007145 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007146 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007147 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007148 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007149 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007150 if (regOpcH != X86::MOV32rr)
7151 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007152 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007153
Dale Johannesene4d209d2009-02-03 20:21:25 +00007154 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007155 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007156 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007157 MIB.addReg(t2);
7158
Dale Johannesene4d209d2009-02-03 20:21:25 +00007159 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007160 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007161 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007162 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007163
Dale Johannesene4d209d2009-02-03 20:21:25 +00007164 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007165 for (int i=0; i <= lastAddrIndx; ++i)
7166 (*MIB).addOperand(*argOpers[i]);
7167
7168 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7169 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7170
Dale Johannesene4d209d2009-02-03 20:21:25 +00007171 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007172 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007173 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007174 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007175
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007176 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007177 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007178
7179 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7180 return nextMBB;
7181}
7182
7183// private utility function
7184MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007185X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7186 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007187 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007188 // For the atomic min/max operator, we generate
7189 // thisMBB:
7190 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007191 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007192 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007193 // cmp t1, t2
7194 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007195 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007196 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7197 // bz newMBB
7198 // fallthrough -->nextMBB
7199 //
7200 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7201 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007202 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007203 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007204
Mon P Wang63307c32008-05-05 19:05:59 +00007205 /// First build the CFG
7206 MachineFunction *F = MBB->getParent();
7207 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007208 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7209 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7210 F->insert(MBBIter, newMBB);
7211 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007212
Mon P Wang63307c32008-05-05 19:05:59 +00007213 // Move all successors to thisMBB to nextMBB
7214 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007215
Mon P Wang63307c32008-05-05 19:05:59 +00007216 // Update thisMBB to fall through to newMBB
7217 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007218
Mon P Wang63307c32008-05-05 19:05:59 +00007219 // newMBB jumps to newMBB and fall through to nextMBB
7220 newMBB->addSuccessor(nextMBB);
7221 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007222
Dale Johannesene4d209d2009-02-03 20:21:25 +00007223 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007224 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007225 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007226 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007227 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007228 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007229 int numArgs = mInstr->getNumOperands() - 1;
7230 for (int i=0; i < numArgs; ++i)
7231 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007232
Mon P Wang63307c32008-05-05 19:05:59 +00007233 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007234 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7235 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007236
Mon P Wangab3e7472008-05-05 22:56:23 +00007237 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007238 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007239 for (int i=0; i <= lastAddrIndx; ++i)
7240 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007241
Mon P Wang63307c32008-05-05 19:05:59 +00007242 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007243 assert((argOpers[valArgIndx]->isReg() ||
7244 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007245 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007246
7247 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007248 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007249 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007250 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007251 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007252 (*MIB).addOperand(*argOpers[valArgIndx]);
7253
Dale Johannesene4d209d2009-02-03 20:21:25 +00007254 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007255 MIB.addReg(t1);
7256
Dale Johannesene4d209d2009-02-03 20:21:25 +00007257 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007258 MIB.addReg(t1);
7259 MIB.addReg(t2);
7260
7261 // Generate movc
7262 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007263 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007264 MIB.addReg(t2);
7265 MIB.addReg(t1);
7266
7267 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007268 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007269 for (int i=0; i <= lastAddrIndx; ++i)
7270 (*MIB).addOperand(*argOpers[i]);
7271 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007272 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7273 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007274
Dale Johannesene4d209d2009-02-03 20:21:25 +00007275 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007276 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007277
Mon P Wang63307c32008-05-05 19:05:59 +00007278 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007279 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007280
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007281 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007282 return nextMBB;
7283}
7284
7285
Evan Cheng60c07e12006-07-05 22:17:51 +00007286MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007287X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007288 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007289 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007290 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007291 switch (MI->getOpcode()) {
7292 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007293 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007294 case X86::CMOV_FR32:
7295 case X86::CMOV_FR64:
7296 case X86::CMOV_V4F32:
7297 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007298 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007299 // To "insert" a SELECT_CC instruction, we actually have to insert the
7300 // diamond control-flow pattern. The incoming instruction knows the
7301 // destination vreg to set, the condition code register to branch on, the
7302 // true/false values to select between, and a branch opcode to use.
7303 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007304 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007305 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007306
Evan Cheng60c07e12006-07-05 22:17:51 +00007307 // thisMBB:
7308 // ...
7309 // TrueVal = ...
7310 // cmpTY ccX, r1, r2
7311 // bCC copy1MBB
7312 // fallthrough --> copy0MBB
7313 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007314 MachineFunction *F = BB->getParent();
7315 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7316 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007317 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007318 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007319 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007320 F->insert(It, copy0MBB);
7321 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007322 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007323 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007324 sinkMBB->transferSuccessors(BB);
7325
7326 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007327 BB->addSuccessor(copy0MBB);
7328 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007329
Evan Cheng60c07e12006-07-05 22:17:51 +00007330 // copy0MBB:
7331 // %FalseValue = ...
7332 // # fallthrough to sinkMBB
7333 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007334
Evan Cheng60c07e12006-07-05 22:17:51 +00007335 // Update machine-CFG edges
7336 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007337
Evan Cheng60c07e12006-07-05 22:17:51 +00007338 // sinkMBB:
7339 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7340 // ...
7341 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007342 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007343 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7344 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7345
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007346 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007347 return BB;
7348 }
7349
Dale Johannesen849f2142007-07-03 00:53:03 +00007350 case X86::FP32_TO_INT16_IN_MEM:
7351 case X86::FP32_TO_INT32_IN_MEM:
7352 case X86::FP32_TO_INT64_IN_MEM:
7353 case X86::FP64_TO_INT16_IN_MEM:
7354 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007355 case X86::FP64_TO_INT64_IN_MEM:
7356 case X86::FP80_TO_INT16_IN_MEM:
7357 case X86::FP80_TO_INT32_IN_MEM:
7358 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007359 // Change the floating point control register to use "round towards zero"
7360 // mode when truncating to an integer value.
7361 MachineFunction *F = BB->getParent();
7362 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007363 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007364
7365 // Load the old value of the high byte of the control word...
7366 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007367 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007368 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007369 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007370
7371 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007372 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007373 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007374
7375 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007376 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007377
7378 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007379 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007380 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007381
7382 // Get the X86 opcode to use.
7383 unsigned Opc;
7384 switch (MI->getOpcode()) {
7385 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007386 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7387 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7388 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7389 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7390 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7391 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007392 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7393 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7394 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007395 }
7396
7397 X86AddressMode AM;
7398 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007399 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007400 AM.BaseType = X86AddressMode::RegBase;
7401 AM.Base.Reg = Op.getReg();
7402 } else {
7403 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007404 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007405 }
7406 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007407 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007408 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007409 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007410 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007411 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007412 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007413 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007414 AM.GV = Op.getGlobal();
7415 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007416 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007417 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007418 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007419 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007420
7421 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007422 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007423
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007424 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007425 return BB;
7426 }
Mon P Wang63307c32008-05-05 19:05:59 +00007427 case X86::ATOMAND32:
7428 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007429 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007430 X86::LCMPXCHG32, X86::MOV32rr,
7431 X86::NOT32r, X86::EAX,
7432 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007433 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007434 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7435 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007436 X86::LCMPXCHG32, X86::MOV32rr,
7437 X86::NOT32r, X86::EAX,
7438 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007439 case X86::ATOMXOR32:
7440 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007441 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007442 X86::LCMPXCHG32, X86::MOV32rr,
7443 X86::NOT32r, X86::EAX,
7444 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007445 case X86::ATOMNAND32:
7446 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007447 X86::AND32ri, X86::MOV32rm,
7448 X86::LCMPXCHG32, X86::MOV32rr,
7449 X86::NOT32r, X86::EAX,
7450 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007451 case X86::ATOMMIN32:
7452 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7453 case X86::ATOMMAX32:
7454 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7455 case X86::ATOMUMIN32:
7456 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7457 case X86::ATOMUMAX32:
7458 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007459
7460 case X86::ATOMAND16:
7461 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7462 X86::AND16ri, X86::MOV16rm,
7463 X86::LCMPXCHG16, X86::MOV16rr,
7464 X86::NOT16r, X86::AX,
7465 X86::GR16RegisterClass);
7466 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007467 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007468 X86::OR16ri, X86::MOV16rm,
7469 X86::LCMPXCHG16, X86::MOV16rr,
7470 X86::NOT16r, X86::AX,
7471 X86::GR16RegisterClass);
7472 case X86::ATOMXOR16:
7473 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7474 X86::XOR16ri, X86::MOV16rm,
7475 X86::LCMPXCHG16, X86::MOV16rr,
7476 X86::NOT16r, X86::AX,
7477 X86::GR16RegisterClass);
7478 case X86::ATOMNAND16:
7479 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7480 X86::AND16ri, X86::MOV16rm,
7481 X86::LCMPXCHG16, X86::MOV16rr,
7482 X86::NOT16r, X86::AX,
7483 X86::GR16RegisterClass, true);
7484 case X86::ATOMMIN16:
7485 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7486 case X86::ATOMMAX16:
7487 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7488 case X86::ATOMUMIN16:
7489 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7490 case X86::ATOMUMAX16:
7491 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7492
7493 case X86::ATOMAND8:
7494 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7495 X86::AND8ri, X86::MOV8rm,
7496 X86::LCMPXCHG8, X86::MOV8rr,
7497 X86::NOT8r, X86::AL,
7498 X86::GR8RegisterClass);
7499 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007500 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007501 X86::OR8ri, X86::MOV8rm,
7502 X86::LCMPXCHG8, X86::MOV8rr,
7503 X86::NOT8r, X86::AL,
7504 X86::GR8RegisterClass);
7505 case X86::ATOMXOR8:
7506 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7507 X86::XOR8ri, X86::MOV8rm,
7508 X86::LCMPXCHG8, X86::MOV8rr,
7509 X86::NOT8r, X86::AL,
7510 X86::GR8RegisterClass);
7511 case X86::ATOMNAND8:
7512 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7513 X86::AND8ri, X86::MOV8rm,
7514 X86::LCMPXCHG8, X86::MOV8rr,
7515 X86::NOT8r, X86::AL,
7516 X86::GR8RegisterClass, true);
7517 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007518 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007519 case X86::ATOMAND64:
7520 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007521 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007522 X86::LCMPXCHG64, X86::MOV64rr,
7523 X86::NOT64r, X86::RAX,
7524 X86::GR64RegisterClass);
7525 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7527 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007528 X86::LCMPXCHG64, X86::MOV64rr,
7529 X86::NOT64r, X86::RAX,
7530 X86::GR64RegisterClass);
7531 case X86::ATOMXOR64:
7532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007533 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007534 X86::LCMPXCHG64, X86::MOV64rr,
7535 X86::NOT64r, X86::RAX,
7536 X86::GR64RegisterClass);
7537 case X86::ATOMNAND64:
7538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7539 X86::AND64ri32, X86::MOV64rm,
7540 X86::LCMPXCHG64, X86::MOV64rr,
7541 X86::NOT64r, X86::RAX,
7542 X86::GR64RegisterClass, true);
7543 case X86::ATOMMIN64:
7544 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7545 case X86::ATOMMAX64:
7546 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7547 case X86::ATOMUMIN64:
7548 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7549 case X86::ATOMUMAX64:
7550 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007551
7552 // This group does 64-bit operations on a 32-bit host.
7553 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007554 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007555 X86::AND32rr, X86::AND32rr,
7556 X86::AND32ri, X86::AND32ri,
7557 false);
7558 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007559 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007560 X86::OR32rr, X86::OR32rr,
7561 X86::OR32ri, X86::OR32ri,
7562 false);
7563 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007564 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007565 X86::XOR32rr, X86::XOR32rr,
7566 X86::XOR32ri, X86::XOR32ri,
7567 false);
7568 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007569 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007570 X86::AND32rr, X86::AND32rr,
7571 X86::AND32ri, X86::AND32ri,
7572 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007573 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007574 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007575 X86::ADD32rr, X86::ADC32rr,
7576 X86::ADD32ri, X86::ADC32ri,
7577 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007578 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007579 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007580 X86::SUB32rr, X86::SBB32rr,
7581 X86::SUB32ri, X86::SBB32ri,
7582 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007583 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007584 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007585 X86::MOV32rr, X86::MOV32rr,
7586 X86::MOV32ri, X86::MOV32ri,
7587 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007588 }
7589}
7590
7591//===----------------------------------------------------------------------===//
7592// X86 Optimization Hooks
7593//===----------------------------------------------------------------------===//
7594
Dan Gohman475871a2008-07-27 21:46:04 +00007595void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007596 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007597 APInt &KnownZero,
7598 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007599 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007600 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007601 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007602 assert((Opc >= ISD::BUILTIN_OP_END ||
7603 Opc == ISD::INTRINSIC_WO_CHAIN ||
7604 Opc == ISD::INTRINSIC_W_CHAIN ||
7605 Opc == ISD::INTRINSIC_VOID) &&
7606 "Should use MaskedValueIsZero if you don't know whether Op"
7607 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007608
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007609 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007610 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007611 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007612 case X86ISD::ADD:
7613 case X86ISD::SUB:
7614 case X86ISD::SMUL:
7615 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007616 case X86ISD::INC:
7617 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007618 // These nodes' second result is a boolean.
7619 if (Op.getResNo() == 0)
7620 break;
7621 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007622 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007623 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7624 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007625 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007626 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007627}
Chris Lattner259e97c2006-01-31 19:43:35 +00007628
Evan Cheng206ee9d2006-07-07 08:33:52 +00007629/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007630/// node is a GlobalAddress + offset.
7631bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7632 GlobalValue* &GA, int64_t &Offset) const{
7633 if (N->getOpcode() == X86ISD::Wrapper) {
7634 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007635 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007636 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007637 return true;
7638 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007639 }
Evan Chengad4196b2008-05-12 19:56:52 +00007640 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007641}
7642
Evan Chengad4196b2008-05-12 19:56:52 +00007643static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7644 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007645 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007646 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007647 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007648 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007649 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007650 return false;
7651}
7652
Nate Begeman9008ca62009-04-27 18:41:29 +00007653static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7654 MVT EVT, SDNode *&Base,
Evan Chengad4196b2008-05-12 19:56:52 +00007655 SelectionDAG &DAG, MachineFrameInfo *MFI,
7656 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007657 Base = NULL;
7658 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007659 if (N->getMaskElt(i) < 0) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007660 if (!Base)
7661 return false;
7662 continue;
7663 }
7664
Dan Gohman475871a2008-07-27 21:46:04 +00007665 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007666 if (!Elt.getNode() ||
7667 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007668 return false;
7669 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007670 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007671 if (Base->getOpcode() == ISD::UNDEF)
7672 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007673 continue;
7674 }
7675 if (Elt.getOpcode() == ISD::UNDEF)
7676 continue;
7677
Nate Begemanabc01992009-06-05 21:37:30 +00007678 LoadSDNode *LD = cast<LoadSDNode>(Elt);
7679 LoadSDNode *LDBase = cast<LoadSDNode>(Base);
7680 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007681 return false;
7682 }
7683 return true;
7684}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007685
7686/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7687/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7688/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007689/// order. In the case of v2i64, it will see if it can rewrite the
7690/// shuffle to be an appropriate build vector so it can take advantage of
7691// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007692static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007693 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007694 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007695 MVT VT = N->getValueType(0);
7696 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007697 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7698 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007699
7700 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7701 // where the upper half is 0, it is advantageous to rewrite it as a build
7702 // vector of (0, val) so it can use movq.
7703 if (VT == MVT::v2i64) {
7704 SDValue In[2];
7705 In[0] = N->getOperand(0);
7706 In[1] = N->getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00007707 int Idx0 = SVN->getMaskElt(0);
7708 int Idx1 = SVN->getMaskElt(1);
7709 // FIXME: can we take advantage of undef index?
7710 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang1e955802009-04-03 02:43:30 +00007711 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7712 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7713 ConstantSDNode* InsertVecIdx =
7714 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7715 if (InsertVecIdx &&
Nate Begeman9008ca62009-04-27 18:41:29 +00007716 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang1e955802009-04-03 02:43:30 +00007717 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7718 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7719 In[Idx0/2].getOperand(1),
7720 In[Idx1/2].getOperand(Idx1 % 2));
7721 }
7722 }
7723 }
7724
7725 // Try to combine a vector_shuffle into a 128-bit load.
7726 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007727 SDNode *Base = NULL;
Nate Begeman9008ca62009-04-27 18:41:29 +00007728 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007729 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007730
Dan Gohmand3006222007-07-27 17:16:43 +00007731 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007732 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007733 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007734 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007735 LD->isVolatile());
7736 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7737 LD->getSrcValue(), LD->getSrcValueOffset(),
7738 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007739}
7740
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007741/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007742static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007743 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007744 const X86Subtarget *Subtarget,
7745 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007746 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007747 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007748
Evan Chengd880b972008-05-09 21:53:03 +00007749 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007750 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007751 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007752
Duncan Sands83ec4b62008-06-06 12:08:01 +00007753 MVT VT = N->getValueType(0);
7754 MVT EVT = VT.getVectorElementType();
Nate Begemanabc01992009-06-05 21:37:30 +00007755
7756 // Before or during type legalization, we want to try and convert a
7757 // build_vector of an i64 load and a zero value into vzext_movl before the
7758 // legalizer can break it up.
7759 // FIXME: does the case below remove the need to do this?
7760 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) {
7761 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00007762 return SDValue();
Nate Begemanabc01992009-06-05 21:37:30 +00007763
7764 // This must be an insertion into a zero vector.
7765 SDValue HighElt = N->getOperand(1);
7766 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007767 return SDValue();
Nate Begemanabc01992009-06-05 21:37:30 +00007768
7769 // Value must be a load.
7770 SDNode *Base = N->getOperand(0).getNode();
7771 if (!isa<LoadSDNode>(Base)) {
7772 if (Base->getOpcode() != ISD::BIT_CONVERT)
7773 return SDValue();
7774 Base = Base->getOperand(0).getNode();
7775 if (!isa<LoadSDNode>(Base))
7776 return SDValue();
7777 }
7778
7779 // Transform it into VZEXT_LOAD addr.
7780 LoadSDNode *LD = cast<LoadSDNode>(Base);
7781
7782 // Load must not be an extload.
7783 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
7784 return SDValue();
7785
7786 // Load type should legal type so we don't have to legalize it.
7787 if (!TLI.isTypeLegal(VT))
7788 return SDValue();
7789
7790 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7791 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7792 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
7793 TargetLowering::TargetLoweringOpt TLO(DAG);
7794 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7795 DCI.CommitTargetLoweringOpt(TLO);
7796 return ResNode;
Evan Chengd880b972008-05-09 21:53:03 +00007797 }
Evan Chengd880b972008-05-09 21:53:03 +00007798
Nate Begemanabc01992009-06-05 21:37:30 +00007799 // The type legalizer will have broken apart v2i64 build_vector created during
7800 // widening before the code which handles that case is run. Look for build
7801 // vector (load, load + 4, 0/undef, 0/undef)
7802 if (VT == MVT::v4i32 || VT == MVT::v4f32) {
7803 LoadSDNode *LD0 = dyn_cast<LoadSDNode>(N->getOperand(0));
7804 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(N->getOperand(1));
7805 if (!LD0 || !LD1)
7806 return SDValue();
7807 if (LD0->getExtensionType() != ISD::NON_EXTLOAD ||
7808 LD1->getExtensionType() != ISD::NON_EXTLOAD)
7809 return SDValue();
7810 // Make sure the second elt is a consecutive load.
7811 if (!TLI.isConsecutiveLoad(LD1, LD0, EVT.getSizeInBits()/8, 1,
7812 DAG.getMachineFunction().getFrameInfo()))
7813 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007814
Nate Begemanabc01992009-06-05 21:37:30 +00007815 SDValue N2 = N->getOperand(2);
7816 SDValue N3 = N->getOperand(3);
7817 if (!isZeroNode(N2) && N2.getOpcode() != ISD::UNDEF)
7818 return SDValue();
7819 if (!isZeroNode(N3) && N3.getOpcode() != ISD::UNDEF)
7820 return SDValue();
7821
7822 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
7823 SDValue Ops[] = { LD0->getChain(), LD0->getBasePtr() };
7824 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
7825 TargetLowering::TargetLoweringOpt TLO(DAG);
7826 TLO.CombineTo(SDValue(LD0, 1), ResNode.getValue(1));
7827 DCI.CommitTargetLoweringOpt(TLO);
7828 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7829 }
7830 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007831}
Evan Chengd880b972008-05-09 21:53:03 +00007832
Chris Lattner83e6c992006-10-04 06:57:07 +00007833/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007834static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007835 const X86Subtarget *Subtarget) {
7836 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007837 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007838 // Get the LHS/RHS of the select.
7839 SDValue LHS = N->getOperand(1);
7840 SDValue RHS = N->getOperand(2);
7841
Chris Lattner83e6c992006-10-04 06:57:07 +00007842 // If we have SSE[12] support, try to form min/max nodes.
7843 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007844 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7845 Cond.getOpcode() == ISD::SETCC) {
7846 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007847
Chris Lattner47b4ce82009-03-11 05:48:52 +00007848 unsigned Opcode = 0;
7849 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7850 switch (CC) {
7851 default: break;
7852 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7853 case ISD::SETULE:
7854 case ISD::SETLE:
7855 if (!UnsafeFPMath) break;
7856 // FALL THROUGH.
7857 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7858 case ISD::SETLT:
7859 Opcode = X86ISD::FMIN;
7860 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007861
Chris Lattner47b4ce82009-03-11 05:48:52 +00007862 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7863 case ISD::SETUGT:
7864 case ISD::SETGT:
7865 if (!UnsafeFPMath) break;
7866 // FALL THROUGH.
7867 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7868 case ISD::SETGE:
7869 Opcode = X86ISD::FMAX;
7870 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007871 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007872 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7873 switch (CC) {
7874 default: break;
7875 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7876 case ISD::SETUGT:
7877 case ISD::SETGT:
7878 if (!UnsafeFPMath) break;
7879 // FALL THROUGH.
7880 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7881 case ISD::SETGE:
7882 Opcode = X86ISD::FMIN;
7883 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007884
Chris Lattner47b4ce82009-03-11 05:48:52 +00007885 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7886 case ISD::SETULE:
7887 case ISD::SETLE:
7888 if (!UnsafeFPMath) break;
7889 // FALL THROUGH.
7890 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7891 case ISD::SETLT:
7892 Opcode = X86ISD::FMAX;
7893 break;
7894 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007895 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007896
Chris Lattner47b4ce82009-03-11 05:48:52 +00007897 if (Opcode)
7898 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007899 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007900
Chris Lattnerd1980a52009-03-12 06:52:53 +00007901 // If this is a select between two integer constants, try to do some
7902 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007903 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7904 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007905 // Don't do this for crazy integer types.
7906 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7907 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007908 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007909 bool NeedsCondInvert = false;
7910
Chris Lattnercee56e72009-03-13 05:53:31 +00007911 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007912 // Efficiently invertible.
7913 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7914 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7915 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7916 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007917 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007918 }
7919
7920 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007921 if (FalseC->getAPIntValue() == 0 &&
7922 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007923 if (NeedsCondInvert) // Invert the condition if needed.
7924 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7925 DAG.getConstant(1, Cond.getValueType()));
7926
7927 // Zero extend the condition if needed.
7928 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7929
Chris Lattnercee56e72009-03-13 05:53:31 +00007930 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007931 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7932 DAG.getConstant(ShAmt, MVT::i8));
7933 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007934
7935 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007936 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007937 if (NeedsCondInvert) // Invert the condition if needed.
7938 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7939 DAG.getConstant(1, Cond.getValueType()));
7940
7941 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007942 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7943 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007944 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007945 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007946 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007947
7948 // Optimize cases that will turn into an LEA instruction. This requires
7949 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7950 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7951 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7952 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7953
7954 bool isFastMultiplier = false;
7955 if (Diff < 10) {
7956 switch ((unsigned char)Diff) {
7957 default: break;
7958 case 1: // result = add base, cond
7959 case 2: // result = lea base( , cond*2)
7960 case 3: // result = lea base(cond, cond*2)
7961 case 4: // result = lea base( , cond*4)
7962 case 5: // result = lea base(cond, cond*4)
7963 case 8: // result = lea base( , cond*8)
7964 case 9: // result = lea base(cond, cond*8)
7965 isFastMultiplier = true;
7966 break;
7967 }
7968 }
7969
7970 if (isFastMultiplier) {
7971 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7972 if (NeedsCondInvert) // Invert the condition if needed.
7973 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7974 DAG.getConstant(1, Cond.getValueType()));
7975
7976 // Zero extend the condition if needed.
7977 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7978 Cond);
7979 // Scale the condition by the difference.
7980 if (Diff != 1)
7981 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7982 DAG.getConstant(Diff, Cond.getValueType()));
7983
7984 // Add the base if non-zero.
7985 if (FalseC->getAPIntValue() != 0)
7986 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7987 SDValue(FalseC, 0));
7988 return Cond;
7989 }
7990 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007991 }
7992 }
7993
Dan Gohman475871a2008-07-27 21:46:04 +00007994 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007995}
7996
Chris Lattnerd1980a52009-03-12 06:52:53 +00007997/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7998static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7999 TargetLowering::DAGCombinerInfo &DCI) {
8000 DebugLoc DL = N->getDebugLoc();
8001
8002 // If the flag operand isn't dead, don't touch this CMOV.
8003 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8004 return SDValue();
8005
8006 // If this is a select between two integer constants, try to do some
8007 // optimizations. Note that the operands are ordered the opposite of SELECT
8008 // operands.
8009 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8010 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8011 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8012 // larger than FalseC (the false value).
8013 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8014
8015 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8016 CC = X86::GetOppositeBranchCondition(CC);
8017 std::swap(TrueC, FalseC);
8018 }
8019
8020 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008021 // This is efficient for any integer data type (including i8/i16) and
8022 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008023 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8024 SDValue Cond = N->getOperand(3);
8025 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8026 DAG.getConstant(CC, MVT::i8), Cond);
8027
8028 // Zero extend the condition if needed.
8029 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8030
8031 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8032 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8033 DAG.getConstant(ShAmt, MVT::i8));
8034 if (N->getNumValues() == 2) // Dead flag value?
8035 return DCI.CombineTo(N, Cond, SDValue());
8036 return Cond;
8037 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008038
8039 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8040 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008041 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8042 SDValue Cond = N->getOperand(3);
8043 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8044 DAG.getConstant(CC, MVT::i8), Cond);
8045
8046 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008047 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8048 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008049 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8050 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008051
Chris Lattner97a29a52009-03-13 05:22:11 +00008052 if (N->getNumValues() == 2) // Dead flag value?
8053 return DCI.CombineTo(N, Cond, SDValue());
8054 return Cond;
8055 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008056
8057 // Optimize cases that will turn into an LEA instruction. This requires
8058 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8059 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8060 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8061 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8062
8063 bool isFastMultiplier = false;
8064 if (Diff < 10) {
8065 switch ((unsigned char)Diff) {
8066 default: break;
8067 case 1: // result = add base, cond
8068 case 2: // result = lea base( , cond*2)
8069 case 3: // result = lea base(cond, cond*2)
8070 case 4: // result = lea base( , cond*4)
8071 case 5: // result = lea base(cond, cond*4)
8072 case 8: // result = lea base( , cond*8)
8073 case 9: // result = lea base(cond, cond*8)
8074 isFastMultiplier = true;
8075 break;
8076 }
8077 }
8078
8079 if (isFastMultiplier) {
8080 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8081 SDValue Cond = N->getOperand(3);
8082 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8083 DAG.getConstant(CC, MVT::i8), Cond);
8084 // Zero extend the condition if needed.
8085 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8086 Cond);
8087 // Scale the condition by the difference.
8088 if (Diff != 1)
8089 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8090 DAG.getConstant(Diff, Cond.getValueType()));
8091
8092 // Add the base if non-zero.
8093 if (FalseC->getAPIntValue() != 0)
8094 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8095 SDValue(FalseC, 0));
8096 if (N->getNumValues() == 2) // Dead flag value?
8097 return DCI.CombineTo(N, Cond, SDValue());
8098 return Cond;
8099 }
8100 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008101 }
8102 }
8103 return SDValue();
8104}
8105
8106
Evan Cheng0b0cd912009-03-28 05:57:29 +00008107/// PerformMulCombine - Optimize a single multiply with constant into two
8108/// in order to implement it with two cheaper instructions, e.g.
8109/// LEA + SHL, LEA + LEA.
8110static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8111 TargetLowering::DAGCombinerInfo &DCI) {
8112 if (DAG.getMachineFunction().
8113 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8114 return SDValue();
8115
8116 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8117 return SDValue();
8118
8119 MVT VT = N->getValueType(0);
8120 if (VT != MVT::i64)
8121 return SDValue();
8122
8123 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8124 if (!C)
8125 return SDValue();
8126 uint64_t MulAmt = C->getZExtValue();
8127 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8128 return SDValue();
8129
8130 uint64_t MulAmt1 = 0;
8131 uint64_t MulAmt2 = 0;
8132 if ((MulAmt % 9) == 0) {
8133 MulAmt1 = 9;
8134 MulAmt2 = MulAmt / 9;
8135 } else if ((MulAmt % 5) == 0) {
8136 MulAmt1 = 5;
8137 MulAmt2 = MulAmt / 5;
8138 } else if ((MulAmt % 3) == 0) {
8139 MulAmt1 = 3;
8140 MulAmt2 = MulAmt / 3;
8141 }
8142 if (MulAmt2 &&
8143 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8144 DebugLoc DL = N->getDebugLoc();
8145
8146 if (isPowerOf2_64(MulAmt2) &&
8147 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8148 // If second multiplifer is pow2, issue it first. We want the multiply by
8149 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8150 // is an add.
8151 std::swap(MulAmt1, MulAmt2);
8152
8153 SDValue NewMul;
8154 if (isPowerOf2_64(MulAmt1))
8155 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8156 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8157 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008158 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008159 DAG.getConstant(MulAmt1, VT));
8160
8161 if (isPowerOf2_64(MulAmt2))
8162 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8163 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8164 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008165 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008166 DAG.getConstant(MulAmt2, VT));
8167
8168 // Do not add new nodes to DAG combiner worklist.
8169 DCI.CombineTo(N, NewMul, false);
8170 }
8171 return SDValue();
8172}
8173
8174
Nate Begeman740ab032009-01-26 00:52:55 +00008175/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8176/// when possible.
8177static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8178 const X86Subtarget *Subtarget) {
8179 // On X86 with SSE2 support, we can transform this to a vector shift if
8180 // all elements are shifted by the same amount. We can't do this in legalize
8181 // because the a constant vector is typically transformed to a constant pool
8182 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008183 if (!Subtarget->hasSSE2())
8184 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008185
Nate Begeman740ab032009-01-26 00:52:55 +00008186 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008187 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8188 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008189
Mon P Wang3becd092009-01-28 08:12:05 +00008190 SDValue ShAmtOp = N->getOperand(1);
8191 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008192 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008193 SDValue BaseShAmt;
8194 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8195 unsigned NumElts = VT.getVectorNumElements();
8196 unsigned i = 0;
8197 for (; i != NumElts; ++i) {
8198 SDValue Arg = ShAmtOp.getOperand(i);
8199 if (Arg.getOpcode() == ISD::UNDEF) continue;
8200 BaseShAmt = Arg;
8201 break;
8202 }
8203 for (; i != NumElts; ++i) {
8204 SDValue Arg = ShAmtOp.getOperand(i);
8205 if (Arg.getOpcode() == ISD::UNDEF) continue;
8206 if (Arg != BaseShAmt) {
8207 return SDValue();
8208 }
8209 }
8210 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008211 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8212 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8213 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008214 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008215 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008216
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008217 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008218 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008219 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008220 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008221
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008222 // The shift amount is identical so we can do a vector shift.
8223 SDValue ValOp = N->getOperand(0);
8224 switch (N->getOpcode()) {
8225 default:
8226 assert(0 && "Unknown shift opcode!");
8227 break;
8228 case ISD::SHL:
8229 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008230 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008231 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8232 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008233 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008234 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008235 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8236 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008237 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008238 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008239 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8240 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008241 break;
8242 case ISD::SRA:
8243 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008244 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008245 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8246 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008247 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008248 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008249 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8250 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008251 break;
8252 case ISD::SRL:
8253 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008254 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008255 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8256 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008257 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008258 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008259 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8260 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008261 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008262 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008263 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8264 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008265 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008266 }
8267 return SDValue();
8268}
8269
Chris Lattner149a4e52008-02-22 02:09:43 +00008270/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008271static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008272 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008273 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8274 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008275 // A preferable solution to the general problem is to figure out the right
8276 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008277
8278 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008279 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008280 MVT VT = St->getValue().getValueType();
8281 if (VT.getSizeInBits() != 64)
8282 return SDValue();
8283
8284 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloat && Subtarget->hasSSE2();
8285 if ((VT.isVector() ||
8286 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008287 isa<LoadSDNode>(St->getValue()) &&
8288 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8289 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008290 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008291 LoadSDNode *Ld = 0;
8292 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008293 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008294 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008295 // Must be a store of a load. We currently handle two cases: the load
8296 // is a direct child, and it's under an intervening TokenFactor. It is
8297 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008298 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008299 Ld = cast<LoadSDNode>(St->getChain());
8300 else if (St->getValue().hasOneUse() &&
8301 ChainVal->getOpcode() == ISD::TokenFactor) {
8302 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008303 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008304 TokenFactorIndex = i;
8305 Ld = cast<LoadSDNode>(St->getValue());
8306 } else
8307 Ops.push_back(ChainVal->getOperand(i));
8308 }
8309 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008310
Evan Cheng536e6672009-03-12 05:59:15 +00008311 if (!Ld || !ISD::isNormalLoad(Ld))
8312 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008313
Evan Cheng536e6672009-03-12 05:59:15 +00008314 // If this is not the MMX case, i.e. we are just turning i64 load/store
8315 // into f64 load/store, avoid the transformation if there are multiple
8316 // uses of the loaded value.
8317 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8318 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008319
Evan Cheng536e6672009-03-12 05:59:15 +00008320 DebugLoc LdDL = Ld->getDebugLoc();
8321 DebugLoc StDL = N->getDebugLoc();
8322 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8323 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8324 // pair instead.
8325 if (Subtarget->is64Bit() || F64IsLegal) {
8326 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8327 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8328 Ld->getBasePtr(), Ld->getSrcValue(),
8329 Ld->getSrcValueOffset(), Ld->isVolatile(),
8330 Ld->getAlignment());
8331 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008332 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008333 Ops.push_back(NewChain);
8334 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008335 Ops.size());
8336 }
Evan Cheng536e6672009-03-12 05:59:15 +00008337 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008338 St->getSrcValue(), St->getSrcValueOffset(),
8339 St->isVolatile(), St->getAlignment());
8340 }
Evan Cheng536e6672009-03-12 05:59:15 +00008341
8342 // Otherwise, lower to two pairs of 32-bit loads / stores.
8343 SDValue LoAddr = Ld->getBasePtr();
8344 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8345 DAG.getConstant(4, MVT::i32));
8346
8347 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8348 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8349 Ld->isVolatile(), Ld->getAlignment());
8350 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8351 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8352 Ld->isVolatile(),
8353 MinAlign(Ld->getAlignment(), 4));
8354
8355 SDValue NewChain = LoLd.getValue(1);
8356 if (TokenFactorIndex != -1) {
8357 Ops.push_back(LoLd);
8358 Ops.push_back(HiLd);
8359 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8360 Ops.size());
8361 }
8362
8363 LoAddr = St->getBasePtr();
8364 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8365 DAG.getConstant(4, MVT::i32));
8366
8367 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8368 St->getSrcValue(), St->getSrcValueOffset(),
8369 St->isVolatile(), St->getAlignment());
8370 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8371 St->getSrcValue(),
8372 St->getSrcValueOffset() + 4,
8373 St->isVolatile(),
8374 MinAlign(St->getAlignment(), 4));
8375 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008376 }
Dan Gohman475871a2008-07-27 21:46:04 +00008377 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008378}
8379
Chris Lattner6cf73262008-01-25 06:14:17 +00008380/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8381/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008382static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008383 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8384 // F[X]OR(0.0, x) -> x
8385 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008386 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8387 if (C->getValueAPF().isPosZero())
8388 return N->getOperand(1);
8389 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8390 if (C->getValueAPF().isPosZero())
8391 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008392 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008393}
8394
8395/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008396static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008397 // FAND(0.0, x) -> 0.0
8398 // FAND(x, 0.0) -> 0.0
8399 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8400 if (C->getValueAPF().isPosZero())
8401 return N->getOperand(0);
8402 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8403 if (C->getValueAPF().isPosZero())
8404 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008405 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008406}
8407
Dan Gohmane5af2d32009-01-29 01:59:02 +00008408static SDValue PerformBTCombine(SDNode *N,
8409 SelectionDAG &DAG,
8410 TargetLowering::DAGCombinerInfo &DCI) {
8411 // BT ignores high bits in the bit index operand.
8412 SDValue Op1 = N->getOperand(1);
8413 if (Op1.hasOneUse()) {
8414 unsigned BitWidth = Op1.getValueSizeInBits();
8415 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8416 APInt KnownZero, KnownOne;
8417 TargetLowering::TargetLoweringOpt TLO(DAG);
8418 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8419 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8420 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8421 DCI.CommitTargetLoweringOpt(TLO);
8422 }
8423 return SDValue();
8424}
Chris Lattner83e6c992006-10-04 06:57:07 +00008425
Dan Gohman475871a2008-07-27 21:46:04 +00008426SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008427 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008428 SelectionDAG &DAG = DCI.DAG;
8429 switch (N->getOpcode()) {
8430 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008431 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8432 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008433 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008434 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008435 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008436 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008437 case ISD::SHL:
8438 case ISD::SRA:
8439 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008440 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008441 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008442 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8443 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008444 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008445 }
8446
Dan Gohman475871a2008-07-27 21:46:04 +00008447 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008448}
8449
Evan Cheng60c07e12006-07-05 22:17:51 +00008450//===----------------------------------------------------------------------===//
8451// X86 Inline Assembly Support
8452//===----------------------------------------------------------------------===//
8453
Chris Lattnerf4dff842006-07-11 02:54:03 +00008454/// getConstraintType - Given a constraint letter, return the type of
8455/// constraint it is for this target.
8456X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008457X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8458 if (Constraint.size() == 1) {
8459 switch (Constraint[0]) {
8460 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008461 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008462 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008463 case 'r':
8464 case 'R':
8465 case 'l':
8466 case 'q':
8467 case 'Q':
8468 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008469 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008470 case 'Y':
8471 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008472 case 'e':
8473 case 'Z':
8474 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008475 default:
8476 break;
8477 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008478 }
Chris Lattner4234f572007-03-25 02:14:49 +00008479 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008480}
8481
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008482/// LowerXConstraint - try to replace an X constraint, which matches anything,
8483/// with another that has more specific requirements based on the type of the
8484/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008485const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008486LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008487 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8488 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008489 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008490 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008491 return "Y";
8492 if (Subtarget->hasSSE1())
8493 return "x";
8494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008495
Chris Lattner5e764232008-04-26 23:02:14 +00008496 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008497}
8498
Chris Lattner48884cd2007-08-25 00:47:38 +00008499/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8500/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008501void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008502 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008503 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008504 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008505 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008506 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008507
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008508 switch (Constraint) {
8509 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008510 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008511 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008512 if (C->getZExtValue() <= 31) {
8513 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008514 break;
8515 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008516 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008517 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008518 case 'J':
8519 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8520 if (C->getZExtValue() <= 63) {
8521 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8522 break;
8523 }
8524 }
8525 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008526 case 'N':
8527 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008528 if (C->getZExtValue() <= 255) {
8529 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008530 break;
8531 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008532 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008533 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008534 case 'e': {
8535 // 32-bit signed value
8536 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8537 const ConstantInt *CI = C->getConstantIntValue();
8538 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8539 // Widen to 64 bits here to get it sign extended.
8540 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8541 break;
8542 }
8543 // FIXME gcc accepts some relocatable values here too, but only in certain
8544 // memory models; it's complicated.
8545 }
8546 return;
8547 }
8548 case 'Z': {
8549 // 32-bit unsigned value
8550 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8551 const ConstantInt *CI = C->getConstantIntValue();
8552 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8553 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8554 break;
8555 }
8556 }
8557 // FIXME gcc accepts some relocatable values here too, but only in certain
8558 // memory models; it's complicated.
8559 return;
8560 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008561 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008562 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008563 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008564 // Widen to 64 bits here to get it sign extended.
8565 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008566 break;
8567 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008568
Chris Lattnerdc43a882007-05-03 16:52:29 +00008569 // If we are in non-pic codegen mode, we allow the address of a global (with
8570 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008571 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008572 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008573
Chris Lattner49921962009-05-08 18:23:14 +00008574 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8575 while (1) {
8576 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8577 Offset += GA->getOffset();
8578 break;
8579 } else if (Op.getOpcode() == ISD::ADD) {
8580 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8581 Offset += C->getZExtValue();
8582 Op = Op.getOperand(0);
8583 continue;
8584 }
8585 } else if (Op.getOpcode() == ISD::SUB) {
8586 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8587 Offset += -C->getZExtValue();
8588 Op = Op.getOperand(0);
8589 continue;
8590 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008591 }
Chris Lattner49921962009-05-08 18:23:14 +00008592
8593 // Otherwise, this isn't something we can handle, reject it.
8594 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008595 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008596
Chris Lattner49921962009-05-08 18:23:14 +00008597 if (hasMemory)
8598 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8599 else
8600 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8601 Offset);
8602 Result = Op;
8603 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008604 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008606
Gabor Greifba36cb52008-08-28 21:40:38 +00008607 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008608 Ops.push_back(Result);
8609 return;
8610 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008611 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8612 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008613}
8614
Chris Lattner259e97c2006-01-31 19:43:35 +00008615std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008616getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008617 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008618 if (Constraint.size() == 1) {
8619 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008620 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008621 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008622 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8623 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008624 if (VT == MVT::i32)
8625 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8626 else if (VT == MVT::i16)
8627 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8628 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008629 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008630 else if (VT == MVT::i64)
8631 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8632 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008633 }
8634 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008635
Chris Lattner1efa40f2006-02-22 00:56:39 +00008636 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008637}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008638
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008639std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008640X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008641 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008642 // First, see if this is a constraint that directly corresponds to an LLVM
8643 // register class.
8644 if (Constraint.size() == 1) {
8645 // GCC Constraint Letters
8646 switch (Constraint[0]) {
8647 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008648 case 'r': // GENERAL_REGS
8649 case 'R': // LEGACY_REGS
8650 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008651 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008652 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008653 if (VT == MVT::i16)
8654 return std::make_pair(0U, X86::GR16RegisterClass);
8655 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008656 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008657 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008658 case 'f': // FP Stack registers.
8659 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8660 // value to the correct fpstack register class.
8661 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8662 return std::make_pair(0U, X86::RFP32RegisterClass);
8663 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8664 return std::make_pair(0U, X86::RFP64RegisterClass);
8665 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008666 case 'y': // MMX_REGS if MMX allowed.
8667 if (!Subtarget->hasMMX()) break;
8668 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008669 case 'Y': // SSE_REGS if SSE2 allowed
8670 if (!Subtarget->hasSSE2()) break;
8671 // FALL THROUGH.
8672 case 'x': // SSE_REGS if SSE1 allowed
8673 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008674
8675 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008676 default: break;
8677 // Scalar SSE types.
8678 case MVT::f32:
8679 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008680 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008681 case MVT::f64:
8682 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008683 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008684 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008685 case MVT::v16i8:
8686 case MVT::v8i16:
8687 case MVT::v4i32:
8688 case MVT::v2i64:
8689 case MVT::v4f32:
8690 case MVT::v2f64:
8691 return std::make_pair(0U, X86::VR128RegisterClass);
8692 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008693 break;
8694 }
8695 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008696
Chris Lattnerf76d1802006-07-31 23:26:50 +00008697 // Use the default implementation in TargetLowering to convert the register
8698 // constraint into a member of a register class.
8699 std::pair<unsigned, const TargetRegisterClass*> Res;
8700 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008701
8702 // Not found as a standard register?
8703 if (Res.second == 0) {
8704 // GCC calls "st(0)" just plain "st".
8705 if (StringsEqualNoCase("{st}", Constraint)) {
8706 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008707 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008708 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008709 // 'A' means EAX + EDX.
8710 if (Constraint == "A") {
8711 Res.first = X86::EAX;
8712 Res.second = X86::GRADRegisterClass;
8713 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008714 return Res;
8715 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008716
Chris Lattnerf76d1802006-07-31 23:26:50 +00008717 // Otherwise, check to see if this is a register class of the wrong value
8718 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8719 // turn into {ax},{dx}.
8720 if (Res.second->hasType(VT))
8721 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008722
Chris Lattnerf76d1802006-07-31 23:26:50 +00008723 // All of the single-register GCC register classes map their values onto
8724 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8725 // really want an 8-bit or 32-bit register, map to the appropriate register
8726 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008727 if (Res.second == X86::GR16RegisterClass) {
8728 if (VT == MVT::i8) {
8729 unsigned DestReg = 0;
8730 switch (Res.first) {
8731 default: break;
8732 case X86::AX: DestReg = X86::AL; break;
8733 case X86::DX: DestReg = X86::DL; break;
8734 case X86::CX: DestReg = X86::CL; break;
8735 case X86::BX: DestReg = X86::BL; break;
8736 }
8737 if (DestReg) {
8738 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008739 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008740 }
8741 } else if (VT == MVT::i32) {
8742 unsigned DestReg = 0;
8743 switch (Res.first) {
8744 default: break;
8745 case X86::AX: DestReg = X86::EAX; break;
8746 case X86::DX: DestReg = X86::EDX; break;
8747 case X86::CX: DestReg = X86::ECX; break;
8748 case X86::BX: DestReg = X86::EBX; break;
8749 case X86::SI: DestReg = X86::ESI; break;
8750 case X86::DI: DestReg = X86::EDI; break;
8751 case X86::BP: DestReg = X86::EBP; break;
8752 case X86::SP: DestReg = X86::ESP; break;
8753 }
8754 if (DestReg) {
8755 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008756 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008757 }
8758 } else if (VT == MVT::i64) {
8759 unsigned DestReg = 0;
8760 switch (Res.first) {
8761 default: break;
8762 case X86::AX: DestReg = X86::RAX; break;
8763 case X86::DX: DestReg = X86::RDX; break;
8764 case X86::CX: DestReg = X86::RCX; break;
8765 case X86::BX: DestReg = X86::RBX; break;
8766 case X86::SI: DestReg = X86::RSI; break;
8767 case X86::DI: DestReg = X86::RDI; break;
8768 case X86::BP: DestReg = X86::RBP; break;
8769 case X86::SP: DestReg = X86::RSP; break;
8770 }
8771 if (DestReg) {
8772 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008773 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008774 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008775 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008776 } else if (Res.second == X86::FR32RegisterClass ||
8777 Res.second == X86::FR64RegisterClass ||
8778 Res.second == X86::VR128RegisterClass) {
8779 // Handle references to XMM physical registers that got mapped into the
8780 // wrong class. This can happen with constraints like {xmm0} where the
8781 // target independent register mapper will just pick the first match it can
8782 // find, ignoring the required type.
8783 if (VT == MVT::f32)
8784 Res.second = X86::FR32RegisterClass;
8785 else if (VT == MVT::f64)
8786 Res.second = X86::FR64RegisterClass;
8787 else if (X86::VR128RegisterClass->hasType(VT))
8788 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008789 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008790
Chris Lattnerf76d1802006-07-31 23:26:50 +00008791 return Res;
8792}
Mon P Wang0c397192008-10-30 08:01:45 +00008793
8794//===----------------------------------------------------------------------===//
8795// X86 Widen vector type
8796//===----------------------------------------------------------------------===//
8797
8798/// getWidenVectorType: given a vector type, returns the type to widen
8799/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8800/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008801/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008802/// scalarizing vs using the wider vector type.
8803
Dan Gohmanc13cf132009-01-15 17:34:08 +00008804MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008805 assert(VT.isVector());
8806 if (isTypeLegal(VT))
8807 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008808
Mon P Wang0c397192008-10-30 08:01:45 +00008809 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8810 // type based on element type. This would speed up our search (though
8811 // it may not be worth it since the size of the list is relatively
8812 // small).
8813 MVT EltVT = VT.getVectorElementType();
8814 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008815
Mon P Wang0c397192008-10-30 08:01:45 +00008816 // On X86, it make sense to widen any vector wider than 1
8817 if (NElts <= 1)
8818 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008819
8820 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008821 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8822 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008823
8824 if (isTypeLegal(SVT) &&
8825 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008826 SVT.getVectorNumElements() > NElts)
8827 return SVT;
8828 }
8829 return MVT::Other;
8830}