blob: 4c2bc45098f435e90569307e312c20222d8e215b [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"
Chris Lattnerb903bed2009-06-26 21:20:29 +000022#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000033#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000034#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000035#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000038#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000039#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000040#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Mon P Wang3c81d352008-11-23 04:37:22 +000043static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000044DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000045
Evan Cheng10e86422008-04-25 19:11:04 +000046// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000047static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
48 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000049
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000050X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000051 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000052 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000053 X86ScalarSSEf64 = Subtarget->hasSSE2();
54 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000055 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000056
Anton Korobeynikov2365f512007-07-14 14:06:15 +000057 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000058 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000059
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000060 // Set up the TargetLowering object.
61
62 // X86 is weird, it always uses i8 for shift amounts and setcc results.
63 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000064 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000065 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000066 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000067 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000068
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000070 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000071 setUseUnderscoreSetJmp(false);
72 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000073 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000074 // MS runtime is weird: it exports _setjmp, but longjmp!
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(false);
77 } else {
78 setUseUnderscoreSetJmp(true);
79 setUseUnderscoreLongJmp(true);
80 }
Scott Michelfdc40a02009-02-17 22:15:04 +000081
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000082 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000083 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
84 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
85 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000086 if (Subtarget->is64Bit())
87 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000088
Evan Cheng03294662008-10-14 21:26:46 +000089 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000090
Scott Michelfdc40a02009-02-17 22:15:04 +000091 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +000092 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
93 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
96 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000097 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
98
99 // SETOEQ and SETUNE require checking two conditions.
100 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
101 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
102 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
103 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
104 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000106
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000107 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
108 // operation.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
110 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
111 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000112
Evan Cheng25ab6902006-09-08 06:48:29 +0000113 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000114 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000115 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000116 } else if (!UseSoftFloat) {
117 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000118 // We have an impenetrably clever algorithm for ui64->double only.
119 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000120 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000121 // We have an algorithm for SSE2, and we turn this into a 64-bit
122 // FILD for other targets.
123 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000124 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000125
126 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
127 // this operation.
128 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
129 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000130
Devang Patel6a784892009-06-05 18:48:29 +0000131 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000132 // SSE has no i16 to fp conversion, only i32
133 if (X86ScalarSSEf32) {
134 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
135 // f32 and f64 cases are Legal, f80 case is not
136 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
137 } else {
138 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
139 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
140 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000141 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000142 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
143 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000144 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000145
Dale Johannesen73328d12007-09-19 23:55:34 +0000146 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
147 // are Legal, f80 is custom lowered.
148 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
149 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000150
Evan Cheng02568ff2006-01-30 22:13:22 +0000151 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
152 // this operation.
153 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
154 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
155
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000157 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000158 // f32 and f64 cases are Legal, f80 case is not
159 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000160 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000162 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000163 }
164
165 // Handle FP_TO_UINT by promoting the destination to a larger signed
166 // conversion.
167 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
168 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
169 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
170
Evan Cheng25ab6902006-09-08 06:48:29 +0000171 if (Subtarget->is64Bit()) {
172 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000173 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000174 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000175 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000176 // Expand FP_TO_UINT into a select.
177 // FIXME: We would like to use a Custom expander here eventually to do
178 // the optimal thing for SSE vs. the default expansion in the legalizer.
179 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
180 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000181 // With SSE3 we can use fisttpll to convert to a signed i64; without
182 // SSE, we're stuck with a fistpll.
183 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000184 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000185
Chris Lattner399610a2006-12-05 18:22:22 +0000186 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000187 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000188 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
189 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
190 }
Chris Lattner21f66852005-12-23 05:15:23 +0000191
Dan Gohmanb00ee212008-02-18 19:34:53 +0000192 // Scalar integer divide and remainder are lowered to use operations that
193 // produce two results, to match the available instructions. This exposes
194 // the two-result form to trivial CSE, which is able to combine x/y and x%y
195 // into a single instruction.
196 //
197 // Scalar integer multiply-high is also lowered to use two-result
198 // operations, to match the available instructions. However, plain multiply
199 // (low) operations are left as Legal, as there are single-result
200 // instructions for this in x86. Using the two-result multiply instructions
201 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000202 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
203 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
204 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
205 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
206 setOperationAction(ISD::SREM , MVT::i8 , Expand);
207 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000208 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
209 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
210 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
211 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
212 setOperationAction(ISD::SREM , MVT::i16 , Expand);
213 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000214 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
215 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
216 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
217 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
218 setOperationAction(ISD::SREM , MVT::i32 , Expand);
219 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000220 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
221 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
222 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
223 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
224 setOperationAction(ISD::SREM , MVT::i64 , Expand);
225 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000226
Evan Chengc35497f2006-10-30 08:02:39 +0000227 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000228 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000229 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
230 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000232 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
234 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
236 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000237 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000239 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000240 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000241
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000242 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000243 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
244 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000245 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000246 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
247 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000249 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
250 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000251 if (Subtarget->is64Bit()) {
252 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000253 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
254 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000255 }
256
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000257 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000258 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000259
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260 // These should be promoted to a larger select which is supported.
261 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
262 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000263 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000264 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
265 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
266 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
267 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000268 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000269 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
270 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
271 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
272 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
273 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000274 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000275 if (Subtarget->is64Bit()) {
276 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
277 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
278 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000279 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000280 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000281 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000282
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000283 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000284 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000285 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000286 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000287 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000288 if (Subtarget->is64Bit())
289 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000290 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 if (Subtarget->is64Bit()) {
292 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
293 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
294 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000295 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000296 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000297 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000298 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
299 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
300 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000301 if (Subtarget->is64Bit()) {
302 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
303 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
304 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
305 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306
Evan Chengd2cde682008-03-10 19:38:10 +0000307 if (Subtarget->hasSSE1())
308 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000309
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000310 if (!Subtarget->hasSSE2())
311 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
312
Mon P Wang63307c32008-05-05 19:05:59 +0000313 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000314 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
315 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
316 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
317 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000318
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000319 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000323
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000324 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000325 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
328 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
330 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
331 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000332 }
333
Dan Gohman7f460202008-06-30 20:59:49 +0000334 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
335 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000336 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000337 if (!Subtarget->isTargetDarwin() &&
338 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000339 !Subtarget->isTargetCygMing()) {
340 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
341 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
342 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000343
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000344 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
345 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
346 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
347 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
348 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000349 setExceptionPointerRegister(X86::RAX);
350 setExceptionSelectorRegister(X86::RDX);
351 } else {
352 setExceptionPointerRegister(X86::EAX);
353 setExceptionSelectorRegister(X86::EDX);
354 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000355 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000356 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
357
Duncan Sandsf7331b32007-09-11 14:10:23 +0000358 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000359
Chris Lattnerda68d302008-01-15 21:58:22 +0000360 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000361
Nate Begemanacc398c2006-01-25 18:21:52 +0000362 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
363 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000364 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000365 if (Subtarget->is64Bit()) {
366 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000367 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000368 } else {
369 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000370 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000371 }
Evan Chengae642192007-03-02 23:16:35 +0000372
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000373 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000374 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000375 if (Subtarget->is64Bit())
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000377 if (Subtarget->isTargetCygMing())
378 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
379 else
380 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000381
Evan Chengc7ce29b2009-02-13 22:36:38 +0000382 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000383 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000384 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000385 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
386 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000387
Evan Cheng223547a2006-01-31 22:28:30 +0000388 // Use ANDPD to simulate FABS.
389 setOperationAction(ISD::FABS , MVT::f64, Custom);
390 setOperationAction(ISD::FABS , MVT::f32, Custom);
391
392 // Use XORP to simulate FNEG.
393 setOperationAction(ISD::FNEG , MVT::f64, Custom);
394 setOperationAction(ISD::FNEG , MVT::f32, Custom);
395
Evan Cheng68c47cb2007-01-05 07:55:56 +0000396 // Use ANDPD and ORPD to simulate FCOPYSIGN.
397 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
398 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
399
Evan Chengd25e9e82006-02-02 00:28:23 +0000400 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000401 setOperationAction(ISD::FSIN , MVT::f64, Expand);
402 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403 setOperationAction(ISD::FSIN , MVT::f32, Expand);
404 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000405
Chris Lattnera54aa942006-01-29 06:26:08 +0000406 // Expand FP immediates into loads from the stack, except for the special
407 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000408 addLegalFPImmediate(APFloat(+0.0)); // xorpd
409 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000410 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000411 // Use SSE for f32, x87 for f64.
412 // Set up the FP register classes.
413 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
414 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
415
416 // Use ANDPS to simulate FABS.
417 setOperationAction(ISD::FABS , MVT::f32, Custom);
418
419 // Use XORP to simulate FNEG.
420 setOperationAction(ISD::FNEG , MVT::f32, Custom);
421
422 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
423
424 // Use ANDPS and ORPS to simulate FCOPYSIGN.
425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
427
428 // We don't support sin/cos/fmod
429 setOperationAction(ISD::FSIN , MVT::f32, Expand);
430 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000431
Nate Begemane1795842008-02-14 08:57:00 +0000432 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000433 addLegalFPImmediate(APFloat(+0.0f)); // xorps
434 addLegalFPImmediate(APFloat(+0.0)); // FLD0
435 addLegalFPImmediate(APFloat(+1.0)); // FLD1
436 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
437 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
438
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000439 if (!UnsafeFPMath) {
440 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
441 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
442 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000443 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000444 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000445 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000446 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
447 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000448
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000450 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000451 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
452 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000453
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000454 if (!UnsafeFPMath) {
455 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
456 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
457 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000458 addLegalFPImmediate(APFloat(+0.0)); // FLD0
459 addLegalFPImmediate(APFloat(+1.0)); // FLD1
460 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
461 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
463 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
464 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
465 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000466 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000467
Dale Johannesen59a58732007-08-05 18:49:15 +0000468 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000469 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000470 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
471 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
472 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
473 {
474 bool ignored;
475 APFloat TmpFlt(+0.0);
476 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
477 &ignored);
478 addLegalFPImmediate(TmpFlt); // FLD0
479 TmpFlt.changeSign();
480 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
481 APFloat TmpFlt2(+1.0);
482 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
483 &ignored);
484 addLegalFPImmediate(TmpFlt2); // FLD1
485 TmpFlt2.changeSign();
486 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
487 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000488
Evan Chengc7ce29b2009-02-13 22:36:38 +0000489 if (!UnsafeFPMath) {
490 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
492 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000493 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000494
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000495 // Always use a library call for pow.
496 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
497 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
498 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
499
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000500 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000502 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000503 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000504 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
505
Mon P Wangf007a8b2008-11-06 05:31:54 +0000506 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000507 // (for widening) or expand (for scalarization). Then we will selectively
508 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000509 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
510 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000511 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000524 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000526 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000527 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000528 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000550 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Eli Friedman23ef1052009-06-06 03:57:58 +0000555 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000559 }
560
Evan Chengc7ce29b2009-02-13 22:36:38 +0000561 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
562 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000563 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000564 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
565 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
566 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000567 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000568 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000569
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000570 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
571 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
572 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000573 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000574
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000575 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
576 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
577 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000578 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000579
Bill Wendling74027e92007-03-15 21:24:36 +0000580 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
581 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
582
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000583 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000584 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000585 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000586 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
587 setOperationAction(ISD::AND, MVT::v2i32, Promote);
588 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
589 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000590
591 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000592 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000593 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000594 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
595 setOperationAction(ISD::OR, MVT::v2i32, Promote);
596 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
597 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000598
599 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000600 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000601 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000602 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
603 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
604 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
605 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000606
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000607 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000608 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000609 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000610 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
611 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
612 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000613 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
614 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000615 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000616
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000617 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000620 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000621 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000622
623 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
624 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
625 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000626 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000627
Evan Cheng52672b82008-07-22 18:39:19 +0000628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
630 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000631 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000632
633 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000634
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000635 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000636 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
637 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
638 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
639 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
640 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000641 }
642
Evan Cheng92722532009-03-26 23:06:32 +0000643 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000644 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
645
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000646 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
647 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
648 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
649 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000650 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
651 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000652 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
653 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000655 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000656 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000657 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000658 }
659
Evan Cheng92722532009-03-26 23:06:32 +0000660 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000661 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000662
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000663 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
664 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000665 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
666 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
667 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
668 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
669
Evan Chengf7c378e2006-04-10 07:23:14 +0000670 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
671 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
672 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000673 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000674 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000675 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
676 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
677 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000678 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000679 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000680 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
681 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
682 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
683 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000684 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
685 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000686
Nate Begeman30a0de92008-07-17 16:51:19 +0000687 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
688 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
689 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
690 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000691
Evan Chengf7c378e2006-04-10 07:23:14 +0000692 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000694 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000697
Evan Cheng2c3ae372006-04-12 21:21:57 +0000698 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000699 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
700 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000701 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000702 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000703 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000704 // Do not attempt to custom lower non-128-bit vectors
705 if (!VT.is128BitVector())
706 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000707 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
708 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
709 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000710 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000711
Evan Cheng2c3ae372006-04-12 21:21:57 +0000712 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
713 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
714 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
715 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000717 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000718
Nate Begemancdd1eec2008-02-12 22:51:28 +0000719 if (Subtarget->is64Bit()) {
720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000722 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000723
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000724 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
David Greene9b9838d2009-06-29 16:47:10 +0000725 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
726 MVT VT = (MVT::SimpleValueType)i;
727
728 // Do not attempt to promote non-128-bit vectors
729 if (!VT.is128BitVector()) {
730 continue;
731 }
732 setOperationAction(ISD::AND, VT, Promote);
733 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
734 setOperationAction(ISD::OR, VT, Promote);
735 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
736 setOperationAction(ISD::XOR, VT, Promote);
737 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
738 setOperationAction(ISD::LOAD, VT, Promote);
739 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
740 setOperationAction(ISD::SELECT, VT, Promote);
741 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000742 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000743
Chris Lattnerddf89562008-01-17 19:59:44 +0000744 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000745
Evan Cheng2c3ae372006-04-12 21:21:57 +0000746 // Custom lower v2i64 and v2f64 selects.
747 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000748 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000749 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000750 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000751
Eli Friedman23ef1052009-06-06 03:57:58 +0000752 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
753 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
754 if (!DisableMMX && Subtarget->hasMMX()) {
755 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
756 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
757 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000758 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000759
Nate Begeman14d12ca2008-02-11 04:19:36 +0000760 if (Subtarget->hasSSE41()) {
761 // FIXME: Do we need to handle scalar-to-vector here?
762 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
763
764 // i8 and i16 vectors are custom , because the source register and source
765 // source memory operand types are not the same width. f32 vectors are
766 // custom since the immediate controlling the insert encodes additional
767 // information.
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000771 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
772
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
774 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000775 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000776 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000777
778 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000779 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000781 }
782 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000783
Nate Begeman30a0de92008-07-17 16:51:19 +0000784 if (Subtarget->hasSSE42()) {
785 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
786 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000787
David Greene9b9838d2009-06-29 16:47:10 +0000788 if (!UseSoftFloat && Subtarget->hasAVX()) {
David Greened94c1012009-06-29 22:50:51 +0000789 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
790 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
791 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
792 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
793
David Greene9b9838d2009-06-29 16:47:10 +0000794 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
795 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
796 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
797 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
798 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
799 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
800 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
801 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
802 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
803 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
804 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
805 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
806 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
807 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
808 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
809
810 // Operations to consider commented out -v16i16 v32i8
811 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
812 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
813 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
814 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
815 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
816 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
817 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
818 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
819 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
820 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
821 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
822 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
823 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
824 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
825
826 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
827 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
828 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
829 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
830
831 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
832 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
833 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
834 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
836
837 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
838 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
839 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
840 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
842 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
843
844#if 0
845 // Not sure we want to do this since there are no 256-bit integer
846 // operations in AVX
847
848 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
849 // This includes 256-bit vectors
850 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
851 MVT VT = (MVT::SimpleValueType)i;
852
853 // Do not attempt to custom lower non-power-of-2 vectors
854 if (!isPowerOf2_32(VT.getVectorNumElements()))
855 continue;
856
857 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
858 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
859 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
860 }
861
862 if (Subtarget->is64Bit()) {
863 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
864 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
865 }
866#endif
867
868#if 0
869 // Not sure we want to do this since there are no 256-bit integer
870 // operations in AVX
871
872 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
873 // Including 256-bit vectors
874 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
875 MVT VT = (MVT::SimpleValueType)i;
876
877 if (!VT.is256BitVector()) {
878 continue;
879 }
880 setOperationAction(ISD::AND, VT, Promote);
881 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
882 setOperationAction(ISD::OR, VT, Promote);
883 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
884 setOperationAction(ISD::XOR, VT, Promote);
885 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
886 setOperationAction(ISD::LOAD, VT, Promote);
887 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
888 setOperationAction(ISD::SELECT, VT, Promote);
889 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
890 }
891
892 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
893#endif
894 }
895
Evan Cheng6be2c582006-04-05 23:38:46 +0000896 // We want to custom lower some of our intrinsics.
897 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
898
Bill Wendling74c37652008-12-09 22:08:41 +0000899 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000900 setOperationAction(ISD::SADDO, MVT::i32, Custom);
901 setOperationAction(ISD::SADDO, MVT::i64, Custom);
902 setOperationAction(ISD::UADDO, MVT::i32, Custom);
903 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000904 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
905 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
906 setOperationAction(ISD::USUBO, MVT::i32, Custom);
907 setOperationAction(ISD::USUBO, MVT::i64, Custom);
908 setOperationAction(ISD::SMULO, MVT::i32, Custom);
909 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000910
Evan Chengd54f2d52009-03-31 19:38:51 +0000911 if (!Subtarget->is64Bit()) {
912 // These libcalls are not available in 32-bit.
913 setLibcallName(RTLIB::SHL_I128, 0);
914 setLibcallName(RTLIB::SRL_I128, 0);
915 setLibcallName(RTLIB::SRA_I128, 0);
916 }
917
Evan Cheng206ee9d2006-07-07 08:33:52 +0000918 // We have target-specific dag combine patterns for the following nodes:
919 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000920 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000921 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000922 setTargetDAGCombine(ISD::SHL);
923 setTargetDAGCombine(ISD::SRA);
924 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000925 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000926 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000927 if (Subtarget->is64Bit())
928 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000929
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000930 computeRegisterProperties();
931
Evan Cheng87ed7162006-02-14 08:25:08 +0000932 // FIXME: These should be based on subtarget info. Plus, the values should
933 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000934 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
935 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
936 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000937 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000938 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000939 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000940}
941
Scott Michel5b8f82e2008-03-10 15:42:14 +0000942
Duncan Sands5480c042009-01-01 15:52:00 +0000943MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000944 return MVT::i8;
945}
946
947
Evan Cheng29286502008-01-23 23:17:41 +0000948/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
949/// the desired ByVal argument alignment.
950static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
951 if (MaxAlign == 16)
952 return;
953 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
954 if (VTy->getBitWidth() == 128)
955 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000956 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
957 unsigned EltAlign = 0;
958 getMaxByValAlign(ATy->getElementType(), EltAlign);
959 if (EltAlign > MaxAlign)
960 MaxAlign = EltAlign;
961 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
962 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
963 unsigned EltAlign = 0;
964 getMaxByValAlign(STy->getElementType(i), EltAlign);
965 if (EltAlign > MaxAlign)
966 MaxAlign = EltAlign;
967 if (MaxAlign == 16)
968 break;
969 }
970 }
971 return;
972}
973
974/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
975/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000976/// that contain SSE vectors are placed at 16-byte boundaries while the rest
977/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000978unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000979 if (Subtarget->is64Bit()) {
980 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000981 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000982 if (TyAlign > 8)
983 return TyAlign;
984 return 8;
985 }
986
Evan Cheng29286502008-01-23 23:17:41 +0000987 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000988 if (Subtarget->hasSSE1())
989 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000990 return Align;
991}
Chris Lattner2b02a442007-02-25 08:29:00 +0000992
Evan Chengf0df0312008-05-15 08:39:06 +0000993/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000994/// and store operations as a result of memset, memcpy, and memmove
995/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000996/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000997MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000998X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +0000999 bool isSrcConst, bool isSrcStr,
1000 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001001 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1002 // linux. This is because the stack realignment code can't handle certain
1003 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001004 const Function *F = DAG.getMachineFunction().getFunction();
1005 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1006 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001007 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
1008 return MVT::v4i32;
1009 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
1010 return MVT::v4f32;
1011 }
Evan Chengf0df0312008-05-15 08:39:06 +00001012 if (Subtarget->is64Bit() && Size >= 8)
1013 return MVT::i64;
1014 return MVT::i32;
1015}
1016
Evan Chengcc415862007-11-09 01:32:10 +00001017/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1018/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001019SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001020 SelectionDAG &DAG) const {
1021 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001022 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001023 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001024 // This doesn't have DebugLoc associated with it, but is not really the
1025 // same as a Register.
1026 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1027 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001028 return Table;
1029}
1030
Bill Wendlingb4202b82009-07-01 18:50:55 +00001031/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001032unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
1033 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
1034}
1035
Chris Lattner2b02a442007-02-25 08:29:00 +00001036//===----------------------------------------------------------------------===//
1037// Return Value Calling Convention Implementation
1038//===----------------------------------------------------------------------===//
1039
Chris Lattner59ed56b2007-02-28 04:55:35 +00001040#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001041
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001042/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +00001043SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001044 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001045 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +00001046
Chris Lattner9774c912007-02-27 05:28:59 +00001047 SmallVector<CCValAssign, 16> RVLocs;
1048 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +00001049 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1050 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +00001051 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001053 // If this is the first return lowered for this function, add the regs to the
1054 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001055 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001056 for (unsigned i = 0; i != RVLocs.size(); ++i)
1057 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001058 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001059 }
Dan Gohman475871a2008-07-27 21:46:04 +00001060 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001061
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001062 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001063 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001064 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00001065 SDValue TailCall = Chain;
1066 SDValue TargetAddress = TailCall.getOperand(1);
1067 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +00001068 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001069 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001070 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R11)) ||
Bill Wendling056292f2008-09-16 21:48:12 +00001071 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +00001072 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001073 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +00001074 assert(StackAdjustment.getOpcode() == ISD::Constant &&
1075 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001076
Dan Gohman475871a2008-07-27 21:46:04 +00001077 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001078 Operands.push_back(Chain.getOperand(0));
1079 Operands.push_back(TargetAddress);
1080 Operands.push_back(StackAdjustment);
1081 // Copy registers used by the call. Last operand is a flag so it is not
1082 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001083 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001084 Operands.push_back(Chain.getOperand(i));
1085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001086 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +00001087 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001088 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001089
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001090 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +00001091 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001092
Dan Gohman475871a2008-07-27 21:46:04 +00001093 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001094 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1095 // Operand #1 = Bytes To Pop
1096 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001097
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001098 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001099 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1100 CCValAssign &VA = RVLocs[i];
1101 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +00001102 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001103
Chris Lattner447ff682008-03-11 03:23:40 +00001104 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1105 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001106 if (VA.getLocReg() == X86::ST0 ||
1107 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001108 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1109 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001110 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +00001111 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001112 RetOps.push_back(ValToCopy);
1113 // Don't emit a copytoreg.
1114 continue;
1115 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001116
Evan Cheng242b38b2009-02-23 09:03:22 +00001117 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1118 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001119 if (Subtarget->is64Bit()) {
1120 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001121 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +00001122 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001123 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1124 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1125 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001126 }
1127
Dale Johannesendd64c412009-02-04 00:33:20 +00001128 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001129 Flag = Chain.getValue(1);
1130 }
Dan Gohman61a92132008-04-21 23:59:07 +00001131
1132 // The x86-64 ABI for returning structs by value requires that we copy
1133 // the sret argument into %rax for the return. We saved the argument into
1134 // a virtual register in the entry block, so now we copy the value out
1135 // and into %rax.
1136 if (Subtarget->is64Bit() &&
1137 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1138 MachineFunction &MF = DAG.getMachineFunction();
1139 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1140 unsigned Reg = FuncInfo->getSRetReturnReg();
1141 if (!Reg) {
1142 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1143 FuncInfo->setSRetReturnReg(Reg);
1144 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001145 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001146
Dale Johannesendd64c412009-02-04 00:33:20 +00001147 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001148 Flag = Chain.getValue(1);
1149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001150
Chris Lattner447ff682008-03-11 03:23:40 +00001151 RetOps[0] = Chain; // Update chain.
1152
1153 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001154 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001155 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001156
1157 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001158 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001159}
1160
1161
Chris Lattner3085e152007-02-25 08:59:22 +00001162/// LowerCallResult - Lower the result values of an ISD::CALL into the
1163/// appropriate copies out of appropriate physical registers. This assumes that
1164/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1165/// being lowered. The returns a SDNode with the same number of values as the
1166/// ISD::CALL.
1167SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001168LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001169 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001170
Scott Michelfdc40a02009-02-17 22:15:04 +00001171 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001172 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001173 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001174 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001175 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001176 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001177 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1178
Dan Gohman475871a2008-07-27 21:46:04 +00001179 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001180
Chris Lattner3085e152007-02-25 08:59:22 +00001181 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001182 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001183 CCValAssign &VA = RVLocs[i];
1184 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001185
Torok Edwin3f142c32009-02-01 18:15:56 +00001186 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001187 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001188 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1189 cerr << "SSE register return with SSE disabled\n";
1190 exit(1);
1191 }
1192
Chris Lattner8e6da152008-03-10 21:08:41 +00001193 // If this is a call to a function that returns an fp value on the floating
1194 // point stack, but where we prefer to use the value in xmm registers, copy
1195 // 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 +00001196 if ((VA.getLocReg() == X86::ST0 ||
1197 VA.getLocReg() == X86::ST1) &&
1198 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001199 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001201
Evan Cheng79fb3b42009-02-20 20:43:02 +00001202 SDValue Val;
1203 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001204 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1205 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1206 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1207 MVT::v2i64, InFlag).getValue(1);
1208 Val = Chain.getValue(0);
1209 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1210 Val, DAG.getConstant(0, MVT::i64));
1211 } else {
1212 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1213 MVT::i64, InFlag).getValue(1);
1214 Val = Chain.getValue(0);
1215 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001216 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1217 } else {
1218 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1219 CopyVT, InFlag).getValue(1);
1220 Val = Chain.getValue(0);
1221 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001222 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001223
Dan Gohman37eed792009-02-04 17:28:58 +00001224 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001225 // Round the F80 the right size, which also moves to the appropriate xmm
1226 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001227 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001228 // This truncation won't change the value.
1229 DAG.getIntPtrConstant(1));
1230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001231
Chris Lattner8e6da152008-03-10 21:08:41 +00001232 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001233 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001234
Chris Lattner3085e152007-02-25 08:59:22 +00001235 // Merge everything together with a MERGE_VALUES node.
1236 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001237 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1238 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001239}
1240
1241
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001242//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001243// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001244//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001245// StdCall calling convention seems to be standard for many Windows' API
1246// routines and around. It differs from C calling convention just a little:
1247// callee should clean up the stack, not caller. Symbols should be also
1248// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001249// For info on fast calling convention see Fast Calling Convention (tail call)
1250// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001251
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001252/// CallIsStructReturn - Determines whether a CALL node uses struct return
1253/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001254static bool CallIsStructReturn(CallSDNode *TheCall) {
1255 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001256 if (!NumOps)
1257 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001258
Dan Gohman095cc292008-09-13 01:54:27 +00001259 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001260}
1261
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001262/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1263/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001264static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001265 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001266 if (!NumArgs)
1267 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001268
1269 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001270}
1271
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001272/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1273/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001274/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001275bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001276 if (IsVarArg)
1277 return false;
1278
Dan Gohman095cc292008-09-13 01:54:27 +00001279 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001280 default:
1281 return false;
1282 case CallingConv::X86_StdCall:
1283 return !Subtarget->is64Bit();
1284 case CallingConv::X86_FastCall:
1285 return !Subtarget->is64Bit();
1286 case CallingConv::Fast:
1287 return PerformTailCallOpt;
1288 }
1289}
1290
Dan Gohman095cc292008-09-13 01:54:27 +00001291/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1292/// given CallingConvention value.
1293CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001294 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001295 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001296 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001297 else
1298 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001299 }
1300
Gordon Henriksen86737662008-01-05 16:56:59 +00001301 if (CC == CallingConv::X86_FastCall)
1302 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001303 else if (CC == CallingConv::Fast)
1304 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001305 else
1306 return CC_X86_32_C;
1307}
1308
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001309/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1310/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001311NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001312X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001313 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001314 if (CC == CallingConv::X86_FastCall)
1315 return FastCall;
1316 else if (CC == CallingConv::X86_StdCall)
1317 return StdCall;
1318 return None;
1319}
1320
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001321
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001322/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1323/// in a register before calling.
1324bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1325 return !IsTailCall && !Is64Bit &&
1326 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1327 Subtarget->isPICStyleGOT();
1328}
1329
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001330/// CallRequiresFnAddressInReg - Check whether the call requires the function
1331/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001332bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001333X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001334 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001335 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1336 Subtarget->isPICStyleGOT();
1337}
1338
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001339/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1340/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001341/// the specific parameter attribute. The copy will be passed as a byval
1342/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001343static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001344CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001345 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1346 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001348 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001349 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001350}
1351
Dan Gohman475871a2008-07-27 21:46:04 +00001352SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001353 const CCValAssign &VA,
1354 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001355 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001356 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001357 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001358 ISD::ArgFlagsTy Flags =
1359 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001360 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001361 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001362
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001363 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001364 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001365 // In case of tail call optimization mark all arguments mutable. Since they
1366 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001367 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001368 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001369 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001370 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001371 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001372 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001373 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001374}
1375
Dan Gohman475871a2008-07-27 21:46:04 +00001376SDValue
1377X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001378 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001379 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001380 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001381
Gordon Henriksen86737662008-01-05 16:56:59 +00001382 const Function* Fn = MF.getFunction();
1383 if (Fn->hasExternalLinkage() &&
1384 Subtarget->isTargetCygMing() &&
1385 Fn->getName() == "main")
1386 FuncInfo->setForceFramePointer(true);
1387
1388 // Decorate the function name.
1389 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001390
Evan Cheng1bc78042006-04-26 01:20:17 +00001391 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001393 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001394 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001395 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001396 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001397
1398 assert(!(isVarArg && CC == CallingConv::Fast) &&
1399 "Var args not supported with calling convention fastcc");
1400
Chris Lattner638402b2007-02-28 07:00:42 +00001401 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001402 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001403 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001404 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
Dan Gohman475871a2008-07-27 21:46:04 +00001406 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001407 unsigned LastVal = ~0U;
1408 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1409 CCValAssign &VA = ArgLocs[i];
1410 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1411 // places.
1412 assert(VA.getValNo() != LastVal &&
1413 "Don't support value assigned to multiple locs yet");
1414 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001415
Chris Lattnerf39f7712007-02-28 05:46:49 +00001416 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001417 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001418 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001419 if (RegVT == MVT::i32)
1420 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001421 else if (Is64Bit && RegVT == MVT::i64)
1422 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001423 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001424 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001425 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001426 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001427 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001428 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001429 else if (RegVT.isVector()) {
1430 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001431 if (!Is64Bit)
1432 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1433 else {
1434 // Darwin calling convention passes MMX values in either GPRs or
1435 // XMMs in x86-64. Other targets pass them in memory.
1436 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1437 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1438 RegVT = MVT::v2i64;
1439 } else {
1440 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1441 RegVT = MVT::i64;
1442 }
1443 }
1444 } else {
1445 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001446 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001447
Bob Wilson998e1252009-04-20 18:36:57 +00001448 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001449 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450
Chris Lattnerf39f7712007-02-28 05:46:49 +00001451 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1452 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1453 // right size.
1454 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001455 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001456 DAG.getValueType(VA.getValVT()));
1457 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001458 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001459 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001460
Chris Lattnerf39f7712007-02-28 05:46:49 +00001461 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001462 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001463
Gordon Henriksen86737662008-01-05 16:56:59 +00001464 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001465 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001466 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001467 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001468 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001469 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1470 ArgValue, DAG.getConstant(0, MVT::i64));
1471 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001472 }
1473 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001474
Chris Lattnerf39f7712007-02-28 05:46:49 +00001475 ArgValues.push_back(ArgValue);
1476 } else {
1477 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001478 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001479 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001480 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001481
Dan Gohman61a92132008-04-21 23:59:07 +00001482 // The x86-64 ABI for returning structs by value requires that we copy
1483 // the sret argument into %rax for the return. Save the argument into
1484 // a virtual register so that we can access it from the return points.
1485 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1486 MachineFunction &MF = DAG.getMachineFunction();
1487 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1488 unsigned Reg = FuncInfo->getSRetReturnReg();
1489 if (!Reg) {
1490 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1491 FuncInfo->setSRetReturnReg(Reg);
1492 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001493 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001494 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001495 }
1496
Chris Lattnerf39f7712007-02-28 05:46:49 +00001497 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001498 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001499 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001500 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001501
Evan Cheng1bc78042006-04-26 01:20:17 +00001502 // If the function takes variable number of arguments, make a frame index for
1503 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001504 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001505 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1506 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1507 }
1508 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001509 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1510
1511 // FIXME: We should really autogenerate these arrays
1512 static const unsigned GPR64ArgRegsWin64[] = {
1513 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001514 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001515 static const unsigned XMMArgRegsWin64[] = {
1516 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1517 };
1518 static const unsigned GPR64ArgRegs64Bit[] = {
1519 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1520 };
1521 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001522 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1523 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1524 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001525 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1526
1527 if (IsWin64) {
1528 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1529 GPR64ArgRegs = GPR64ArgRegsWin64;
1530 XMMArgRegs = XMMArgRegsWin64;
1531 } else {
1532 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1533 GPR64ArgRegs = GPR64ArgRegs64Bit;
1534 XMMArgRegs = XMMArgRegs64Bit;
1535 }
1536 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1537 TotalNumIntRegs);
1538 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1539 TotalNumXMMRegs);
1540
Devang Patel578efa92009-06-05 21:57:13 +00001541 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001542 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001543 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001544 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001545 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001546 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001547 // Kernel mode asks for SSE to be disabled, so don't push them
1548 // on the stack.
1549 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001550
Gordon Henriksen86737662008-01-05 16:56:59 +00001551 // For X86-64, if there are vararg parameters that are passed via
1552 // registers, then we must store them to their spots on the stack so they
1553 // may be loaded by deferencing the result of va_next.
1554 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001555 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1556 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1557 TotalNumXMMRegs * 16, 16);
1558
Gordon Henriksen86737662008-01-05 16:56:59 +00001559 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001560 SmallVector<SDValue, 8> MemOps;
1561 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001562 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001563 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001564 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001565 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1566 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001567 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001568 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001569 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001570 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001571 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001572 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001573 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001574 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001575
Gordon Henriksen86737662008-01-05 16:56:59 +00001576 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001577 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001578 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001579 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001580 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1581 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001582 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001583 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001584 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001585 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001586 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001587 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001588 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001589 }
1590 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001591 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001592 &MemOps[0], MemOps.size());
1593 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001595
Gordon Henriksenae636f82008-01-03 16:47:34 +00001596 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001597
Gordon Henriksen86737662008-01-05 16:56:59 +00001598 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001599 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001600 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001601 BytesCallerReserves = 0;
1602 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001603 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001604 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001605 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001606 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001607 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001608 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001609
Gordon Henriksen86737662008-01-05 16:56:59 +00001610 if (!Is64Bit) {
1611 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1612 if (CC == CallingConv::X86_FastCall)
1613 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1614 }
Evan Cheng25caf632006-05-23 21:06:34 +00001615
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001616 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001617
Evan Cheng25caf632006-05-23 21:06:34 +00001618 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001619 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001620 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001621}
1622
Dan Gohman475871a2008-07-27 21:46:04 +00001623SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001624X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001625 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001626 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001627 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001628 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001629 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001630 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001631 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001632 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001633 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001634 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001635 }
Dale Johannesenace16102009-02-03 19:33:06 +00001636 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001637 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001638}
1639
Bill Wendling64e87322009-01-16 19:25:27 +00001640/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001641/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001642SDValue
1643X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001644 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001645 SDValue Chain,
1646 bool IsTailCall,
1647 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001648 int FPDiff,
1649 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001650 if (!IsTailCall || FPDiff==0) return Chain;
1651
1652 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001653 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001654 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001655
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001656 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001657 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001658 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001659}
1660
1661/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1662/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001663static SDValue
1664EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001665 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001666 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001667 // Store the return address to the appropriate stack slot.
1668 if (!FPDiff) return Chain;
1669 // Calculate the new stack slot for the return address.
1670 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001671 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001672 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001673 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001674 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001675 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001676 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001677 return Chain;
1678}
1679
Dan Gohman475871a2008-07-27 21:46:04 +00001680SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001681 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001682 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1683 SDValue Chain = TheCall->getChain();
1684 unsigned CC = TheCall->getCallingConv();
1685 bool isVarArg = TheCall->isVarArg();
1686 bool IsTailCall = TheCall->isTailCall() &&
1687 CC == CallingConv::Fast && PerformTailCallOpt;
1688 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001689 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001690 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001691 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001692
1693 assert(!(isVarArg && CC == CallingConv::Fast) &&
1694 "Var args not supported with calling convention fastcc");
1695
Chris Lattner638402b2007-02-28 07:00:42 +00001696 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001697 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001698 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001699 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Chris Lattner423c5f42007-02-28 05:31:48 +00001701 // Get a count of how many bytes are to be pushed on the stack.
1702 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001703 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001704 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001705
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 int FPDiff = 0;
1707 if (IsTailCall) {
1708 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001709 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1711 FPDiff = NumBytesCallerPushed - NumBytes;
1712
1713 // Set the delta of movement of the returnaddr stackslot.
1714 // But only set if delta is greater than previous delta.
1715 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1716 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1717 }
1718
Chris Lattnere563bbc2008-10-11 22:08:30 +00001719 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001720
Dan Gohman475871a2008-07-27 21:46:04 +00001721 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001722 // Load return adress for tail calls.
1723 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001724 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001725
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1727 SmallVector<SDValue, 8> MemOpChains;
1728 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001729
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001730 // Walk the register/memloc assignments, inserting copies/loads. In the case
1731 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001732 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1733 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001734 SDValue Arg = TheCall->getArg(i);
1735 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1736 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Chris Lattner423c5f42007-02-28 05:31:48 +00001738 // Promote the value if needed.
1739 switch (VA.getLocInfo()) {
1740 default: assert(0 && "Unknown loc info!");
1741 case CCValAssign::Full: break;
1742 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001743 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001744 break;
1745 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001746 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001747 break;
1748 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001749 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001750 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001752
Chris Lattner423c5f42007-02-28 05:31:48 +00001753 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001754 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001755 MVT RegVT = VA.getLocVT();
1756 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001757 switch (VA.getLocReg()) {
1758 default:
1759 break;
1760 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1761 case X86::R8: {
1762 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001763 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001764 break;
1765 }
1766 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1767 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1768 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001769 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1770 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001771 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001772 break;
1773 }
1774 }
1775 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001776 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1777 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001778 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001779 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001780 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001781 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001782
Dan Gohman095cc292008-09-13 01:54:27 +00001783 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1784 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001785 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001786 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001787 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001788
Evan Cheng32fe1032006-05-25 00:59:30 +00001789 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001790 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001791 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001792
Evan Cheng347d5f72006-04-28 21:29:37 +00001793 // Build a sequence of copy-to-reg nodes chained together with token chain
1794 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001795 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001796 // Tail call byval lowering might overwrite argument registers so in case of
1797 // tail call optimization the copies to registers are lowered later.
1798 if (!IsTailCall)
1799 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001800 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001801 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001802 InFlag = Chain.getValue(1);
1803 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001804
Evan Chengf4684712007-02-21 21:18:14 +00001805 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001806 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001807 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001808 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001809 DAG.getNode(X86ISD::GlobalBaseReg,
1810 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001811 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001812 InFlag);
1813 InFlag = Chain.getValue(1);
1814 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001815 // If we are tail calling and generating PIC/GOT style code load the address
1816 // of the callee into ecx. The value in ecx is used as target of the tail
1817 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1818 // calls on PIC/GOT architectures. Normally we would just put the address of
1819 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1820 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001821 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001822 // Note: The actual moving to ecx is done further down.
1823 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001824 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001825 !G->getGlobal()->hasProtectedVisibility())
1826 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001827 else if (isa<ExternalSymbolSDNode>(Callee))
1828 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001829 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001830
Gordon Henriksen86737662008-01-05 16:56:59 +00001831 if (Is64Bit && isVarArg) {
1832 // From AMD64 ABI document:
1833 // For calls that may call functions that use varargs or stdargs
1834 // (prototype-less calls or calls to functions containing ellipsis (...) in
1835 // the declaration) %al is used as hidden argument to specify the number
1836 // of SSE registers used. The contents of %al do not need to match exactly
1837 // the number of registers, but must be an ubound on the number of SSE
1838 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001839
1840 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001841 // Count the number of XMM registers allocated.
1842 static const unsigned XMMArgRegs[] = {
1843 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1844 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1845 };
1846 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001847 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001848 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Dale Johannesendd64c412009-02-04 00:33:20 +00001850 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1852 InFlag = Chain.getValue(1);
1853 }
1854
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001855
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001856 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001857 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001858 SmallVector<SDValue, 8> MemOpChains2;
1859 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001860 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001861 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001862 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1864 CCValAssign &VA = ArgLocs[i];
1865 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001866 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001867 SDValue Arg = TheCall->getArg(i);
1868 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001869 // Create frame index.
1870 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001871 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001872 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001873 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001874
Duncan Sands276dcbd2008-03-21 09:14:45 +00001875 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001876 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001877 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001879 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001880 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001881 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001882
1883 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001884 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001885 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001886 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001887 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001888 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001889 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001890 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 }
1892 }
1893
1894 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001895 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001896 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001897
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001898 // Copy arguments to their registers.
1899 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001900 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001901 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001902 InFlag = Chain.getValue(1);
1903 }
Dan Gohman475871a2008-07-27 21:46:04 +00001904 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001905
Gordon Henriksen86737662008-01-05 16:56:59 +00001906 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001907 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001908 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001909 }
1910
Evan Cheng32fe1032006-05-25 00:59:30 +00001911 // If the callee is a GlobalAddress node (quite common, every direct call is)
1912 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001913 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001914 // We should use extra load for direct calls to dllimported functions in
1915 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001916 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1917 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001918 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1919 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001920 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1921 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001922 } else if (IsTailCall) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001923 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001924
Dale Johannesendd64c412009-02-04 00:33:20 +00001925 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001926 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001927 Callee,InFlag);
1928 Callee = DAG.getRegister(Opc, getPointerTy());
1929 // Add register as live out.
1930 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001931 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001932
Chris Lattnerd96d0722007-02-25 06:40:16 +00001933 // Returns a chain & a flag for retval copy to use.
1934 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001935 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001936
1937 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001938 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1939 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001940 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001941
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 // Returns a chain & a flag for retval copy to use.
1943 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1944 Ops.clear();
1945 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001946
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001947 Ops.push_back(Chain);
1948 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001949
Gordon Henriksen86737662008-01-05 16:56:59 +00001950 if (IsTailCall)
1951 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001952
Gordon Henriksen86737662008-01-05 16:56:59 +00001953 // Add argument registers to the end of the list so that they are known live
1954 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001955 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1956 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1957 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001958
Evan Cheng586ccac2008-03-18 23:36:35 +00001959 // Add an implicit use GOT pointer in EBX.
1960 if (!IsTailCall && !Is64Bit &&
1961 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1962 Subtarget->isPICStyleGOT())
1963 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1964
1965 // Add an implicit use of AL for x86 vararg functions.
1966 if (Is64Bit && isVarArg)
1967 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1968
Gabor Greifba36cb52008-08-28 21:40:38 +00001969 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001970 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001973 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001974 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001975 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001976 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001977
Gabor Greifba36cb52008-08-28 21:40:38 +00001978 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 }
1980
Dale Johannesenace16102009-02-03 19:33:06 +00001981 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001982 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001983
Chris Lattner2d297092006-05-23 18:50:38 +00001984 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001985 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001986 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001988 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001989 // If this is is a call to a struct-return function, the callee
1990 // pops the hidden struct pointer, so we have to push it back.
1991 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001992 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001994 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001995
Gordon Henriksenae636f82008-01-03 16:47:34 +00001996 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001997 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001998 DAG.getIntPtrConstant(NumBytes, true),
1999 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2000 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002001 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002002 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002003
Chris Lattner3085e152007-02-25 08:59:22 +00002004 // Handle result values, copying them out of physregs into vregs that we
2005 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00002006 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00002007 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002008}
2009
Evan Cheng25ab6902006-09-08 06:48:29 +00002010
2011//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002012// Fast Calling Convention (tail call) implementation
2013//===----------------------------------------------------------------------===//
2014
2015// Like std call, callee cleans arguments, convention except that ECX is
2016// reserved for storing the tail called function address. Only 2 registers are
2017// free for argument passing (inreg). Tail call optimization is performed
2018// provided:
2019// * tailcallopt is enabled
2020// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002021// On X86_64 architecture with GOT-style position independent code only local
2022// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002023// To keep the stack aligned according to platform abi the function
2024// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2025// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002026// If a tail called function callee has more arguments than the caller the
2027// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002028// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002029// original REtADDR, but before the saved framepointer or the spilled registers
2030// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2031// stack layout:
2032// arg1
2033// arg2
2034// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002035// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002036// move area ]
2037// (possible EBP)
2038// ESI
2039// EDI
2040// local1 ..
2041
2042/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2043/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002044unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002045 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002046 MachineFunction &MF = DAG.getMachineFunction();
2047 const TargetMachine &TM = MF.getTarget();
2048 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2049 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002050 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002051 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002052 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002053 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2054 // Number smaller than 12 so just add the difference.
2055 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2056 } else {
2057 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002058 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002059 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002060 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002061 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002062}
2063
2064/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00002065/// following the call is a return. A function is eligible if caller/callee
2066/// calling conventions match, currently only fastcc supports tail calls, and
2067/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00002068bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002070 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00002071 if (!PerformTailCallOpt)
2072 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002073
Dan Gohman095cc292008-09-13 01:54:27 +00002074 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002075 MachineFunction &MF = DAG.getMachineFunction();
2076 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00002077 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002078 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00002079 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002080 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00002081 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002082 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00002083 return true;
2084
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002085 // Can only do local tail calls (in same module, hidden or protected) on
2086 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2088 return G->getGlobal()->hasHiddenVisibility()
2089 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002090 }
2091 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00002092
2093 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002094}
2095
Dan Gohman3df24e62008-09-03 23:12:08 +00002096FastISel *
2097X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002098 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002099 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002100 DenseMap<const Value *, unsigned> &vm,
2101 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002102 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002103 DenseMap<const AllocaInst *, int> &am
2104#ifndef NDEBUG
2105 , SmallSet<Instruction*, 8> &cil
2106#endif
2107 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002108 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002109#ifndef NDEBUG
2110 , cil
2111#endif
2112 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002113}
2114
2115
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002116//===----------------------------------------------------------------------===//
2117// Other Lowering Hooks
2118//===----------------------------------------------------------------------===//
2119
2120
Dan Gohman475871a2008-07-27 21:46:04 +00002121SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002122 MachineFunction &MF = DAG.getMachineFunction();
2123 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2124 int ReturnAddrIndex = FuncInfo->getRAIndex();
2125
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002126 if (ReturnAddrIndex == 0) {
2127 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002128 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002129 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002130 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002131 }
2132
Evan Cheng25ab6902006-09-08 06:48:29 +00002133 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002134}
2135
2136
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002137/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2138/// specific condition code, returning the condition code and the LHS/RHS of the
2139/// comparison to make.
2140static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2141 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002142 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002143 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2144 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2145 // X > -1 -> X == 0, jump !sign.
2146 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002147 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002148 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2149 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002150 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002151 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002152 // X < 1 -> X <= 0
2153 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002154 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002155 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002156 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002157
Evan Chengd9558e02006-01-06 00:43:03 +00002158 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002159 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002160 case ISD::SETEQ: return X86::COND_E;
2161 case ISD::SETGT: return X86::COND_G;
2162 case ISD::SETGE: return X86::COND_GE;
2163 case ISD::SETLT: return X86::COND_L;
2164 case ISD::SETLE: return X86::COND_LE;
2165 case ISD::SETNE: return X86::COND_NE;
2166 case ISD::SETULT: return X86::COND_B;
2167 case ISD::SETUGT: return X86::COND_A;
2168 case ISD::SETULE: return X86::COND_BE;
2169 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002170 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002171 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002172
Chris Lattner4c78e022008-12-23 23:42:27 +00002173 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002174
Chris Lattner4c78e022008-12-23 23:42:27 +00002175 // If LHS is a foldable load, but RHS is not, flip the condition.
2176 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2177 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2178 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2179 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002180 }
2181
Chris Lattner4c78e022008-12-23 23:42:27 +00002182 switch (SetCCOpcode) {
2183 default: break;
2184 case ISD::SETOLT:
2185 case ISD::SETOLE:
2186 case ISD::SETUGT:
2187 case ISD::SETUGE:
2188 std::swap(LHS, RHS);
2189 break;
2190 }
2191
2192 // On a floating point condition, the flags are set as follows:
2193 // ZF PF CF op
2194 // 0 | 0 | 0 | X > Y
2195 // 0 | 0 | 1 | X < Y
2196 // 1 | 0 | 0 | X == Y
2197 // 1 | 1 | 1 | unordered
2198 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002199 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002200 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002201 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002202 case ISD::SETOLT: // flipped
2203 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002204 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002205 case ISD::SETOLE: // flipped
2206 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002207 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002208 case ISD::SETUGT: // flipped
2209 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002210 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002211 case ISD::SETUGE: // flipped
2212 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002213 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002214 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002215 case ISD::SETNE: return X86::COND_NE;
2216 case ISD::SETUO: return X86::COND_P;
2217 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002218 }
Evan Chengd9558e02006-01-06 00:43:03 +00002219}
2220
Evan Cheng4a460802006-01-11 00:33:36 +00002221/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2222/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002223/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002224static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002225 switch (X86CC) {
2226 default:
2227 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002228 case X86::COND_B:
2229 case X86::COND_BE:
2230 case X86::COND_E:
2231 case X86::COND_P:
2232 case X86::COND_A:
2233 case X86::COND_AE:
2234 case X86::COND_NE:
2235 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002236 return true;
2237 }
2238}
2239
Nate Begeman9008ca62009-04-27 18:41:29 +00002240/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2241/// the specified range (L, H].
2242static bool isUndefOrInRange(int Val, int Low, int Hi) {
2243 return (Val < 0) || (Val >= Low && Val < Hi);
2244}
2245
2246/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2247/// specified value.
2248static bool isUndefOrEqual(int Val, int CmpVal) {
2249 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002250 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002251 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002252}
2253
Nate Begeman9008ca62009-04-27 18:41:29 +00002254/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2255/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2256/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002257static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002258 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2259 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2260 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2261 return (Mask[0] < 2 && Mask[1] < 2);
2262 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002263}
2264
Nate Begeman9008ca62009-04-27 18:41:29 +00002265bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2266 SmallVector<int, 8> M;
2267 N->getMask(M);
2268 return ::isPSHUFDMask(M, N->getValueType(0));
2269}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002270
Nate Begeman9008ca62009-04-27 18:41:29 +00002271/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2272/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002273static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002274 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002275 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002276
2277 // Lower quadword copied in order or undef.
2278 for (int i = 0; i != 4; ++i)
2279 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002280 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002281
Evan Cheng506d3df2006-03-29 23:07:14 +00002282 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002283 for (int i = 4; i != 8; ++i)
2284 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002285 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002286
Evan Cheng506d3df2006-03-29 23:07:14 +00002287 return true;
2288}
2289
Nate Begeman9008ca62009-04-27 18:41:29 +00002290bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2291 SmallVector<int, 8> M;
2292 N->getMask(M);
2293 return ::isPSHUFHWMask(M, N->getValueType(0));
2294}
Evan Cheng506d3df2006-03-29 23:07:14 +00002295
Nate Begeman9008ca62009-04-27 18:41:29 +00002296/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2297/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002298static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002299 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002300 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002301
Rafael Espindola15684b22009-04-24 12:40:33 +00002302 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002303 for (int i = 4; i != 8; ++i)
2304 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002305 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002306
Rafael Espindola15684b22009-04-24 12:40:33 +00002307 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002308 for (int i = 0; i != 4; ++i)
2309 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002310 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002311
Rafael Espindola15684b22009-04-24 12:40:33 +00002312 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002313}
2314
Nate Begeman9008ca62009-04-27 18:41:29 +00002315bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2316 SmallVector<int, 8> M;
2317 N->getMask(M);
2318 return ::isPSHUFLWMask(M, N->getValueType(0));
2319}
2320
Evan Cheng14aed5e2006-03-24 01:18:28 +00002321/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2322/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002323static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002324 int NumElems = VT.getVectorNumElements();
2325 if (NumElems != 2 && NumElems != 4)
2326 return false;
2327
2328 int Half = NumElems / 2;
2329 for (int i = 0; i < Half; ++i)
2330 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002331 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002332 for (int i = Half; i < NumElems; ++i)
2333 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002334 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002335
Evan Cheng14aed5e2006-03-24 01:18:28 +00002336 return true;
2337}
2338
Nate Begeman9008ca62009-04-27 18:41:29 +00002339bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2340 SmallVector<int, 8> M;
2341 N->getMask(M);
2342 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002343}
2344
Evan Cheng213d2cf2007-05-17 18:45:50 +00002345/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002346/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2347/// half elements to come from vector 1 (which would equal the dest.) and
2348/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002349static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002350 int NumElems = VT.getVectorNumElements();
2351
2352 if (NumElems != 2 && NumElems != 4)
2353 return false;
2354
2355 int Half = NumElems / 2;
2356 for (int i = 0; i < Half; ++i)
2357 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002358 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002359 for (int i = Half; i < NumElems; ++i)
2360 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002361 return false;
2362 return true;
2363}
2364
Nate Begeman9008ca62009-04-27 18:41:29 +00002365static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2366 SmallVector<int, 8> M;
2367 N->getMask(M);
2368 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002369}
2370
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002371/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2372/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002373bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2374 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002375 return false;
2376
Evan Cheng2064a2b2006-03-28 06:50:32 +00002377 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002378 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2379 isUndefOrEqual(N->getMaskElt(1), 7) &&
2380 isUndefOrEqual(N->getMaskElt(2), 2) &&
2381 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002382}
2383
Evan Cheng5ced1d82006-04-06 23:23:56 +00002384/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2385/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002386bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2387 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002388
Evan Cheng5ced1d82006-04-06 23:23:56 +00002389 if (NumElems != 2 && NumElems != 4)
2390 return false;
2391
Evan Chengc5cdff22006-04-07 21:53:05 +00002392 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002393 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002394 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002395
Evan Chengc5cdff22006-04-07 21:53:05 +00002396 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002397 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002398 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002399
2400 return true;
2401}
2402
2403/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002404/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2405/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002406bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2407 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002408
Evan Cheng5ced1d82006-04-06 23:23:56 +00002409 if (NumElems != 2 && NumElems != 4)
2410 return false;
2411
Evan Chengc5cdff22006-04-07 21:53:05 +00002412 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002413 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002414 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002415
Nate Begeman9008ca62009-04-27 18:41:29 +00002416 for (unsigned i = 0; i < NumElems/2; ++i)
2417 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002418 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002419
2420 return true;
2421}
2422
Nate Begeman9008ca62009-04-27 18:41:29 +00002423/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2424/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2425/// <2, 3, 2, 3>
2426bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2427 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2428
2429 if (NumElems != 4)
2430 return false;
2431
2432 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2433 isUndefOrEqual(N->getMaskElt(1), 3) &&
2434 isUndefOrEqual(N->getMaskElt(2), 2) &&
2435 isUndefOrEqual(N->getMaskElt(3), 3);
2436}
2437
Evan Cheng0038e592006-03-28 00:39:58 +00002438/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2439/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002440static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002441 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002442 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002443 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002444 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002445
2446 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2447 int BitI = Mask[i];
2448 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002449 if (!isUndefOrEqual(BitI, j))
2450 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002451 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002452 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002453 return false;
2454 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002455 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002456 return false;
2457 }
Evan Cheng0038e592006-03-28 00:39:58 +00002458 }
Evan Cheng0038e592006-03-28 00:39:58 +00002459 return true;
2460}
2461
Nate Begeman9008ca62009-04-27 18:41:29 +00002462bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2463 SmallVector<int, 8> M;
2464 N->getMask(M);
2465 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002466}
2467
Evan Cheng4fcb9222006-03-28 02:43:26 +00002468/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2469/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002470static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002471 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002472 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002473 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002474 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002475
2476 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2477 int BitI = Mask[i];
2478 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002479 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002480 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002481 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002482 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002483 return false;
2484 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002485 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002486 return false;
2487 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002488 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002489 return true;
2490}
2491
Nate Begeman9008ca62009-04-27 18:41:29 +00002492bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2493 SmallVector<int, 8> M;
2494 N->getMask(M);
2495 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002496}
2497
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002498/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2499/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2500/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002501static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002502 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002503 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002504 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002505
2506 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2507 int BitI = Mask[i];
2508 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002509 if (!isUndefOrEqual(BitI, j))
2510 return false;
2511 if (!isUndefOrEqual(BitI1, j))
2512 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002513 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002514 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002515}
2516
Nate Begeman9008ca62009-04-27 18:41:29 +00002517bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2518 SmallVector<int, 8> M;
2519 N->getMask(M);
2520 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2521}
2522
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002523/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2524/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2525/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002526static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002527 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002528 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2529 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002530
2531 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2532 int BitI = Mask[i];
2533 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002534 if (!isUndefOrEqual(BitI, j))
2535 return false;
2536 if (!isUndefOrEqual(BitI1, j))
2537 return false;
2538 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002539 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002540}
2541
Nate Begeman9008ca62009-04-27 18:41:29 +00002542bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2543 SmallVector<int, 8> M;
2544 N->getMask(M);
2545 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2546}
2547
Evan Cheng017dcc62006-04-21 01:05:10 +00002548/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2549/// specifies a shuffle of elements that is suitable for input to MOVSS,
2550/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002551static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002552 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002553 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002554
2555 int NumElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002556
2557 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002558 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002559
2560 for (int i = 1; i < NumElts; ++i)
2561 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002562 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002563
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002564 return true;
2565}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002566
Nate Begeman9008ca62009-04-27 18:41:29 +00002567bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2568 SmallVector<int, 8> M;
2569 N->getMask(M);
2570 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002571}
2572
Evan Cheng017dcc62006-04-21 01:05:10 +00002573/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2574/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002575/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002576static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002577 bool V2IsSplat = false, bool V2IsUndef = false) {
2578 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002579 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002580 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002581
2582 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002583 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002584
2585 for (int i = 1; i < NumOps; ++i)
2586 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2587 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2588 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002589 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002590
Evan Cheng39623da2006-04-20 08:58:49 +00002591 return true;
2592}
2593
Nate Begeman9008ca62009-04-27 18:41:29 +00002594static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002595 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 SmallVector<int, 8> M;
2597 N->getMask(M);
2598 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002599}
2600
Evan Chengd9539472006-04-14 21:59:03 +00002601/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2602/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002603bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2604 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002605 return false;
2606
2607 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002608 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002609 int Elt = N->getMaskElt(i);
2610 if (Elt >= 0 && Elt != 1)
2611 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002612 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002613
2614 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002615 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002616 int Elt = N->getMaskElt(i);
2617 if (Elt >= 0 && Elt != 3)
2618 return false;
2619 if (Elt == 3)
2620 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002621 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002622 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002623 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002624 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002625}
2626
2627/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2628/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002629bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2630 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002631 return false;
2632
2633 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002634 for (unsigned i = 0; i < 2; ++i)
2635 if (N->getMaskElt(i) > 0)
2636 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002637
2638 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002639 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 int Elt = N->getMaskElt(i);
2641 if (Elt >= 0 && Elt != 2)
2642 return false;
2643 if (Elt == 2)
2644 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002645 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002647 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002648}
2649
Evan Cheng0b457f02008-09-25 20:50:48 +00002650/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2651/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002652bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2653 int e = N->getValueType(0).getVectorNumElements() / 2;
2654
2655 for (int i = 0; i < e; ++i)
2656 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002657 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 for (int i = 0; i < e; ++i)
2659 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002660 return false;
2661 return true;
2662}
2663
Evan Cheng63d33002006-03-22 08:01:21 +00002664/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2665/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2666/// instructions.
2667unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002668 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2669 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2670
Evan Chengb9df0ca2006-03-22 02:53:00 +00002671 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2672 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002673 for (int i = 0; i < NumOperands; ++i) {
2674 int Val = SVOp->getMaskElt(NumOperands-i-1);
2675 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002676 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002677 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002678 if (i != NumOperands - 1)
2679 Mask <<= Shift;
2680 }
Evan Cheng63d33002006-03-22 08:01:21 +00002681 return Mask;
2682}
2683
Evan Cheng506d3df2006-03-29 23:07:14 +00002684/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2685/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2686/// instructions.
2687unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002688 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002689 unsigned Mask = 0;
2690 // 8 nodes, but we only care about the last 4.
2691 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002692 int Val = SVOp->getMaskElt(i);
2693 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002694 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002695 if (i != 4)
2696 Mask <<= 2;
2697 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002698 return Mask;
2699}
2700
2701/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2702/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2703/// instructions.
2704unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002706 unsigned Mask = 0;
2707 // 8 nodes, but we only care about the first 4.
2708 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 int Val = SVOp->getMaskElt(i);
2710 if (Val >= 0)
2711 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002712 if (i != 0)
2713 Mask <<= 2;
2714 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002715 return Mask;
2716}
2717
Nate Begeman9008ca62009-04-27 18:41:29 +00002718/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2719/// their permute mask.
2720static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2721 SelectionDAG &DAG) {
2722 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002723 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002724 SmallVector<int, 8> MaskVec;
2725
Nate Begeman5a5ca152009-04-29 05:20:52 +00002726 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002727 int idx = SVOp->getMaskElt(i);
2728 if (idx < 0)
2729 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002730 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002731 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002732 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002733 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002734 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002735 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2736 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002737}
2738
Evan Cheng779ccea2007-12-07 21:30:01 +00002739/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2740/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002741static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002742 unsigned NumElems = VT.getVectorNumElements();
2743 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002744 int idx = Mask[i];
2745 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002746 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002747 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002748 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002749 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002750 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002751 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002752}
2753
Evan Cheng533a0aa2006-04-19 20:35:22 +00002754/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2755/// match movhlps. The lower half elements should come from upper half of
2756/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002757/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002758static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2759 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002760 return false;
2761 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002762 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002763 return false;
2764 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002765 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002766 return false;
2767 return true;
2768}
2769
Evan Cheng5ced1d82006-04-06 23:23:56 +00002770/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002771/// is promoted to a vector. It also returns the LoadSDNode by reference if
2772/// required.
2773static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002774 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2775 return false;
2776 N = N->getOperand(0).getNode();
2777 if (!ISD::isNON_EXTLoad(N))
2778 return false;
2779 if (LD)
2780 *LD = cast<LoadSDNode>(N);
2781 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002782}
2783
Evan Cheng533a0aa2006-04-19 20:35:22 +00002784/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2785/// match movlp{s|d}. The lower half elements should come from lower half of
2786/// V1 (and in order), and the upper half elements should come from the upper
2787/// half of V2 (and in order). And since V1 will become the source of the
2788/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002789static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2790 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002791 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002792 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002793 // Is V2 is a vector load, don't do this transformation. We will try to use
2794 // load folding shufps op.
2795 if (ISD::isNON_EXTLoad(V2))
2796 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002797
Nate Begeman5a5ca152009-04-29 05:20:52 +00002798 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002799
Evan Cheng533a0aa2006-04-19 20:35:22 +00002800 if (NumElems != 2 && NumElems != 4)
2801 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002802 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002804 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002805 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002806 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002807 return false;
2808 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002809}
2810
Evan Cheng39623da2006-04-20 08:58:49 +00002811/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2812/// all the same.
2813static bool isSplatVector(SDNode *N) {
2814 if (N->getOpcode() != ISD::BUILD_VECTOR)
2815 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002816
Dan Gohman475871a2008-07-27 21:46:04 +00002817 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002818 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2819 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002820 return false;
2821 return true;
2822}
2823
Evan Cheng213d2cf2007-05-17 18:45:50 +00002824/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2825/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002826static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002827 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002828 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002829 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002830 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002831}
2832
2833/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002834/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002835/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002836static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002837 SDValue V1 = N->getOperand(0);
2838 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002839 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2840 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002841 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002842 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002844 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2845 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002846 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2847 return false;
2848 } else if (Idx >= 0) {
2849 unsigned Opc = V1.getOpcode();
2850 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2851 continue;
2852 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002853 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002854 }
2855 }
2856 return true;
2857}
2858
2859/// getZeroVector - Returns a vector of specified type with all zero elements.
2860///
Dale Johannesenace16102009-02-03 19:33:06 +00002861static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2862 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002863 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002864
Chris Lattner8a594482007-11-25 00:24:49 +00002865 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2866 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002867 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002869 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002870 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002871 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002872 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002873 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002874 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002875 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002876 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002877 }
Dale Johannesenace16102009-02-03 19:33:06 +00002878 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002879}
2880
Chris Lattner8a594482007-11-25 00:24:49 +00002881/// getOnesVector - Returns a vector of specified type with all bits set.
2882///
Dale Johannesenace16102009-02-03 19:33:06 +00002883static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002885
Chris Lattner8a594482007-11-25 00:24:49 +00002886 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2887 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002888 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2889 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002890 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002891 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002892 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002893 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002894 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002895}
2896
2897
Evan Cheng39623da2006-04-20 08:58:49 +00002898/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2899/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002900static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2901 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002902 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002903
Evan Cheng39623da2006-04-20 08:58:49 +00002904 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002905 SmallVector<int, 8> MaskVec;
2906 SVOp->getMask(MaskVec);
2907
Nate Begeman5a5ca152009-04-29 05:20:52 +00002908 for (unsigned i = 0; i != NumElems; ++i) {
2909 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002910 MaskVec[i] = NumElems;
2911 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002912 }
Evan Cheng39623da2006-04-20 08:58:49 +00002913 }
Evan Cheng39623da2006-04-20 08:58:49 +00002914 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002915 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2916 SVOp->getOperand(1), &MaskVec[0]);
2917 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002918}
2919
Evan Cheng017dcc62006-04-21 01:05:10 +00002920/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2921/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002922static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2923 SDValue V2) {
2924 unsigned NumElems = VT.getVectorNumElements();
2925 SmallVector<int, 8> Mask;
2926 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002927 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002928 Mask.push_back(i);
2929 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002930}
2931
Nate Begeman9008ca62009-04-27 18:41:29 +00002932/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2933static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2934 SDValue V2) {
2935 unsigned NumElems = VT.getVectorNumElements();
2936 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002937 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 Mask.push_back(i);
2939 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002940 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002941 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002942}
2943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2945static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2946 SDValue V2) {
2947 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002948 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002950 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 Mask.push_back(i + Half);
2952 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002953 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002955}
2956
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002957/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002958static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2959 bool HasSSE2) {
2960 if (SV->getValueType(0).getVectorNumElements() <= 4)
2961 return SDValue(SV, 0);
2962
2963 MVT PVT = MVT::v4f32;
2964 MVT VT = SV->getValueType(0);
2965 DebugLoc dl = SV->getDebugLoc();
2966 SDValue V1 = SV->getOperand(0);
2967 int NumElems = VT.getVectorNumElements();
2968 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002969
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 // unpack elements to the correct location
2971 while (NumElems > 4) {
2972 if (EltNo < NumElems/2) {
2973 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2974 } else {
2975 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2976 EltNo -= NumElems/2;
2977 }
2978 NumElems >>= 1;
2979 }
2980
2981 // Perform the splat.
2982 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002983 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002984 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2985 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002986}
2987
Evan Chengba05f722006-04-21 23:03:30 +00002988/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002989/// vector of zero or undef vector. This produces a shuffle where the low
2990/// element of V2 is swizzled into the zero/undef vector, landing at element
2991/// 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 +00002992static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002993 bool isZero, bool HasSSE2,
2994 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002995 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002996 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002997 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2998 unsigned NumElems = VT.getVectorNumElements();
2999 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003000 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 // If this is the insertion idx, put the low elt of V2 here.
3002 MaskVec.push_back(i == Idx ? NumElems : i);
3003 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003004}
3005
Evan Chengf26ffe92008-05-29 08:22:04 +00003006/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3007/// a shuffle that is zero.
3008static
Nate Begeman9008ca62009-04-27 18:41:29 +00003009unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3010 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003011 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003012 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003013 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 int Idx = SVOp->getMaskElt(Index);
3015 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003016 ++NumZeros;
3017 continue;
3018 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003019 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00003020 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003021 ++NumZeros;
3022 else
3023 break;
3024 }
3025 return NumZeros;
3026}
3027
3028/// isVectorShift - Returns true if the shuffle can be implemented as a
3029/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003030/// FIXME: split into pslldqi, psrldqi, palignr variants.
3031static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003032 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003034
3035 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003037 if (!NumZeros) {
3038 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003040 if (!NumZeros)
3041 return false;
3042 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003043 bool SeenV1 = false;
3044 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003045 for (int i = NumZeros; i < NumElems; ++i) {
3046 int Val = isLeft ? (i - NumZeros) : i;
3047 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3048 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003049 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003051 SeenV1 = true;
3052 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003053 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003054 SeenV2 = true;
3055 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003057 return false;
3058 }
3059 if (SeenV1 && SeenV2)
3060 return false;
3061
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003063 ShAmt = NumZeros;
3064 return true;
3065}
3066
3067
Evan Chengc78d3b42006-04-24 18:01:45 +00003068/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3069///
Dan Gohman475871a2008-07-27 21:46:04 +00003070static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003071 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003072 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003073 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003074 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003075
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003076 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003078 bool First = true;
3079 for (unsigned i = 0; i < 16; ++i) {
3080 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3081 if (ThisIsNonZero && First) {
3082 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003083 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003084 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003085 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003086 First = false;
3087 }
3088
3089 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003090 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003091 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3092 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003093 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003094 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003095 }
3096 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003097 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3098 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00003099 ThisElt, DAG.getConstant(8, MVT::i8));
3100 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003101 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003102 } else
3103 ThisElt = LastElt;
3104
Gabor Greifba36cb52008-08-28 21:40:38 +00003105 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003106 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003107 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003108 }
3109 }
3110
Dale Johannesenace16102009-02-03 19:33:06 +00003111 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003112}
3113
Bill Wendlinga348c562007-03-22 18:42:45 +00003114/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003115///
Dan Gohman475871a2008-07-27 21:46:04 +00003116static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003117 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003118 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003119 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003120 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003121
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003122 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003124 bool First = true;
3125 for (unsigned i = 0; i < 8; ++i) {
3126 bool isNonZero = (NonZeros & (1 << i)) != 0;
3127 if (isNonZero) {
3128 if (First) {
3129 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003130 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003131 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003132 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003133 First = false;
3134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003135 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003136 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003137 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003138 }
3139 }
3140
3141 return V;
3142}
3143
Evan Chengf26ffe92008-05-29 08:22:04 +00003144/// getVShift - Return a vector logical shift node.
3145///
Dan Gohman475871a2008-07-27 21:46:04 +00003146static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 unsigned NumBits, SelectionDAG &DAG,
3148 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 bool isMMX = VT.getSizeInBits() == 64;
3150 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003151 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003152 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3153 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3154 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003155 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003156}
3157
Dan Gohman475871a2008-07-27 21:46:04 +00003158SDValue
3159X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003160 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003161 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003162 if (ISD::isBuildVectorAllZeros(Op.getNode())
3163 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003164 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3165 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3166 // eliminated on x86-32 hosts.
3167 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3168 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003169
Gabor Greifba36cb52008-08-28 21:40:38 +00003170 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003171 return getOnesVector(Op.getValueType(), DAG, dl);
3172 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003173 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003174
Duncan Sands83ec4b62008-06-06 12:08:01 +00003175 MVT VT = Op.getValueType();
3176 MVT EVT = VT.getVectorElementType();
3177 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003178
3179 unsigned NumElems = Op.getNumOperands();
3180 unsigned NumZero = 0;
3181 unsigned NumNonZero = 0;
3182 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003183 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003184 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003185 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003186 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003187 if (Elt.getOpcode() == ISD::UNDEF)
3188 continue;
3189 Values.insert(Elt);
3190 if (Elt.getOpcode() != ISD::Constant &&
3191 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003192 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003193 if (isZeroNode(Elt))
3194 NumZero++;
3195 else {
3196 NonZeros |= (1 << i);
3197 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003198 }
3199 }
3200
Dan Gohman7f321562007-06-25 16:23:39 +00003201 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003202 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003203 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003204 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003205
Chris Lattner67f453a2008-03-09 05:42:06 +00003206 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003207 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003208 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003210
Chris Lattner62098042008-03-09 01:05:04 +00003211 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3212 // the value are obviously zero, truncate the value to i32 and do the
3213 // insertion that way. Only do this if the value is non-constant or if the
3214 // value is a constant being inserted into element 0. It is cheaper to do
3215 // a constant pool load than it is to do a movd + shuffle.
3216 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3217 (!IsAllConstants || Idx == 0)) {
3218 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3219 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003220 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3221 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003222
Chris Lattner62098042008-03-09 01:05:04 +00003223 // Truncate the value (which may itself be a constant) to i32, and
3224 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003225 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3226 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003227 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3228 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003229
Chris Lattner62098042008-03-09 01:05:04 +00003230 // Now we have our 32-bit value zero extended in the low element of
3231 // a vector. If Idx != 0, swizzle it into place.
3232 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 SmallVector<int, 4> Mask;
3234 Mask.push_back(Idx);
3235 for (unsigned i = 1; i != VecElts; ++i)
3236 Mask.push_back(i);
3237 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3238 DAG.getUNDEF(Item.getValueType()),
3239 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003240 }
Dale Johannesenace16102009-02-03 19:33:06 +00003241 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003242 }
3243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003244
Chris Lattner19f79692008-03-08 22:59:52 +00003245 // If we have a constant or non-constant insertion into the low element of
3246 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3247 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003248 // depending on what the source datatype is.
3249 if (Idx == 0) {
3250 if (NumZero == 0) {
3251 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3252 } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 ||
3253 (EVT == MVT::i64 && Subtarget->is64Bit())) {
3254 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3255 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3256 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3257 DAG);
3258 } else if (EVT == MVT::i16 || EVT == MVT::i8) {
3259 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3260 MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
3261 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3262 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3263 Subtarget->hasSSE2(), DAG);
3264 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3265 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003266 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003267
3268 // Is it a vector logical left shift?
3269 if (NumElems == 2 && Idx == 1 &&
3270 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003271 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003272 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003273 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003274 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003275 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003276 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003277
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003278 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003279 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003280
Chris Lattner19f79692008-03-08 22:59:52 +00003281 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3282 // is a non-constant being inserted into an element other than the low one,
3283 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3284 // movd/movss) to move this into the low element, then shuffle it into
3285 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003286 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003287 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003288
Evan Cheng0db9fe62006-04-25 20:13:52 +00003289 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003290 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3291 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003293 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003294 MaskVec.push_back(i == Idx ? 0 : 1);
3295 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003296 }
3297 }
3298
Chris Lattner67f453a2008-03-09 05:42:06 +00003299 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3300 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003301 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003302
Dan Gohmana3941172007-07-24 22:55:08 +00003303 // A vector full of immediates; various special cases are already
3304 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003305 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003306 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003307
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003308 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003309 if (EVTBits == 64) {
3310 if (NumNonZero == 1) {
3311 // One half is zero or undef.
3312 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003313 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003314 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003315 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3316 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003317 }
Dan Gohman475871a2008-07-27 21:46:04 +00003318 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003319 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003320
3321 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003322 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003323 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003324 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003325 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003326 }
3327
Bill Wendling826f36f2007-03-28 00:57:11 +00003328 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003329 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003330 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003331 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003332 }
3333
3334 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003335 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003336 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003337 if (NumElems == 4 && NumZero > 0) {
3338 for (unsigned i = 0; i < 4; ++i) {
3339 bool isZero = !(NonZeros & (1 << i));
3340 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003341 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003342 else
Dale Johannesenace16102009-02-03 19:33:06 +00003343 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003344 }
3345
3346 for (unsigned i = 0; i < 2; ++i) {
3347 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3348 default: break;
3349 case 0:
3350 V[i] = V[i*2]; // Must be a zero vector.
3351 break;
3352 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003353 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003354 break;
3355 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003357 break;
3358 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003360 break;
3361 }
3362 }
3363
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003365 bool Reverse = (NonZeros & 0x3) == 2;
3366 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003367 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003368 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3369 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003370 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3371 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003372 }
3373
3374 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3376 // values to be inserted is equal to the number of elements, in which case
3377 // use the unpack code below in the hopes of matching the consecutive elts
3378 // load merge pattern for shuffles.
3379 // FIXME: We could probably just check that here directly.
3380 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3381 getSubtarget()->hasSSE41()) {
3382 V[0] = DAG.getUNDEF(VT);
3383 for (unsigned i = 0; i < NumElems; ++i)
3384 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3385 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3386 Op.getOperand(i), DAG.getIntPtrConstant(i));
3387 return V[0];
3388 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003389 // Expand into a number of unpckl*.
3390 // e.g. for v4f32
3391 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3392 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3393 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003394 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003395 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003396 NumElems >>= 1;
3397 while (NumElems != 0) {
3398 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003400 NumElems >>= 1;
3401 }
3402 return V[0];
3403 }
3404
Dan Gohman475871a2008-07-27 21:46:04 +00003405 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003406}
3407
Nate Begemanb9a47b82009-02-23 08:49:38 +00003408// v8i16 shuffles - Prefer shuffles in the following order:
3409// 1. [all] pshuflw, pshufhw, optional move
3410// 2. [ssse3] 1 x pshufb
3411// 3. [ssse3] 2 x pshufb + 1 x por
3412// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003413static
Nate Begeman9008ca62009-04-27 18:41:29 +00003414SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3415 SelectionDAG &DAG, X86TargetLowering &TLI) {
3416 SDValue V1 = SVOp->getOperand(0);
3417 SDValue V2 = SVOp->getOperand(1);
3418 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003419 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003420
Nate Begemanb9a47b82009-02-23 08:49:38 +00003421 // Determine if more than 1 of the words in each of the low and high quadwords
3422 // of the result come from the same quadword of one of the two inputs. Undef
3423 // mask values count as coming from any quadword, for better codegen.
3424 SmallVector<unsigned, 4> LoQuad(4);
3425 SmallVector<unsigned, 4> HiQuad(4);
3426 BitVector InputQuads(4);
3427 for (unsigned i = 0; i < 8; ++i) {
3428 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003430 MaskVals.push_back(EltIdx);
3431 if (EltIdx < 0) {
3432 ++Quad[0];
3433 ++Quad[1];
3434 ++Quad[2];
3435 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003436 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003437 }
3438 ++Quad[EltIdx / 4];
3439 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003440 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003441
Nate Begemanb9a47b82009-02-23 08:49:38 +00003442 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003443 unsigned MaxQuad = 1;
3444 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003445 if (LoQuad[i] > MaxQuad) {
3446 BestLoQuad = i;
3447 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003448 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003449 }
3450
Nate Begemanb9a47b82009-02-23 08:49:38 +00003451 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003452 MaxQuad = 1;
3453 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003454 if (HiQuad[i] > MaxQuad) {
3455 BestHiQuad = i;
3456 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003457 }
3458 }
3459
Nate Begemanb9a47b82009-02-23 08:49:38 +00003460 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3461 // of the two input vectors, shuffle them into one input vector so only a
3462 // single pshufb instruction is necessary. If There are more than 2 input
3463 // quads, disable the next transformation since it does not help SSSE3.
3464 bool V1Used = InputQuads[0] || InputQuads[1];
3465 bool V2Used = InputQuads[2] || InputQuads[3];
3466 if (TLI.getSubtarget()->hasSSSE3()) {
3467 if (InputQuads.count() == 2 && V1Used && V2Used) {
3468 BestLoQuad = InputQuads.find_first();
3469 BestHiQuad = InputQuads.find_next(BestLoQuad);
3470 }
3471 if (InputQuads.count() > 2) {
3472 BestLoQuad = -1;
3473 BestHiQuad = -1;
3474 }
3475 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003476
Nate Begemanb9a47b82009-02-23 08:49:38 +00003477 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3478 // the shuffle mask. If a quad is scored as -1, that means that it contains
3479 // words from all 4 input quadwords.
3480 SDValue NewV;
3481 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 SmallVector<int, 8> MaskV;
3483 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3484 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3485 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3486 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3487 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003488 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003489
Nate Begemanb9a47b82009-02-23 08:49:38 +00003490 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3491 // source words for the shuffle, to aid later transformations.
3492 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003493 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003494 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003495 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003496 if (idx != (int)i)
3497 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003498 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003499 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003500 AllWordsInNewV = false;
3501 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003502 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003503
Nate Begemanb9a47b82009-02-23 08:49:38 +00003504 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3505 if (AllWordsInNewV) {
3506 for (int i = 0; i != 8; ++i) {
3507 int idx = MaskVals[i];
3508 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003509 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003510 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3511 if ((idx != i) && idx < 4)
3512 pshufhw = false;
3513 if ((idx != i) && idx > 3)
3514 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003515 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003516 V1 = NewV;
3517 V2Used = false;
3518 BestLoQuad = 0;
3519 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003520 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003521
Nate Begemanb9a47b82009-02-23 08:49:38 +00003522 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3523 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003524 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003525 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3526 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003527 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003528 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003529
3530 // If we have SSSE3, and all words of the result are from 1 input vector,
3531 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3532 // is present, fall back to case 4.
3533 if (TLI.getSubtarget()->hasSSSE3()) {
3534 SmallVector<SDValue,16> pshufbMask;
3535
3536 // If we have elements from both input vectors, set the high bit of the
3537 // shuffle mask element to zero out elements that come from V2 in the V1
3538 // mask, and elements that come from V1 in the V2 mask, so that the two
3539 // results can be OR'd together.
3540 bool TwoInputs = V1Used && V2Used;
3541 for (unsigned i = 0; i != 8; ++i) {
3542 int EltIdx = MaskVals[i] * 2;
3543 if (TwoInputs && (EltIdx >= 16)) {
3544 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3545 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3546 continue;
3547 }
3548 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3549 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3550 }
3551 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3552 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003553 DAG.getNode(ISD::BUILD_VECTOR, dl,
3554 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003555 if (!TwoInputs)
3556 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3557
3558 // Calculate the shuffle mask for the second input, shuffle it, and
3559 // OR it with the first shuffled input.
3560 pshufbMask.clear();
3561 for (unsigned i = 0; i != 8; ++i) {
3562 int EltIdx = MaskVals[i] * 2;
3563 if (EltIdx < 16) {
3564 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3565 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3566 continue;
3567 }
3568 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3569 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3570 }
3571 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3572 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003573 DAG.getNode(ISD::BUILD_VECTOR, dl,
3574 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003575 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3576 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3577 }
3578
3579 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3580 // and update MaskVals with new element order.
3581 BitVector InOrder(8);
3582 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003584 for (int i = 0; i != 4; ++i) {
3585 int idx = MaskVals[i];
3586 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003587 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003588 InOrder.set(i);
3589 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003590 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003591 InOrder.set(i);
3592 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003593 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003594 }
3595 }
3596 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003597 MaskV.push_back(i);
3598 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3599 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003600 }
3601
3602 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3603 // and update MaskVals with the new element order.
3604 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003605 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003606 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003607 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003608 for (unsigned i = 4; i != 8; ++i) {
3609 int idx = MaskVals[i];
3610 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003612 InOrder.set(i);
3613 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003615 InOrder.set(i);
3616 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003617 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003618 }
3619 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3621 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003622 }
3623
3624 // In case BestHi & BestLo were both -1, which means each quadword has a word
3625 // from each of the four input quadwords, calculate the InOrder bitvector now
3626 // before falling through to the insert/extract cleanup.
3627 if (BestLoQuad == -1 && BestHiQuad == -1) {
3628 NewV = V1;
3629 for (int i = 0; i != 8; ++i)
3630 if (MaskVals[i] < 0 || MaskVals[i] == i)
3631 InOrder.set(i);
3632 }
3633
3634 // The other elements are put in the right place using pextrw and pinsrw.
3635 for (unsigned i = 0; i != 8; ++i) {
3636 if (InOrder[i])
3637 continue;
3638 int EltIdx = MaskVals[i];
3639 if (EltIdx < 0)
3640 continue;
3641 SDValue ExtOp = (EltIdx < 8)
3642 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3643 DAG.getIntPtrConstant(EltIdx))
3644 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3645 DAG.getIntPtrConstant(EltIdx - 8));
3646 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3647 DAG.getIntPtrConstant(i));
3648 }
3649 return NewV;
3650}
3651
3652// v16i8 shuffles - Prefer shuffles in the following order:
3653// 1. [ssse3] 1 x pshufb
3654// 2. [ssse3] 2 x pshufb + 1 x por
3655// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3656static
Nate Begeman9008ca62009-04-27 18:41:29 +00003657SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3658 SelectionDAG &DAG, X86TargetLowering &TLI) {
3659 SDValue V1 = SVOp->getOperand(0);
3660 SDValue V2 = SVOp->getOperand(1);
3661 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003662 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003663 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003664
3665 // If we have SSSE3, case 1 is generated when all result bytes come from
3666 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3667 // present, fall back to case 3.
3668 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3669 bool V1Only = true;
3670 bool V2Only = true;
3671 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003672 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003673 if (EltIdx < 0)
3674 continue;
3675 if (EltIdx < 16)
3676 V2Only = false;
3677 else
3678 V1Only = false;
3679 }
3680
3681 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3682 if (TLI.getSubtarget()->hasSSSE3()) {
3683 SmallVector<SDValue,16> pshufbMask;
3684
3685 // If all result elements are from one input vector, then only translate
3686 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3687 //
3688 // Otherwise, we have elements from both input vectors, and must zero out
3689 // elements that come from V2 in the first mask, and V1 in the second mask
3690 // so that we can OR them together.
3691 bool TwoInputs = !(V1Only || V2Only);
3692 for (unsigned i = 0; i != 16; ++i) {
3693 int EltIdx = MaskVals[i];
3694 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3695 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3696 continue;
3697 }
3698 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3699 }
3700 // If all the elements are from V2, assign it to V1 and return after
3701 // building the first pshufb.
3702 if (V2Only)
3703 V1 = V2;
3704 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003705 DAG.getNode(ISD::BUILD_VECTOR, dl,
3706 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003707 if (!TwoInputs)
3708 return V1;
3709
3710 // Calculate the shuffle mask for the second input, shuffle it, and
3711 // OR it with the first shuffled input.
3712 pshufbMask.clear();
3713 for (unsigned i = 0; i != 16; ++i) {
3714 int EltIdx = MaskVals[i];
3715 if (EltIdx < 16) {
3716 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3717 continue;
3718 }
3719 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3720 }
3721 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003722 DAG.getNode(ISD::BUILD_VECTOR, dl,
3723 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003724 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3725 }
3726
3727 // No SSSE3 - Calculate in place words and then fix all out of place words
3728 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3729 // the 16 different words that comprise the two doublequadword input vectors.
3730 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3731 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3732 SDValue NewV = V2Only ? V2 : V1;
3733 for (int i = 0; i != 8; ++i) {
3734 int Elt0 = MaskVals[i*2];
3735 int Elt1 = MaskVals[i*2+1];
3736
3737 // This word of the result is all undef, skip it.
3738 if (Elt0 < 0 && Elt1 < 0)
3739 continue;
3740
3741 // This word of the result is already in the correct place, skip it.
3742 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3743 continue;
3744 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3745 continue;
3746
3747 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3748 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3749 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003750
3751 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3752 // using a single extract together, load it and store it.
3753 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3754 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3755 DAG.getIntPtrConstant(Elt1 / 2));
3756 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3757 DAG.getIntPtrConstant(i));
3758 continue;
3759 }
3760
Nate Begemanb9a47b82009-02-23 08:49:38 +00003761 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003762 // source byte is not also odd, shift the extracted word left 8 bits
3763 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003764 if (Elt1 >= 0) {
3765 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3766 DAG.getIntPtrConstant(Elt1 / 2));
3767 if ((Elt1 & 1) == 0)
3768 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3769 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003770 else if (Elt0 >= 0)
3771 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3772 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003773 }
3774 // If Elt0 is defined, extract it from the appropriate source. If the
3775 // source byte is not also even, shift the extracted word right 8 bits. If
3776 // Elt1 was also defined, OR the extracted values together before
3777 // inserting them in the result.
3778 if (Elt0 >= 0) {
3779 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3780 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3781 if ((Elt0 & 1) != 0)
3782 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3783 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003784 else if (Elt1 >= 0)
3785 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3786 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003787 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3788 : InsElt0;
3789 }
3790 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3791 DAG.getIntPtrConstant(i));
3792 }
3793 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003794}
3795
Evan Cheng7a831ce2007-12-15 03:00:47 +00003796/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3797/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3798/// done when every pair / quad of shuffle mask elements point to elements in
3799/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003800/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3801static
Nate Begeman9008ca62009-04-27 18:41:29 +00003802SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3803 SelectionDAG &DAG,
3804 TargetLowering &TLI, DebugLoc dl) {
3805 MVT VT = SVOp->getValueType(0);
3806 SDValue V1 = SVOp->getOperand(0);
3807 SDValue V2 = SVOp->getOperand(1);
3808 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003809 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003810 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003811 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003812 MVT NewVT = MaskVT;
3813 switch (VT.getSimpleVT()) {
3814 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003815 case MVT::v4f32: NewVT = MVT::v2f64; break;
3816 case MVT::v4i32: NewVT = MVT::v2i64; break;
3817 case MVT::v8i16: NewVT = MVT::v4i32; break;
3818 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003819 }
3820
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003821 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003823 NewVT = MVT::v2i64;
3824 else
3825 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003826 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 int Scale = NumElems / NewWidth;
3828 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003829 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 int StartIdx = -1;
3831 for (int j = 0; j < Scale; ++j) {
3832 int EltIdx = SVOp->getMaskElt(i+j);
3833 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003834 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003835 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003836 StartIdx = EltIdx - (EltIdx % Scale);
3837 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003838 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003839 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003840 if (StartIdx == -1)
3841 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003842 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003844 }
3845
Dale Johannesenace16102009-02-03 19:33:06 +00003846 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3847 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003849}
3850
Evan Chengd880b972008-05-09 21:53:03 +00003851/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003852///
Dan Gohman475871a2008-07-27 21:46:04 +00003853static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003854 SDValue SrcOp, SelectionDAG &DAG,
3855 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003856 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3857 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003858 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003859 LD = dyn_cast<LoadSDNode>(SrcOp);
3860 if (!LD) {
3861 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3862 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003863 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003864 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3865 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3866 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3867 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3868 // PR2108
3869 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003870 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3871 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3872 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3873 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003874 SrcOp.getOperand(0)
3875 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003876 }
3877 }
3878 }
3879
Dale Johannesenace16102009-02-03 19:33:06 +00003880 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3881 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003882 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003883 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003884}
3885
Evan Chengace3c172008-07-22 21:13:36 +00003886/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3887/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003888static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003889LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3890 SDValue V1 = SVOp->getOperand(0);
3891 SDValue V2 = SVOp->getOperand(1);
3892 DebugLoc dl = SVOp->getDebugLoc();
3893 MVT VT = SVOp->getValueType(0);
3894
Evan Chengace3c172008-07-22 21:13:36 +00003895 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003896 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 SmallVector<int, 8> Mask1(4U, -1);
3898 SmallVector<int, 8> PermMask;
3899 SVOp->getMask(PermMask);
3900
Evan Chengace3c172008-07-22 21:13:36 +00003901 unsigned NumHi = 0;
3902 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003903 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 int Idx = PermMask[i];
3905 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003906 Locs[i] = std::make_pair(-1, -1);
3907 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003908 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3909 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003910 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003912 NumLo++;
3913 } else {
3914 Locs[i] = std::make_pair(1, NumHi);
3915 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003916 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003917 NumHi++;
3918 }
3919 }
3920 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003921
Evan Chengace3c172008-07-22 21:13:36 +00003922 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003923 // If no more than two elements come from either vector. This can be
3924 // implemented with two shuffles. First shuffle gather the elements.
3925 // The second shuffle, which takes the first shuffle as both of its
3926 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003928
Nate Begeman9008ca62009-04-27 18:41:29 +00003929 SmallVector<int, 8> Mask2(4U, -1);
3930
Evan Chengace3c172008-07-22 21:13:36 +00003931 for (unsigned i = 0; i != 4; ++i) {
3932 if (Locs[i].first == -1)
3933 continue;
3934 else {
3935 unsigned Idx = (i < 2) ? 0 : 4;
3936 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003938 }
3939 }
3940
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003942 } else if (NumLo == 3 || NumHi == 3) {
3943 // Otherwise, we must have three elements from one vector, call it X, and
3944 // one element from the other, call it Y. First, use a shufps to build an
3945 // intermediate vector with the one element from Y and the element from X
3946 // that will be in the same half in the final destination (the indexes don't
3947 // matter). Then, use a shufps to build the final vector, taking the half
3948 // containing the element from Y from the intermediate, and the other half
3949 // from X.
3950 if (NumHi == 3) {
3951 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003953 std::swap(V1, V2);
3954 }
3955
3956 // Find the element from V2.
3957 unsigned HiIndex;
3958 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 int Val = PermMask[HiIndex];
3960 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003961 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003962 if (Val >= 4)
3963 break;
3964 }
3965
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 Mask1[0] = PermMask[HiIndex];
3967 Mask1[1] = -1;
3968 Mask1[2] = PermMask[HiIndex^1];
3969 Mask1[3] = -1;
3970 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003971
3972 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003973 Mask1[0] = PermMask[0];
3974 Mask1[1] = PermMask[1];
3975 Mask1[2] = HiIndex & 1 ? 6 : 4;
3976 Mask1[3] = HiIndex & 1 ? 4 : 6;
3977 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003978 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003979 Mask1[0] = HiIndex & 1 ? 2 : 0;
3980 Mask1[1] = HiIndex & 1 ? 0 : 2;
3981 Mask1[2] = PermMask[2];
3982 Mask1[3] = PermMask[3];
3983 if (Mask1[2] >= 0)
3984 Mask1[2] += 4;
3985 if (Mask1[3] >= 0)
3986 Mask1[3] += 4;
3987 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003988 }
Evan Chengace3c172008-07-22 21:13:36 +00003989 }
3990
3991 // Break it into (shuffle shuffle_hi, shuffle_lo).
3992 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003993 SmallVector<int,8> LoMask(4U, -1);
3994 SmallVector<int,8> HiMask(4U, -1);
3995
3996 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003997 unsigned MaskIdx = 0;
3998 unsigned LoIdx = 0;
3999 unsigned HiIdx = 2;
4000 for (unsigned i = 0; i != 4; ++i) {
4001 if (i == 2) {
4002 MaskPtr = &HiMask;
4003 MaskIdx = 1;
4004 LoIdx = 0;
4005 HiIdx = 2;
4006 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004007 int Idx = PermMask[i];
4008 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004009 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004010 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004011 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004012 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004013 LoIdx++;
4014 } else {
4015 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004016 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004017 HiIdx++;
4018 }
4019 }
4020
Nate Begeman9008ca62009-04-27 18:41:29 +00004021 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4022 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4023 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004024 for (unsigned i = 0; i != 4; ++i) {
4025 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004026 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004027 } else {
4028 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004029 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004030 }
4031 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004032 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004033}
4034
Dan Gohman475871a2008-07-27 21:46:04 +00004035SDValue
4036X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004038 SDValue V1 = Op.getOperand(0);
4039 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004040 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004041 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004042 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004043 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004044 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4045 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004046 bool V1IsSplat = false;
4047 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004048
Nate Begeman9008ca62009-04-27 18:41:29 +00004049 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004050 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004051
Nate Begeman9008ca62009-04-27 18:41:29 +00004052 // Promote splats to v4f32.
4053 if (SVOp->isSplat()) {
4054 if (isMMX || NumElems < 4)
4055 return Op;
4056 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004057 }
4058
Evan Cheng7a831ce2007-12-15 03:00:47 +00004059 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4060 // do it!
4061 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004062 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004063 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004064 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004065 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00004066 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4067 // FIXME: Figure out a cleaner way to do this.
4068 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004069 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004070 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004071 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004072 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4073 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4074 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004075 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004076 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004077 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4078 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004079 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004080 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004081 }
4082 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004083
4084 if (X86::isPSHUFDMask(SVOp))
4085 return Op;
4086
Evan Chengf26ffe92008-05-29 08:22:04 +00004087 // Check if this can be converted into a logical shift.
4088 bool isLeft = false;
4089 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004090 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004091 bool isShift = getSubtarget()->hasSSE2() &&
4092 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004093 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004094 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004095 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004096 MVT EVT = VT.getVectorElementType();
4097 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004098 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004099 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004100
4101 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004102 if (V1IsUndef)
4103 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004104 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004105 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004106 if (!isMMX)
4107 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004108 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004109
4110 // FIXME: fold these into legal mask.
4111 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4112 X86::isMOVSLDUPMask(SVOp) ||
4113 X86::isMOVHLPSMask(SVOp) ||
4114 X86::isMOVHPMask(SVOp) ||
4115 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004116 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004117
Nate Begeman9008ca62009-04-27 18:41:29 +00004118 if (ShouldXformToMOVHLPS(SVOp) ||
4119 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4120 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004121
Evan Chengf26ffe92008-05-29 08:22:04 +00004122 if (isShift) {
4123 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 MVT EVT = VT.getVectorElementType();
4125 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004126 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004127 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004128
Evan Cheng9eca5e82006-10-25 21:49:50 +00004129 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004130 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4131 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 V1IsSplat = isSplatVector(V1.getNode());
4133 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004134
Chris Lattner8a594482007-11-25 00:24:49 +00004135 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004136 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 Op = CommuteVectorShuffle(SVOp, DAG);
4138 SVOp = cast<ShuffleVectorSDNode>(Op);
4139 V1 = SVOp->getOperand(0);
4140 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004141 std::swap(V1IsSplat, V2IsSplat);
4142 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004143 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004144 }
4145
Nate Begeman9008ca62009-04-27 18:41:29 +00004146 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4147 // Shuffling low element of v1 into undef, just return v1.
4148 if (V2IsUndef)
4149 return V1;
4150 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4151 // the instruction selector will not match, so get a canonical MOVL with
4152 // swapped operands to undo the commute.
4153 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004154 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155
Nate Begeman9008ca62009-04-27 18:41:29 +00004156 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4157 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4158 X86::isUNPCKLMask(SVOp) ||
4159 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004160 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004161
Evan Cheng9bbbb982006-10-25 20:48:19 +00004162 if (V2IsSplat) {
4163 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004164 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004165 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 SDValue NewMask = NormalizeMask(SVOp, DAG);
4167 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4168 if (NSVOp != SVOp) {
4169 if (X86::isUNPCKLMask(NSVOp, true)) {
4170 return NewMask;
4171 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4172 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004173 }
4174 }
4175 }
4176
Evan Cheng9eca5e82006-10-25 21:49:50 +00004177 if (Commuted) {
4178 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004179 // FIXME: this seems wrong.
4180 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4181 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4182 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4183 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4184 X86::isUNPCKLMask(NewSVOp) ||
4185 X86::isUNPCKHMask(NewSVOp))
4186 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004187 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004188
Nate Begemanb9a47b82009-02-23 08:49:38 +00004189 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004190
4191 // Normalize the node to match x86 shuffle ops if needed
4192 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4193 return CommuteVectorShuffle(SVOp, DAG);
4194
4195 // Check for legal shuffle and return?
4196 SmallVector<int, 16> PermMask;
4197 SVOp->getMask(PermMask);
4198 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004199 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004200
Evan Cheng14b32e12007-12-11 01:46:18 +00004201 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4202 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004203 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004204 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004205 return NewOp;
4206 }
4207
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004210 if (NewOp.getNode())
4211 return NewOp;
4212 }
4213
Evan Chengace3c172008-07-22 21:13:36 +00004214 // Handle all 4 wide cases with a number of shuffles except for MMX.
4215 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004216 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004217
Dan Gohman475871a2008-07-27 21:46:04 +00004218 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004219}
4220
Dan Gohman475871a2008-07-27 21:46:04 +00004221SDValue
4222X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004223 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004224 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004225 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004226 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004227 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004228 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004229 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004230 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004231 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004232 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004233 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4234 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4235 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004236 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4237 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4238 DAG.getNode(ISD::BIT_CONVERT, dl,
4239 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004240 Op.getOperand(0)),
4241 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004242 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004243 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004244 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004245 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004246 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004247 } else if (VT == MVT::f32) {
4248 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4249 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004250 // result has a single use which is a store or a bitcast to i32. And in
4251 // the case of a store, it's not worth it if the index is a constant 0,
4252 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004253 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004254 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004255 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004256 if ((User->getOpcode() != ISD::STORE ||
4257 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4258 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004259 (User->getOpcode() != ISD::BIT_CONVERT ||
4260 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004261 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004262 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004263 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004264 Op.getOperand(0)),
4265 Op.getOperand(1));
4266 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004267 } else if (VT == MVT::i32) {
4268 // ExtractPS works with constant index.
4269 if (isa<ConstantSDNode>(Op.getOperand(1)))
4270 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004271 }
Dan Gohman475871a2008-07-27 21:46:04 +00004272 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004273}
4274
4275
Dan Gohman475871a2008-07-27 21:46:04 +00004276SDValue
4277X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004278 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004279 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004280
Evan Cheng62a3f152008-03-24 21:52:23 +00004281 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004282 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004283 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004284 return Res;
4285 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004286
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004288 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004289 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004290 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004292 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004293 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004294 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4295 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004296 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004297 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004298 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004299 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004300 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004301 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004303 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004304 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004305 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004306 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004307 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004308 if (Idx == 0)
4309 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004310
Evan Cheng0db9fe62006-04-25 20:13:52 +00004311 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 int Mask[4] = { Idx, -1, -1, -1 };
4313 MVT VVT = Op.getOperand(0).getValueType();
4314 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4315 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004316 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004317 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004318 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004319 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4320 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4321 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004322 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004323 if (Idx == 0)
4324 return Op;
4325
4326 // UNPCKHPD the element to the lowest double word, then movsd.
4327 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4328 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004329 int Mask[2] = { 1, -1 };
4330 MVT VVT = Op.getOperand(0).getValueType();
4331 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4332 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004333 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004334 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 }
4336
Dan Gohman475871a2008-07-27 21:46:04 +00004337 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338}
4339
Dan Gohman475871a2008-07-27 21:46:04 +00004340SDValue
4341X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004342 MVT VT = Op.getValueType();
4343 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004344 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004345
Dan Gohman475871a2008-07-27 21:46:04 +00004346 SDValue N0 = Op.getOperand(0);
4347 SDValue N1 = Op.getOperand(1);
4348 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004349
Dan Gohmanef521f12008-08-14 22:53:18 +00004350 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4351 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004352 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004353 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004354 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4355 // argument.
4356 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004357 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004358 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004359 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004360 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004361 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004362 // Bits [7:6] of the constant are the source select. This will always be
4363 // zero here. The DAG Combiner may combine an extract_elt index into these
4364 // bits. For example (insert (extract, 3), 2) could be matched by putting
4365 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004366 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004367 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004368 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004369 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004370 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004371 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004372 } else if (EVT == MVT::i32) {
4373 // InsertPS works with constant index.
4374 if (isa<ConstantSDNode>(N2))
4375 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004376 }
Dan Gohman475871a2008-07-27 21:46:04 +00004377 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004378}
4379
Dan Gohman475871a2008-07-27 21:46:04 +00004380SDValue
4381X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004382 MVT VT = Op.getValueType();
4383 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004384
4385 if (Subtarget->hasSSE41())
4386 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4387
Evan Cheng794405e2007-12-12 07:55:34 +00004388 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004389 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004390
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004391 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004392 SDValue N0 = Op.getOperand(0);
4393 SDValue N1 = Op.getOperand(1);
4394 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004395
Eli Friedman30e71eb2009-06-06 06:32:50 +00004396 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004397 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4398 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004400 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004402 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004403 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 }
Dan Gohman475871a2008-07-27 21:46:04 +00004405 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004406}
4407
Dan Gohman475871a2008-07-27 21:46:04 +00004408SDValue
4409X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004410 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004411 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004412 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4413 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4414 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004415 Op.getOperand(0))));
4416
Dale Johannesenace16102009-02-03 19:33:06 +00004417 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004418 MVT VT = MVT::v2i32;
4419 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004420 default: break;
4421 case MVT::v16i8:
4422 case MVT::v8i16:
4423 VT = MVT::v4i32;
4424 break;
4425 }
Dale Johannesenace16102009-02-03 19:33:06 +00004426 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4427 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004428}
4429
Bill Wendling056292f2008-09-16 21:48:12 +00004430// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4431// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4432// one of the above mentioned nodes. It has to be wrapped because otherwise
4433// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4434// be used to form addressing mode. These wrapped nodes will be selected
4435// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004436SDValue
4437X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004438 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Chris Lattner41621a22009-06-26 19:22:52 +00004439
4440 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4441 // global base reg.
4442 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004443 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattner41621a22009-06-26 19:22:52 +00004444 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
4445 if (Subtarget->isPICStyleStub())
4446 OpFlag = X86II::MO_PIC_BASE_OFFSET;
4447 else if (Subtarget->isPICStyleGOT())
4448 OpFlag = X86II::MO_GOTOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00004449 else if (Subtarget->isPICStyleRIPRel() &&
4450 getTargetMachine().getCodeModel() == CodeModel::Small)
4451 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner41621a22009-06-26 19:22:52 +00004452 }
4453
Evan Cheng1606e8e2009-03-13 07:51:59 +00004454 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004455 CP->getAlignment(),
4456 CP->getOffset(), OpFlag);
4457 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004458 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004459 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004460 if (OpFlag) {
4461 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004462 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004463 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004464 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004465 }
4466
4467 return Result;
4468}
4469
Chris Lattner18c59872009-06-27 04:16:01 +00004470SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4471 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4472
4473 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4474 // global base reg.
4475 unsigned char OpFlag = 0;
4476 unsigned WrapperKind = X86ISD::Wrapper;
4477 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
4478 if (Subtarget->isPICStyleStub())
4479 OpFlag = X86II::MO_PIC_BASE_OFFSET;
4480 else if (Subtarget->isPICStyleGOT())
4481 OpFlag = X86II::MO_GOTOFF;
4482 else if (Subtarget->isPICStyleRIPRel())
4483 WrapperKind = X86ISD::WrapperRIP;
4484 }
4485
4486 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4487 OpFlag);
4488 DebugLoc DL = JT->getDebugLoc();
4489 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4490
4491 // With PIC, the address is actually $g + Offset.
4492 if (OpFlag) {
4493 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4494 DAG.getNode(X86ISD::GlobalBaseReg,
4495 DebugLoc::getUnknownLoc(), getPointerTy()),
4496 Result);
4497 }
4498
4499 return Result;
4500}
4501
4502SDValue
4503X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4504 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4505
4506 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4507 // global base reg.
4508 unsigned char OpFlag = 0;
4509 unsigned WrapperKind = X86ISD::Wrapper;
4510 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
4511 if (Subtarget->isPICStyleStub())
4512 OpFlag = X86II::MO_PIC_BASE_OFFSET;
4513 else if (Subtarget->isPICStyleGOT())
4514 OpFlag = X86II::MO_GOTOFF;
4515 else if (Subtarget->isPICStyleRIPRel())
4516 WrapperKind = X86ISD::WrapperRIP;
4517 }
4518
4519 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
4520
4521 DebugLoc DL = Op.getDebugLoc();
4522 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4523
4524
4525 // With PIC, the address is actually $g + Offset.
4526 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4527 !Subtarget->isPICStyleRIPRel()) {
4528 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4529 DAG.getNode(X86ISD::GlobalBaseReg,
4530 DebugLoc::getUnknownLoc(),
4531 getPointerTy()),
4532 Result);
4533 }
4534
4535 return Result;
4536}
4537
Dan Gohman475871a2008-07-27 21:46:04 +00004538SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004539X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004540 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004541 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004542 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4543 bool ExtraLoadRequired =
4544 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4545
4546 // Create the TargetGlobalAddress node, folding in the constant
4547 // offset if it is legal.
4548 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004549 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004550 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4551 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004552 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004553 unsigned char OpFlags = 0;
4554
4555 if (Subtarget->isPICStyleRIPRel() &&
4556 getTargetMachine().getRelocationModel() != Reloc::Static) {
4557 if (ExtraLoadRequired)
4558 OpFlags = X86II::MO_GOTPCREL;
4559 } else if (Subtarget->isPICStyleGOT() &&
4560 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
4561 if (ExtraLoadRequired)
4562 OpFlags = X86II::MO_GOT;
4563 else
4564 OpFlags = X86II::MO_GOTOFF;
4565 }
4566
4567 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004568 }
4569
4570 if (Subtarget->isPICStyleRIPRel() &&
4571 getTargetMachine().getCodeModel() == CodeModel::Small)
4572 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4573 else
4574 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004575
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004576 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004577 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004578 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4579 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004580 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004582
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004583 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4584 // load the value at address GV, not the value of GV itself. This means that
4585 // the GlobalAddress must be in the base or index register of the address, not
4586 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004587 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004588 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004589 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004590 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591
Dan Gohman6520e202008-10-18 02:06:02 +00004592 // If there was a non-zero offset that we didn't fold, create an explicit
4593 // addition for it.
4594 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004595 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004596 DAG.getConstant(Offset, getPointerTy()));
4597
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 return Result;
4599}
4600
Evan Chengda43bcf2008-09-24 00:05:32 +00004601SDValue
4602X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4603 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004604 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004605 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004606}
4607
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004608static SDValue
4609GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004610 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg,
4611 unsigned char OperandFlags) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004612 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4613 DebugLoc dl = GA->getDebugLoc();
4614 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4615 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004616 GA->getOffset(),
4617 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004618 if (InFlag) {
4619 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004620 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004621 } else {
4622 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004623 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004624 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004625 SDValue Flag = Chain.getValue(1);
4626 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004627}
4628
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004629// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004630static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004631LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004632 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004633 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004634 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4635 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004636 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004637 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004638 PtrVT), InFlag);
4639 InFlag = Chain.getValue(1);
4640
Chris Lattnerb903bed2009-06-26 21:20:29 +00004641 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004642}
4643
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004644// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004645static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004646LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004647 const MVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004648 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4649 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004650}
4651
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004652// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4653// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004654static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004655 const MVT PtrVT, TLSModel::Model model,
4656 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004657 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004658 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004659 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4660 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004661 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4662 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004663
4664 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4665 NULL, 0);
4666
Chris Lattnerb903bed2009-06-26 21:20:29 +00004667 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004668 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
4669 // initialexec.
4670 unsigned WrapperKind = X86ISD::Wrapper;
4671 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004672 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00004673 } else if (is64Bit) {
4674 assert(model == TLSModel::InitialExec);
4675 OperandFlags = X86II::MO_GOTTPOFF;
4676 WrapperKind = X86ISD::WrapperRIP;
4677 } else {
4678 assert(model == TLSModel::InitialExec);
4679 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00004680 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004681
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004682 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4683 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00004684 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004685 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004686 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004687
Rafael Espindola9a580232009-02-27 13:37:18 +00004688 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004689 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004690 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004691
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004692 // The address of the thread local variable is the add of the thread
4693 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004694 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004695}
4696
Dan Gohman475871a2008-07-27 21:46:04 +00004697SDValue
4698X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004699 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004700 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004701 assert(Subtarget->isTargetELF() &&
4702 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004703 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00004704 const GlobalValue *GV = GA->getGlobal();
4705
4706 // If GV is an alias then use the aliasee for determining
4707 // thread-localness.
4708 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
4709 GV = GA->resolveAliasedGlobal(false);
4710
4711 TLSModel::Model model = getTLSModel(GV,
4712 getTargetMachine().getRelocationModel());
4713
4714 switch (model) {
4715 case TLSModel::GeneralDynamic:
4716 case TLSModel::LocalDynamic: // not implemented
4717 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00004718 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00004719 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4720
4721 case TLSModel::InitialExec:
4722 case TLSModel::LocalExec:
4723 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
4724 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004725 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004726
Chris Lattner5867de12009-04-01 22:14:45 +00004727 assert(0 && "Unreachable");
4728 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004729}
4730
Evan Cheng0db9fe62006-04-25 20:13:52 +00004731
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004732/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004733/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004734SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004735 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004736 MVT VT = Op.getValueType();
4737 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004738 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004739 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004740 SDValue ShOpLo = Op.getOperand(0);
4741 SDValue ShOpHi = Op.getOperand(1);
4742 SDValue ShAmt = Op.getOperand(2);
4743 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004744 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004745 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004746 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004747
Dan Gohman475871a2008-07-27 21:46:04 +00004748 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004749 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004750 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4751 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004752 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004753 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4754 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004755 }
Evan Chenge3413162006-01-09 18:33:28 +00004756
Dale Johannesenace16102009-02-03 19:33:06 +00004757 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004758 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004759 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004760 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004761
Dan Gohman475871a2008-07-27 21:46:04 +00004762 SDValue Hi, Lo;
4763 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4764 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4765 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004766
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004767 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004768 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4769 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004770 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004771 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4772 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004773 }
4774
Dan Gohman475871a2008-07-27 21:46:04 +00004775 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004776 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004777}
Evan Chenga3195e82006-01-12 22:54:21 +00004778
Dan Gohman475871a2008-07-27 21:46:04 +00004779SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004780 MVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004781
4782 if (SrcVT.isVector()) {
4783 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
4784 return Op;
4785 }
4786 return SDValue();
4787 }
4788
Duncan Sands8e4eb092008-06-08 20:54:56 +00004789 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004790 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004791
Eli Friedman36df4992009-05-27 00:47:34 +00004792 // These are really Legal; return the operand so the caller accepts it as
4793 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004794 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004795 return Op;
4796 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4797 Subtarget->is64Bit()) {
4798 return Op;
4799 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004800
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004801 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004802 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004803 MachineFunction &MF = DAG.getMachineFunction();
4804 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004805 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004806 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004807 StackSlot,
4808 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004809 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4810}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811
Eli Friedman948e95a2009-05-23 09:59:16 +00004812SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4813 SDValue StackSlot,
4814 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004815 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004816 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004817 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004818 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004819 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004820 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4821 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004822 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004823 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004824 Ops.push_back(Chain);
4825 Ops.push_back(StackSlot);
4826 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004827 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004828 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004830 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004832 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004833
4834 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4835 // shouldn't be necessary except that RFP cannot be live across
4836 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004837 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004839 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004840 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004841 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004842 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004843 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004844 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004845 Ops.push_back(DAG.getValueType(Op.getValueType()));
4846 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004847 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4848 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004849 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004850 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004851
Evan Cheng0db9fe62006-04-25 20:13:52 +00004852 return Result;
4853}
4854
Bill Wendling8b8a6362009-01-17 03:56:04 +00004855// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4856SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4857 // This algorithm is not obvious. Here it is in C code, more or less:
4858 /*
4859 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4860 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4861 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004862
Bill Wendling8b8a6362009-01-17 03:56:04 +00004863 // Copy ints to xmm registers.
4864 __m128i xh = _mm_cvtsi32_si128( hi );
4865 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004866
Bill Wendling8b8a6362009-01-17 03:56:04 +00004867 // Combine into low half of a single xmm register.
4868 __m128i x = _mm_unpacklo_epi32( xh, xl );
4869 __m128d d;
4870 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004871
Bill Wendling8b8a6362009-01-17 03:56:04 +00004872 // Merge in appropriate exponents to give the integer bits the right
4873 // magnitude.
4874 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004875
Bill Wendling8b8a6362009-01-17 03:56:04 +00004876 // Subtract away the biases to deal with the IEEE-754 double precision
4877 // implicit 1.
4878 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004879
Bill Wendling8b8a6362009-01-17 03:56:04 +00004880 // All conversions up to here are exact. The correctly rounded result is
4881 // calculated using the current rounding mode using the following
4882 // horizontal add.
4883 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4884 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4885 // store doesn't really need to be here (except
4886 // maybe to zero the other double)
4887 return sd;
4888 }
4889 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004890
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004891 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004892
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004893 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004894 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004895 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4896 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4897 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4898 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4899 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004900 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004901
Bill Wendling8b8a6362009-01-17 03:56:04 +00004902 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004903 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4904 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4905 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004906 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004907
Dale Johannesenace16102009-02-03 19:33:06 +00004908 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4909 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004910 Op.getOperand(0),
4911 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004912 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4913 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004914 Op.getOperand(0),
4915 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004916 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004917 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004918 PseudoSourceValue::getConstantPool(), 0,
4919 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004920 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004921 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4922 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004923 PseudoSourceValue::getConstantPool(), 0,
4924 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004925 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004926
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004927 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004928 int ShufMask[2] = { 1, -1 };
4929 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4930 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004931 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4932 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004933 DAG.getIntPtrConstant(0));
4934}
4935
Bill Wendling8b8a6362009-01-17 03:56:04 +00004936// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4937SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004938 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004939 // FP constant to bias correct the final result.
4940 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4941 MVT::f64);
4942
4943 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004944 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4945 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004946 Op.getOperand(0),
4947 DAG.getIntPtrConstant(0)));
4948
Dale Johannesenace16102009-02-03 19:33:06 +00004949 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4950 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004951 DAG.getIntPtrConstant(0));
4952
4953 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004954 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4955 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4956 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004957 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004958 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4959 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004960 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004961 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4962 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004963 DAG.getIntPtrConstant(0));
4964
4965 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004966 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004967
4968 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004969 MVT DestVT = Op.getValueType();
4970
4971 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004972 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004973 DAG.getIntPtrConstant(0));
4974 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004975 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004976 }
4977
4978 // Handle final rounding.
4979 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004980}
4981
4982SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004983 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004984 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004985
Evan Chenga06ec9e2009-01-19 08:08:22 +00004986 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4987 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4988 // the optimization here.
4989 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004990 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004991
4992 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004993 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004994 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004995 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004996 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004997
Bill Wendling8b8a6362009-01-17 03:56:04 +00004998 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004999 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005000 return LowerUINT_TO_FP_i32(Op, DAG);
5001 }
5002
Eli Friedman948e95a2009-05-23 09:59:16 +00005003 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
5004
5005 // Make a 64-bit buffer, and use it to build an FILD.
5006 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
5007 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5008 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5009 getPointerTy(), StackSlot, WordOff);
5010 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5011 StackSlot, NULL, 0);
5012 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5013 OffsetSlot, NULL, 0);
5014 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005015}
5016
Dan Gohman475871a2008-07-27 21:46:04 +00005017std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005018FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005019 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005020
5021 MVT DstTy = Op.getValueType();
5022
5023 if (!IsSigned) {
5024 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5025 DstTy = MVT::i64;
5026 }
5027
5028 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5029 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005030 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005031
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005032 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00005033 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005034 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005035 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005036 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00005037 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005038 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005039 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005040
Evan Cheng87c89352007-10-15 20:11:21 +00005041 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5042 // stack slot.
5043 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005044 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00005045 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00005046 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00005047
Evan Cheng0db9fe62006-04-25 20:13:52 +00005048 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00005049 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005050 default: assert(0 && "Invalid FP_TO_SINT to lower!");
5051 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5052 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5053 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005055
Dan Gohman475871a2008-07-27 21:46:04 +00005056 SDValue Chain = DAG.getEntryNode();
5057 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005058 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00005059 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005060 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00005061 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00005062 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005063 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005064 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5065 };
Dale Johannesenace16102009-02-03 19:33:06 +00005066 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067 Chain = Value.getValue(1);
5068 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5069 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5070 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005071
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005073 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00005074 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005075
Chris Lattner27a6c732007-11-24 07:07:01 +00005076 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005077}
5078
Dan Gohman475871a2008-07-27 21:46:04 +00005079SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005080 if (Op.getValueType().isVector()) {
5081 if (Op.getValueType() == MVT::v2i32 &&
5082 Op.getOperand(0).getValueType() == MVT::v2f64) {
5083 return Op;
5084 }
5085 return SDValue();
5086 }
5087
Eli Friedman948e95a2009-05-23 09:59:16 +00005088 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005089 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005090 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5091 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005092
Chris Lattner27a6c732007-11-24 07:07:01 +00005093 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005094 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005095 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005096}
5097
Eli Friedman948e95a2009-05-23 09:59:16 +00005098SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5099 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5100 SDValue FIST = Vals.first, StackSlot = Vals.second;
5101 assert(FIST.getNode() && "Unexpected failure");
5102
5103 // Load the result.
5104 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5105 FIST, StackSlot, NULL, 0);
5106}
5107
Dan Gohman475871a2008-07-27 21:46:04 +00005108SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005109 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005110 MVT VT = Op.getValueType();
5111 MVT EltVT = VT;
5112 if (VT.isVector())
5113 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005114 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005115 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005116 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005117 CV.push_back(C);
5118 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005119 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005120 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005121 CV.push_back(C);
5122 CV.push_back(C);
5123 CV.push_back(C);
5124 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005125 }
Dan Gohmand3006222007-07-27 17:16:43 +00005126 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005127 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005128 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005129 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005130 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005131 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005132}
5133
Dan Gohman475871a2008-07-27 21:46:04 +00005134SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005135 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005136 MVT VT = Op.getValueType();
5137 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00005138 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005139 if (VT.isVector()) {
5140 EltVT = VT.getVectorElementType();
5141 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00005142 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005143 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005144 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005145 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005146 CV.push_back(C);
5147 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005148 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005149 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005150 CV.push_back(C);
5151 CV.push_back(C);
5152 CV.push_back(C);
5153 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005154 }
Dan Gohmand3006222007-07-27 17:16:43 +00005155 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005156 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005157 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005158 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005159 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005160 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005161 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5162 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00005163 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005164 Op.getOperand(0)),
5165 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005166 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005167 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005168 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005169}
5170
Dan Gohman475871a2008-07-27 21:46:04 +00005171SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5172 SDValue Op0 = Op.getOperand(0);
5173 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005174 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005175 MVT VT = Op.getValueType();
5176 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005177
5178 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005179 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005180 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005181 SrcVT = VT;
5182 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005183 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005184 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005185 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005186 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005187 }
5188
5189 // At this point the operands and the result should have the same
5190 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005191
Evan Cheng68c47cb2007-01-05 07:55:56 +00005192 // First get the sign bit of second operand.
5193 std::vector<Constant*> CV;
5194 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005195 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5196 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005197 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005198 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5199 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5200 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5201 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005202 }
Dan Gohmand3006222007-07-27 17:16:43 +00005203 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005204 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005205 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005206 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005207 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005208 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005209
5210 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005211 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005212 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005213 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5214 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005215 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005216 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5217 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005218 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005219 }
5220
Evan Cheng73d6cf12007-01-05 21:37:56 +00005221 // Clear first operand sign bit.
5222 CV.clear();
5223 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005224 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5225 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005226 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005227 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5228 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5229 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5230 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005231 }
Dan Gohmand3006222007-07-27 17:16:43 +00005232 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005233 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005234 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005235 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005236 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005237 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005238
5239 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005240 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005241}
5242
Dan Gohman076aee32009-03-04 19:44:21 +00005243/// Emit nodes that will be selected as "test Op0,Op0", or something
5244/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005245SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5246 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005247 DebugLoc dl = Op.getDebugLoc();
5248
Dan Gohman31125812009-03-07 01:58:32 +00005249 // CF and OF aren't always set the way we want. Determine which
5250 // of these we need.
5251 bool NeedCF = false;
5252 bool NeedOF = false;
5253 switch (X86CC) {
5254 case X86::COND_A: case X86::COND_AE:
5255 case X86::COND_B: case X86::COND_BE:
5256 NeedCF = true;
5257 break;
5258 case X86::COND_G: case X86::COND_GE:
5259 case X86::COND_L: case X86::COND_LE:
5260 case X86::COND_O: case X86::COND_NO:
5261 NeedOF = true;
5262 break;
5263 default: break;
5264 }
5265
Dan Gohman076aee32009-03-04 19:44:21 +00005266 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005267 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5268 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5269 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005270 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005271 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005272 switch (Op.getNode()->getOpcode()) {
5273 case ISD::ADD:
5274 // Due to an isel shortcoming, be conservative if this add is likely to
5275 // be selected as part of a load-modify-store instruction. When the root
5276 // node in a match is a store, isel doesn't know how to remap non-chain
5277 // non-flag uses of other nodes in the match, such as the ADD in this
5278 // case. This leads to the ADD being left around and reselected, with
5279 // the result being two adds in the output.
5280 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5281 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5282 if (UI->getOpcode() == ISD::STORE)
5283 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005284 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005285 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5286 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005287 if (C->getAPIntValue() == 1) {
5288 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005289 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005290 break;
5291 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005292 // An add of negative one (subtract of one) will be selected as a DEC.
5293 if (C->getAPIntValue().isAllOnesValue()) {
5294 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005295 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005296 break;
5297 }
5298 }
Dan Gohman076aee32009-03-04 19:44:21 +00005299 // Otherwise use a regular EFLAGS-setting add.
5300 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005301 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005302 break;
5303 case ISD::SUB:
5304 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5305 // likely to be selected as part of a load-modify-store instruction.
5306 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5307 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5308 if (UI->getOpcode() == ISD::STORE)
5309 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005310 // Otherwise use a regular EFLAGS-setting sub.
5311 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005312 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005313 break;
5314 case X86ISD::ADD:
5315 case X86ISD::SUB:
5316 case X86ISD::INC:
5317 case X86ISD::DEC:
5318 return SDValue(Op.getNode(), 1);
5319 default:
5320 default_case:
5321 break;
5322 }
5323 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005324 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005325 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005326 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005327 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005328 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005329 DAG.ReplaceAllUsesWith(Op, New);
5330 return SDValue(New.getNode(), 1);
5331 }
5332 }
5333
5334 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5335 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5336 DAG.getConstant(0, Op.getValueType()));
5337}
5338
5339/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5340/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005341SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5342 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5344 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005345 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005346
5347 DebugLoc dl = Op0.getDebugLoc();
5348 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5349}
5350
Dan Gohman475871a2008-07-27 21:46:04 +00005351SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005352 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005353 SDValue Op0 = Op.getOperand(0);
5354 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005355 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005356 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005357
Dan Gohmane5af2d32009-01-29 01:59:02 +00005358 // Lower (X & (1 << N)) == 0 to BT(X, N).
5359 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5360 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005361 if (Op0.getOpcode() == ISD::AND &&
5362 Op0.hasOneUse() &&
5363 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005364 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005365 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005366 SDValue LHS, RHS;
5367 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5368 if (ConstantSDNode *Op010C =
5369 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5370 if (Op010C->getZExtValue() == 1) {
5371 LHS = Op0.getOperand(0);
5372 RHS = Op0.getOperand(1).getOperand(1);
5373 }
5374 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5375 if (ConstantSDNode *Op000C =
5376 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5377 if (Op000C->getZExtValue() == 1) {
5378 LHS = Op0.getOperand(1);
5379 RHS = Op0.getOperand(0).getOperand(1);
5380 }
5381 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5382 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5383 SDValue AndLHS = Op0.getOperand(0);
5384 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5385 LHS = AndLHS.getOperand(0);
5386 RHS = AndLHS.getOperand(1);
5387 }
5388 }
Evan Cheng0488db92007-09-25 01:57:46 +00005389
Dan Gohmane5af2d32009-01-29 01:59:02 +00005390 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005391 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5392 // instruction. Since the shift amount is in-range-or-undefined, we know
5393 // that doing a bittest on the i16 value is ok. We extend to i32 because
5394 // the encoding for the i16 version is larger than the i32 version.
5395 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005396 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005397
5398 // If the operand types disagree, extend the shift amount to match. Since
5399 // BT ignores high bits (like shifts) we can use anyextend.
5400 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005401 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005402
Dale Johannesenace16102009-02-03 19:33:06 +00005403 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005404 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005405 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005406 DAG.getConstant(Cond, MVT::i8), BT);
5407 }
5408 }
5409
5410 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5411 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005412
Dan Gohman31125812009-03-07 01:58:32 +00005413 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005414 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005415 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005416}
5417
Dan Gohman475871a2008-07-27 21:46:04 +00005418SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5419 SDValue Cond;
5420 SDValue Op0 = Op.getOperand(0);
5421 SDValue Op1 = Op.getOperand(1);
5422 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005423 MVT VT = Op.getValueType();
5424 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5425 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005426 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005427
5428 if (isFP) {
5429 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005430 MVT VT0 = Op0.getValueType();
5431 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5432 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005433 bool Swap = false;
5434
5435 switch (SetCCOpcode) {
5436 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005437 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005438 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005439 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005440 case ISD::SETGT: Swap = true; // Fallthrough
5441 case ISD::SETLT:
5442 case ISD::SETOLT: SSECC = 1; break;
5443 case ISD::SETOGE:
5444 case ISD::SETGE: Swap = true; // Fallthrough
5445 case ISD::SETLE:
5446 case ISD::SETOLE: SSECC = 2; break;
5447 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005448 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005449 case ISD::SETNE: SSECC = 4; break;
5450 case ISD::SETULE: Swap = true;
5451 case ISD::SETUGE: SSECC = 5; break;
5452 case ISD::SETULT: Swap = true;
5453 case ISD::SETUGT: SSECC = 6; break;
5454 case ISD::SETO: SSECC = 7; break;
5455 }
5456 if (Swap)
5457 std::swap(Op0, Op1);
5458
Nate Begemanfb8ead02008-07-25 19:05:58 +00005459 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005460 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005461 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005462 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005463 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5464 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5465 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005466 }
5467 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005468 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005469 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5470 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5471 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005472 }
5473 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005474 }
5475 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005476 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005477 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005478
Nate Begeman30a0de92008-07-17 16:51:19 +00005479 // We are handling one of the integer comparisons here. Since SSE only has
5480 // GT and EQ comparisons for integer, swapping operands and multiple
5481 // operations may be required for some comparisons.
5482 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5483 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005484
Nate Begeman30a0de92008-07-17 16:51:19 +00005485 switch (VT.getSimpleVT()) {
5486 default: break;
5487 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5488 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5489 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5490 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5491 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005492
Nate Begeman30a0de92008-07-17 16:51:19 +00005493 switch (SetCCOpcode) {
5494 default: break;
5495 case ISD::SETNE: Invert = true;
5496 case ISD::SETEQ: Opc = EQOpc; break;
5497 case ISD::SETLT: Swap = true;
5498 case ISD::SETGT: Opc = GTOpc; break;
5499 case ISD::SETGE: Swap = true;
5500 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5501 case ISD::SETULT: Swap = true;
5502 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5503 case ISD::SETUGE: Swap = true;
5504 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5505 }
5506 if (Swap)
5507 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005508
Nate Begeman30a0de92008-07-17 16:51:19 +00005509 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5510 // bits of the inputs before performing those operations.
5511 if (FlipSigns) {
5512 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005513 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5514 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005515 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005516 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5517 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005518 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5519 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005521
Dale Johannesenace16102009-02-03 19:33:06 +00005522 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005523
5524 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005525 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005526 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005527
Nate Begeman30a0de92008-07-17 16:51:19 +00005528 return Result;
5529}
Evan Cheng0488db92007-09-25 01:57:46 +00005530
Evan Cheng370e5342008-12-03 08:38:43 +00005531// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005532static bool isX86LogicalCmp(SDValue Op) {
5533 unsigned Opc = Op.getNode()->getOpcode();
5534 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5535 return true;
5536 if (Op.getResNo() == 1 &&
5537 (Opc == X86ISD::ADD ||
5538 Opc == X86ISD::SUB ||
5539 Opc == X86ISD::SMUL ||
5540 Opc == X86ISD::UMUL ||
5541 Opc == X86ISD::INC ||
5542 Opc == X86ISD::DEC))
5543 return true;
5544
5545 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005546}
5547
Dan Gohman475871a2008-07-27 21:46:04 +00005548SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005549 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005550 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005551 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005552 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005553
Evan Cheng734503b2006-09-11 02:19:56 +00005554 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005555 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005556
Evan Cheng3f41d662007-10-08 22:16:29 +00005557 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5558 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005559 if (Cond.getOpcode() == X86ISD::SETCC) {
5560 CC = Cond.getOperand(0);
5561
Dan Gohman475871a2008-07-27 21:46:04 +00005562 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005563 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005564 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005565
Evan Cheng3f41d662007-10-08 22:16:29 +00005566 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005567 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005568 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005569 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005570
Chris Lattnerd1980a52009-03-12 06:52:53 +00005571 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5572 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005573 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005574 addTest = false;
5575 }
5576 }
5577
5578 if (addTest) {
5579 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005580 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005581 }
5582
Dan Gohmanfc166572009-04-09 23:54:40 +00005583 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005584 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005585 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5586 // condition is true.
5587 Ops.push_back(Op.getOperand(2));
5588 Ops.push_back(Op.getOperand(1));
5589 Ops.push_back(CC);
5590 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005591 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005592}
5593
Evan Cheng370e5342008-12-03 08:38:43 +00005594// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5595// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5596// from the AND / OR.
5597static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5598 Opc = Op.getOpcode();
5599 if (Opc != ISD::OR && Opc != ISD::AND)
5600 return false;
5601 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5602 Op.getOperand(0).hasOneUse() &&
5603 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5604 Op.getOperand(1).hasOneUse());
5605}
5606
Evan Cheng961d6d42009-02-02 08:19:07 +00005607// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5608// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005609static bool isXor1OfSetCC(SDValue Op) {
5610 if (Op.getOpcode() != ISD::XOR)
5611 return false;
5612 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5613 if (N1C && N1C->getAPIntValue() == 1) {
5614 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5615 Op.getOperand(0).hasOneUse();
5616 }
5617 return false;
5618}
5619
Dan Gohman475871a2008-07-27 21:46:04 +00005620SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005621 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue Chain = Op.getOperand(0);
5623 SDValue Cond = Op.getOperand(1);
5624 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005625 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005626 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005627
Evan Cheng0db9fe62006-04-25 20:13:52 +00005628 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005629 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005630#if 0
5631 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005632 else if (Cond.getOpcode() == X86ISD::ADD ||
5633 Cond.getOpcode() == X86ISD::SUB ||
5634 Cond.getOpcode() == X86ISD::SMUL ||
5635 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005636 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005637#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005638
Evan Cheng3f41d662007-10-08 22:16:29 +00005639 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5640 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005641 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005642 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005643
Dan Gohman475871a2008-07-27 21:46:04 +00005644 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005645 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005646 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005647 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005648 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005649 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005650 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005651 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005652 default: break;
5653 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005654 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005655 // These can only come from an arithmetic instruction with overflow,
5656 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005657 Cond = Cond.getNode()->getOperand(1);
5658 addTest = false;
5659 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005660 }
Evan Cheng0488db92007-09-25 01:57:46 +00005661 }
Evan Cheng370e5342008-12-03 08:38:43 +00005662 } else {
5663 unsigned CondOpc;
5664 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5665 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005666 if (CondOpc == ISD::OR) {
5667 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5668 // two branches instead of an explicit OR instruction with a
5669 // separate test.
5670 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005671 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005672 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005673 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005674 Chain, Dest, CC, Cmp);
5675 CC = Cond.getOperand(1).getOperand(0);
5676 Cond = Cmp;
5677 addTest = false;
5678 }
5679 } else { // ISD::AND
5680 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5681 // two branches instead of an explicit AND instruction with a
5682 // separate test. However, we only do this if this block doesn't
5683 // have a fall-through edge, because this requires an explicit
5684 // jmp when the condition is false.
5685 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005686 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005687 Op.getNode()->hasOneUse()) {
5688 X86::CondCode CCode =
5689 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5690 CCode = X86::GetOppositeBranchCondition(CCode);
5691 CC = DAG.getConstant(CCode, MVT::i8);
5692 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5693 // Look for an unconditional branch following this conditional branch.
5694 // We need this because we need to reverse the successors in order
5695 // to implement FCMP_OEQ.
5696 if (User.getOpcode() == ISD::BR) {
5697 SDValue FalseBB = User.getOperand(1);
5698 SDValue NewBR =
5699 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5700 assert(NewBR == User);
5701 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005702
Dale Johannesene4d209d2009-02-03 20:21:25 +00005703 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005704 Chain, Dest, CC, Cmp);
5705 X86::CondCode CCode =
5706 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5707 CCode = X86::GetOppositeBranchCondition(CCode);
5708 CC = DAG.getConstant(CCode, MVT::i8);
5709 Cond = Cmp;
5710 addTest = false;
5711 }
5712 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005713 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005714 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5715 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5716 // It should be transformed during dag combiner except when the condition
5717 // is set by a arithmetics with overflow node.
5718 X86::CondCode CCode =
5719 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5720 CCode = X86::GetOppositeBranchCondition(CCode);
5721 CC = DAG.getConstant(CCode, MVT::i8);
5722 Cond = Cond.getOperand(0).getOperand(1);
5723 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005724 }
Evan Cheng0488db92007-09-25 01:57:46 +00005725 }
5726
5727 if (addTest) {
5728 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005729 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005730 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005731 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005732 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005733}
5734
Anton Korobeynikove060b532007-04-17 19:34:00 +00005735
5736// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5737// Calls to _alloca is needed to probe the stack when allocating more than 4k
5738// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5739// that the guard pages used by the OS virtual memory manager are allocated in
5740// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005741SDValue
5742X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005743 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005744 assert(Subtarget->isTargetCygMing() &&
5745 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005746 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005747
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005748 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005749 SDValue Chain = Op.getOperand(0);
5750 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005751 // FIXME: Ensure alignment here
5752
Dan Gohman475871a2008-07-27 21:46:04 +00005753 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005754
Duncan Sands83ec4b62008-06-06 12:08:01 +00005755 MVT IntPtr = getPointerTy();
5756 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005757
Chris Lattnere563bbc2008-10-11 22:08:30 +00005758 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005759
Dale Johannesendd64c412009-02-04 00:33:20 +00005760 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005761 Flag = Chain.getValue(1);
5762
5763 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005764 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005765 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005766 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005767 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005768 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005769 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005770 Flag = Chain.getValue(1);
5771
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005772 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005773 DAG.getIntPtrConstant(0, true),
5774 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005775 Flag);
5776
Dale Johannesendd64c412009-02-04 00:33:20 +00005777 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005778
Dan Gohman475871a2008-07-27 21:46:04 +00005779 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005780 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005781}
5782
Dan Gohman475871a2008-07-27 21:46:04 +00005783SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005784X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005785 SDValue Chain,
5786 SDValue Dst, SDValue Src,
5787 SDValue Size, unsigned Align,
5788 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005789 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005790 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005791
Bill Wendling6f287b22008-09-30 21:22:07 +00005792 // If not DWORD aligned or size is more than the threshold, call the library.
5793 // The libc version is likely to be faster for these cases. It can use the
5794 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005795 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005796 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005797 ConstantSize->getZExtValue() >
5798 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005799 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005800
5801 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005802 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005803
Bill Wendling6158d842008-10-01 00:59:58 +00005804 if (const char *bzeroEntry = V &&
5805 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5806 MVT IntPtr = getPointerTy();
5807 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005808 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005809 TargetLowering::ArgListEntry Entry;
5810 Entry.Node = Dst;
5811 Entry.Ty = IntPtrTy;
5812 Args.push_back(Entry);
5813 Entry.Node = Size;
5814 Args.push_back(Entry);
5815 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005816 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005817 0, CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005818 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005819 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005820 }
5821
Dan Gohman707e0182008-04-12 04:36:06 +00005822 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005823 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005824 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005825
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005826 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005827 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005828 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005830 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005831 unsigned BytesLeft = 0;
5832 bool TwoRepStos = false;
5833 if (ValC) {
5834 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005835 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005836
Evan Cheng0db9fe62006-04-25 20:13:52 +00005837 // If the value is a constant, then we can potentially use larger sets.
5838 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005839 case 2: // WORD aligned
5840 AVT = MVT::i16;
5841 ValReg = X86::AX;
5842 Val = (Val << 8) | Val;
5843 break;
5844 case 0: // DWORD aligned
5845 AVT = MVT::i32;
5846 ValReg = X86::EAX;
5847 Val = (Val << 8) | Val;
5848 Val = (Val << 16) | Val;
5849 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5850 AVT = MVT::i64;
5851 ValReg = X86::RAX;
5852 Val = (Val << 32) | Val;
5853 }
5854 break;
5855 default: // Byte aligned
5856 AVT = MVT::i8;
5857 ValReg = X86::AL;
5858 Count = DAG.getIntPtrConstant(SizeVal);
5859 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005860 }
5861
Duncan Sands8e4eb092008-06-08 20:54:56 +00005862 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005863 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005864 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5865 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005866 }
5867
Dale Johannesen0f502f62009-02-03 22:26:09 +00005868 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005869 InFlag);
5870 InFlag = Chain.getValue(1);
5871 } else {
5872 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005873 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005874 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005875 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005876 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005877
Scott Michelfdc40a02009-02-17 22:15:04 +00005878 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005879 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005880 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005881 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005882 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005883 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005884 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005885 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005886
Chris Lattnerd96d0722007-02-25 06:40:16 +00005887 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005888 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005889 Ops.push_back(Chain);
5890 Ops.push_back(DAG.getValueType(AVT));
5891 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005892 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005893
Evan Cheng0db9fe62006-04-25 20:13:52 +00005894 if (TwoRepStos) {
5895 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005896 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005897 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005898 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005899 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005900 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005901 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005902 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005903 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005904 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005905 Ops.clear();
5906 Ops.push_back(Chain);
5907 Ops.push_back(DAG.getValueType(MVT::i8));
5908 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005909 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005911 // Handle the last 1 - 7 bytes.
5912 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005913 MVT AddrVT = Dst.getValueType();
5914 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005915
Dale Johannesen0f502f62009-02-03 22:26:09 +00005916 Chain = DAG.getMemset(Chain, dl,
5917 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005918 DAG.getConstant(Offset, AddrVT)),
5919 Src,
5920 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005921 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005922 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005923
Dan Gohman707e0182008-04-12 04:36:06 +00005924 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925 return Chain;
5926}
Evan Cheng11e15b32006-04-03 20:53:28 +00005927
Dan Gohman475871a2008-07-27 21:46:04 +00005928SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005929X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005930 SDValue Chain, SDValue Dst, SDValue Src,
5931 SDValue Size, unsigned Align,
5932 bool AlwaysInline,
5933 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005934 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005935 // This requires the copy size to be a constant, preferrably
5936 // within a subtarget-specific limit.
5937 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5938 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005939 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005940 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005941 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005942 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005943
Evan Cheng1887c1c2008-08-21 21:00:15 +00005944 /// If not DWORD aligned, call the library.
5945 if ((Align & 3) != 0)
5946 return SDValue();
5947
5948 // DWORD aligned
5949 MVT AVT = MVT::i32;
5950 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005951 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005952
Duncan Sands83ec4b62008-06-06 12:08:01 +00005953 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005954 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005955 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005956 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005957
Dan Gohman475871a2008-07-27 21:46:04 +00005958 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005959 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005960 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005961 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005962 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005963 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005964 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005965 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005966 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005967 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005968 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005969 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005970 InFlag = Chain.getValue(1);
5971
Chris Lattnerd96d0722007-02-25 06:40:16 +00005972 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005973 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005974 Ops.push_back(Chain);
5975 Ops.push_back(DAG.getValueType(AVT));
5976 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005977 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005978
Dan Gohman475871a2008-07-27 21:46:04 +00005979 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005980 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005981 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005982 // Handle the last 1 - 7 bytes.
5983 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005984 MVT DstVT = Dst.getValueType();
5985 MVT SrcVT = Src.getValueType();
5986 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005987 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005988 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005989 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005990 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005991 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005992 DAG.getConstant(BytesLeft, SizeVT),
5993 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005994 DstSV, DstSVOff + Offset,
5995 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005996 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005997
Scott Michelfdc40a02009-02-17 22:15:04 +00005998 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005999 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006000}
6001
Dan Gohman475871a2008-07-27 21:46:04 +00006002SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006003 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006004 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006005
Evan Cheng25ab6902006-09-08 06:48:29 +00006006 if (!Subtarget->is64Bit()) {
6007 // vastart just stores the address of the VarArgsFrameIndex slot into the
6008 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006009 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006010 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006011 }
6012
6013 // __va_list_tag:
6014 // gp_offset (0 - 6 * 8)
6015 // fp_offset (48 - 48 + 8 * 16)
6016 // overflow_arg_area (point to parameters coming in memory).
6017 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006018 SmallVector<SDValue, 8> MemOps;
6019 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006020 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006021 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00006022 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006023 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006024 MemOps.push_back(Store);
6025
6026 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006027 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006028 FIN, DAG.getIntPtrConstant(4));
6029 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00006030 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006031 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006032 MemOps.push_back(Store);
6033
6034 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006035 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006036 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006037 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006038 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006039 MemOps.push_back(Store);
6040
6041 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006042 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006043 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006044 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006045 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006046 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00006047 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006048 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006049}
6050
Dan Gohman475871a2008-07-27 21:46:04 +00006051SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006052 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6053 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006054 SDValue Chain = Op.getOperand(0);
6055 SDValue SrcPtr = Op.getOperand(1);
6056 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006057
Torok Edwinab7c09b2009-07-08 18:01:40 +00006058 LLVM_UNREACHABLE("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006059 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006060}
6061
Dan Gohman475871a2008-07-27 21:46:04 +00006062SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006063 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006064 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006065 SDValue Chain = Op.getOperand(0);
6066 SDValue DstPtr = Op.getOperand(1);
6067 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006068 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6069 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006070 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006071
Dale Johannesendd64c412009-02-04 00:33:20 +00006072 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006073 DAG.getIntPtrConstant(24), 8, false,
6074 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006075}
6076
Dan Gohman475871a2008-07-27 21:46:04 +00006077SDValue
6078X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006079 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006080 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006081 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006082 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006083 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006084 case Intrinsic::x86_sse_comieq_ss:
6085 case Intrinsic::x86_sse_comilt_ss:
6086 case Intrinsic::x86_sse_comile_ss:
6087 case Intrinsic::x86_sse_comigt_ss:
6088 case Intrinsic::x86_sse_comige_ss:
6089 case Intrinsic::x86_sse_comineq_ss:
6090 case Intrinsic::x86_sse_ucomieq_ss:
6091 case Intrinsic::x86_sse_ucomilt_ss:
6092 case Intrinsic::x86_sse_ucomile_ss:
6093 case Intrinsic::x86_sse_ucomigt_ss:
6094 case Intrinsic::x86_sse_ucomige_ss:
6095 case Intrinsic::x86_sse_ucomineq_ss:
6096 case Intrinsic::x86_sse2_comieq_sd:
6097 case Intrinsic::x86_sse2_comilt_sd:
6098 case Intrinsic::x86_sse2_comile_sd:
6099 case Intrinsic::x86_sse2_comigt_sd:
6100 case Intrinsic::x86_sse2_comige_sd:
6101 case Intrinsic::x86_sse2_comineq_sd:
6102 case Intrinsic::x86_sse2_ucomieq_sd:
6103 case Intrinsic::x86_sse2_ucomilt_sd:
6104 case Intrinsic::x86_sse2_ucomile_sd:
6105 case Intrinsic::x86_sse2_ucomigt_sd:
6106 case Intrinsic::x86_sse2_ucomige_sd:
6107 case Intrinsic::x86_sse2_ucomineq_sd: {
6108 unsigned Opc = 0;
6109 ISD::CondCode CC = ISD::SETCC_INVALID;
6110 switch (IntNo) {
6111 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006112 case Intrinsic::x86_sse_comieq_ss:
6113 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006114 Opc = X86ISD::COMI;
6115 CC = ISD::SETEQ;
6116 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006117 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006118 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006119 Opc = X86ISD::COMI;
6120 CC = ISD::SETLT;
6121 break;
6122 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006123 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006124 Opc = X86ISD::COMI;
6125 CC = ISD::SETLE;
6126 break;
6127 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006128 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006129 Opc = X86ISD::COMI;
6130 CC = ISD::SETGT;
6131 break;
6132 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006133 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006134 Opc = X86ISD::COMI;
6135 CC = ISD::SETGE;
6136 break;
6137 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006138 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006139 Opc = X86ISD::COMI;
6140 CC = ISD::SETNE;
6141 break;
6142 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006143 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006144 Opc = X86ISD::UCOMI;
6145 CC = ISD::SETEQ;
6146 break;
6147 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006148 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006149 Opc = X86ISD::UCOMI;
6150 CC = ISD::SETLT;
6151 break;
6152 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006153 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006154 Opc = X86ISD::UCOMI;
6155 CC = ISD::SETLE;
6156 break;
6157 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006158 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006159 Opc = X86ISD::UCOMI;
6160 CC = ISD::SETGT;
6161 break;
6162 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006163 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006164 Opc = X86ISD::UCOMI;
6165 CC = ISD::SETGE;
6166 break;
6167 case Intrinsic::x86_sse_ucomineq_ss:
6168 case Intrinsic::x86_sse2_ucomineq_sd:
6169 Opc = X86ISD::UCOMI;
6170 CC = ISD::SETNE;
6171 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006172 }
Evan Cheng734503b2006-09-11 02:19:56 +00006173
Dan Gohman475871a2008-07-27 21:46:04 +00006174 SDValue LHS = Op.getOperand(1);
6175 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006176 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006177 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6178 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00006179 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006180 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006181 }
Evan Cheng5759f972008-05-04 09:15:50 +00006182
6183 // Fix vector shift instructions where the last operand is a non-immediate
6184 // i32 value.
6185 case Intrinsic::x86_sse2_pslli_w:
6186 case Intrinsic::x86_sse2_pslli_d:
6187 case Intrinsic::x86_sse2_pslli_q:
6188 case Intrinsic::x86_sse2_psrli_w:
6189 case Intrinsic::x86_sse2_psrli_d:
6190 case Intrinsic::x86_sse2_psrli_q:
6191 case Intrinsic::x86_sse2_psrai_w:
6192 case Intrinsic::x86_sse2_psrai_d:
6193 case Intrinsic::x86_mmx_pslli_w:
6194 case Intrinsic::x86_mmx_pslli_d:
6195 case Intrinsic::x86_mmx_pslli_q:
6196 case Intrinsic::x86_mmx_psrli_w:
6197 case Intrinsic::x86_mmx_psrli_d:
6198 case Intrinsic::x86_mmx_psrli_q:
6199 case Intrinsic::x86_mmx_psrai_w:
6200 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006201 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006202 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006203 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006204
6205 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006206 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006207 switch (IntNo) {
6208 case Intrinsic::x86_sse2_pslli_w:
6209 NewIntNo = Intrinsic::x86_sse2_psll_w;
6210 break;
6211 case Intrinsic::x86_sse2_pslli_d:
6212 NewIntNo = Intrinsic::x86_sse2_psll_d;
6213 break;
6214 case Intrinsic::x86_sse2_pslli_q:
6215 NewIntNo = Intrinsic::x86_sse2_psll_q;
6216 break;
6217 case Intrinsic::x86_sse2_psrli_w:
6218 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6219 break;
6220 case Intrinsic::x86_sse2_psrli_d:
6221 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6222 break;
6223 case Intrinsic::x86_sse2_psrli_q:
6224 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6225 break;
6226 case Intrinsic::x86_sse2_psrai_w:
6227 NewIntNo = Intrinsic::x86_sse2_psra_w;
6228 break;
6229 case Intrinsic::x86_sse2_psrai_d:
6230 NewIntNo = Intrinsic::x86_sse2_psra_d;
6231 break;
6232 default: {
6233 ShAmtVT = MVT::v2i32;
6234 switch (IntNo) {
6235 case Intrinsic::x86_mmx_pslli_w:
6236 NewIntNo = Intrinsic::x86_mmx_psll_w;
6237 break;
6238 case Intrinsic::x86_mmx_pslli_d:
6239 NewIntNo = Intrinsic::x86_mmx_psll_d;
6240 break;
6241 case Intrinsic::x86_mmx_pslli_q:
6242 NewIntNo = Intrinsic::x86_mmx_psll_q;
6243 break;
6244 case Intrinsic::x86_mmx_psrli_w:
6245 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6246 break;
6247 case Intrinsic::x86_mmx_psrli_d:
6248 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6249 break;
6250 case Intrinsic::x86_mmx_psrli_q:
6251 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6252 break;
6253 case Intrinsic::x86_mmx_psrai_w:
6254 NewIntNo = Intrinsic::x86_mmx_psra_w;
6255 break;
6256 case Intrinsic::x86_mmx_psrai_d:
6257 NewIntNo = Intrinsic::x86_mmx_psra_d;
6258 break;
Torok Edwinab7c09b2009-07-08 18:01:40 +00006259 default: LLVM_UNREACHABLE("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006260 }
6261 break;
6262 }
6263 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006264 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006265 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6266 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6267 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006268 DAG.getConstant(NewIntNo, MVT::i32),
6269 Op.getOperand(1), ShAmt);
6270 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006271 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006272}
Evan Cheng72261582005-12-20 06:22:03 +00006273
Dan Gohman475871a2008-07-27 21:46:04 +00006274SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006275 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006276 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006277
6278 if (Depth > 0) {
6279 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6280 SDValue Offset =
6281 DAG.getConstant(TD->getPointerSize(),
6282 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006283 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006284 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006285 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006286 NULL, 0);
6287 }
6288
6289 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006290 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006291 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006292 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006293}
6294
Dan Gohman475871a2008-07-27 21:46:04 +00006295SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006296 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6297 MFI->setFrameAddressIsTaken(true);
6298 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006299 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006300 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6301 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006302 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006303 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006304 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006305 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006306}
6307
Dan Gohman475871a2008-07-27 21:46:04 +00006308SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006309 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006310 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006311}
6312
Dan Gohman475871a2008-07-27 21:46:04 +00006313SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006314{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006315 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006316 SDValue Chain = Op.getOperand(0);
6317 SDValue Offset = Op.getOperand(1);
6318 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006319 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006320
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006321 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6322 getPointerTy());
6323 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006324
Dale Johannesene4d209d2009-02-03 20:21:25 +00006325 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006326 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006327 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6328 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006329 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006330 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006331
Dale Johannesene4d209d2009-02-03 20:21:25 +00006332 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006333 MVT::Other,
6334 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006335}
6336
Dan Gohman475871a2008-07-27 21:46:04 +00006337SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006338 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006339 SDValue Root = Op.getOperand(0);
6340 SDValue Trmp = Op.getOperand(1); // trampoline
6341 SDValue FPtr = Op.getOperand(2); // nested function
6342 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006343 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006344
Dan Gohman69de1932008-02-06 22:27:42 +00006345 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006346
Duncan Sands339e14f2008-01-16 22:55:25 +00006347 const X86InstrInfo *TII =
6348 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6349
Duncan Sandsb116fac2007-07-27 20:02:49 +00006350 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006351 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006352
6353 // Large code-model.
6354
6355 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6356 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6357
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006358 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6359 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006360
6361 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6362
6363 // Load the pointer to the nested function into R11.
6364 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006365 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006366 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6367 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006368
Scott Michelfdc40a02009-02-17 22:15:04 +00006369 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006370 DAG.getConstant(2, MVT::i64));
6371 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006372
6373 // Load the 'nest' parameter value into R10.
6374 // R10 is specified in X86CallingConv.td
6375 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006376 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006377 DAG.getConstant(10, MVT::i64));
6378 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6379 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006380
Scott Michelfdc40a02009-02-17 22:15:04 +00006381 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006382 DAG.getConstant(12, MVT::i64));
6383 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006384
6385 // Jump to the nested function.
6386 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006387 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006388 DAG.getConstant(20, MVT::i64));
6389 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6390 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006391
6392 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006393 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006394 DAG.getConstant(22, MVT::i64));
6395 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006396 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006397
Dan Gohman475871a2008-07-27 21:46:04 +00006398 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006399 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6400 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006401 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006402 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006403 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6404 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006405 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006406
6407 switch (CC) {
6408 default:
6409 assert(0 && "Unsupported calling convention");
6410 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006411 case CallingConv::X86_StdCall: {
6412 // Pass 'nest' parameter in ECX.
6413 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006414 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006415
6416 // Check that ECX wasn't needed by an 'inreg' parameter.
6417 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006418 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006419
Chris Lattner58d74912008-03-12 17:45:29 +00006420 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006421 unsigned InRegCount = 0;
6422 unsigned Idx = 1;
6423
6424 for (FunctionType::param_iterator I = FTy->param_begin(),
6425 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006426 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006427 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006428 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006429
6430 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006431 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006432 }
6433 }
6434 break;
6435 }
6436 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006437 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006438 // Pass 'nest' parameter in EAX.
6439 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006440 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006441 break;
6442 }
6443
Dan Gohman475871a2008-07-27 21:46:04 +00006444 SDValue OutChains[4];
6445 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006446
Scott Michelfdc40a02009-02-17 22:15:04 +00006447 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006448 DAG.getConstant(10, MVT::i32));
6449 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006450
Duncan Sands339e14f2008-01-16 22:55:25 +00006451 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006452 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006453 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006454 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006455 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006456
Scott Michelfdc40a02009-02-17 22:15:04 +00006457 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006458 DAG.getConstant(1, MVT::i32));
6459 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006460
Duncan Sands339e14f2008-01-16 22:55:25 +00006461 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006462 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006463 DAG.getConstant(5, MVT::i32));
6464 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006465 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006466
Scott Michelfdc40a02009-02-17 22:15:04 +00006467 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006468 DAG.getConstant(6, MVT::i32));
6469 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006470
Dan Gohman475871a2008-07-27 21:46:04 +00006471 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006472 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6473 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006474 }
6475}
6476
Dan Gohman475871a2008-07-27 21:46:04 +00006477SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006478 /*
6479 The rounding mode is in bits 11:10 of FPSR, and has the following
6480 settings:
6481 00 Round to nearest
6482 01 Round to -inf
6483 10 Round to +inf
6484 11 Round to 0
6485
6486 FLT_ROUNDS, on the other hand, expects the following:
6487 -1 Undefined
6488 0 Round to 0
6489 1 Round to nearest
6490 2 Round to +inf
6491 3 Round to -inf
6492
6493 To perform the conversion, we do:
6494 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6495 */
6496
6497 MachineFunction &MF = DAG.getMachineFunction();
6498 const TargetMachine &TM = MF.getTarget();
6499 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6500 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006501 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006502 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006503
6504 // Save FP Control Word to stack slot
6505 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006506 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006507
Dale Johannesene4d209d2009-02-03 20:21:25 +00006508 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006509 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006510
6511 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006512 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006513
6514 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006515 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006516 DAG.getNode(ISD::SRL, dl, MVT::i16,
6517 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006518 CWD, DAG.getConstant(0x800, MVT::i16)),
6519 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006520 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006521 DAG.getNode(ISD::SRL, dl, MVT::i16,
6522 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006523 CWD, DAG.getConstant(0x400, MVT::i16)),
6524 DAG.getConstant(9, MVT::i8));
6525
Dan Gohman475871a2008-07-27 21:46:04 +00006526 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006527 DAG.getNode(ISD::AND, dl, MVT::i16,
6528 DAG.getNode(ISD::ADD, dl, MVT::i16,
6529 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006530 DAG.getConstant(1, MVT::i16)),
6531 DAG.getConstant(3, MVT::i16));
6532
6533
Duncan Sands83ec4b62008-06-06 12:08:01 +00006534 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006535 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006536}
6537
Dan Gohman475871a2008-07-27 21:46:04 +00006538SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006539 MVT VT = Op.getValueType();
6540 MVT OpVT = VT;
6541 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006542 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006543
6544 Op = Op.getOperand(0);
6545 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006546 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006547 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006548 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006549 }
Evan Cheng18efe262007-12-14 02:13:44 +00006550
Evan Cheng152804e2007-12-14 08:30:15 +00006551 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6552 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006553 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006554
6555 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006556 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006557 Ops.push_back(Op);
6558 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6559 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6560 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006561 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006562
6563 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006564 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006565
Evan Cheng18efe262007-12-14 02:13:44 +00006566 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006567 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006568 return Op;
6569}
6570
Dan Gohman475871a2008-07-27 21:46:04 +00006571SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006572 MVT VT = Op.getValueType();
6573 MVT OpVT = VT;
6574 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006575 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006576
6577 Op = Op.getOperand(0);
6578 if (VT == MVT::i8) {
6579 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006580 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006581 }
Evan Cheng152804e2007-12-14 08:30:15 +00006582
6583 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6584 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006585 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006586
6587 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006588 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006589 Ops.push_back(Op);
6590 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6591 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6592 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006593 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006594
Evan Cheng18efe262007-12-14 02:13:44 +00006595 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006596 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006597 return Op;
6598}
6599
Mon P Wangaf9b9522008-12-18 21:42:19 +00006600SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6601 MVT VT = Op.getValueType();
6602 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006603 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006604
Mon P Wangaf9b9522008-12-18 21:42:19 +00006605 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6606 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6607 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6608 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6609 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6610 //
6611 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6612 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6613 // return AloBlo + AloBhi + AhiBlo;
6614
6615 SDValue A = Op.getOperand(0);
6616 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006617
Dale Johannesene4d209d2009-02-03 20:21:25 +00006618 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006619 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6620 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006621 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006622 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6623 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006624 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006625 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6626 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006627 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006628 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6629 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006630 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006631 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6632 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006633 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006634 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6635 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006636 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006637 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6638 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006639 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6640 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006641 return Res;
6642}
6643
6644
Bill Wendling74c37652008-12-09 22:08:41 +00006645SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6646 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6647 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006648 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6649 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006650 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006651 SDValue LHS = N->getOperand(0);
6652 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006653 unsigned BaseOp = 0;
6654 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006655 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006656
6657 switch (Op.getOpcode()) {
6658 default: assert(0 && "Unknown ovf instruction!");
6659 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006660 // A subtract of one will be selected as a INC. Note that INC doesn't
6661 // set CF, so we can't do this for UADDO.
6662 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6663 if (C->getAPIntValue() == 1) {
6664 BaseOp = X86ISD::INC;
6665 Cond = X86::COND_O;
6666 break;
6667 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006668 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006669 Cond = X86::COND_O;
6670 break;
6671 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006672 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006673 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006674 break;
6675 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006676 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6677 // set CF, so we can't do this for USUBO.
6678 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6679 if (C->getAPIntValue() == 1) {
6680 BaseOp = X86ISD::DEC;
6681 Cond = X86::COND_O;
6682 break;
6683 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006684 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006685 Cond = X86::COND_O;
6686 break;
6687 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006688 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006689 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006690 break;
6691 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006692 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006693 Cond = X86::COND_O;
6694 break;
6695 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006696 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006697 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006698 break;
6699 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006700
Bill Wendling61edeb52008-12-02 01:06:39 +00006701 // Also sets EFLAGS.
6702 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006703 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006704
Bill Wendling61edeb52008-12-02 01:06:39 +00006705 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006706 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006707 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006708
Bill Wendling61edeb52008-12-02 01:06:39 +00006709 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6710 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006711}
6712
Dan Gohman475871a2008-07-27 21:46:04 +00006713SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006714 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006715 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006716 unsigned Reg = 0;
6717 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006718 switch(T.getSimpleVT()) {
6719 default:
6720 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006721 case MVT::i8: Reg = X86::AL; size = 1; break;
6722 case MVT::i16: Reg = X86::AX; size = 2; break;
6723 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006724 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006725 assert(Subtarget->is64Bit() && "Node not type legal!");
6726 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006727 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006728 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006729 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006730 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006731 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006732 Op.getOperand(1),
6733 Op.getOperand(3),
6734 DAG.getTargetConstant(size, MVT::i8),
6735 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006736 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006737 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006738 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006739 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006740 return cpOut;
6741}
6742
Duncan Sands1607f052008-12-01 11:39:25 +00006743SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006744 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006745 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006746 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006747 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006748 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006749 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006750 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6751 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006752 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006753 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006754 DAG.getConstant(32, MVT::i8));
6755 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006756 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006757 rdx.getValue(1)
6758 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006759 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006760}
6761
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006762SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6763 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006764 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006765 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006766 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006767 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006768 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006769 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006770 Node->getOperand(0),
6771 Node->getOperand(1), negOp,
6772 cast<AtomicSDNode>(Node)->getSrcValue(),
6773 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006774}
6775
Evan Cheng0db9fe62006-04-25 20:13:52 +00006776/// LowerOperation - Provide custom lowering hooks for some operations.
6777///
Dan Gohman475871a2008-07-27 21:46:04 +00006778SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006779 switch (Op.getOpcode()) {
6780 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006781 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6782 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006783 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6784 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6785 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6786 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6787 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6788 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6789 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006790 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006791 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006792 case ISD::SHL_PARTS:
6793 case ISD::SRA_PARTS:
6794 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6795 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006796 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006797 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006798 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799 case ISD::FABS: return LowerFABS(Op, DAG);
6800 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006801 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006802 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006803 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006804 case ISD::SELECT: return LowerSELECT(Op, DAG);
6805 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006806 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006807 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006808 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006809 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006810 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006811 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006812 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006813 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006814 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6815 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006816 case ISD::FRAME_TO_ARGS_OFFSET:
6817 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006818 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006819 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006820 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006821 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006822 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6823 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006824 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006825 case ISD::SADDO:
6826 case ISD::UADDO:
6827 case ISD::SSUBO:
6828 case ISD::USUBO:
6829 case ISD::SMULO:
6830 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006831 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006832 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006833}
6834
Duncan Sands1607f052008-12-01 11:39:25 +00006835void X86TargetLowering::
6836ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6837 SelectionDAG &DAG, unsigned NewOp) {
6838 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006839 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006840 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6841
6842 SDValue Chain = Node->getOperand(0);
6843 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006844 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006845 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006846 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006847 Node->getOperand(2), DAG.getIntPtrConstant(1));
6848 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6849 // have a MemOperand. Pass the info through as a normal operand.
6850 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6851 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6852 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006853 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006854 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006855 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006856 Results.push_back(Result.getValue(2));
6857}
6858
Duncan Sands126d9072008-07-04 11:47:58 +00006859/// ReplaceNodeResults - Replace a node with an illegal result type
6860/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006861void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6862 SmallVectorImpl<SDValue>&Results,
6863 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006864 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006865 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006866 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006867 assert(false && "Do not know how to custom type legalize this operation!");
6868 return;
6869 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006870 std::pair<SDValue,SDValue> Vals =
6871 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006872 SDValue FIST = Vals.first, StackSlot = Vals.second;
6873 if (FIST.getNode() != 0) {
6874 MVT VT = N->getValueType(0);
6875 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006876 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006877 }
6878 return;
6879 }
6880 case ISD::READCYCLECOUNTER: {
6881 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6882 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006883 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006884 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006885 rd.getValue(1));
6886 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006887 eax.getValue(2));
6888 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6889 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006890 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006891 Results.push_back(edx.getValue(1));
6892 return;
6893 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006894 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006895 MVT T = N->getValueType(0);
6896 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6897 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006898 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006899 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006900 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006901 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006902 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6903 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006904 cpInL.getValue(1));
6905 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006906 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006907 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006908 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006909 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006910 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006911 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006912 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006913 swapInL.getValue(1));
6914 SDValue Ops[] = { swapInH.getValue(0),
6915 N->getOperand(1),
6916 swapInH.getValue(1) };
6917 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006918 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006919 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6920 MVT::i32, Result.getValue(1));
6921 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6922 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006923 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006924 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006925 Results.push_back(cpOutH.getValue(1));
6926 return;
6927 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006928 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006929 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6930 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006931 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006932 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6933 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006934 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006935 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6936 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006937 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006938 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6939 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006940 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006941 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6942 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006943 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006944 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6945 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006946 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006947 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6948 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006949 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006950}
6951
Evan Cheng72261582005-12-20 06:22:03 +00006952const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6953 switch (Opcode) {
6954 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006955 case X86ISD::BSF: return "X86ISD::BSF";
6956 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006957 case X86ISD::SHLD: return "X86ISD::SHLD";
6958 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006959 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006960 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006961 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006962 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006963 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006964 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006965 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6966 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6967 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006968 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006969 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006970 case X86ISD::CALL: return "X86ISD::CALL";
6971 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6972 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006973 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006974 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006975 case X86ISD::COMI: return "X86ISD::COMI";
6976 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006977 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006978 case X86ISD::CMOV: return "X86ISD::CMOV";
6979 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006980 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006981 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6982 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006983 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006984 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00006985 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006986 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006987 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006988 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6989 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006990 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006991 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006992 case X86ISD::FMAX: return "X86ISD::FMAX";
6993 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006994 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6995 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006996 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006997 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006998 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006999 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007000 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007001 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7002 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007003 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7004 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7005 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7006 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7007 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7008 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007009 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7010 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007011 case X86ISD::VSHL: return "X86ISD::VSHL";
7012 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007013 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7014 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7015 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7016 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7017 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7018 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7019 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7020 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7021 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7022 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007023 case X86ISD::ADD: return "X86ISD::ADD";
7024 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007025 case X86ISD::SMUL: return "X86ISD::SMUL";
7026 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007027 case X86ISD::INC: return "X86ISD::INC";
7028 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00007029 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00007030 }
7031}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007032
Chris Lattnerc9addb72007-03-30 23:15:24 +00007033// isLegalAddressingMode - Return true if the addressing mode represented
7034// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007035bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007036 const Type *Ty) const {
7037 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00007038
Chris Lattnerc9addb72007-03-30 23:15:24 +00007039 // X86 allows a sign-extended 32-bit immediate field as a displacement.
7040 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
7041 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007042
Chris Lattnerc9addb72007-03-30 23:15:24 +00007043 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00007044 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00007045 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
7046 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00007047 // If BaseGV requires a register, we cannot also have a BaseReg.
7048 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
7049 AM.HasBaseReg)
7050 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007051
7052 // X86-64 only supports addr of globals in small code model.
7053 if (Subtarget->is64Bit()) {
7054 if (getTargetMachine().getCodeModel() != CodeModel::Small)
7055 return false;
7056 // If lower 4G is not available, then we must use rip-relative addressing.
7057 if (AM.BaseOffs || AM.Scale > 1)
7058 return false;
7059 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00007060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007061
Chris Lattnerc9addb72007-03-30 23:15:24 +00007062 switch (AM.Scale) {
7063 case 0:
7064 case 1:
7065 case 2:
7066 case 4:
7067 case 8:
7068 // These scales always work.
7069 break;
7070 case 3:
7071 case 5:
7072 case 9:
7073 // These scales are formed with basereg+scalereg. Only accept if there is
7074 // no basereg yet.
7075 if (AM.HasBaseReg)
7076 return false;
7077 break;
7078 default: // Other stuff never works.
7079 return false;
7080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007081
Chris Lattnerc9addb72007-03-30 23:15:24 +00007082 return true;
7083}
7084
7085
Evan Cheng2bd122c2007-10-26 01:56:11 +00007086bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7087 if (!Ty1->isInteger() || !Ty2->isInteger())
7088 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007089 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7090 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007091 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007092 return false;
7093 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007094}
7095
Duncan Sands83ec4b62008-06-06 12:08:01 +00007096bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
7097 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007098 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007099 unsigned NumBits1 = VT1.getSizeInBits();
7100 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007101 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007102 return false;
7103 return Subtarget->is64Bit() || NumBits1 < 64;
7104}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007105
Dan Gohman97121ba2009-04-08 00:15:30 +00007106bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007107 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00007108 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
7109}
7110
7111bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007112 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00007113 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
7114}
7115
Evan Cheng8b944d32009-05-28 00:35:15 +00007116bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
7117 // i16 instructions are longer (0x66 prefix) and potentially slower.
7118 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
7119}
7120
Evan Cheng60c07e12006-07-05 22:17:51 +00007121/// isShuffleMaskLegal - Targets can use this to indicate that they only
7122/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7123/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7124/// are assumed to be legal.
7125bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007126X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
7127 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007128 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007129 if (VT.getSizeInBits() == 64)
7130 return false;
7131
7132 // FIXME: pshufb, blends, palignr, shifts.
7133 return (VT.getVectorNumElements() == 2 ||
7134 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7135 isMOVLMask(M, VT) ||
7136 isSHUFPMask(M, VT) ||
7137 isPSHUFDMask(M, VT) ||
7138 isPSHUFHWMask(M, VT) ||
7139 isPSHUFLWMask(M, VT) ||
7140 isUNPCKLMask(M, VT) ||
7141 isUNPCKHMask(M, VT) ||
7142 isUNPCKL_v_undef_Mask(M, VT) ||
7143 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007144}
7145
Dan Gohman7d8143f2008-04-09 20:09:42 +00007146bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007147X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00007148 MVT VT) const {
7149 unsigned NumElts = VT.getVectorNumElements();
7150 // FIXME: This collection of masks seems suspect.
7151 if (NumElts == 2)
7152 return true;
7153 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7154 return (isMOVLMask(Mask, VT) ||
7155 isCommutedMOVLMask(Mask, VT, true) ||
7156 isSHUFPMask(Mask, VT) ||
7157 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007158 }
7159 return false;
7160}
7161
7162//===----------------------------------------------------------------------===//
7163// X86 Scheduler Hooks
7164//===----------------------------------------------------------------------===//
7165
Mon P Wang63307c32008-05-05 19:05:59 +00007166// private utility function
7167MachineBasicBlock *
7168X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7169 MachineBasicBlock *MBB,
7170 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007171 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007172 unsigned LoadOpc,
7173 unsigned CXchgOpc,
7174 unsigned copyOpc,
7175 unsigned notOpc,
7176 unsigned EAXreg,
7177 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007178 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007179 // For the atomic bitwise operator, we generate
7180 // thisMBB:
7181 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007182 // ld t1 = [bitinstr.addr]
7183 // op t2 = t1, [bitinstr.val]
7184 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007185 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7186 // bz newMBB
7187 // fallthrough -->nextMBB
7188 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7189 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007190 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007191 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007192
Mon P Wang63307c32008-05-05 19:05:59 +00007193 /// First build the CFG
7194 MachineFunction *F = MBB->getParent();
7195 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007196 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7197 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7198 F->insert(MBBIter, newMBB);
7199 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007200
Mon P Wang63307c32008-05-05 19:05:59 +00007201 // Move all successors to thisMBB to nextMBB
7202 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007203
Mon P Wang63307c32008-05-05 19:05:59 +00007204 // Update thisMBB to fall through to newMBB
7205 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007206
Mon P Wang63307c32008-05-05 19:05:59 +00007207 // newMBB jumps to itself and fall through to nextMBB
7208 newMBB->addSuccessor(nextMBB);
7209 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007210
Mon P Wang63307c32008-05-05 19:05:59 +00007211 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007212 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007213 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007214 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007215 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007216 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007217 int numArgs = bInstr->getNumOperands() - 1;
7218 for (int i=0; i < numArgs; ++i)
7219 argOpers[i] = &bInstr->getOperand(i+1);
7220
7221 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007222 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7223 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007224
Dale Johannesen140be2d2008-08-19 18:47:28 +00007225 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007226 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007227 for (int i=0; i <= lastAddrIndx; ++i)
7228 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007229
Dale Johannesen140be2d2008-08-19 18:47:28 +00007230 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007231 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007232 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007233 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007234 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007235 tt = t1;
7236
Dale Johannesen140be2d2008-08-19 18:47:28 +00007237 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007238 assert((argOpers[valArgIndx]->isReg() ||
7239 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007240 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007241 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007242 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007243 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007244 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007245 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007246 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007247
Dale Johannesene4d209d2009-02-03 20:21:25 +00007248 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007249 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007250
Dale Johannesene4d209d2009-02-03 20:21:25 +00007251 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007252 for (int i=0; i <= lastAddrIndx; ++i)
7253 (*MIB).addOperand(*argOpers[i]);
7254 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007255 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7256 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7257
Dale Johannesene4d209d2009-02-03 20:21:25 +00007258 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007259 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007260
Mon P Wang63307c32008-05-05 19:05:59 +00007261 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007262 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007263
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007264 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007265 return nextMBB;
7266}
7267
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007268// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007269MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007270X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7271 MachineBasicBlock *MBB,
7272 unsigned regOpcL,
7273 unsigned regOpcH,
7274 unsigned immOpcL,
7275 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007276 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007277 // For the atomic bitwise operator, we generate
7278 // thisMBB (instructions are in pairs, except cmpxchg8b)
7279 // ld t1,t2 = [bitinstr.addr]
7280 // newMBB:
7281 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7282 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007283 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007284 // mov ECX, EBX <- t5, t6
7285 // mov EAX, EDX <- t1, t2
7286 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7287 // mov t3, t4 <- EAX, EDX
7288 // bz newMBB
7289 // result in out1, out2
7290 // fallthrough -->nextMBB
7291
7292 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7293 const unsigned LoadOpc = X86::MOV32rm;
7294 const unsigned copyOpc = X86::MOV32rr;
7295 const unsigned NotOpc = X86::NOT32r;
7296 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7297 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7298 MachineFunction::iterator MBBIter = MBB;
7299 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007300
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007301 /// First build the CFG
7302 MachineFunction *F = MBB->getParent();
7303 MachineBasicBlock *thisMBB = MBB;
7304 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7305 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7306 F->insert(MBBIter, newMBB);
7307 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007308
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007309 // Move all successors to thisMBB to nextMBB
7310 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007311
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007312 // Update thisMBB to fall through to newMBB
7313 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007314
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007315 // newMBB jumps to itself and fall through to nextMBB
7316 newMBB->addSuccessor(nextMBB);
7317 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007318
Dale Johannesene4d209d2009-02-03 20:21:25 +00007319 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007320 // Insert instructions into newMBB based on incoming instruction
7321 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007322 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007323 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007324 MachineOperand& dest1Oper = bInstr->getOperand(0);
7325 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007326 MachineOperand* argOpers[2 + X86AddrNumOperands];
7327 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007328 argOpers[i] = &bInstr->getOperand(i+2);
7329
7330 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007331 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007332
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007333 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007334 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007335 for (int i=0; i <= lastAddrIndx; ++i)
7336 (*MIB).addOperand(*argOpers[i]);
7337 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007338 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007339 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007340 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007341 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007342 MachineOperand newOp3 = *(argOpers[3]);
7343 if (newOp3.isImm())
7344 newOp3.setImm(newOp3.getImm()+4);
7345 else
7346 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007347 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007348 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007349
7350 // t3/4 are defined later, at the bottom of the loop
7351 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7352 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007353 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007354 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007355 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007356 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7357
7358 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7359 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007360 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007361 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7362 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007363 } else {
7364 tt1 = t1;
7365 tt2 = t2;
7366 }
7367
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007368 int valArgIndx = lastAddrIndx + 1;
7369 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007370 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007371 "invalid operand");
7372 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7373 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007374 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007375 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007376 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007377 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007378 if (regOpcL != X86::MOV32rr)
7379 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007380 (*MIB).addOperand(*argOpers[valArgIndx]);
7381 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007382 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007383 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007384 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007385 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007386 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007387 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007388 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007389 if (regOpcH != X86::MOV32rr)
7390 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007391 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007392
Dale Johannesene4d209d2009-02-03 20:21:25 +00007393 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007394 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007395 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007396 MIB.addReg(t2);
7397
Dale Johannesene4d209d2009-02-03 20:21:25 +00007398 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007399 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007400 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007401 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007402
Dale Johannesene4d209d2009-02-03 20:21:25 +00007403 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007404 for (int i=0; i <= lastAddrIndx; ++i)
7405 (*MIB).addOperand(*argOpers[i]);
7406
7407 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7408 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7409
Dale Johannesene4d209d2009-02-03 20:21:25 +00007410 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007411 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007413 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007414
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007415 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007416 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007417
7418 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7419 return nextMBB;
7420}
7421
7422// private utility function
7423MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007424X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7425 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007426 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007427 // For the atomic min/max operator, we generate
7428 // thisMBB:
7429 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007430 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007431 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007432 // cmp t1, t2
7433 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007434 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007435 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7436 // bz newMBB
7437 // fallthrough -->nextMBB
7438 //
7439 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7440 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007441 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007442 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007443
Mon P Wang63307c32008-05-05 19:05:59 +00007444 /// First build the CFG
7445 MachineFunction *F = MBB->getParent();
7446 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007447 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7448 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7449 F->insert(MBBIter, newMBB);
7450 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007451
Mon P Wang63307c32008-05-05 19:05:59 +00007452 // Move all successors to thisMBB to nextMBB
7453 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007454
Mon P Wang63307c32008-05-05 19:05:59 +00007455 // Update thisMBB to fall through to newMBB
7456 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007457
Mon P Wang63307c32008-05-05 19:05:59 +00007458 // newMBB jumps to newMBB and fall through to nextMBB
7459 newMBB->addSuccessor(nextMBB);
7460 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007461
Dale Johannesene4d209d2009-02-03 20:21:25 +00007462 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007463 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007464 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007465 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007466 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007467 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007468 int numArgs = mInstr->getNumOperands() - 1;
7469 for (int i=0; i < numArgs; ++i)
7470 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007471
Mon P Wang63307c32008-05-05 19:05:59 +00007472 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007473 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7474 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007475
Mon P Wangab3e7472008-05-05 22:56:23 +00007476 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007477 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007478 for (int i=0; i <= lastAddrIndx; ++i)
7479 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007480
Mon P Wang63307c32008-05-05 19:05:59 +00007481 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007482 assert((argOpers[valArgIndx]->isReg() ||
7483 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007484 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007485
7486 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007487 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007488 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007489 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007490 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007491 (*MIB).addOperand(*argOpers[valArgIndx]);
7492
Dale Johannesene4d209d2009-02-03 20:21:25 +00007493 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007494 MIB.addReg(t1);
7495
Dale Johannesene4d209d2009-02-03 20:21:25 +00007496 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007497 MIB.addReg(t1);
7498 MIB.addReg(t2);
7499
7500 // Generate movc
7501 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007502 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007503 MIB.addReg(t2);
7504 MIB.addReg(t1);
7505
7506 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007507 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007508 for (int i=0; i <= lastAddrIndx; ++i)
7509 (*MIB).addOperand(*argOpers[i]);
7510 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007511 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7512 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007513
Dale Johannesene4d209d2009-02-03 20:21:25 +00007514 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007515 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007516
Mon P Wang63307c32008-05-05 19:05:59 +00007517 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007518 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007519
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007520 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007521 return nextMBB;
7522}
7523
7524
Evan Cheng60c07e12006-07-05 22:17:51 +00007525MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007526X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007527 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007528 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007530 switch (MI->getOpcode()) {
7531 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007532 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007533 case X86::CMOV_FR32:
7534 case X86::CMOV_FR64:
7535 case X86::CMOV_V4F32:
7536 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007537 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007538 // To "insert" a SELECT_CC instruction, we actually have to insert the
7539 // diamond control-flow pattern. The incoming instruction knows the
7540 // destination vreg to set, the condition code register to branch on, the
7541 // true/false values to select between, and a branch opcode to use.
7542 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007543 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007544 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007545
Evan Cheng60c07e12006-07-05 22:17:51 +00007546 // thisMBB:
7547 // ...
7548 // TrueVal = ...
7549 // cmpTY ccX, r1, r2
7550 // bCC copy1MBB
7551 // fallthrough --> copy0MBB
7552 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007553 MachineFunction *F = BB->getParent();
7554 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7555 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007556 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007557 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007558 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007559 F->insert(It, copy0MBB);
7560 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007561 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007562 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007563 sinkMBB->transferSuccessors(BB);
7564
7565 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007566 BB->addSuccessor(copy0MBB);
7567 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007568
Evan Cheng60c07e12006-07-05 22:17:51 +00007569 // copy0MBB:
7570 // %FalseValue = ...
7571 // # fallthrough to sinkMBB
7572 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007573
Evan Cheng60c07e12006-07-05 22:17:51 +00007574 // Update machine-CFG edges
7575 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007576
Evan Cheng60c07e12006-07-05 22:17:51 +00007577 // sinkMBB:
7578 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7579 // ...
7580 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007581 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007582 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7583 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7584
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007585 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007586 return BB;
7587 }
7588
Dale Johannesen849f2142007-07-03 00:53:03 +00007589 case X86::FP32_TO_INT16_IN_MEM:
7590 case X86::FP32_TO_INT32_IN_MEM:
7591 case X86::FP32_TO_INT64_IN_MEM:
7592 case X86::FP64_TO_INT16_IN_MEM:
7593 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007594 case X86::FP64_TO_INT64_IN_MEM:
7595 case X86::FP80_TO_INT16_IN_MEM:
7596 case X86::FP80_TO_INT32_IN_MEM:
7597 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007598 // Change the floating point control register to use "round towards zero"
7599 // mode when truncating to an integer value.
7600 MachineFunction *F = BB->getParent();
7601 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007602 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007603
7604 // Load the old value of the high byte of the control word...
7605 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007606 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007607 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007608 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007609
7610 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007611 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007612 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007613
7614 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007615 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007616
7617 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007618 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007619 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007620
7621 // Get the X86 opcode to use.
7622 unsigned Opc;
7623 switch (MI->getOpcode()) {
7624 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007625 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7626 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7627 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7628 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7629 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7630 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007631 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7632 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7633 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007634 }
7635
7636 X86AddressMode AM;
7637 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007638 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007639 AM.BaseType = X86AddressMode::RegBase;
7640 AM.Base.Reg = Op.getReg();
7641 } else {
7642 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007643 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007644 }
7645 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007646 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007647 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007648 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007649 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007650 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007651 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007652 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007653 AM.GV = Op.getGlobal();
7654 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007655 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007656 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007657 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007658 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007659
7660 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007661 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007662
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007663 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007664 return BB;
7665 }
Mon P Wang63307c32008-05-05 19:05:59 +00007666 case X86::ATOMAND32:
7667 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007668 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007669 X86::LCMPXCHG32, X86::MOV32rr,
7670 X86::NOT32r, X86::EAX,
7671 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007672 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007673 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7674 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007675 X86::LCMPXCHG32, X86::MOV32rr,
7676 X86::NOT32r, X86::EAX,
7677 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007678 case X86::ATOMXOR32:
7679 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007680 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007681 X86::LCMPXCHG32, X86::MOV32rr,
7682 X86::NOT32r, X86::EAX,
7683 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007684 case X86::ATOMNAND32:
7685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007686 X86::AND32ri, X86::MOV32rm,
7687 X86::LCMPXCHG32, X86::MOV32rr,
7688 X86::NOT32r, X86::EAX,
7689 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007690 case X86::ATOMMIN32:
7691 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7692 case X86::ATOMMAX32:
7693 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7694 case X86::ATOMUMIN32:
7695 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7696 case X86::ATOMUMAX32:
7697 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007698
7699 case X86::ATOMAND16:
7700 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7701 X86::AND16ri, X86::MOV16rm,
7702 X86::LCMPXCHG16, X86::MOV16rr,
7703 X86::NOT16r, X86::AX,
7704 X86::GR16RegisterClass);
7705 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007706 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007707 X86::OR16ri, X86::MOV16rm,
7708 X86::LCMPXCHG16, X86::MOV16rr,
7709 X86::NOT16r, X86::AX,
7710 X86::GR16RegisterClass);
7711 case X86::ATOMXOR16:
7712 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7713 X86::XOR16ri, X86::MOV16rm,
7714 X86::LCMPXCHG16, X86::MOV16rr,
7715 X86::NOT16r, X86::AX,
7716 X86::GR16RegisterClass);
7717 case X86::ATOMNAND16:
7718 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7719 X86::AND16ri, X86::MOV16rm,
7720 X86::LCMPXCHG16, X86::MOV16rr,
7721 X86::NOT16r, X86::AX,
7722 X86::GR16RegisterClass, true);
7723 case X86::ATOMMIN16:
7724 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7725 case X86::ATOMMAX16:
7726 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7727 case X86::ATOMUMIN16:
7728 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7729 case X86::ATOMUMAX16:
7730 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7731
7732 case X86::ATOMAND8:
7733 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7734 X86::AND8ri, X86::MOV8rm,
7735 X86::LCMPXCHG8, X86::MOV8rr,
7736 X86::NOT8r, X86::AL,
7737 X86::GR8RegisterClass);
7738 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007739 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007740 X86::OR8ri, X86::MOV8rm,
7741 X86::LCMPXCHG8, X86::MOV8rr,
7742 X86::NOT8r, X86::AL,
7743 X86::GR8RegisterClass);
7744 case X86::ATOMXOR8:
7745 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7746 X86::XOR8ri, X86::MOV8rm,
7747 X86::LCMPXCHG8, X86::MOV8rr,
7748 X86::NOT8r, X86::AL,
7749 X86::GR8RegisterClass);
7750 case X86::ATOMNAND8:
7751 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7752 X86::AND8ri, X86::MOV8rm,
7753 X86::LCMPXCHG8, X86::MOV8rr,
7754 X86::NOT8r, X86::AL,
7755 X86::GR8RegisterClass, true);
7756 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007757 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007758 case X86::ATOMAND64:
7759 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007760 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007761 X86::LCMPXCHG64, X86::MOV64rr,
7762 X86::NOT64r, X86::RAX,
7763 X86::GR64RegisterClass);
7764 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007765 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7766 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007767 X86::LCMPXCHG64, X86::MOV64rr,
7768 X86::NOT64r, X86::RAX,
7769 X86::GR64RegisterClass);
7770 case X86::ATOMXOR64:
7771 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007772 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007773 X86::LCMPXCHG64, X86::MOV64rr,
7774 X86::NOT64r, X86::RAX,
7775 X86::GR64RegisterClass);
7776 case X86::ATOMNAND64:
7777 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7778 X86::AND64ri32, X86::MOV64rm,
7779 X86::LCMPXCHG64, X86::MOV64rr,
7780 X86::NOT64r, X86::RAX,
7781 X86::GR64RegisterClass, true);
7782 case X86::ATOMMIN64:
7783 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7784 case X86::ATOMMAX64:
7785 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7786 case X86::ATOMUMIN64:
7787 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7788 case X86::ATOMUMAX64:
7789 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007790
7791 // This group does 64-bit operations on a 32-bit host.
7792 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007793 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007794 X86::AND32rr, X86::AND32rr,
7795 X86::AND32ri, X86::AND32ri,
7796 false);
7797 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007798 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007799 X86::OR32rr, X86::OR32rr,
7800 X86::OR32ri, X86::OR32ri,
7801 false);
7802 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007803 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007804 X86::XOR32rr, X86::XOR32rr,
7805 X86::XOR32ri, X86::XOR32ri,
7806 false);
7807 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007808 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007809 X86::AND32rr, X86::AND32rr,
7810 X86::AND32ri, X86::AND32ri,
7811 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007812 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007813 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007814 X86::ADD32rr, X86::ADC32rr,
7815 X86::ADD32ri, X86::ADC32ri,
7816 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007817 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007818 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007819 X86::SUB32rr, X86::SBB32rr,
7820 X86::SUB32ri, X86::SBB32ri,
7821 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007822 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007823 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007824 X86::MOV32rr, X86::MOV32rr,
7825 X86::MOV32ri, X86::MOV32ri,
7826 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007827 }
7828}
7829
7830//===----------------------------------------------------------------------===//
7831// X86 Optimization Hooks
7832//===----------------------------------------------------------------------===//
7833
Dan Gohman475871a2008-07-27 21:46:04 +00007834void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007835 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007836 APInt &KnownZero,
7837 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007838 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007839 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007840 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007841 assert((Opc >= ISD::BUILTIN_OP_END ||
7842 Opc == ISD::INTRINSIC_WO_CHAIN ||
7843 Opc == ISD::INTRINSIC_W_CHAIN ||
7844 Opc == ISD::INTRINSIC_VOID) &&
7845 "Should use MaskedValueIsZero if you don't know whether Op"
7846 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007847
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007848 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007849 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007850 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007851 case X86ISD::ADD:
7852 case X86ISD::SUB:
7853 case X86ISD::SMUL:
7854 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007855 case X86ISD::INC:
7856 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007857 // These nodes' second result is a boolean.
7858 if (Op.getResNo() == 0)
7859 break;
7860 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007861 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007862 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7863 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007864 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007865 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007866}
Chris Lattner259e97c2006-01-31 19:43:35 +00007867
Evan Cheng206ee9d2006-07-07 08:33:52 +00007868/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007869/// node is a GlobalAddress + offset.
7870bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7871 GlobalValue* &GA, int64_t &Offset) const{
7872 if (N->getOpcode() == X86ISD::Wrapper) {
7873 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007874 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007875 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007876 return true;
7877 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007878 }
Evan Chengad4196b2008-05-12 19:56:52 +00007879 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007880}
7881
Evan Chengad4196b2008-05-12 19:56:52 +00007882static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7883 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007884 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007885 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007886 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007887 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007888 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007889 return false;
7890}
7891
Nate Begeman9008ca62009-04-27 18:41:29 +00007892static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Eli Friedman7a5e5552009-06-07 06:52:44 +00007893 MVT EVT, LoadSDNode *&LDBase,
7894 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00007895 SelectionDAG &DAG, MachineFrameInfo *MFI,
7896 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007897 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00007898 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007899 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007900 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007901 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00007902 return false;
7903 continue;
7904 }
7905
Dan Gohman475871a2008-07-27 21:46:04 +00007906 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007907 if (!Elt.getNode() ||
7908 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007909 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007910 if (!LDBase) {
7911 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00007912 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007913 LDBase = cast<LoadSDNode>(Elt.getNode());
7914 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007915 continue;
7916 }
7917 if (Elt.getOpcode() == ISD::UNDEF)
7918 continue;
7919
Nate Begemanabc01992009-06-05 21:37:30 +00007920 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Nate Begemanabc01992009-06-05 21:37:30 +00007921 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007922 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007923 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007924 }
7925 return true;
7926}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007927
7928/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7929/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7930/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007931/// order. In the case of v2i64, it will see if it can rewrite the
7932/// shuffle to be an appropriate build vector so it can take advantage of
7933// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007934static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007935 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007936 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007937 MVT VT = N->getValueType(0);
7938 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007939 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7940 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007941
Eli Friedman7a5e5552009-06-07 06:52:44 +00007942 if (VT.getSizeInBits() != 128)
7943 return SDValue();
7944
Mon P Wang1e955802009-04-03 02:43:30 +00007945 // Try to combine a vector_shuffle into a 128-bit load.
7946 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00007947 LoadSDNode *LD = NULL;
7948 unsigned LastLoadedElt;
7949 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG,
7950 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007951 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007952
Eli Friedman7a5e5552009-06-07 06:52:44 +00007953 if (LastLoadedElt == NumElems - 1) {
7954 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI))
7955 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7956 LD->getSrcValue(), LD->getSrcValueOffset(),
7957 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007959 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00007960 LD->isVolatile(), LD->getAlignment());
7961 } else if (NumElems == 4 && LastLoadedElt == 1) {
7962 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00007963 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7964 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00007965 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7966 }
7967 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007968}
Evan Chengd880b972008-05-09 21:53:03 +00007969
Chris Lattner83e6c992006-10-04 06:57:07 +00007970/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007971static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007972 const X86Subtarget *Subtarget) {
7973 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007974 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007975 // Get the LHS/RHS of the select.
7976 SDValue LHS = N->getOperand(1);
7977 SDValue RHS = N->getOperand(2);
7978
Chris Lattner83e6c992006-10-04 06:57:07 +00007979 // If we have SSE[12] support, try to form min/max nodes.
7980 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007981 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7982 Cond.getOpcode() == ISD::SETCC) {
7983 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007984
Chris Lattner47b4ce82009-03-11 05:48:52 +00007985 unsigned Opcode = 0;
7986 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7987 switch (CC) {
7988 default: break;
7989 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7990 case ISD::SETULE:
7991 case ISD::SETLE:
7992 if (!UnsafeFPMath) break;
7993 // FALL THROUGH.
7994 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7995 case ISD::SETLT:
7996 Opcode = X86ISD::FMIN;
7997 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007998
Chris Lattner47b4ce82009-03-11 05:48:52 +00007999 case ISD::SETOGT: // (X > Y) ? X : Y -> max
8000 case ISD::SETUGT:
8001 case ISD::SETGT:
8002 if (!UnsafeFPMath) break;
8003 // FALL THROUGH.
8004 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
8005 case ISD::SETGE:
8006 Opcode = X86ISD::FMAX;
8007 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008008 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008009 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8010 switch (CC) {
8011 default: break;
8012 case ISD::SETOGT: // (X > Y) ? Y : X -> min
8013 case ISD::SETUGT:
8014 case ISD::SETGT:
8015 if (!UnsafeFPMath) break;
8016 // FALL THROUGH.
8017 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
8018 case ISD::SETGE:
8019 Opcode = X86ISD::FMIN;
8020 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008021
Chris Lattner47b4ce82009-03-11 05:48:52 +00008022 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
8023 case ISD::SETULE:
8024 case ISD::SETLE:
8025 if (!UnsafeFPMath) break;
8026 // FALL THROUGH.
8027 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
8028 case ISD::SETLT:
8029 Opcode = X86ISD::FMAX;
8030 break;
8031 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008032 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008033
Chris Lattner47b4ce82009-03-11 05:48:52 +00008034 if (Opcode)
8035 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008036 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008037
Chris Lattnerd1980a52009-03-12 06:52:53 +00008038 // If this is a select between two integer constants, try to do some
8039 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008040 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8041 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008042 // Don't do this for crazy integer types.
8043 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8044 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008045 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008046 bool NeedsCondInvert = false;
8047
Chris Lattnercee56e72009-03-13 05:53:31 +00008048 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008049 // Efficiently invertible.
8050 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8051 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8052 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8053 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008054 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008055 }
8056
8057 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008058 if (FalseC->getAPIntValue() == 0 &&
8059 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008060 if (NeedsCondInvert) // Invert the condition if needed.
8061 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8062 DAG.getConstant(1, Cond.getValueType()));
8063
8064 // Zero extend the condition if needed.
8065 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
8066
Chris Lattnercee56e72009-03-13 05:53:31 +00008067 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008068 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
8069 DAG.getConstant(ShAmt, MVT::i8));
8070 }
Chris Lattner97a29a52009-03-13 05:22:11 +00008071
8072 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008073 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008074 if (NeedsCondInvert) // Invert the condition if needed.
8075 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8076 DAG.getConstant(1, Cond.getValueType()));
8077
8078 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008079 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8080 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008081 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008082 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008083 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008084
8085 // Optimize cases that will turn into an LEA instruction. This requires
8086 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8087 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8088 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8089 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8090
8091 bool isFastMultiplier = false;
8092 if (Diff < 10) {
8093 switch ((unsigned char)Diff) {
8094 default: break;
8095 case 1: // result = add base, cond
8096 case 2: // result = lea base( , cond*2)
8097 case 3: // result = lea base(cond, cond*2)
8098 case 4: // result = lea base( , cond*4)
8099 case 5: // result = lea base(cond, cond*4)
8100 case 8: // result = lea base( , cond*8)
8101 case 9: // result = lea base(cond, cond*8)
8102 isFastMultiplier = true;
8103 break;
8104 }
8105 }
8106
8107 if (isFastMultiplier) {
8108 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8109 if (NeedsCondInvert) // Invert the condition if needed.
8110 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8111 DAG.getConstant(1, Cond.getValueType()));
8112
8113 // Zero extend the condition if needed.
8114 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8115 Cond);
8116 // Scale the condition by the difference.
8117 if (Diff != 1)
8118 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8119 DAG.getConstant(Diff, Cond.getValueType()));
8120
8121 // Add the base if non-zero.
8122 if (FalseC->getAPIntValue() != 0)
8123 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8124 SDValue(FalseC, 0));
8125 return Cond;
8126 }
8127 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008128 }
8129 }
8130
Dan Gohman475871a2008-07-27 21:46:04 +00008131 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008132}
8133
Chris Lattnerd1980a52009-03-12 06:52:53 +00008134/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8135static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8136 TargetLowering::DAGCombinerInfo &DCI) {
8137 DebugLoc DL = N->getDebugLoc();
8138
8139 // If the flag operand isn't dead, don't touch this CMOV.
8140 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8141 return SDValue();
8142
8143 // If this is a select between two integer constants, try to do some
8144 // optimizations. Note that the operands are ordered the opposite of SELECT
8145 // operands.
8146 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8147 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8148 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8149 // larger than FalseC (the false value).
8150 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8151
8152 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8153 CC = X86::GetOppositeBranchCondition(CC);
8154 std::swap(TrueC, FalseC);
8155 }
8156
8157 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008158 // This is efficient for any integer data type (including i8/i16) and
8159 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008160 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8161 SDValue Cond = N->getOperand(3);
8162 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8163 DAG.getConstant(CC, MVT::i8), Cond);
8164
8165 // Zero extend the condition if needed.
8166 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8167
8168 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8169 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8170 DAG.getConstant(ShAmt, MVT::i8));
8171 if (N->getNumValues() == 2) // Dead flag value?
8172 return DCI.CombineTo(N, Cond, SDValue());
8173 return Cond;
8174 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008175
8176 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8177 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008178 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8179 SDValue Cond = N->getOperand(3);
8180 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8181 DAG.getConstant(CC, MVT::i8), Cond);
8182
8183 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008184 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8185 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008186 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8187 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008188
Chris Lattner97a29a52009-03-13 05:22:11 +00008189 if (N->getNumValues() == 2) // Dead flag value?
8190 return DCI.CombineTo(N, Cond, SDValue());
8191 return Cond;
8192 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008193
8194 // Optimize cases that will turn into an LEA instruction. This requires
8195 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8196 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8197 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8198 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8199
8200 bool isFastMultiplier = false;
8201 if (Diff < 10) {
8202 switch ((unsigned char)Diff) {
8203 default: break;
8204 case 1: // result = add base, cond
8205 case 2: // result = lea base( , cond*2)
8206 case 3: // result = lea base(cond, cond*2)
8207 case 4: // result = lea base( , cond*4)
8208 case 5: // result = lea base(cond, cond*4)
8209 case 8: // result = lea base( , cond*8)
8210 case 9: // result = lea base(cond, cond*8)
8211 isFastMultiplier = true;
8212 break;
8213 }
8214 }
8215
8216 if (isFastMultiplier) {
8217 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8218 SDValue Cond = N->getOperand(3);
8219 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8220 DAG.getConstant(CC, MVT::i8), Cond);
8221 // Zero extend the condition if needed.
8222 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8223 Cond);
8224 // Scale the condition by the difference.
8225 if (Diff != 1)
8226 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8227 DAG.getConstant(Diff, Cond.getValueType()));
8228
8229 // Add the base if non-zero.
8230 if (FalseC->getAPIntValue() != 0)
8231 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8232 SDValue(FalseC, 0));
8233 if (N->getNumValues() == 2) // Dead flag value?
8234 return DCI.CombineTo(N, Cond, SDValue());
8235 return Cond;
8236 }
8237 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008238 }
8239 }
8240 return SDValue();
8241}
8242
8243
Evan Cheng0b0cd912009-03-28 05:57:29 +00008244/// PerformMulCombine - Optimize a single multiply with constant into two
8245/// in order to implement it with two cheaper instructions, e.g.
8246/// LEA + SHL, LEA + LEA.
8247static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8248 TargetLowering::DAGCombinerInfo &DCI) {
8249 if (DAG.getMachineFunction().
8250 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8251 return SDValue();
8252
8253 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8254 return SDValue();
8255
8256 MVT VT = N->getValueType(0);
8257 if (VT != MVT::i64)
8258 return SDValue();
8259
8260 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8261 if (!C)
8262 return SDValue();
8263 uint64_t MulAmt = C->getZExtValue();
8264 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8265 return SDValue();
8266
8267 uint64_t MulAmt1 = 0;
8268 uint64_t MulAmt2 = 0;
8269 if ((MulAmt % 9) == 0) {
8270 MulAmt1 = 9;
8271 MulAmt2 = MulAmt / 9;
8272 } else if ((MulAmt % 5) == 0) {
8273 MulAmt1 = 5;
8274 MulAmt2 = MulAmt / 5;
8275 } else if ((MulAmt % 3) == 0) {
8276 MulAmt1 = 3;
8277 MulAmt2 = MulAmt / 3;
8278 }
8279 if (MulAmt2 &&
8280 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8281 DebugLoc DL = N->getDebugLoc();
8282
8283 if (isPowerOf2_64(MulAmt2) &&
8284 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8285 // If second multiplifer is pow2, issue it first. We want the multiply by
8286 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8287 // is an add.
8288 std::swap(MulAmt1, MulAmt2);
8289
8290 SDValue NewMul;
8291 if (isPowerOf2_64(MulAmt1))
8292 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8293 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8294 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008295 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008296 DAG.getConstant(MulAmt1, VT));
8297
8298 if (isPowerOf2_64(MulAmt2))
8299 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8300 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8301 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008302 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008303 DAG.getConstant(MulAmt2, VT));
8304
8305 // Do not add new nodes to DAG combiner worklist.
8306 DCI.CombineTo(N, NewMul, false);
8307 }
8308 return SDValue();
8309}
8310
8311
Nate Begeman740ab032009-01-26 00:52:55 +00008312/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8313/// when possible.
8314static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8315 const X86Subtarget *Subtarget) {
8316 // On X86 with SSE2 support, we can transform this to a vector shift if
8317 // all elements are shifted by the same amount. We can't do this in legalize
8318 // because the a constant vector is typically transformed to a constant pool
8319 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008320 if (!Subtarget->hasSSE2())
8321 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008322
Nate Begeman740ab032009-01-26 00:52:55 +00008323 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008324 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8325 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008326
Mon P Wang3becd092009-01-28 08:12:05 +00008327 SDValue ShAmtOp = N->getOperand(1);
8328 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008329 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008330 SDValue BaseShAmt;
8331 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8332 unsigned NumElts = VT.getVectorNumElements();
8333 unsigned i = 0;
8334 for (; i != NumElts; ++i) {
8335 SDValue Arg = ShAmtOp.getOperand(i);
8336 if (Arg.getOpcode() == ISD::UNDEF) continue;
8337 BaseShAmt = Arg;
8338 break;
8339 }
8340 for (; i != NumElts; ++i) {
8341 SDValue Arg = ShAmtOp.getOperand(i);
8342 if (Arg.getOpcode() == ISD::UNDEF) continue;
8343 if (Arg != BaseShAmt) {
8344 return SDValue();
8345 }
8346 }
8347 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008348 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8349 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8350 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008351 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008352 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008353
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008354 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008355 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008356 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008357 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008358
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008359 // The shift amount is identical so we can do a vector shift.
8360 SDValue ValOp = N->getOperand(0);
8361 switch (N->getOpcode()) {
8362 default:
8363 assert(0 && "Unknown shift opcode!");
8364 break;
8365 case ISD::SHL:
8366 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008367 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008368 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8369 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008370 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008371 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008372 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8373 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008374 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008375 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008376 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8377 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008378 break;
8379 case ISD::SRA:
8380 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008381 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008382 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8383 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008384 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008385 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008386 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8387 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008388 break;
8389 case ISD::SRL:
8390 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008392 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8393 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008394 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008396 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8397 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008398 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008399 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008400 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8401 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008402 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008403 }
8404 return SDValue();
8405}
8406
Chris Lattner149a4e52008-02-22 02:09:43 +00008407/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008408static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008409 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008410 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8411 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008412 // A preferable solution to the general problem is to figure out the right
8413 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008414
8415 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008416 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008417 MVT VT = St->getValue().getValueType();
8418 if (VT.getSizeInBits() != 64)
8419 return SDValue();
8420
Devang Patel578efa92009-06-05 21:57:13 +00008421 const Function *F = DAG.getMachineFunction().getFunction();
8422 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8423 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8424 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008425 if ((VT.isVector() ||
8426 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008427 isa<LoadSDNode>(St->getValue()) &&
8428 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8429 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008430 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008431 LoadSDNode *Ld = 0;
8432 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008433 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008434 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008435 // Must be a store of a load. We currently handle two cases: the load
8436 // is a direct child, and it's under an intervening TokenFactor. It is
8437 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008438 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008439 Ld = cast<LoadSDNode>(St->getChain());
8440 else if (St->getValue().hasOneUse() &&
8441 ChainVal->getOpcode() == ISD::TokenFactor) {
8442 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008443 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008444 TokenFactorIndex = i;
8445 Ld = cast<LoadSDNode>(St->getValue());
8446 } else
8447 Ops.push_back(ChainVal->getOperand(i));
8448 }
8449 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008450
Evan Cheng536e6672009-03-12 05:59:15 +00008451 if (!Ld || !ISD::isNormalLoad(Ld))
8452 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008453
Evan Cheng536e6672009-03-12 05:59:15 +00008454 // If this is not the MMX case, i.e. we are just turning i64 load/store
8455 // into f64 load/store, avoid the transformation if there are multiple
8456 // uses of the loaded value.
8457 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8458 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008459
Evan Cheng536e6672009-03-12 05:59:15 +00008460 DebugLoc LdDL = Ld->getDebugLoc();
8461 DebugLoc StDL = N->getDebugLoc();
8462 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8463 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8464 // pair instead.
8465 if (Subtarget->is64Bit() || F64IsLegal) {
8466 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8467 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8468 Ld->getBasePtr(), Ld->getSrcValue(),
8469 Ld->getSrcValueOffset(), Ld->isVolatile(),
8470 Ld->getAlignment());
8471 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008472 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008473 Ops.push_back(NewChain);
8474 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008475 Ops.size());
8476 }
Evan Cheng536e6672009-03-12 05:59:15 +00008477 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008478 St->getSrcValue(), St->getSrcValueOffset(),
8479 St->isVolatile(), St->getAlignment());
8480 }
Evan Cheng536e6672009-03-12 05:59:15 +00008481
8482 // Otherwise, lower to two pairs of 32-bit loads / stores.
8483 SDValue LoAddr = Ld->getBasePtr();
8484 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8485 DAG.getConstant(4, MVT::i32));
8486
8487 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8488 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8489 Ld->isVolatile(), Ld->getAlignment());
8490 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8491 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8492 Ld->isVolatile(),
8493 MinAlign(Ld->getAlignment(), 4));
8494
8495 SDValue NewChain = LoLd.getValue(1);
8496 if (TokenFactorIndex != -1) {
8497 Ops.push_back(LoLd);
8498 Ops.push_back(HiLd);
8499 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8500 Ops.size());
8501 }
8502
8503 LoAddr = St->getBasePtr();
8504 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8505 DAG.getConstant(4, MVT::i32));
8506
8507 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8508 St->getSrcValue(), St->getSrcValueOffset(),
8509 St->isVolatile(), St->getAlignment());
8510 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8511 St->getSrcValue(),
8512 St->getSrcValueOffset() + 4,
8513 St->isVolatile(),
8514 MinAlign(St->getAlignment(), 4));
8515 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008516 }
Dan Gohman475871a2008-07-27 21:46:04 +00008517 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008518}
8519
Chris Lattner6cf73262008-01-25 06:14:17 +00008520/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8521/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008522static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008523 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8524 // F[X]OR(0.0, x) -> x
8525 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008526 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8527 if (C->getValueAPF().isPosZero())
8528 return N->getOperand(1);
8529 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8530 if (C->getValueAPF().isPosZero())
8531 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008532 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008533}
8534
8535/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008536static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008537 // FAND(0.0, x) -> 0.0
8538 // FAND(x, 0.0) -> 0.0
8539 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8540 if (C->getValueAPF().isPosZero())
8541 return N->getOperand(0);
8542 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8543 if (C->getValueAPF().isPosZero())
8544 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008545 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008546}
8547
Dan Gohmane5af2d32009-01-29 01:59:02 +00008548static SDValue PerformBTCombine(SDNode *N,
8549 SelectionDAG &DAG,
8550 TargetLowering::DAGCombinerInfo &DCI) {
8551 // BT ignores high bits in the bit index operand.
8552 SDValue Op1 = N->getOperand(1);
8553 if (Op1.hasOneUse()) {
8554 unsigned BitWidth = Op1.getValueSizeInBits();
8555 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8556 APInt KnownZero, KnownOne;
8557 TargetLowering::TargetLoweringOpt TLO(DAG);
8558 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8559 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8560 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8561 DCI.CommitTargetLoweringOpt(TLO);
8562 }
8563 return SDValue();
8564}
Chris Lattner83e6c992006-10-04 06:57:07 +00008565
Eli Friedman7a5e5552009-06-07 06:52:44 +00008566static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
8567 SDValue Op = N->getOperand(0);
8568 if (Op.getOpcode() == ISD::BIT_CONVERT)
8569 Op = Op.getOperand(0);
8570 MVT VT = N->getValueType(0), OpVT = Op.getValueType();
8571 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
8572 VT.getVectorElementType().getSizeInBits() ==
8573 OpVT.getVectorElementType().getSizeInBits()) {
8574 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
8575 }
8576 return SDValue();
8577}
8578
Owen Anderson99177002009-06-29 18:04:45 +00008579// On X86 and X86-64, atomic operations are lowered to locked instructions.
8580// Locked instructions, in turn, have implicit fence semantics (all memory
8581// operations are flushed before issuing the locked instruction, and the
8582// are not buffered), so we can fold away the common pattern of
8583// fence-atomic-fence.
8584static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
8585 SDValue atomic = N->getOperand(0);
8586 switch (atomic.getOpcode()) {
8587 case ISD::ATOMIC_CMP_SWAP:
8588 case ISD::ATOMIC_SWAP:
8589 case ISD::ATOMIC_LOAD_ADD:
8590 case ISD::ATOMIC_LOAD_SUB:
8591 case ISD::ATOMIC_LOAD_AND:
8592 case ISD::ATOMIC_LOAD_OR:
8593 case ISD::ATOMIC_LOAD_XOR:
8594 case ISD::ATOMIC_LOAD_NAND:
8595 case ISD::ATOMIC_LOAD_MIN:
8596 case ISD::ATOMIC_LOAD_MAX:
8597 case ISD::ATOMIC_LOAD_UMIN:
8598 case ISD::ATOMIC_LOAD_UMAX:
8599 break;
8600 default:
8601 return SDValue();
8602 }
8603
8604 SDValue fence = atomic.getOperand(0);
8605 if (fence.getOpcode() != ISD::MEMBARRIER)
8606 return SDValue();
8607
8608 switch (atomic.getOpcode()) {
8609 case ISD::ATOMIC_CMP_SWAP:
8610 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8611 atomic.getOperand(1), atomic.getOperand(2),
8612 atomic.getOperand(3));
8613 case ISD::ATOMIC_SWAP:
8614 case ISD::ATOMIC_LOAD_ADD:
8615 case ISD::ATOMIC_LOAD_SUB:
8616 case ISD::ATOMIC_LOAD_AND:
8617 case ISD::ATOMIC_LOAD_OR:
8618 case ISD::ATOMIC_LOAD_XOR:
8619 case ISD::ATOMIC_LOAD_NAND:
8620 case ISD::ATOMIC_LOAD_MIN:
8621 case ISD::ATOMIC_LOAD_MAX:
8622 case ISD::ATOMIC_LOAD_UMIN:
8623 case ISD::ATOMIC_LOAD_UMAX:
8624 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8625 atomic.getOperand(1), atomic.getOperand(2));
8626 default:
8627 return SDValue();
8628 }
8629}
8630
Dan Gohman475871a2008-07-27 21:46:04 +00008631SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008632 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008633 SelectionDAG &DAG = DCI.DAG;
8634 switch (N->getOpcode()) {
8635 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008636 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008637 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008638 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008639 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008640 case ISD::SHL:
8641 case ISD::SRA:
8642 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008643 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008644 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008645 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8646 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008647 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00008648 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00008649 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008650 }
8651
Dan Gohman475871a2008-07-27 21:46:04 +00008652 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008653}
8654
Evan Cheng60c07e12006-07-05 22:17:51 +00008655//===----------------------------------------------------------------------===//
8656// X86 Inline Assembly Support
8657//===----------------------------------------------------------------------===//
8658
Chris Lattnerf4dff842006-07-11 02:54:03 +00008659/// getConstraintType - Given a constraint letter, return the type of
8660/// constraint it is for this target.
8661X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008662X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8663 if (Constraint.size() == 1) {
8664 switch (Constraint[0]) {
8665 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008666 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008667 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008668 case 'r':
8669 case 'R':
8670 case 'l':
8671 case 'q':
8672 case 'Q':
8673 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008674 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008675 case 'Y':
8676 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008677 case 'e':
8678 case 'Z':
8679 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008680 default:
8681 break;
8682 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008683 }
Chris Lattner4234f572007-03-25 02:14:49 +00008684 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008685}
8686
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008687/// LowerXConstraint - try to replace an X constraint, which matches anything,
8688/// with another that has more specific requirements based on the type of the
8689/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008690const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008691LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008692 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8693 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008694 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008695 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008696 return "Y";
8697 if (Subtarget->hasSSE1())
8698 return "x";
8699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008700
Chris Lattner5e764232008-04-26 23:02:14 +00008701 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008702}
8703
Chris Lattner48884cd2007-08-25 00:47:38 +00008704/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8705/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008706void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008707 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008708 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008709 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008710 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008711 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008712
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008713 switch (Constraint) {
8714 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008715 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008716 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008717 if (C->getZExtValue() <= 31) {
8718 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008719 break;
8720 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008721 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008722 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008723 case 'J':
8724 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008725 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00008726 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8727 break;
8728 }
8729 }
8730 return;
8731 case 'K':
8732 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008733 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00008734 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8735 break;
8736 }
8737 }
8738 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008739 case 'N':
8740 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008741 if (C->getZExtValue() <= 255) {
8742 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008743 break;
8744 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008745 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008746 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008747 case 'e': {
8748 // 32-bit signed value
8749 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8750 const ConstantInt *CI = C->getConstantIntValue();
8751 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8752 // Widen to 64 bits here to get it sign extended.
8753 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8754 break;
8755 }
8756 // FIXME gcc accepts some relocatable values here too, but only in certain
8757 // memory models; it's complicated.
8758 }
8759 return;
8760 }
8761 case 'Z': {
8762 // 32-bit unsigned value
8763 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8764 const ConstantInt *CI = C->getConstantIntValue();
8765 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8766 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8767 break;
8768 }
8769 }
8770 // FIXME gcc accepts some relocatable values here too, but only in certain
8771 // memory models; it's complicated.
8772 return;
8773 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008774 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008775 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008776 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008777 // Widen to 64 bits here to get it sign extended.
8778 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008779 break;
8780 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008781
Chris Lattnerdc43a882007-05-03 16:52:29 +00008782 // If we are in non-pic codegen mode, we allow the address of a global (with
8783 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008784 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008785 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008786
Chris Lattner49921962009-05-08 18:23:14 +00008787 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8788 while (1) {
8789 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8790 Offset += GA->getOffset();
8791 break;
8792 } else if (Op.getOpcode() == ISD::ADD) {
8793 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8794 Offset += C->getZExtValue();
8795 Op = Op.getOperand(0);
8796 continue;
8797 }
8798 } else if (Op.getOpcode() == ISD::SUB) {
8799 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8800 Offset += -C->getZExtValue();
8801 Op = Op.getOperand(0);
8802 continue;
8803 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008804 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008805
Chris Lattner49921962009-05-08 18:23:14 +00008806 // Otherwise, this isn't something we can handle, reject it.
8807 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008808 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008809 // If we require an extra load to get this address, as in PIC mode, we
8810 // can't accept it.
8811 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(),
8812 getTargetMachine(), false))
8813 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00008814
Chris Lattner49921962009-05-08 18:23:14 +00008815 if (hasMemory)
8816 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8817 else
8818 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8819 Offset);
8820 Result = Op;
8821 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008822 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008824
Gabor Greifba36cb52008-08-28 21:40:38 +00008825 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008826 Ops.push_back(Result);
8827 return;
8828 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008829 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8830 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008831}
8832
Chris Lattner259e97c2006-01-31 19:43:35 +00008833std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008834getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008835 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008836 if (Constraint.size() == 1) {
8837 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008838 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008839 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008840 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8841 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008842 if (VT == MVT::i32)
8843 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8844 else if (VT == MVT::i16)
8845 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8846 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008847 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008848 else if (VT == MVT::i64)
8849 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8850 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008851 }
8852 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008853
Chris Lattner1efa40f2006-02-22 00:56:39 +00008854 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008855}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008856
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008857std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008858X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008859 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008860 // First, see if this is a constraint that directly corresponds to an LLVM
8861 // register class.
8862 if (Constraint.size() == 1) {
8863 // GCC Constraint Letters
8864 switch (Constraint[0]) {
8865 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008866 case 'r': // GENERAL_REGS
8867 case 'R': // LEGACY_REGS
8868 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008869 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008870 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008871 if (VT == MVT::i16)
8872 return std::make_pair(0U, X86::GR16RegisterClass);
8873 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008874 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008875 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008876 case 'f': // FP Stack registers.
8877 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8878 // value to the correct fpstack register class.
8879 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8880 return std::make_pair(0U, X86::RFP32RegisterClass);
8881 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8882 return std::make_pair(0U, X86::RFP64RegisterClass);
8883 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008884 case 'y': // MMX_REGS if MMX allowed.
8885 if (!Subtarget->hasMMX()) break;
8886 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008887 case 'Y': // SSE_REGS if SSE2 allowed
8888 if (!Subtarget->hasSSE2()) break;
8889 // FALL THROUGH.
8890 case 'x': // SSE_REGS if SSE1 allowed
8891 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008892
8893 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008894 default: break;
8895 // Scalar SSE types.
8896 case MVT::f32:
8897 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008898 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008899 case MVT::f64:
8900 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008901 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008902 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008903 case MVT::v16i8:
8904 case MVT::v8i16:
8905 case MVT::v4i32:
8906 case MVT::v2i64:
8907 case MVT::v4f32:
8908 case MVT::v2f64:
8909 return std::make_pair(0U, X86::VR128RegisterClass);
8910 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008911 break;
8912 }
8913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008914
Chris Lattnerf76d1802006-07-31 23:26:50 +00008915 // Use the default implementation in TargetLowering to convert the register
8916 // constraint into a member of a register class.
8917 std::pair<unsigned, const TargetRegisterClass*> Res;
8918 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008919
8920 // Not found as a standard register?
8921 if (Res.second == 0) {
8922 // GCC calls "st(0)" just plain "st".
8923 if (StringsEqualNoCase("{st}", Constraint)) {
8924 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008925 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008926 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008927 // 'A' means EAX + EDX.
8928 if (Constraint == "A") {
8929 Res.first = X86::EAX;
8930 Res.second = X86::GRADRegisterClass;
8931 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008932 return Res;
8933 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008934
Chris Lattnerf76d1802006-07-31 23:26:50 +00008935 // Otherwise, check to see if this is a register class of the wrong value
8936 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8937 // turn into {ax},{dx}.
8938 if (Res.second->hasType(VT))
8939 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008940
Chris Lattnerf76d1802006-07-31 23:26:50 +00008941 // All of the single-register GCC register classes map their values onto
8942 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8943 // really want an 8-bit or 32-bit register, map to the appropriate register
8944 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008945 if (Res.second == X86::GR16RegisterClass) {
8946 if (VT == MVT::i8) {
8947 unsigned DestReg = 0;
8948 switch (Res.first) {
8949 default: break;
8950 case X86::AX: DestReg = X86::AL; break;
8951 case X86::DX: DestReg = X86::DL; break;
8952 case X86::CX: DestReg = X86::CL; break;
8953 case X86::BX: DestReg = X86::BL; break;
8954 }
8955 if (DestReg) {
8956 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008957 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008958 }
8959 } else if (VT == MVT::i32) {
8960 unsigned DestReg = 0;
8961 switch (Res.first) {
8962 default: break;
8963 case X86::AX: DestReg = X86::EAX; break;
8964 case X86::DX: DestReg = X86::EDX; break;
8965 case X86::CX: DestReg = X86::ECX; break;
8966 case X86::BX: DestReg = X86::EBX; break;
8967 case X86::SI: DestReg = X86::ESI; break;
8968 case X86::DI: DestReg = X86::EDI; break;
8969 case X86::BP: DestReg = X86::EBP; break;
8970 case X86::SP: DestReg = X86::ESP; break;
8971 }
8972 if (DestReg) {
8973 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008974 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008975 }
8976 } else if (VT == MVT::i64) {
8977 unsigned DestReg = 0;
8978 switch (Res.first) {
8979 default: break;
8980 case X86::AX: DestReg = X86::RAX; break;
8981 case X86::DX: DestReg = X86::RDX; break;
8982 case X86::CX: DestReg = X86::RCX; break;
8983 case X86::BX: DestReg = X86::RBX; break;
8984 case X86::SI: DestReg = X86::RSI; break;
8985 case X86::DI: DestReg = X86::RDI; break;
8986 case X86::BP: DestReg = X86::RBP; break;
8987 case X86::SP: DestReg = X86::RSP; break;
8988 }
8989 if (DestReg) {
8990 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008991 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008992 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008993 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008994 } else if (Res.second == X86::FR32RegisterClass ||
8995 Res.second == X86::FR64RegisterClass ||
8996 Res.second == X86::VR128RegisterClass) {
8997 // Handle references to XMM physical registers that got mapped into the
8998 // wrong class. This can happen with constraints like {xmm0} where the
8999 // target independent register mapper will just pick the first match it can
9000 // find, ignoring the required type.
9001 if (VT == MVT::f32)
9002 Res.second = X86::FR32RegisterClass;
9003 else if (VT == MVT::f64)
9004 Res.second = X86::FR64RegisterClass;
9005 else if (X86::VR128RegisterClass->hasType(VT))
9006 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00009007 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009008
Chris Lattnerf76d1802006-07-31 23:26:50 +00009009 return Res;
9010}
Mon P Wang0c397192008-10-30 08:01:45 +00009011
9012//===----------------------------------------------------------------------===//
9013// X86 Widen vector type
9014//===----------------------------------------------------------------------===//
9015
9016/// getWidenVectorType: given a vector type, returns the type to widen
9017/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
9018/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00009019/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00009020/// scalarizing vs using the wider vector type.
9021
Dan Gohmanc13cf132009-01-15 17:34:08 +00009022MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00009023 assert(VT.isVector());
9024 if (isTypeLegal(VT))
9025 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009026
Mon P Wang0c397192008-10-30 08:01:45 +00009027 // TODO: In computeRegisterProperty, we can compute the list of legal vector
9028 // type based on element type. This would speed up our search (though
9029 // it may not be worth it since the size of the list is relatively
9030 // small).
9031 MVT EltVT = VT.getVectorElementType();
9032 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00009033
Mon P Wang0c397192008-10-30 08:01:45 +00009034 // On X86, it make sense to widen any vector wider than 1
9035 if (NElts <= 1)
9036 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00009037
9038 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00009039 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
9040 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009041
9042 if (isTypeLegal(SVT) &&
9043 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00009044 SVT.getVectorNumElements() > NElts)
9045 return SVT;
9046 }
9047 return MVT::Other;
9048}