blob: cb226d34402d2e6f153f44499bfb703304859456 [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"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.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 Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042using namespace llvm;
43
Mon P Wang3c81d352008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000046
Evan Cheng10e86422008-04-25 19:11:04 +000047// Forward declarations.
Dale Johannesenace16102009-02-03 19:33:06 +000048static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl);
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
Chris Lattnerd43d00c2008-01-24 08:07:48 +000057 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000058
Anton Korobeynikov2365f512007-07-14 14:06:15 +000059 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000060 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000061
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000062 // Set up the TargetLowering object.
63
64 // X86 is weird, it always uses i8 for shift amounts and setcc results.
65 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000066 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000067 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000069 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000070
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000071 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000072 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000073 setUseUnderscoreSetJmp(false);
74 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000075 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000076 // MS runtime is weird: it exports _setjmp, but longjmp!
77 setUseUnderscoreSetJmp(true);
78 setUseUnderscoreLongJmp(false);
79 } else {
80 setUseUnderscoreSetJmp(true);
81 setUseUnderscoreLongJmp(true);
82 }
83
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000084 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000085 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
86 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
87 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000088 if (Subtarget->is64Bit())
89 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000090
Evan Cheng03294662008-10-14 21:26:46 +000091 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000092
Chris Lattnerddf89562008-01-17 19:59:44 +000093 // We don't accept any truncstore of integer registers.
94 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
95 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
96 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
97 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
98 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000099 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
100
101 // SETOEQ and SETUNE require checking two conditions.
102 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
103 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
104 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
107 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000108
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000109 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
110 // operation.
111 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000114
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 if (Subtarget->is64Bit()) {
116 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000118 } else {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000119 if (X86ScalarSSEf64) {
120 // We have an impenetrably clever algorithm for ui64->double only.
121 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling8b8a6362009-01-17 03:56:04 +0000122
123 // We have faster algorithm for ui32->single only.
124 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000125 } else
Evan Cheng25ab6902006-09-08 06:48:29 +0000126 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
127 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128
129 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
130 // this operation.
131 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
132 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000133 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000134 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000135 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000136 // f32 and f64 cases are Legal, f80 case is not
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
140 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
141 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000142
Dale Johannesen73328d12007-09-19 23:55:34 +0000143 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
144 // are Legal, f80 is custom lowered.
145 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
146 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000147
Evan Cheng02568ff2006-01-30 22:13:22 +0000148 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
149 // this operation.
150 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
151 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
152
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000153 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000154 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000155 // f32 and f64 cases are Legal, f80 case is not
156 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000157 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000158 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000159 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000160 }
161
162 // Handle FP_TO_UINT by promoting the destination to a larger signed
163 // conversion.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
165 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
166 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
167
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 if (Subtarget->is64Bit()) {
169 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000170 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000171 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000172 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 // Expand FP_TO_UINT into a select.
174 // FIXME: We would like to use a Custom expander here eventually to do
175 // the optimal thing for SSE vs. the default expansion in the legalizer.
176 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
177 else
178 // With SSE3 we can use fisttpll to convert to a signed i64.
179 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
180 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
Chris Lattner399610a2006-12-05 18:22:22 +0000182 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000183 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000184 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
185 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
186 }
Chris Lattner21f66852005-12-23 05:15:23 +0000187
Dan Gohmanb00ee212008-02-18 19:34:53 +0000188 // Scalar integer divide and remainder are lowered to use operations that
189 // produce two results, to match the available instructions. This exposes
190 // the two-result form to trivial CSE, which is able to combine x/y and x%y
191 // into a single instruction.
192 //
193 // Scalar integer multiply-high is also lowered to use two-result
194 // operations, to match the available instructions. However, plain multiply
195 // (low) operations are left as Legal, as there are single-result
196 // instructions for this in x86. Using the two-result multiply instructions
197 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000198 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
199 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
200 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
201 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
202 setOperationAction(ISD::SREM , MVT::i8 , Expand);
203 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000204 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
205 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
206 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
207 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
208 setOperationAction(ISD::SREM , MVT::i16 , Expand);
209 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000210 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
211 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
212 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
213 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
214 setOperationAction(ISD::SREM , MVT::i32 , Expand);
215 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000216 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
217 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
218 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
219 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
220 setOperationAction(ISD::SREM , MVT::i64 , Expand);
221 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000222
Evan Chengc35497f2006-10-30 08:02:39 +0000223 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000224 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000225 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
226 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000228 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
232 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000233 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000234 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000235 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000236 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000237
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000239 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
240 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000241 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000242 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
243 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000245 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
246 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 if (Subtarget->is64Bit()) {
248 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000249 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
250 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000251 }
252
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000253 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000254 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000255
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000256 // These should be promoted to a larger select which is supported.
257 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
258 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000259 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000260 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
261 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
262 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
263 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000264 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000265 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
266 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
267 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
268 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
269 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000270 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
273 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
274 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000275 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000276 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000277 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000278
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000279 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000280 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000281 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000282 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000283 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000284 if (Subtarget->is64Bit())
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000286 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 if (Subtarget->is64Bit()) {
288 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
289 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
290 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000291 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000293 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000294 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
295 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
296 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000297 if (Subtarget->is64Bit()) {
298 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
299 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
300 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
301 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000302
Evan Chengd2cde682008-03-10 19:38:10 +0000303 if (Subtarget->hasSSE1())
304 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000305
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000306 if (!Subtarget->hasSSE2())
307 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
308
Mon P Wang63307c32008-05-05 19:05:59 +0000309 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000310 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
311 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
313 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000314
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000315 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000319
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000320 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000321 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000328 }
329
Dan Gohman7f460202008-06-30 20:59:49 +0000330 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
331 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000332 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000333 if (!Subtarget->isTargetDarwin() &&
334 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000335 !Subtarget->isTargetCygMing()) {
336 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
337 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
338 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000339
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000340 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
341 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
342 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
343 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
344 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000345 setExceptionPointerRegister(X86::RAX);
346 setExceptionSelectorRegister(X86::RDX);
347 } else {
348 setExceptionPointerRegister(X86::EAX);
349 setExceptionSelectorRegister(X86::EDX);
350 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000351 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000352 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
353
Duncan Sandsf7331b32007-09-11 14:10:23 +0000354 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000355
Chris Lattnerda68d302008-01-15 21:58:22 +0000356 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000357
Nate Begemanacc398c2006-01-25 18:21:52 +0000358 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
359 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000360 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000361 if (Subtarget->is64Bit()) {
362 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000363 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000364 } else {
365 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000366 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000367 }
Evan Chengae642192007-03-02 23:16:35 +0000368
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000369 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000370 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000371 if (Subtarget->is64Bit())
372 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000373 if (Subtarget->isTargetCygMing())
374 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
375 else
376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000377
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000378 if (X86ScalarSSEf64) {
379 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000380 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000381 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
382 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000383
Evan Cheng223547a2006-01-31 22:28:30 +0000384 // Use ANDPD to simulate FABS.
385 setOperationAction(ISD::FABS , MVT::f64, Custom);
386 setOperationAction(ISD::FABS , MVT::f32, Custom);
387
388 // Use XORP to simulate FNEG.
389 setOperationAction(ISD::FNEG , MVT::f64, Custom);
390 setOperationAction(ISD::FNEG , MVT::f32, Custom);
391
Evan Cheng68c47cb2007-01-05 07:55:56 +0000392 // Use ANDPD and ORPD to simulate FCOPYSIGN.
393 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
394 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
395
Evan Chengd25e9e82006-02-02 00:28:23 +0000396 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000397 setOperationAction(ISD::FSIN , MVT::f64, Expand);
398 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000399 setOperationAction(ISD::FSIN , MVT::f32, Expand);
400 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000401
Chris Lattnera54aa942006-01-29 06:26:08 +0000402 // Expand FP immediates into loads from the stack, except for the special
403 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000404 addLegalFPImmediate(APFloat(+0.0)); // xorpd
405 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000406
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000407 // Floating truncations from f80 and extensions to f80 go through memory.
408 // If optimizing, we lie about this though and handle it in
409 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
410 if (Fast) {
411 setConvertAction(MVT::f32, MVT::f80, Expand);
412 setConvertAction(MVT::f64, MVT::f80, Expand);
413 setConvertAction(MVT::f80, MVT::f32, Expand);
414 setConvertAction(MVT::f80, MVT::f64, Expand);
415 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000416 } else if (X86ScalarSSEf32) {
417 // Use SSE for f32, x87 for f64.
418 // Set up the FP register classes.
419 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421
422 // Use ANDPS to simulate FABS.
423 setOperationAction(ISD::FABS , MVT::f32, Custom);
424
425 // Use XORP to simulate FNEG.
426 setOperationAction(ISD::FNEG , MVT::f32, Custom);
427
428 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
429
430 // Use ANDPS and ORPS to simulate FCOPYSIGN.
431 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
432 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
433
434 // We don't support sin/cos/fmod
435 setOperationAction(ISD::FSIN , MVT::f32, Expand);
436 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000437
Nate Begemane1795842008-02-14 08:57:00 +0000438 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000439 addLegalFPImmediate(APFloat(+0.0f)); // xorps
440 addLegalFPImmediate(APFloat(+0.0)); // FLD0
441 addLegalFPImmediate(APFloat(+1.0)); // FLD1
442 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
443 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
444
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000445 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
446 // this though and handle it in InstructionSelectPreprocess so that
447 // dagcombine2 can hack on these.
448 if (Fast) {
449 setConvertAction(MVT::f32, MVT::f64, Expand);
450 setConvertAction(MVT::f32, MVT::f80, Expand);
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 // And x87->x87 truncations also.
454 setConvertAction(MVT::f80, MVT::f64, Expand);
455 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
457 if (!UnsafeFPMath) {
458 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
459 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
460 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000463 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
465 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000466
Evan Cheng68c47cb2007-01-05 07:55:56 +0000467 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000468 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000469 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000471
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000472 // Floating truncations go through memory. If optimizing, we lie about
473 // this though and handle it in InstructionSelectPreprocess so that
474 // dagcombine2 can hack on these.
475 if (Fast) {
476 setConvertAction(MVT::f80, MVT::f32, Expand);
477 setConvertAction(MVT::f64, MVT::f32, Expand);
478 setConvertAction(MVT::f80, MVT::f64, Expand);
479 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000480
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000481 if (!UnsafeFPMath) {
482 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
483 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
484 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000485 addLegalFPImmediate(APFloat(+0.0)); // FLD0
486 addLegalFPImmediate(APFloat(+1.0)); // FLD1
487 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
488 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
490 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
491 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
492 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000494
Dale Johannesen59a58732007-08-05 18:49:15 +0000495 // Long double always uses X87.
496 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000497 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000499 {
Dale Johannesen23a98552008-10-09 23:00:39 +0000500 bool ignored;
Chris Lattner71d07a02008-01-27 06:19:31 +0000501 APFloat TmpFlt(+0.0);
Dale Johannesen23a98552008-10-09 23:00:39 +0000502 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
503 &ignored);
Chris Lattner71d07a02008-01-27 06:19:31 +0000504 addLegalFPImmediate(TmpFlt); // FLD0
505 TmpFlt.changeSign();
506 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
507 APFloat TmpFlt2(+1.0);
Dale Johannesen23a98552008-10-09 23:00:39 +0000508 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
509 &ignored);
Chris Lattner71d07a02008-01-27 06:19:31 +0000510 addLegalFPImmediate(TmpFlt2); // FLD1
511 TmpFlt2.changeSign();
512 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
513 }
514
Dale Johannesen2f429012007-09-26 21:10:55 +0000515 if (!UnsafeFPMath) {
516 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
517 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
518 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000519
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000520 // Always use a library call for pow.
521 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
522 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
523 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
524
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000525 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000526 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000527 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000528 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000529 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
530
Mon P Wangf007a8b2008-11-06 05:31:54 +0000531 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000532 // (for widening) or expand (for scalarization). Then we will selectively
533 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000534 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
535 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000536 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000549 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
551 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000552 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000574 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000579 }
580
Mon P Wang3c81d352008-11-23 04:37:22 +0000581 if (!DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000582 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
583 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
584 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000585 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000586 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000587
Evan Chengd30bf012006-03-01 01:11:20 +0000588 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000589
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000590 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
591 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
592 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000593 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000594
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000595 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
596 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
597 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000598 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000599
Bill Wendling74027e92007-03-15 21:24:36 +0000600 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
601 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
602
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000603 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000604 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000605 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000606 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::AND, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
609 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000610
611 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000612 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000613 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000614 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
615 setOperationAction(ISD::OR, MVT::v2i32, Promote);
616 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
617 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000618
619 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000620 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000621 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000622 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
623 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
624 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
625 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000626
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000627 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000628 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000629 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000630 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
631 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
632 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000633 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
634 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000635 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000636
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000637 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
638 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
639 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000640 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000641 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000642
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000646 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000647
Evan Cheng52672b82008-07-22 18:39:19 +0000648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
650 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000651 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000652
653 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000654
655 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
656 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
657 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
658 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
659 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
660 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000661 }
662
Evan Chenga88973f2006-03-22 19:22:18 +0000663 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000664 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
665
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000666 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
667 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
668 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
669 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000670 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
671 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000672 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000675 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000676 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000677 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000678 }
679
Evan Chenga88973f2006-03-22 19:22:18 +0000680 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000681 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
684 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
685 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
686
Evan Chengf7c378e2006-04-10 07:23:14 +0000687 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
688 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
689 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000690 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000691 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000692 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
693 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
694 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000695 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000696 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000697 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
698 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
699 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
700 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000701 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
702 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000703
Nate Begeman30a0de92008-07-17 16:51:19 +0000704 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
706 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
707 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000708
Evan Chengf7c378e2006-04-10 07:23:14 +0000709 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
710 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000712 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000713 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000714
Evan Cheng2c3ae372006-04-12 21:21:57 +0000715 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000716 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
717 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000718 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000719 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000720 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
722 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000724 }
725 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
726 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
727 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
728 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000729 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000731 if (Subtarget->is64Bit()) {
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000733 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000734 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000735
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000736 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000737 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000738 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
739 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
740 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
741 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
742 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
743 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
744 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
745 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
746 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
747 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000748 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000749
Chris Lattnerddf89562008-01-17 19:59:44 +0000750 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000751
Evan Cheng2c3ae372006-04-12 21:21:57 +0000752 // Custom lower v2i64 and v2f64 selects.
753 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000754 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000755 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000756 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000757
Evan Cheng470a6ad2006-02-22 02:26:30 +0000758 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000759
760 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 }
787
Evan Cheng6be2c582006-04-05 23:38:46 +0000788 // We want to custom lower some of our intrinsics.
789 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
790
Bill Wendling74c37652008-12-09 22:08:41 +0000791 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000792 setOperationAction(ISD::SADDO, MVT::i32, Custom);
793 setOperationAction(ISD::SADDO, MVT::i64, Custom);
794 setOperationAction(ISD::UADDO, MVT::i32, Custom);
795 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000796 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
797 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
798 setOperationAction(ISD::USUBO, MVT::i32, Custom);
799 setOperationAction(ISD::USUBO, MVT::i64, Custom);
800 setOperationAction(ISD::SMULO, MVT::i32, Custom);
801 setOperationAction(ISD::SMULO, MVT::i64, Custom);
802 setOperationAction(ISD::UMULO, MVT::i32, Custom);
803 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000804
Evan Cheng206ee9d2006-07-07 08:33:52 +0000805 // We have target-specific dag combine patterns for the following nodes:
806 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000807 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000808 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000809 setTargetDAGCombine(ISD::SHL);
810 setTargetDAGCombine(ISD::SRA);
811 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000812 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000813
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000814 computeRegisterProperties();
815
Evan Cheng87ed7162006-02-14 08:25:08 +0000816 // FIXME: These should be based on subtarget info. Plus, the values should
817 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000818 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
819 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
820 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000821 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000822 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000823}
824
Scott Michel5b8f82e2008-03-10 15:42:14 +0000825
Duncan Sands5480c042009-01-01 15:52:00 +0000826MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000827 return MVT::i8;
828}
829
830
Evan Cheng29286502008-01-23 23:17:41 +0000831/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
832/// the desired ByVal argument alignment.
833static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
834 if (MaxAlign == 16)
835 return;
836 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
837 if (VTy->getBitWidth() == 128)
838 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000839 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
840 unsigned EltAlign = 0;
841 getMaxByValAlign(ATy->getElementType(), EltAlign);
842 if (EltAlign > MaxAlign)
843 MaxAlign = EltAlign;
844 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
845 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
846 unsigned EltAlign = 0;
847 getMaxByValAlign(STy->getElementType(i), EltAlign);
848 if (EltAlign > MaxAlign)
849 MaxAlign = EltAlign;
850 if (MaxAlign == 16)
851 break;
852 }
853 }
854 return;
855}
856
857/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
858/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000859/// that contain SSE vectors are placed at 16-byte boundaries while the rest
860/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000861unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000862 if (Subtarget->is64Bit()) {
863 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000864 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000865 if (TyAlign > 8)
866 return TyAlign;
867 return 8;
868 }
869
Evan Cheng29286502008-01-23 23:17:41 +0000870 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000871 if (Subtarget->hasSSE1())
872 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000873 return Align;
874}
Chris Lattner2b02a442007-02-25 08:29:00 +0000875
Evan Chengf0df0312008-05-15 08:39:06 +0000876/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000877/// and store operations as a result of memset, memcpy, and memmove
878/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000879/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000881X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
882 bool isSrcConst, bool isSrcStr) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000883 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
884 // linux. This is because the stack realignment code can't handle certain
885 // cases like PR2962. This should be removed when PR2962 is fixed.
886 if (Subtarget->getStackAlignment() >= 16) {
887 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
888 return MVT::v4i32;
889 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
890 return MVT::v4f32;
891 }
Evan Chengf0df0312008-05-15 08:39:06 +0000892 if (Subtarget->is64Bit() && Size >= 8)
893 return MVT::i64;
894 return MVT::i32;
895}
896
897
Evan Chengcc415862007-11-09 01:32:10 +0000898/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
899/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000900SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000901 SelectionDAG &DAG) const {
902 if (usesGlobalOffsetTable())
903 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
904 if (!Subtarget->isPICStyleRIPRel())
905 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
906 return Table;
907}
908
Chris Lattner2b02a442007-02-25 08:29:00 +0000909//===----------------------------------------------------------------------===//
910// Return Value Calling Convention Implementation
911//===----------------------------------------------------------------------===//
912
Chris Lattner59ed56b2007-02-28 04:55:35 +0000913#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000914
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000915/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000916SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +0000917 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000918 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
919
Chris Lattner9774c912007-02-27 05:28:59 +0000920 SmallVector<CCValAssign, 16> RVLocs;
921 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000922 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
923 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000924 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000925
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000926 // If this is the first return lowered for this function, add the regs to the
927 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000928 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000929 for (unsigned i = 0; i != RVLocs.size(); ++i)
930 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000931 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000932 }
Dan Gohman475871a2008-07-27 21:46:04 +0000933 SDValue Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000934
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000935 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000936 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000937 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue TailCall = Chain;
939 SDValue TargetAddress = TailCall.getOperand(1);
940 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000941 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000942 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000943 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000944 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000945 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
946 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000947 assert(StackAdjustment.getOpcode() == ISD::Constant &&
948 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000949
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000951 Operands.push_back(Chain.getOperand(0));
952 Operands.push_back(TargetAddress);
953 Operands.push_back(StackAdjustment);
954 // Copy registers used by the call. Last operand is a flag so it is not
955 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000956 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000957 Operands.push_back(Chain.getOperand(i));
958 }
Dale Johannesenace16102009-02-03 19:33:06 +0000959 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000960 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000961 }
962
963 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000964 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000965
Dan Gohman475871a2008-07-27 21:46:04 +0000966 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000967 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
968 // Operand #1 = Bytes To Pop
969 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
970
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000971 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000972 for (unsigned i = 0; i != RVLocs.size(); ++i) {
973 CCValAssign &VA = RVLocs[i];
974 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000975 SDValue ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000976
Chris Lattner447ff682008-03-11 03:23:40 +0000977 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
978 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000979 if (VA.getLocReg() == X86::ST0 ||
980 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000981 // If this is a copy from an xmm register to ST(0), use an FPExtend to
982 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000983 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000984 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000985 RetOps.push_back(ValToCopy);
986 // Don't emit a copytoreg.
987 continue;
988 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000989
Dale Johannesendd64c412009-02-04 00:33:20 +0000990 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000991 Flag = Chain.getValue(1);
992 }
Dan Gohman61a92132008-04-21 23:59:07 +0000993
994 // The x86-64 ABI for returning structs by value requires that we copy
995 // the sret argument into %rax for the return. We saved the argument into
996 // a virtual register in the entry block, so now we copy the value out
997 // and into %rax.
998 if (Subtarget->is64Bit() &&
999 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1000 MachineFunction &MF = DAG.getMachineFunction();
1001 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1002 unsigned Reg = FuncInfo->getSRetReturnReg();
1003 if (!Reg) {
1004 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1005 FuncInfo->setSRetReturnReg(Reg);
1006 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001007 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001008
Dale Johannesendd64c412009-02-04 00:33:20 +00001009 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001010 Flag = Chain.getValue(1);
1011 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001012
Chris Lattner447ff682008-03-11 03:23:40 +00001013 RetOps[0] = Chain; // Update chain.
1014
1015 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001016 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001017 RetOps.push_back(Flag);
1018
Dale Johannesenace16102009-02-03 19:33:06 +00001019 return DAG.getNode(X86ISD::RET_FLAG, dl,
1020 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001021}
1022
1023
Chris Lattner3085e152007-02-25 08:59:22 +00001024/// LowerCallResult - Lower the result values of an ISD::CALL into the
1025/// appropriate copies out of appropriate physical registers. This assumes that
1026/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1027/// being lowered. The returns a SDNode with the same number of values as the
1028/// ISD::CALL.
1029SDNode *X86TargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +00001030LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001031 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001032
1033 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001034 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001035 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001036 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001037 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001038 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001039 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1040
Dan Gohman475871a2008-07-27 21:46:04 +00001041 SmallVector<SDValue, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +00001042
1043 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001044 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001045 CCValAssign &VA = RVLocs[i];
1046 MVT CopyVT = VA.getValVT();
Torok Edwin3f142c32009-02-01 18:15:56 +00001047
1048 // If this is x86-64, and we disabled SSE, we can't return FP values
1049 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
1050 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1051 cerr << "SSE register return with SSE disabled\n";
1052 exit(1);
1053 }
1054
Chris Lattner8e6da152008-03-10 21:08:41 +00001055 // If this is a call to a function that returns an fp value on the floating
1056 // point stack, but where we prefer to use the value in xmm registers, copy
1057 // 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 +00001058 if ((VA.getLocReg() == X86::ST0 ||
1059 VA.getLocReg() == X86::ST1) &&
1060 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001061 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001062 }
Chris Lattner3085e152007-02-25 08:59:22 +00001063
Dan Gohman37eed792009-02-04 17:28:58 +00001064 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Chris Lattner8e6da152008-03-10 21:08:41 +00001065 CopyVT, InFlag).getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00001066 SDValue Val = Chain.getValue(0);
Chris Lattner8e6da152008-03-10 21:08:41 +00001067 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001068
Dan Gohman37eed792009-02-04 17:28:58 +00001069 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001070 // Round the F80 the right size, which also moves to the appropriate xmm
1071 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001072 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001073 // This truncation won't change the value.
1074 DAG.getIntPtrConstant(1));
1075 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +00001076
Chris Lattner8e6da152008-03-10 21:08:41 +00001077 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001078 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001079
Chris Lattner3085e152007-02-25 08:59:22 +00001080 // Merge everything together with a MERGE_VALUES node.
1081 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001082 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1083 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001084}
1085
1086
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001087//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001088// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001089//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001090// StdCall calling convention seems to be standard for many Windows' API
1091// routines and around. It differs from C calling convention just a little:
1092// callee should clean up the stack, not caller. Symbols should be also
1093// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001094// For info on fast calling convention see Fast Calling Convention (tail call)
1095// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001096
Evan Cheng85e38002006-04-27 05:35:28 +00001097/// AddLiveIn - This helper function adds the specified physical register to the
1098/// MachineFunction as a live in value. It also creates a corresponding virtual
1099/// register for it.
1100static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001101 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001102 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001103 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1104 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001105 return VReg;
1106}
1107
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001108/// CallIsStructReturn - Determines whether a CALL node uses struct return
1109/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001110static bool CallIsStructReturn(CallSDNode *TheCall) {
1111 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001112 if (!NumOps)
1113 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001114
Dan Gohman095cc292008-09-13 01:54:27 +00001115 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001116}
1117
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001118/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1119/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001120static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001121 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001122 if (!NumArgs)
1123 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001124
1125 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001126}
1127
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001128/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1129/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001130/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001131bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001132 if (IsVarArg)
1133 return false;
1134
Dan Gohman095cc292008-09-13 01:54:27 +00001135 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001136 default:
1137 return false;
1138 case CallingConv::X86_StdCall:
1139 return !Subtarget->is64Bit();
1140 case CallingConv::X86_FastCall:
1141 return !Subtarget->is64Bit();
1142 case CallingConv::Fast:
1143 return PerformTailCallOpt;
1144 }
1145}
1146
Dan Gohman095cc292008-09-13 01:54:27 +00001147/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1148/// given CallingConvention value.
1149CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001150 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001151 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001152 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001153 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1154 return CC_X86_64_TailCall;
1155 else
1156 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001157 }
1158
Gordon Henriksen86737662008-01-05 16:56:59 +00001159 if (CC == CallingConv::X86_FastCall)
1160 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001161 else if (CC == CallingConv::Fast)
1162 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001163 else
1164 return CC_X86_32_C;
1165}
1166
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001167/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1168/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001169NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001170X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001171 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001172 if (CC == CallingConv::X86_FastCall)
1173 return FastCall;
1174 else if (CC == CallingConv::X86_StdCall)
1175 return StdCall;
1176 return None;
1177}
1178
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001179
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001180/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1181/// in a register before calling.
1182bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1183 return !IsTailCall && !Is64Bit &&
1184 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1185 Subtarget->isPICStyleGOT();
1186}
1187
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001188/// CallRequiresFnAddressInReg - Check whether the call requires the function
1189/// address to be loaded in a register.
1190bool
1191X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1192 return !Is64Bit && IsTailCall &&
1193 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1194 Subtarget->isPICStyleGOT();
1195}
1196
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001197/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1198/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001199/// the specific parameter attribute. The copy will be passed as a byval
1200/// function parameter.
Dan Gohman475871a2008-07-27 21:46:04 +00001201static SDValue
1202CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001203 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1204 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001205 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001206 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001207 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001208}
1209
Dan Gohman475871a2008-07-27 21:46:04 +00001210SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001211 const CCValAssign &VA,
1212 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001213 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001214 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001215 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001216 ISD::ArgFlagsTy Flags =
1217 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001218 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001219 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001220
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001221 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1222 // changed with more analysis.
1223 // In case of tail call optimization mark all arguments mutable. Since they
1224 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001225 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001226 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001228 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001229 return FIN;
Dale Johannesenace16102009-02-03 19:33:06 +00001230 return DAG.getLoad(VA.getValVT(), Op.getNode()->getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001231 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001232}
1233
Dan Gohman475871a2008-07-27 21:46:04 +00001234SDValue
1235X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001236 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001237 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesenace16102009-02-03 19:33:06 +00001238 DebugLoc dl = Op.getNode()->getDebugLoc();
Gordon Henriksen86737662008-01-05 16:56:59 +00001239
1240 const Function* Fn = MF.getFunction();
1241 if (Fn->hasExternalLinkage() &&
1242 Subtarget->isTargetCygMing() &&
1243 Fn->getName() == "main")
1244 FuncInfo->setForceFramePointer(true);
1245
1246 // Decorate the function name.
1247 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1248
Evan Cheng1bc78042006-04-26 01:20:17 +00001249 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001250 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001251 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001252 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001253 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001254 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001255
1256 assert(!(isVarArg && CC == CallingConv::Fast) &&
1257 "Var args not supported with calling convention fastcc");
1258
Chris Lattner638402b2007-02-28 07:00:42 +00001259 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001260 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001261 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001262 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001263
Dan Gohman475871a2008-07-27 21:46:04 +00001264 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001265 unsigned LastVal = ~0U;
1266 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1267 CCValAssign &VA = ArgLocs[i];
1268 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1269 // places.
1270 assert(VA.getValNo() != LastVal &&
1271 "Don't support value assigned to multiple locs yet");
1272 LastVal = VA.getValNo();
1273
1274 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001275 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001276 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001277 if (RegVT == MVT::i32)
1278 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001279 else if (Is64Bit && RegVT == MVT::i64)
1280 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001281 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001282 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001283 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001284 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001285 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001286 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001287 else if (RegVT.isVector()) {
1288 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001289 if (!Is64Bit)
1290 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1291 else {
1292 // Darwin calling convention passes MMX values in either GPRs or
1293 // XMMs in x86-64. Other targets pass them in memory.
1294 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1295 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1296 RegVT = MVT::v2i64;
1297 } else {
1298 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1299 RegVT = MVT::i64;
1300 }
1301 }
1302 } else {
1303 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001304 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001305
Chris Lattner82932a52007-03-02 05:12:29 +00001306 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001307 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001308
1309 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1310 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1311 // right size.
1312 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001313 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001314 DAG.getValueType(VA.getValVT()));
1315 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001316 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001317 DAG.getValueType(VA.getValVT()));
1318
1319 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001320 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001321
Gordon Henriksen86737662008-01-05 16:56:59 +00001322 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001323 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001324 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001325 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001326 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001327 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1328 ArgValue, DAG.getConstant(0, MVT::i64));
1329 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001330 }
1331 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001332
Chris Lattnerf39f7712007-02-28 05:46:49 +00001333 ArgValues.push_back(ArgValue);
1334 } else {
1335 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001336 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001337 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001338 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001339
Dan Gohman61a92132008-04-21 23:59:07 +00001340 // The x86-64 ABI for returning structs by value requires that we copy
1341 // the sret argument into %rax for the return. Save the argument into
1342 // a virtual register so that we can access it from the return points.
1343 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1344 MachineFunction &MF = DAG.getMachineFunction();
1345 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1346 unsigned Reg = FuncInfo->getSRetReturnReg();
1347 if (!Reg) {
1348 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1349 FuncInfo->setSRetReturnReg(Reg);
1350 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001351 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001352 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001353 }
1354
Chris Lattnerf39f7712007-02-28 05:46:49 +00001355 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001356 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001357 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001358 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001359
Evan Cheng1bc78042006-04-26 01:20:17 +00001360 // If the function takes variable number of arguments, make a frame index for
1361 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001362 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001363 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1364 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1365 }
1366 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001367 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1368
1369 // FIXME: We should really autogenerate these arrays
1370 static const unsigned GPR64ArgRegsWin64[] = {
1371 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001372 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001373 static const unsigned XMMArgRegsWin64[] = {
1374 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1375 };
1376 static const unsigned GPR64ArgRegs64Bit[] = {
1377 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1378 };
1379 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1381 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1382 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001383 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1384
1385 if (IsWin64) {
1386 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1387 GPR64ArgRegs = GPR64ArgRegsWin64;
1388 XMMArgRegs = XMMArgRegsWin64;
1389 } else {
1390 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1391 GPR64ArgRegs = GPR64ArgRegs64Bit;
1392 XMMArgRegs = XMMArgRegs64Bit;
1393 }
1394 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1395 TotalNumIntRegs);
1396 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1397 TotalNumXMMRegs);
1398
Torok Edwin3f142c32009-02-01 18:15:56 +00001399 assert((Subtarget->hasSSE1() || !NumXMMRegs) &&
1400 "SSE register cannot be used when SSE is disabled!");
1401 if (!Subtarget->hasSSE1()) {
1402 // Kernel mode asks for SSE to be disabled, so don't push them
1403 // on the stack.
1404 TotalNumXMMRegs = 0;
1405 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001406 // For X86-64, if there are vararg parameters that are passed via
1407 // registers, then we must store them to their spots on the stack so they
1408 // may be loaded by deferencing the result of va_next.
1409 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001410 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1411 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1412 TotalNumXMMRegs * 16, 16);
1413
Gordon Henriksen86737662008-01-05 16:56:59 +00001414 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001415 SmallVector<SDValue, 8> MemOps;
1416 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001417 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001418 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001419 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001420 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1421 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001422 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001423 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001424 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001425 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001426 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001427 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001428 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001429 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001430
Gordon Henriksen86737662008-01-05 16:56:59 +00001431 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001432 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001433 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001434 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001435 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1436 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001437 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001439 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001440 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001441 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001442 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001443 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001444 }
1445 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001446 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001447 &MemOps[0], MemOps.size());
1448 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001449 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001450
Gordon Henriksenae636f82008-01-03 16:47:34 +00001451 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001452
Gordon Henriksen86737662008-01-05 16:56:59 +00001453 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001454 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001455 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001456 BytesCallerReserves = 0;
1457 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001458 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001459 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001460 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001461 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001462 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001463 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001464
Gordon Henriksen86737662008-01-05 16:56:59 +00001465 if (!Is64Bit) {
1466 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1467 if (CC == CallingConv::X86_FastCall)
1468 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1469 }
Evan Cheng25caf632006-05-23 21:06:34 +00001470
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001471 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001472
Evan Cheng25caf632006-05-23 21:06:34 +00001473 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001474 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001475 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001476}
1477
Dan Gohman475871a2008-07-27 21:46:04 +00001478SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001479X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001480 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001481 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001482 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001483 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001484 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001485 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001486 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001487 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001488 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001489 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001490 }
Dale Johannesenace16102009-02-03 19:33:06 +00001491 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001492 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001493}
1494
Bill Wendling64e87322009-01-16 19:25:27 +00001495/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001496/// optimization is performed and it is required.
Dan Gohman475871a2008-07-27 21:46:04 +00001497SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001498X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001499 SDValue &OutRetAddr,
1500 SDValue Chain,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001501 bool IsTailCall,
1502 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001503 int FPDiff,
1504 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001505 if (!IsTailCall || FPDiff==0) return Chain;
1506
1507 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001508 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001509 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001510
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001511 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001512 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001513 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001514}
1515
1516/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1517/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman475871a2008-07-27 21:46:04 +00001518static SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001519EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001520 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001521 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001522 // Store the return address to the appropriate stack slot.
1523 if (!FPDiff) return Chain;
1524 // Calculate the new stack slot for the return address.
1525 int SlotSize = Is64Bit ? 8 : 4;
1526 int NewReturnAddrFI =
1527 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001528 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Dale Johannesenace16102009-02-03 19:33:06 +00001530 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001531 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001532 return Chain;
1533}
1534
Dan Gohman475871a2008-07-27 21:46:04 +00001535SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001536 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001537 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1538 SDValue Chain = TheCall->getChain();
1539 unsigned CC = TheCall->getCallingConv();
1540 bool isVarArg = TheCall->isVarArg();
1541 bool IsTailCall = TheCall->isTailCall() &&
1542 CC == CallingConv::Fast && PerformTailCallOpt;
1543 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001544 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001545 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001546 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001547
1548 assert(!(isVarArg && CC == CallingConv::Fast) &&
1549 "Var args not supported with calling convention fastcc");
1550
Chris Lattner638402b2007-02-28 07:00:42 +00001551 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001552 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001553 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001554 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001555
Chris Lattner423c5f42007-02-28 05:31:48 +00001556 // Get a count of how many bytes are to be pushed on the stack.
1557 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001558 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001559 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001560
Gordon Henriksen86737662008-01-05 16:56:59 +00001561 int FPDiff = 0;
1562 if (IsTailCall) {
1563 // Lower arguments at fp - stackoffset + fpdiff.
1564 unsigned NumBytesCallerPushed =
1565 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1566 FPDiff = NumBytesCallerPushed - NumBytes;
1567
1568 // Set the delta of movement of the returnaddr stackslot.
1569 // But only set if delta is greater than previous delta.
1570 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1571 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1572 }
1573
Chris Lattnere563bbc2008-10-11 22:08:30 +00001574 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001575
Dan Gohman475871a2008-07-27 21:46:04 +00001576 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001577 // Load return adress for tail calls.
1578 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001579 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001580
Dan Gohman475871a2008-07-27 21:46:04 +00001581 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1582 SmallVector<SDValue, 8> MemOpChains;
1583 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001584
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001585 // Walk the register/memloc assignments, inserting copies/loads. In the case
1586 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001587 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1588 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001589 SDValue Arg = TheCall->getArg(i);
1590 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1591 bool isByVal = Flags.isByVal();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001592
Chris Lattner423c5f42007-02-28 05:31:48 +00001593 // Promote the value if needed.
1594 switch (VA.getLocInfo()) {
1595 default: assert(0 && "Unknown loc info!");
1596 case CCValAssign::Full: break;
1597 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001598 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001599 break;
1600 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001601 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001602 break;
1603 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001604 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001605 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001606 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001607
1608 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001609 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001610 MVT RegVT = VA.getLocVT();
1611 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001612 switch (VA.getLocReg()) {
1613 default:
1614 break;
1615 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1616 case X86::R8: {
1617 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001618 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001619 break;
1620 }
1621 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1622 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1623 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001624 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1625 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1626 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
Dale Johannesene8d72302009-02-06 23:05:02 +00001627 DAG.getUNDEF(MVT::v2i64), Arg,
Dale Johannesenace16102009-02-03 19:33:06 +00001628 getMOVLMask(2, DAG, dl));
Evan Cheng10e86422008-04-25 19:11:04 +00001629 break;
1630 }
1631 }
1632 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001633 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1634 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001635 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001636 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001637 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001638 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001639
Dan Gohman095cc292008-09-13 01:54:27 +00001640 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1641 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001642 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001643 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001644 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001645
Evan Cheng32fe1032006-05-25 00:59:30 +00001646 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001647 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001648 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001649
Evan Cheng347d5f72006-04-28 21:29:37 +00001650 // Build a sequence of copy-to-reg nodes chained together with token chain
1651 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001652 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001653 // Tail call byval lowering might overwrite argument registers so in case of
1654 // tail call optimization the copies to registers are lowered later.
1655 if (!IsTailCall)
1656 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001657 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1658 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001659 InFlag = Chain.getValue(1);
1660 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001661
Evan Chengf4684712007-02-21 21:18:14 +00001662 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001663 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001664 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001665 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001666 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1667 InFlag);
1668 InFlag = Chain.getValue(1);
1669 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001670 // If we are tail calling and generating PIC/GOT style code load the address
1671 // of the callee into ecx. The value in ecx is used as target of the tail
1672 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1673 // calls on PIC/GOT architectures. Normally we would just put the address of
1674 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1675 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001676 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001677 // Note: The actual moving to ecx is done further down.
1678 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001679 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001680 !G->getGlobal()->hasProtectedVisibility())
1681 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001682 else if (isa<ExternalSymbolSDNode>(Callee))
1683 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001684 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001685
Gordon Henriksen86737662008-01-05 16:56:59 +00001686 if (Is64Bit && isVarArg) {
1687 // From AMD64 ABI document:
1688 // For calls that may call functions that use varargs or stdargs
1689 // (prototype-less calls or calls to functions containing ellipsis (...) in
1690 // the declaration) %al is used as hidden argument to specify the number
1691 // of SSE registers used. The contents of %al do not need to match exactly
1692 // the number of registers, but must be an ubound on the number of SSE
1693 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001694
1695 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 // Count the number of XMM registers allocated.
1697 static const unsigned XMMArgRegs[] = {
1698 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1699 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1700 };
1701 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Torok Edwin3f142c32009-02-01 18:15:56 +00001702 assert((Subtarget->hasSSE1() || !NumXMMRegs)
1703 && "SSE registers cannot be used when SSE is disabled");
Gordon Henriksen86737662008-01-05 16:56:59 +00001704
Dale Johannesendd64c412009-02-04 00:33:20 +00001705 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1707 InFlag = Chain.getValue(1);
1708 }
1709
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001710
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001711 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SmallVector<SDValue, 8> MemOpChains2;
1714 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001715 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001716 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001717 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1719 CCValAssign &VA = ArgLocs[i];
1720 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001721 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001722 SDValue Arg = TheCall->getArg(i);
1723 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001724 // Create frame index.
1725 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001726 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001727 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001728 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001729
Duncan Sands276dcbd2008-03-21 09:14:45 +00001730 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001731 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001732 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001733 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001734 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
1735 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001736 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001737
1738 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001739 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001740 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001741 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001742 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001743 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001744 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001745 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001746 }
1747 }
1748
1749 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001750 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001751 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001752
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001753 // Copy arguments to their registers.
1754 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001755 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1756 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001757 InFlag = Chain.getValue(1);
1758 }
Dan Gohman475871a2008-07-27 21:46:04 +00001759 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001760
Gordon Henriksen86737662008-01-05 16:56:59 +00001761 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001762 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001763 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 }
1765
Evan Cheng32fe1032006-05-25 00:59:30 +00001766 // If the callee is a GlobalAddress node (quite common, every direct call is)
1767 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001768 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001769 // We should use extra load for direct calls to dllimported functions in
1770 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001771 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1772 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001773 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1774 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001775 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1776 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001778 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001779
Dale Johannesendd64c412009-02-04 00:33:20 +00001780 Chain = DAG.getCopyToReg(Chain, dl,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001781 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001782 Callee,InFlag);
1783 Callee = DAG.getRegister(Opc, getPointerTy());
1784 // Add register as live out.
1785 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001786 }
1787
Chris Lattnerd96d0722007-02-25 06:40:16 +00001788 // Returns a chain & a flag for retval copy to use.
1789 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001790 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001791
1792 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001793 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1794 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001795 InFlag = Chain.getValue(1);
1796
1797 // Returns a chain & a flag for retval copy to use.
1798 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1799 Ops.clear();
1800 }
1801
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001802 Ops.push_back(Chain);
1803 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001804
Gordon Henriksen86737662008-01-05 16:56:59 +00001805 if (IsTailCall)
1806 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001807
Gordon Henriksen86737662008-01-05 16:56:59 +00001808 // Add argument registers to the end of the list so that they are known live
1809 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001810 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1811 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1812 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001813
Evan Cheng586ccac2008-03-18 23:36:35 +00001814 // Add an implicit use GOT pointer in EBX.
1815 if (!IsTailCall && !Is64Bit &&
1816 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1817 Subtarget->isPICStyleGOT())
1818 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1819
1820 // Add an implicit use of AL for x86 vararg functions.
1821 if (Is64Bit && isVarArg)
1822 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1823
Gabor Greifba36cb52008-08-28 21:40:38 +00001824 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001825 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001826
Gordon Henriksen86737662008-01-05 16:56:59 +00001827 if (IsTailCall) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001828 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001829 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001830 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001831 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001832
Gabor Greifba36cb52008-08-28 21:40:38 +00001833 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 }
1835
Dale Johannesenace16102009-02-03 19:33:06 +00001836 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001837 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001838
Chris Lattner2d297092006-05-23 18:50:38 +00001839 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001841 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001843 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001844 // If this is is a call to a struct-return function, the callee
1845 // pops the hidden struct pointer, so we have to push it back.
1846 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001847 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001848 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001849 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001850
Gordon Henriksenae636f82008-01-03 16:47:34 +00001851 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001852 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001853 DAG.getIntPtrConstant(NumBytes, true),
1854 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1855 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001856 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001857 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001858
Chris Lattner3085e152007-02-25 08:59:22 +00001859 // Handle result values, copying them out of physregs into vregs that we
1860 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001861 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001862 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001863}
1864
Evan Cheng25ab6902006-09-08 06:48:29 +00001865
1866//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001867// Fast Calling Convention (tail call) implementation
1868//===----------------------------------------------------------------------===//
1869
1870// Like std call, callee cleans arguments, convention except that ECX is
1871// reserved for storing the tail called function address. Only 2 registers are
1872// free for argument passing (inreg). Tail call optimization is performed
1873// provided:
1874// * tailcallopt is enabled
1875// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001876// On X86_64 architecture with GOT-style position independent code only local
1877// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001878// To keep the stack aligned according to platform abi the function
1879// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1880// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001881// If a tail called function callee has more arguments than the caller the
1882// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001883// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001884// original REtADDR, but before the saved framepointer or the spilled registers
1885// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1886// stack layout:
1887// arg1
1888// arg2
1889// RETADDR
1890// [ new RETADDR
1891// move area ]
1892// (possible EBP)
1893// ESI
1894// EDI
1895// local1 ..
1896
1897/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1898/// for a 16 byte align requirement.
1899unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1900 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001901 MachineFunction &MF = DAG.getMachineFunction();
1902 const TargetMachine &TM = MF.getTarget();
1903 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1904 unsigned StackAlignment = TFI.getStackAlignment();
1905 uint64_t AlignMask = StackAlignment - 1;
1906 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001907 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001908 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1909 // Number smaller than 12 so just add the difference.
1910 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1911 } else {
1912 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1913 Offset = ((~AlignMask) & Offset) + StackAlignment +
1914 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001915 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001916 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001917}
1918
1919/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001920/// following the call is a return. A function is eligible if caller/callee
1921/// calling conventions match, currently only fastcc supports tail calls, and
1922/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001923bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001924 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001925 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001926 if (!PerformTailCallOpt)
1927 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001928
Dan Gohman095cc292008-09-13 01:54:27 +00001929 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001930 MachineFunction &MF = DAG.getMachineFunction();
1931 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001932 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001933 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001934 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001935 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001936 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001937 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001938 return true;
1939
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001940 // Can only do local tail calls (in same module, hidden or protected) on
1941 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1943 return G->getGlobal()->hasHiddenVisibility()
1944 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001945 }
1946 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001947
1948 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001949}
1950
Dan Gohman3df24e62008-09-03 23:12:08 +00001951FastISel *
1952X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001953 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001954 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001955 DenseMap<const Value *, unsigned> &vm,
1956 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001957 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001958 DenseMap<const AllocaInst *, int> &am
1959#ifndef NDEBUG
1960 , SmallSet<Instruction*, 8> &cil
1961#endif
1962 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001963 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001964#ifndef NDEBUG
1965 , cil
1966#endif
1967 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001968}
1969
1970
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001971//===----------------------------------------------------------------------===//
1972// Other Lowering Hooks
1973//===----------------------------------------------------------------------===//
1974
1975
Dan Gohman475871a2008-07-27 21:46:04 +00001976SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001977 MachineFunction &MF = DAG.getMachineFunction();
1978 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1979 int ReturnAddrIndex = FuncInfo->getRAIndex();
1980
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001981 if (ReturnAddrIndex == 0) {
1982 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00001983 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001984 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001985 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001986 }
1987
Evan Cheng25ab6902006-09-08 06:48:29 +00001988 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001989}
1990
1991
Chris Lattner1c39d4c2008-12-24 23:53:05 +00001992/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1993/// specific condition code, returning the condition code and the LHS/RHS of the
1994/// comparison to make.
1995static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1996 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00001997 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001998 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1999 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2000 // X > -1 -> X == 0, jump !sign.
2001 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002002 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002003 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2004 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002005 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002006 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002007 // X < 1 -> X <= 0
2008 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002009 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002010 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002011 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002012
Evan Chengd9558e02006-01-06 00:43:03 +00002013 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002014 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002015 case ISD::SETEQ: return X86::COND_E;
2016 case ISD::SETGT: return X86::COND_G;
2017 case ISD::SETGE: return X86::COND_GE;
2018 case ISD::SETLT: return X86::COND_L;
2019 case ISD::SETLE: return X86::COND_LE;
2020 case ISD::SETNE: return X86::COND_NE;
2021 case ISD::SETULT: return X86::COND_B;
2022 case ISD::SETUGT: return X86::COND_A;
2023 case ISD::SETULE: return X86::COND_BE;
2024 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002025 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002026 }
2027
2028 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002029
Chris Lattner4c78e022008-12-23 23:42:27 +00002030 // If LHS is a foldable load, but RHS is not, flip the condition.
2031 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2032 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2033 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2034 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002035 }
2036
Chris Lattner4c78e022008-12-23 23:42:27 +00002037 switch (SetCCOpcode) {
2038 default: break;
2039 case ISD::SETOLT:
2040 case ISD::SETOLE:
2041 case ISD::SETUGT:
2042 case ISD::SETUGE:
2043 std::swap(LHS, RHS);
2044 break;
2045 }
2046
2047 // On a floating point condition, the flags are set as follows:
2048 // ZF PF CF op
2049 // 0 | 0 | 0 | X > Y
2050 // 0 | 0 | 1 | X < Y
2051 // 1 | 0 | 0 | X == Y
2052 // 1 | 1 | 1 | unordered
2053 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002054 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002055 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002056 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002057 case ISD::SETOLT: // flipped
2058 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002059 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002060 case ISD::SETOLE: // flipped
2061 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002062 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002063 case ISD::SETUGT: // flipped
2064 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002065 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002066 case ISD::SETUGE: // flipped
2067 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002068 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002069 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002070 case ISD::SETNE: return X86::COND_NE;
2071 case ISD::SETUO: return X86::COND_P;
2072 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002073 }
Evan Chengd9558e02006-01-06 00:43:03 +00002074}
2075
Evan Cheng4a460802006-01-11 00:33:36 +00002076/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2077/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002078/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002079static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002080 switch (X86CC) {
2081 default:
2082 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002083 case X86::COND_B:
2084 case X86::COND_BE:
2085 case X86::COND_E:
2086 case X86::COND_P:
2087 case X86::COND_A:
2088 case X86::COND_AE:
2089 case X86::COND_NE:
2090 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002091 return true;
2092 }
2093}
2094
Evan Cheng5ced1d82006-04-06 23:23:56 +00002095/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002096/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman475871a2008-07-27 21:46:04 +00002097static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002098 if (Op.getOpcode() == ISD::UNDEF)
2099 return true;
2100
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002101 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002102 return (Val >= Low && Val < Hi);
2103}
2104
2105/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2106/// true if Op is undef or if its value equal to the specified value.
Dan Gohman475871a2008-07-27 21:46:04 +00002107static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Evan Chengc5cdff22006-04-07 21:53:05 +00002108 if (Op.getOpcode() == ISD::UNDEF)
2109 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002110 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002111}
2112
Evan Cheng0188ecb2006-03-22 18:59:22 +00002113/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2114/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2115bool X86::isPSHUFDMask(SDNode *N) {
2116 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2117
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002118 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002119 return false;
2120
2121 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002122 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002123 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002124 if (Arg.getOpcode() == ISD::UNDEF) continue;
2125 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002126 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002127 return false;
2128 }
2129
2130 return true;
2131}
2132
2133/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002134/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002135bool X86::isPSHUFHWMask(SDNode *N) {
2136 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2137
2138 if (N->getNumOperands() != 8)
2139 return false;
2140
2141 // Lower quadword copied in order.
2142 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002143 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002144 if (Arg.getOpcode() == ISD::UNDEF) continue;
2145 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002146 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002147 return false;
2148 }
2149
2150 // Upper quadword shuffled.
2151 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002152 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002153 if (Arg.getOpcode() == ISD::UNDEF) continue;
2154 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002155 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002156 if (Val < 4 || Val > 7)
2157 return false;
2158 }
2159
2160 return true;
2161}
2162
2163/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002164/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002165bool X86::isPSHUFLWMask(SDNode *N) {
2166 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2167
2168 if (N->getNumOperands() != 8)
2169 return false;
2170
2171 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002172 for (unsigned i = 4; i != 8; ++i)
2173 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002174 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002175
2176 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002177 for (unsigned i = 0; i != 4; ++i)
2178 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002179 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002180
2181 return true;
2182}
2183
Evan Cheng14aed5e2006-03-24 01:18:28 +00002184/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2185/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Dan Gohmane7852d02009-01-26 04:35:06 +00002186template<class SDOperand>
2187static bool isSHUFPMask(SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002188 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002189
Evan Cheng39623da2006-04-20 08:58:49 +00002190 unsigned Half = NumElems / 2;
2191 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002192 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002193 return false;
2194 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002195 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002196 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002197
2198 return true;
2199}
2200
Evan Cheng39623da2006-04-20 08:58:49 +00002201bool X86::isSHUFPMask(SDNode *N) {
2202 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002203 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002204}
2205
Evan Cheng213d2cf2007-05-17 18:45:50 +00002206/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002207/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2208/// half elements to come from vector 1 (which would equal the dest.) and
2209/// the upper half to come from vector 2.
Dan Gohmane7852d02009-01-26 04:35:06 +00002210template<class SDOperand>
2211static bool isCommutedSHUFP(SDOperand *Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002212 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002213
Chris Lattner5a88b832007-02-25 07:10:00 +00002214 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002215 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002216 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002217 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002218 for (unsigned i = Half; i < NumOps; ++i)
2219 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002220 return false;
2221 return true;
2222}
2223
2224static bool isCommutedSHUFP(SDNode *N) {
2225 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002226 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002227}
2228
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002229/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2230/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2231bool X86::isMOVHLPSMask(SDNode *N) {
2232 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2233
Evan Cheng2064a2b2006-03-28 06:50:32 +00002234 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002235 return false;
2236
Evan Cheng2064a2b2006-03-28 06:50:32 +00002237 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002238 return isUndefOrEqual(N->getOperand(0), 6) &&
2239 isUndefOrEqual(N->getOperand(1), 7) &&
2240 isUndefOrEqual(N->getOperand(2), 2) &&
2241 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002242}
2243
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002244/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2245/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2246/// <2, 3, 2, 3>
2247bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2248 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2249
2250 if (N->getNumOperands() != 4)
2251 return false;
2252
2253 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2254 return isUndefOrEqual(N->getOperand(0), 2) &&
2255 isUndefOrEqual(N->getOperand(1), 3) &&
2256 isUndefOrEqual(N->getOperand(2), 2) &&
2257 isUndefOrEqual(N->getOperand(3), 3);
2258}
2259
Evan Cheng5ced1d82006-04-06 23:23:56 +00002260/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2261/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2262bool X86::isMOVLPMask(SDNode *N) {
2263 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2264
2265 unsigned NumElems = N->getNumOperands();
2266 if (NumElems != 2 && NumElems != 4)
2267 return false;
2268
Evan Chengc5cdff22006-04-07 21:53:05 +00002269 for (unsigned i = 0; i < NumElems/2; ++i)
2270 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2271 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002272
Evan Chengc5cdff22006-04-07 21:53:05 +00002273 for (unsigned i = NumElems/2; i < NumElems; ++i)
2274 if (!isUndefOrEqual(N->getOperand(i), i))
2275 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002276
2277 return true;
2278}
2279
2280/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002281/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2282/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002283bool X86::isMOVHPMask(SDNode *N) {
2284 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2285
2286 unsigned NumElems = N->getNumOperands();
2287 if (NumElems != 2 && NumElems != 4)
2288 return false;
2289
Evan Chengc5cdff22006-04-07 21:53:05 +00002290 for (unsigned i = 0; i < NumElems/2; ++i)
2291 if (!isUndefOrEqual(N->getOperand(i), i))
2292 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002293
2294 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002295 SDValue Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002296 if (!isUndefOrEqual(Arg, i + NumElems))
2297 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002298 }
2299
2300 return true;
2301}
2302
Evan Cheng0038e592006-03-28 00:39:58 +00002303/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2304/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Dan Gohmane7852d02009-01-26 04:35:06 +00002305template<class SDOperand>
2306bool static isUNPCKLMask(SDOperand *Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002307 bool V2IsSplat = false) {
2308 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002309 return false;
2310
Chris Lattner5a88b832007-02-25 07:10:00 +00002311 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002312 SDValue BitI = Elts[i];
2313 SDValue BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002314 if (!isUndefOrEqual(BitI, j))
2315 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002316 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002317 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002318 return false;
2319 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002320 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002321 return false;
2322 }
Evan Cheng0038e592006-03-28 00:39:58 +00002323 }
2324
2325 return true;
2326}
2327
Evan Cheng39623da2006-04-20 08:58:49 +00002328bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2329 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002330 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002331}
2332
Evan Cheng4fcb9222006-03-28 02:43:26 +00002333/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2334/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Dan Gohmane7852d02009-01-26 04:35:06 +00002335template<class SDOperand>
2336bool static isUNPCKHMask(SDOperand *Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002337 bool V2IsSplat = false) {
2338 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002339 return false;
2340
Chris Lattner5a88b832007-02-25 07:10:00 +00002341 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002342 SDValue BitI = Elts[i];
2343 SDValue BitI1 = Elts[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002344 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002345 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002346 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002347 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002348 return false;
2349 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002350 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002351 return false;
2352 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002353 }
2354
2355 return true;
2356}
2357
Evan Cheng39623da2006-04-20 08:58:49 +00002358bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2359 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002360 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002361}
2362
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002363/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2364/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2365/// <0, 0, 1, 1>
2366bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2367 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2368
2369 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002370 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002371 return false;
2372
2373 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002374 SDValue BitI = N->getOperand(i);
2375 SDValue BitI1 = N->getOperand(i+1);
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002376
Evan Chengc5cdff22006-04-07 21:53:05 +00002377 if (!isUndefOrEqual(BitI, j))
2378 return false;
2379 if (!isUndefOrEqual(BitI1, j))
2380 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002381 }
2382
2383 return true;
2384}
2385
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002386/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2387/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2388/// <2, 2, 3, 3>
2389bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2390 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2391
2392 unsigned NumElems = N->getNumOperands();
2393 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2394 return false;
2395
2396 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002397 SDValue BitI = N->getOperand(i);
2398 SDValue BitI1 = N->getOperand(i + 1);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002399
2400 if (!isUndefOrEqual(BitI, j))
2401 return false;
2402 if (!isUndefOrEqual(BitI1, j))
2403 return false;
2404 }
2405
2406 return true;
2407}
2408
Evan Cheng017dcc62006-04-21 01:05:10 +00002409/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2410/// specifies a shuffle of elements that is suitable for input to MOVSS,
2411/// MOVSD, and MOVD, i.e. setting the lowest element.
Dan Gohmane7852d02009-01-26 04:35:06 +00002412template<class SDOperand>
2413static bool isMOVLMask(SDOperand *Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002414 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002415 return false;
2416
Chris Lattner5a88b832007-02-25 07:10:00 +00002417 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002418 return false;
2419
Chris Lattner5a88b832007-02-25 07:10:00 +00002420 for (unsigned i = 1; i < NumElts; ++i) {
2421 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002422 return false;
2423 }
2424
2425 return true;
2426}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002427
Evan Cheng017dcc62006-04-21 01:05:10 +00002428bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002429 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002430 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002431}
2432
Evan Cheng017dcc62006-04-21 01:05:10 +00002433/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2434/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002435/// element of vector 2 and the other elements to come from vector 1 in order.
Dan Gohmane7852d02009-01-26 04:35:06 +00002436template<class SDOperand>
2437static bool isCommutedMOVL(SDOperand *Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002438 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002439 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002440 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002441 return false;
2442
2443 if (!isUndefOrEqual(Ops[0], 0))
2444 return false;
2445
Chris Lattner5a88b832007-02-25 07:10:00 +00002446 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002447 SDValue Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002448 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2449 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2450 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002451 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002452 }
2453
2454 return true;
2455}
2456
Evan Cheng8cf723d2006-09-08 01:50:06 +00002457static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2458 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002459 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002460 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2461 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002462}
2463
Evan Chengd9539472006-04-14 21:59:03 +00002464/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2465/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2466bool X86::isMOVSHDUPMask(SDNode *N) {
2467 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2468
2469 if (N->getNumOperands() != 4)
2470 return false;
2471
2472 // Expect 1, 1, 3, 3
2473 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002474 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002475 if (Arg.getOpcode() == ISD::UNDEF) continue;
2476 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002477 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002478 if (Val != 1) return false;
2479 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002480
2481 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002482 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002483 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002484 if (Arg.getOpcode() == ISD::UNDEF) continue;
2485 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002486 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002487 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002488 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002489 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002490
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002491 // Don't use movshdup if it can be done with a shufps.
2492 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002493}
2494
2495/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2496/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2497bool X86::isMOVSLDUPMask(SDNode *N) {
2498 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2499
2500 if (N->getNumOperands() != 4)
2501 return false;
2502
2503 // Expect 0, 0, 2, 2
2504 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002505 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002506 if (Arg.getOpcode() == ISD::UNDEF) continue;
2507 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002508 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002509 if (Val != 0) return false;
2510 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002511
2512 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002513 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002514 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002515 if (Arg.getOpcode() == ISD::UNDEF) continue;
2516 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002517 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002518 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002519 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002520 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002521
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002522 // Don't use movshdup if it can be done with a shufps.
2523 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002524}
2525
Evan Cheng49892af2007-06-19 00:02:56 +00002526/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2527/// specifies a identity operation on the LHS or RHS.
2528static bool isIdentityMask(SDNode *N, bool RHS = false) {
2529 unsigned NumElems = N->getNumOperands();
2530 for (unsigned i = 0; i < NumElems; ++i)
2531 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2532 return false;
2533 return true;
2534}
2535
Evan Chengb9df0ca2006-03-22 02:53:00 +00002536/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2537/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002538static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002539 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2540
Evan Chengb9df0ca2006-03-22 02:53:00 +00002541 // This is a splat operation if each element of the permute is the same, and
2542 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002543 unsigned NumElems = N->getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002544 SDValue ElementBase;
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002545 unsigned i = 0;
2546 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002547 SDValue Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002548 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002549 ElementBase = Elt;
2550 break;
2551 }
2552 }
2553
Gabor Greifba36cb52008-08-28 21:40:38 +00002554 if (!ElementBase.getNode())
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002555 return false;
2556
2557 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002558 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002559 if (Arg.getOpcode() == ISD::UNDEF) continue;
2560 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002561 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002562 }
2563
2564 // Make sure it is a splat of the first vector operand.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002565 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002566}
2567
Mon P Wang62c75ea2008-12-23 04:03:27 +00002568/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2569/// we want to splat.
2570static SDValue getSplatMaskEltNo(SDNode *N) {
2571 assert(isSplatMask(N) && "Not a splat mask");
2572 unsigned NumElems = N->getNumOperands();
2573 SDValue ElementBase;
2574 unsigned i = 0;
2575 for (; i != NumElems; ++i) {
2576 SDValue Elt = N->getOperand(i);
2577 if (isa<ConstantSDNode>(Elt))
2578 return Elt;
2579 }
2580 assert(0 && " No splat value found!");
2581 return SDValue();
2582}
2583
2584
Evan Chengc575ca22006-04-17 20:43:08 +00002585/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2586/// a splat of a single element and it's a 2 or 4 element mask.
2587bool X86::isSplatMask(SDNode *N) {
2588 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2589
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002590 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002591 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2592 return false;
2593 return ::isSplatMask(N);
2594}
2595
Evan Chengf686d9b2006-10-27 21:08:32 +00002596/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2597/// specifies a splat of zero element.
2598bool X86::isSplatLoMask(SDNode *N) {
2599 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2600
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002601 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002602 if (!isUndefOrEqual(N->getOperand(i), 0))
2603 return false;
2604 return true;
2605}
2606
Evan Cheng0b457f02008-09-25 20:50:48 +00002607/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2608/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2609bool X86::isMOVDDUPMask(SDNode *N) {
2610 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2611
2612 unsigned e = N->getNumOperands() / 2;
2613 for (unsigned i = 0; i < e; ++i)
2614 if (!isUndefOrEqual(N->getOperand(i), i))
2615 return false;
2616 for (unsigned i = 0; i < e; ++i)
2617 if (!isUndefOrEqual(N->getOperand(e+i), i))
2618 return false;
2619 return true;
2620}
2621
Evan Cheng63d33002006-03-22 08:01:21 +00002622/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2623/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2624/// instructions.
2625unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002626 unsigned NumOperands = N->getNumOperands();
2627 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2628 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002629 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002630 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002631 SDValue Arg = N->getOperand(NumOperands-i-1);
Evan Chengef698ca2006-03-31 00:30:29 +00002632 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002633 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002634 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002635 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002636 if (i != NumOperands - 1)
2637 Mask <<= Shift;
2638 }
Evan Cheng63d33002006-03-22 08:01:21 +00002639
2640 return Mask;
2641}
2642
Evan Cheng506d3df2006-03-29 23:07:14 +00002643/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2644/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2645/// instructions.
2646unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2647 unsigned Mask = 0;
2648 // 8 nodes, but we only care about the last 4.
2649 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002650 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SDValue Arg = N->getOperand(i);
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002652 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002653 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002654 Mask |= (Val - 4);
2655 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002656 if (i != 4)
2657 Mask <<= 2;
2658 }
2659
2660 return Mask;
2661}
2662
2663/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2664/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2665/// instructions.
2666unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2667 unsigned Mask = 0;
2668 // 8 nodes, but we only care about the first 4.
2669 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002670 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002671 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002672 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002673 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002674 Mask |= Val;
2675 if (i != 0)
2676 Mask <<= 2;
2677 }
2678
2679 return Mask;
2680}
2681
Evan Chengc21a0532006-04-05 01:47:37 +00002682/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2683/// specifies a 8 element shuffle that can be broken into a pair of
2684/// PSHUFHW and PSHUFLW.
2685static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2686 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2687
2688 if (N->getNumOperands() != 8)
2689 return false;
2690
2691 // Lower quadword shuffled.
2692 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002693 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002694 if (Arg.getOpcode() == ISD::UNDEF) continue;
2695 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002696 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002697 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002698 return false;
2699 }
2700
2701 // Upper quadword shuffled.
2702 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002703 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002704 if (Arg.getOpcode() == ISD::UNDEF) continue;
2705 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002706 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengc21a0532006-04-05 01:47:37 +00002707 if (Val < 4 || Val > 7)
2708 return false;
2709 }
2710
2711 return true;
2712}
2713
Chris Lattner8a594482007-11-25 00:24:49 +00002714/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002715/// values in ther permute mask.
Dan Gohman475871a2008-07-27 21:46:04 +00002716static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2717 SDValue &V2, SDValue &Mask,
Evan Cheng9eca5e82006-10-25 21:49:50 +00002718 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002719 MVT VT = Op.getValueType();
2720 MVT MaskVT = Mask.getValueType();
2721 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002722 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002723 SmallVector<SDValue, 8> MaskVec;
Dale Johannesenace16102009-02-03 19:33:06 +00002724 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002725
2726 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002727 SDValue Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002728 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002729 MaskVec.push_back(DAG.getUNDEF(EltVT));
Evan Cheng80d428c2006-04-19 22:48:17 +00002730 continue;
2731 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002732 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002733 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002734 if (Val < NumElems)
2735 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2736 else
2737 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2738 }
2739
Evan Cheng9eca5e82006-10-25 21:49:50 +00002740 std::swap(V1, V2);
Dale Johannesenace16102009-02-03 19:33:06 +00002741 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
2742 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002743}
2744
Evan Cheng779ccea2007-12-07 21:30:01 +00002745/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2746/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002747static
Dale Johannesenace16102009-02-03 19:33:06 +00002748SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002749 MVT MaskVT = Mask.getValueType();
2750 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002751 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SmallVector<SDValue, 8> MaskVec;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002753 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002754 SDValue Arg = Mask.getOperand(i);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002755 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002756 MaskVec.push_back(DAG.getUNDEF(EltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002757 continue;
2758 }
2759 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002760 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002761 if (Val < NumElems)
2762 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2763 else
2764 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2765 }
Dale Johannesenace16102009-02-03 19:33:06 +00002766 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002767}
2768
2769
Evan Cheng533a0aa2006-04-19 20:35:22 +00002770/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2771/// match movhlps. The lower half elements should come from upper half of
2772/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002773/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002774static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2775 unsigned NumElems = Mask->getNumOperands();
2776 if (NumElems != 4)
2777 return false;
2778 for (unsigned i = 0, e = 2; i != e; ++i)
2779 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2780 return false;
2781 for (unsigned i = 2; i != 4; ++i)
2782 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2783 return false;
2784 return true;
2785}
2786
Evan Cheng5ced1d82006-04-06 23:23:56 +00002787/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002788/// is promoted to a vector. It also returns the LoadSDNode by reference if
2789/// required.
2790static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002791 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2792 return false;
2793 N = N->getOperand(0).getNode();
2794 if (!ISD::isNON_EXTLoad(N))
2795 return false;
2796 if (LD)
2797 *LD = cast<LoadSDNode>(N);
2798 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002799}
2800
Evan Cheng533a0aa2006-04-19 20:35:22 +00002801/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2802/// match movlp{s|d}. The lower half elements should come from lower half of
2803/// V1 (and in order), and the upper half elements should come from the upper
2804/// half of V2 (and in order). And since V1 will become the source of the
2805/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002806static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002807 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002808 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002809 // Is V2 is a vector load, don't do this transformation. We will try to use
2810 // load folding shufps op.
2811 if (ISD::isNON_EXTLoad(V2))
2812 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002813
Evan Cheng533a0aa2006-04-19 20:35:22 +00002814 unsigned NumElems = Mask->getNumOperands();
2815 if (NumElems != 2 && NumElems != 4)
2816 return false;
2817 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2818 if (!isUndefOrEqual(Mask->getOperand(i), i))
2819 return false;
2820 for (unsigned i = NumElems/2; i != NumElems; ++i)
2821 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2822 return false;
2823 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002824}
2825
Evan Cheng39623da2006-04-20 08:58:49 +00002826/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2827/// all the same.
2828static bool isSplatVector(SDNode *N) {
2829 if (N->getOpcode() != ISD::BUILD_VECTOR)
2830 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002831
Dan Gohman475871a2008-07-27 21:46:04 +00002832 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002833 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2834 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002835 return false;
2836 return true;
2837}
2838
Evan Cheng8cf723d2006-09-08 01:50:06 +00002839/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2840/// to an undef.
2841static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002842 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002843 return false;
2844
Dan Gohman475871a2008-07-27 21:46:04 +00002845 SDValue V1 = N->getOperand(0);
2846 SDValue V2 = N->getOperand(1);
2847 SDValue Mask = N->getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002848 unsigned NumElems = Mask.getNumOperands();
2849 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002850 SDValue Arg = Mask.getOperand(i);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002851 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002852 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8cf723d2006-09-08 01:50:06 +00002853 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2854 return false;
2855 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2856 return false;
2857 }
2858 }
2859 return true;
2860}
2861
Evan Cheng213d2cf2007-05-17 18:45:50 +00002862/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2863/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002864static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002865 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002866 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002867 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002868 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002869}
2870
2871/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2872/// to an zero vector.
2873static bool isZeroShuffle(SDNode *N) {
2874 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2875 return false;
2876
Dan Gohman475871a2008-07-27 21:46:04 +00002877 SDValue V1 = N->getOperand(0);
2878 SDValue V2 = N->getOperand(1);
2879 SDValue Mask = N->getOperand(2);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002880 unsigned NumElems = Mask.getNumOperands();
2881 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002882 SDValue Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002883 if (Arg.getOpcode() == ISD::UNDEF)
2884 continue;
2885
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002886 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattner8a594482007-11-25 00:24:49 +00002887 if (Idx < NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002888 unsigned Opc = V1.getNode()->getOpcode();
2889 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002890 continue;
2891 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002892 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002893 return false;
2894 } else if (Idx >= NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002895 unsigned Opc = V2.getNode()->getOpcode();
2896 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002897 continue;
2898 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002899 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattner8a594482007-11-25 00:24:49 +00002900 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002901 }
2902 }
2903 return true;
2904}
2905
2906/// getZeroVector - Returns a vector of specified type with all zero elements.
2907///
Dale Johannesenace16102009-02-03 19:33:06 +00002908static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2909 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002910 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002911
2912 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2913 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002914 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002915 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002916 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00002917 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002918 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002919 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00002920 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002921 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Dale Johannesenace16102009-02-03 19:33:06 +00002923 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002924 }
Dale Johannesenace16102009-02-03 19:33:06 +00002925 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002926}
2927
Chris Lattner8a594482007-11-25 00:24:49 +00002928/// getOnesVector - Returns a vector of specified type with all bits set.
2929///
Dale Johannesenace16102009-02-03 19:33:06 +00002930static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002931 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002932
2933 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2934 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002935 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2936 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002937 if (VT.getSizeInBits() == 64) // MMX
Dale Johannesenace16102009-02-03 19:33:06 +00002938 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002939 else // SSE
Dale Johannesenace16102009-02-03 19:33:06 +00002940 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
2941 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002942}
2943
2944
Evan Cheng39623da2006-04-20 08:58:49 +00002945/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2946/// that point to V2 points to its first element.
Dan Gohman475871a2008-07-27 21:46:04 +00002947static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002948 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2949
2950 bool Changed = false;
Dan Gohman475871a2008-07-27 21:46:04 +00002951 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002952 unsigned NumElems = Mask.getNumOperands();
2953 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002954 SDValue Arg = Mask.getOperand(i);
Evan Cheng39623da2006-04-20 08:58:49 +00002955 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002956 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng39623da2006-04-20 08:58:49 +00002957 if (Val > NumElems) {
2958 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2959 Changed = true;
2960 }
2961 }
2962 MaskVec.push_back(Arg);
2963 }
2964
2965 if (Changed)
Dale Johannesenace16102009-02-03 19:33:06 +00002966 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
2967 Mask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002968 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002969 return Mask;
2970}
2971
Evan Cheng017dcc62006-04-21 01:05:10 +00002972/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2973/// operation of specified width.
Dale Johannesenace16102009-02-03 19:33:06 +00002974static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002975 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2976 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002977
Dan Gohman475871a2008-07-27 21:46:04 +00002978 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002979 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2980 for (unsigned i = 1; i != NumElems; ++i)
2981 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Dale Johannesenace16102009-02-03 19:33:06 +00002982 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2983 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002984}
2985
Evan Chengc575ca22006-04-17 20:43:08 +00002986/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2987/// of specified width.
Dale Johannesenace16102009-02-03 19:33:06 +00002988static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG,
2989 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002990 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2991 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002992 SmallVector<SDValue, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002993 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2994 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2995 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2996 }
Dale Johannesenace16102009-02-03 19:33:06 +00002997 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2998 &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002999}
3000
Evan Cheng39623da2006-04-20 08:58:49 +00003001/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3002/// of specified width.
Dale Johannesenace16102009-02-03 19:33:06 +00003003static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG,
3004 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003005 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3006 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00003007 unsigned Half = NumElems/2;
Dan Gohman475871a2008-07-27 21:46:04 +00003008 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00003009 for (unsigned i = 0; i != Half; ++i) {
3010 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3011 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3012 }
Dale Johannesenace16102009-02-03 19:33:06 +00003013 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3014 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00003015}
3016
Chris Lattner62098042008-03-09 01:05:04 +00003017/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3018/// element #0 of a vector with the specified index, leaving the rest of the
3019/// elements in place.
Dan Gohman475871a2008-07-27 21:46:04 +00003020static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Dale Johannesenace16102009-02-03 19:33:06 +00003021 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003022 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3023 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003024 SmallVector<SDValue, 8> MaskVec;
Chris Lattner62098042008-03-09 01:05:04 +00003025 // Element #0 of the result gets the elt we are replacing.
3026 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3027 for (unsigned i = 1; i != NumElems; ++i)
3028 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003029 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3030 &MaskVec[0], MaskVec.size());
Chris Lattner62098042008-03-09 01:05:04 +00003031}
3032
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003033/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman475871a2008-07-27 21:46:04 +00003034static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003035 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3036 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003037 if (PVT == VT)
3038 return Op;
Dan Gohman475871a2008-07-27 21:46:04 +00003039 SDValue V1 = Op.getOperand(0);
3040 SDValue Mask = Op.getOperand(2);
Mon P Wang62c75ea2008-12-23 04:03:27 +00003041 unsigned MaskNumElems = Mask.getNumOperands();
3042 unsigned NumElems = MaskNumElems;
Dale Johannesenace16102009-02-03 19:33:06 +00003043 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003044 // Special handling of v4f32 -> v4i32.
3045 if (VT != MVT::v4f32) {
Mon P Wang62c75ea2008-12-23 04:03:27 +00003046 // Find which element we want to splat.
3047 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3048 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3049 // unpack elements to the correct location
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003050 while (NumElems > 4) {
Mon P Wang62c75ea2008-12-23 04:03:27 +00003051 if (EltNo < NumElems/2) {
Dale Johannesenace16102009-02-03 19:33:06 +00003052 Mask = getUnpacklMask(MaskNumElems, DAG, dl);
Mon P Wang62c75ea2008-12-23 04:03:27 +00003053 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00003054 Mask = getUnpackhMask(MaskNumElems, DAG, dl);
Mon P Wang62c75ea2008-12-23 04:03:27 +00003055 EltNo -= NumElems/2;
3056 }
Dale Johannesenace16102009-02-03 19:33:06 +00003057 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1, Mask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003058 NumElems >>= 1;
3059 }
Mon P Wang62c75ea2008-12-23 04:03:27 +00003060 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00003061 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengc575ca22006-04-17 20:43:08 +00003062 }
Evan Chengc575ca22006-04-17 20:43:08 +00003063
Dale Johannesenace16102009-02-03 19:33:06 +00003064 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3065 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
Dale Johannesene8d72302009-02-06 23:05:02 +00003066 DAG.getUNDEF(PVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00003067 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Chengc575ca22006-04-17 20:43:08 +00003068}
3069
Evan Cheng0b457f02008-09-25 20:50:48 +00003070/// isVectorLoad - Returns true if the node is a vector load, a scalar
3071/// load that's promoted to vector, or a load bitcasted.
3072static bool isVectorLoad(SDValue Op) {
3073 assert(Op.getValueType().isVector() && "Expected a vector type");
3074 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3075 Op.getOpcode() == ISD::BIT_CONVERT) {
3076 return isa<LoadSDNode>(Op.getOperand(0));
3077 }
3078 return isa<LoadSDNode>(Op);
3079}
3080
3081
3082/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3083///
3084static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3085 SelectionDAG &DAG, bool HasSSE3) {
3086 // If we have sse3 and shuffle has more than one use or input is a load, then
3087 // use movddup. Otherwise, use movlhps.
3088 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3089 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3090 MVT VT = Op.getValueType();
3091 if (VT == PVT)
3092 return Op;
Dale Johannesenace16102009-02-03 19:33:06 +00003093 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0b457f02008-09-25 20:50:48 +00003094 unsigned NumElems = PVT.getVectorNumElements();
3095 if (NumElems == 2) {
3096 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00003097 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng0b457f02008-09-25 20:50:48 +00003098 } else {
3099 assert(NumElems == 4);
3100 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3101 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00003102 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
3103 Cst0, Cst1, Cst0, Cst1);
Evan Cheng0b457f02008-09-25 20:50:48 +00003104 }
3105
Dale Johannesenace16102009-02-03 19:33:06 +00003106 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3107 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
Dale Johannesene8d72302009-02-06 23:05:02 +00003108 DAG.getUNDEF(PVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00003109 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Cheng0b457f02008-09-25 20:50:48 +00003110}
3111
Evan Chengba05f722006-04-21 23:03:30 +00003112/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003113/// vector of zero or undef vector. This produces a shuffle where the low
3114/// element of V2 is swizzled into the zero/undef vector, landing at element
3115/// 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 +00003116static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003117 bool isZero, bool HasSSE2,
3118 SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00003119 DebugLoc dl = V2.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003120 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003121 SDValue V1 = isZero
Dale Johannesene8d72302009-02-06 23:05:02 +00003122 ? getZeroVector(VT, HasSSE2, DAG, dl) : DAG.getUNDEF(VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003123 unsigned NumElems = V2.getValueType().getVectorNumElements();
3124 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3125 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003126 SmallVector<SDValue, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003127 for (unsigned i = 0; i != NumElems; ++i)
3128 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3129 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3130 else
3131 MaskVec.push_back(DAG.getConstant(i, EVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003132 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003133 &MaskVec[0], MaskVec.size());
Dale Johannesenace16102009-02-03 19:33:06 +00003134 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003135}
3136
Evan Chengf26ffe92008-05-29 08:22:04 +00003137/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3138/// a shuffle that is zero.
3139static
Dan Gohman475871a2008-07-27 21:46:04 +00003140unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengf26ffe92008-05-29 08:22:04 +00003141 unsigned NumElems, bool Low,
3142 SelectionDAG &DAG) {
3143 unsigned NumZeros = 0;
3144 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003145 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00003147 if (Idx.getOpcode() == ISD::UNDEF) {
3148 ++NumZeros;
3149 continue;
3150 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003151 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3152 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003153 ++NumZeros;
3154 else
3155 break;
3156 }
3157 return NumZeros;
3158}
3159
3160/// isVectorShift - Returns true if the shuffle can be implemented as a
3161/// logical left or right shift of a vector.
Dan Gohman475871a2008-07-27 21:46:04 +00003162static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3163 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003164 unsigned NumElems = Mask.getNumOperands();
3165
3166 isLeft = true;
3167 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3168 if (!NumZeros) {
3169 isLeft = false;
3170 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3171 if (!NumZeros)
3172 return false;
3173 }
3174
3175 bool SeenV1 = false;
3176 bool SeenV2 = false;
3177 for (unsigned i = NumZeros; i < NumElems; ++i) {
3178 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman475871a2008-07-27 21:46:04 +00003179 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengf26ffe92008-05-29 08:22:04 +00003180 if (Idx.getOpcode() == ISD::UNDEF)
3181 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003182 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengf26ffe92008-05-29 08:22:04 +00003183 if (Index < NumElems)
3184 SeenV1 = true;
3185 else {
3186 Index -= NumElems;
3187 SeenV2 = true;
3188 }
3189 if (Index != Val)
3190 return false;
3191 }
3192 if (SeenV1 && SeenV2)
3193 return false;
3194
3195 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3196 ShAmt = NumZeros;
3197 return true;
3198}
3199
3200
Evan Chengc78d3b42006-04-24 18:01:45 +00003201/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3202///
Dan Gohman475871a2008-07-27 21:46:04 +00003203static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003204 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003205 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003206 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003207 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003208
Dale Johannesenace16102009-02-03 19:33:06 +00003209 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003210 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003211 bool First = true;
3212 for (unsigned i = 0; i < 16; ++i) {
3213 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3214 if (ThisIsNonZero && First) {
3215 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003216 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003217 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003218 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003219 First = false;
3220 }
3221
3222 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003223 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003224 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3225 if (LastIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003226 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
3227 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003228 }
3229 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003230 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3231 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00003232 ThisElt, DAG.getConstant(8, MVT::i8));
3233 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003234 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003235 } else
3236 ThisElt = LastElt;
3237
Gabor Greifba36cb52008-08-28 21:40:38 +00003238 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003239 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003240 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003241 }
3242 }
3243
Dale Johannesenace16102009-02-03 19:33:06 +00003244 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003245}
3246
Bill Wendlinga348c562007-03-22 18:42:45 +00003247/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003248///
Dan Gohman475871a2008-07-27 21:46:04 +00003249static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003250 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003251 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003252 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003253 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003254
Dale Johannesenace16102009-02-03 19:33:06 +00003255 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003256 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003257 bool First = true;
3258 for (unsigned i = 0; i < 8; ++i) {
3259 bool isNonZero = (NonZeros & (1 << i)) != 0;
3260 if (isNonZero) {
3261 if (First) {
3262 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003263 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003264 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003265 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003266 First = false;
3267 }
Dale Johannesenace16102009-02-03 19:33:06 +00003268 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
3269 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003270 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003271 }
3272 }
3273
3274 return V;
3275}
3276
Evan Chengf26ffe92008-05-29 08:22:04 +00003277/// getVShift - Return a vector logical shift node.
3278///
Dan Gohman475871a2008-07-27 21:46:04 +00003279static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003280 unsigned NumBits, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003281 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003282 bool isMMX = VT.getSizeInBits() == 64;
3283 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003284 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003285 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3286 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3287 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003288 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003289}
3290
Dan Gohman475871a2008-07-27 21:46:04 +00003291SDValue
3292X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00003293 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003294 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003295 if (ISD::isBuildVectorAllZeros(Op.getNode())
3296 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003297 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3298 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3299 // eliminated on x86-32 hosts.
3300 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3301 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003302
Gabor Greifba36cb52008-08-28 21:40:38 +00003303 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003304 return getOnesVector(Op.getValueType(), DAG, dl);
3305 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003306 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003307
Duncan Sands83ec4b62008-06-06 12:08:01 +00003308 MVT VT = Op.getValueType();
3309 MVT EVT = VT.getVectorElementType();
3310 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003311
3312 unsigned NumElems = Op.getNumOperands();
3313 unsigned NumZero = 0;
3314 unsigned NumNonZero = 0;
3315 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003316 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003317 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003318 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003319 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003320 if (Elt.getOpcode() == ISD::UNDEF)
3321 continue;
3322 Values.insert(Elt);
3323 if (Elt.getOpcode() != ISD::Constant &&
3324 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003325 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003326 if (isZeroNode(Elt))
3327 NumZero++;
3328 else {
3329 NonZeros |= (1 << i);
3330 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003331 }
3332 }
3333
Dan Gohman7f321562007-06-25 16:23:39 +00003334 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003335 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003336 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003337 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003338
Chris Lattner67f453a2008-03-09 05:42:06 +00003339 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003340 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003341 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003343
Chris Lattner62098042008-03-09 01:05:04 +00003344 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3345 // the value are obviously zero, truncate the value to i32 and do the
3346 // insertion that way. Only do this if the value is non-constant or if the
3347 // value is a constant being inserted into element 0. It is cheaper to do
3348 // a constant pool load than it is to do a movd + shuffle.
3349 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3350 (!IsAllConstants || Idx == 0)) {
3351 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3352 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003353 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3354 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003355
3356 // Truncate the value (which may itself be a constant) to i32, and
3357 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003358 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3359 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003360 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3361 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003362
3363 // Now we have our 32-bit value zero extended in the low element of
3364 // a vector. If Idx != 0, swizzle it into place.
3365 if (Idx != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003366 SDValue Ops[] = {
Dale Johannesene8d72302009-02-06 23:05:02 +00003367 Item, DAG.getUNDEF(Item.getValueType()),
Dale Johannesenace16102009-02-03 19:33:06 +00003368 getSwapEltZeroMask(VecElts, Idx, DAG, dl)
Chris Lattner62098042008-03-09 01:05:04 +00003369 };
Dale Johannesenace16102009-02-03 19:33:06 +00003370 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VecVT, Ops, 3);
Chris Lattner62098042008-03-09 01:05:04 +00003371 }
Dale Johannesenace16102009-02-03 19:33:06 +00003372 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003373 }
3374 }
3375
Chris Lattner19f79692008-03-08 22:59:52 +00003376 // If we have a constant or non-constant insertion into the low element of
3377 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3378 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3379 // depending on what the source datatype is. Because we can only get here
3380 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3381 if (Idx == 0 &&
3382 // Don't do this for i64 values on x86-32.
3383 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003384 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003385 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003386 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3387 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003388 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003389
3390 // Is it a vector logical left shift?
3391 if (NumElems == 2 && Idx == 1 &&
3392 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003393 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003394 return getVShift(true, VT,
3395 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003396 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003397 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003398
3399 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003400 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003401
Chris Lattner19f79692008-03-08 22:59:52 +00003402 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3403 // is a non-constant being inserted into an element other than the low one,
3404 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3405 // movd/movss) to move this into the low element, then shuffle it into
3406 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003407 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003408 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003409
Evan Cheng0db9fe62006-04-25 20:13:52 +00003410 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003411 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3412 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003413 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3414 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003415 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003416 for (unsigned i = 0; i < NumElems; i++)
3417 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003418 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003419 &MaskVec[0], MaskVec.size());
Dale Johannesenace16102009-02-03 19:33:06 +00003420 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, Item,
Dale Johannesene8d72302009-02-06 23:05:02 +00003421 DAG.getUNDEF(VT), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003422 }
3423 }
3424
Chris Lattner67f453a2008-03-09 05:42:06 +00003425 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3426 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003427 return SDValue();
Chris Lattner67f453a2008-03-09 05:42:06 +00003428
Dan Gohmana3941172007-07-24 22:55:08 +00003429 // A vector full of immediates; various special cases are already
3430 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003431 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003432 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003433
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003434 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003435 if (EVTBits == 64) {
3436 if (NumNonZero == 1) {
3437 // One half is zero or undef.
3438 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003439 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003440 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003441 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3442 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003443 }
Dan Gohman475871a2008-07-27 21:46:04 +00003444 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003445 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003446
3447 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003448 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003449 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003450 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003451 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003452 }
3453
Bill Wendling826f36f2007-03-28 00:57:11 +00003454 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003456 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003457 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003458 }
3459
3460 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003461 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003462 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003463 if (NumElems == 4 && NumZero > 0) {
3464 for (unsigned i = 0; i < 4; ++i) {
3465 bool isZero = !(NonZeros & (1 << i));
3466 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003467 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003468 else
Dale Johannesenace16102009-02-03 19:33:06 +00003469 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003470 }
3471
3472 for (unsigned i = 0; i < 2; ++i) {
3473 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3474 default: break;
3475 case 0:
3476 V[i] = V[i*2]; // Must be a zero vector.
3477 break;
3478 case 1:
Dale Johannesenace16102009-02-03 19:33:06 +00003479 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2+1], V[i*2],
3480 getMOVLMask(NumElems, DAG, dl));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003481 break;
3482 case 2:
Dale Johannesenace16102009-02-03 19:33:06 +00003483 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3484 getMOVLMask(NumElems, DAG, dl));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003485 break;
3486 case 3:
Dale Johannesenace16102009-02-03 19:33:06 +00003487 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3488 getUnpacklMask(NumElems, DAG, dl));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003489 break;
3490 }
3491 }
3492
Duncan Sands83ec4b62008-06-06 12:08:01 +00003493 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3494 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003495 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003496 bool Reverse = (NonZeros & 0x3) == 2;
3497 for (unsigned i = 0; i < 2; ++i)
3498 if (Reverse)
3499 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3500 else
3501 MaskVec.push_back(DAG.getConstant(i, EVT));
3502 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3503 for (unsigned i = 0; i < 2; ++i)
3504 if (Reverse)
3505 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3506 else
3507 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003508 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00003509 &MaskVec[0], MaskVec.size());
Dale Johannesenace16102009-02-03 19:33:06 +00003510 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[0], V[1], ShufMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003511 }
3512
3513 if (Values.size() > 2) {
3514 // Expand into a number of unpckl*.
3515 // e.g. for v4f32
3516 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3517 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3518 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dale Johannesenace16102009-02-03 19:33:06 +00003519 SDValue UnpckMask = getUnpacklMask(NumElems, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003520 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003521 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003522 NumElems >>= 1;
3523 while (NumElems != 0) {
3524 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003525 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i], V[i + NumElems],
Evan Cheng0db9fe62006-04-25 20:13:52 +00003526 UnpckMask);
3527 NumElems >>= 1;
3528 }
3529 return V[0];
3530 }
3531
Dan Gohman475871a2008-07-27 21:46:04 +00003532 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003533}
3534
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003535static
Dan Gohman475871a2008-07-27 21:46:04 +00003536SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendlinge85dc492008-08-21 22:35:37 +00003537 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003538 TargetLowering &TLI, DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00003539 SDValue NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3541 MVT MaskEVT = MaskVT.getVectorElementType();
3542 MVT PtrVT = TLI.getPointerTy();
Gabor Greifba36cb52008-08-28 21:40:38 +00003543 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3544 PermMask.getNode()->op_end());
Evan Cheng14b32e12007-12-11 01:46:18 +00003545
3546 // First record which half of which vector the low elements come from.
3547 SmallVector<unsigned, 4> LowQuad(4);
3548 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003549 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003550 if (Elt.getOpcode() == ISD::UNDEF)
3551 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003552 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003553 int QuadIdx = EltIdx / 4;
3554 ++LowQuad[QuadIdx];
3555 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003556
Evan Cheng14b32e12007-12-11 01:46:18 +00003557 int BestLowQuad = -1;
3558 unsigned MaxQuad = 1;
3559 for (unsigned i = 0; i < 4; ++i) {
3560 if (LowQuad[i] > MaxQuad) {
3561 BestLowQuad = i;
3562 MaxQuad = LowQuad[i];
3563 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003564 }
3565
Evan Cheng14b32e12007-12-11 01:46:18 +00003566 // Record which half of which vector the high elements come from.
3567 SmallVector<unsigned, 4> HighQuad(4);
3568 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003569 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003570 if (Elt.getOpcode() == ISD::UNDEF)
3571 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003572 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003573 int QuadIdx = EltIdx / 4;
3574 ++HighQuad[QuadIdx];
3575 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003576
Evan Cheng14b32e12007-12-11 01:46:18 +00003577 int BestHighQuad = -1;
3578 MaxQuad = 1;
3579 for (unsigned i = 0; i < 4; ++i) {
3580 if (HighQuad[i] > MaxQuad) {
3581 BestHighQuad = i;
3582 MaxQuad = HighQuad[i];
3583 }
3584 }
3585
3586 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3587 if (BestLowQuad != -1 || BestHighQuad != -1) {
3588 // First sort the 4 chunks in order using shufpd.
Dan Gohman475871a2008-07-27 21:46:04 +00003589 SmallVector<SDValue, 8> MaskVec;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003590
Evan Cheng14b32e12007-12-11 01:46:18 +00003591 if (BestLowQuad != -1)
3592 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3593 else
3594 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003595
Evan Cheng14b32e12007-12-11 01:46:18 +00003596 if (BestHighQuad != -1)
3597 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3598 else
3599 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003600
Dale Johannesenace16102009-02-03 19:33:06 +00003601 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, &MaskVec[0],2);
3602 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
3603 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3604 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), Mask);
3605 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003606
3607 // Now sort high and low parts separately.
3608 BitVector InOrder(8);
3609 if (BestLowQuad != -1) {
3610 // Sort lower half in order using PSHUFLW.
3611 MaskVec.clear();
3612 bool AnyOutOrder = false;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003613
Evan Cheng14b32e12007-12-11 01:46:18 +00003614 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003615 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003616 if (Elt.getOpcode() == ISD::UNDEF) {
3617 MaskVec.push_back(Elt);
3618 InOrder.set(i);
3619 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003620 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003621 if (EltIdx != i)
3622 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003623
Evan Cheng14b32e12007-12-11 01:46:18 +00003624 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003625
Evan Cheng14b32e12007-12-11 01:46:18 +00003626 // If this element is in the right place after this shuffle, then
3627 // remember it.
3628 if ((int)(EltIdx / 4) == BestLowQuad)
3629 InOrder.set(i);
3630 }
3631 }
3632 if (AnyOutOrder) {
3633 for (unsigned i = 4; i != 8; ++i)
3634 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003635 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3636 &MaskVec[0], 8);
3637 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3638 NewV, NewV, Mask);
Evan Cheng14b32e12007-12-11 01:46:18 +00003639 }
3640 }
3641
3642 if (BestHighQuad != -1) {
3643 // Sort high half in order using PSHUFHW if possible.
3644 MaskVec.clear();
Bill Wendlinge85dc492008-08-21 22:35:37 +00003645
Evan Cheng14b32e12007-12-11 01:46:18 +00003646 for (unsigned i = 0; i != 4; ++i)
3647 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003648
Evan Cheng14b32e12007-12-11 01:46:18 +00003649 bool AnyOutOrder = false;
3650 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003651 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003652 if (Elt.getOpcode() == ISD::UNDEF) {
3653 MaskVec.push_back(Elt);
3654 InOrder.set(i);
3655 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003656 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003657 if (EltIdx != i)
3658 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003659
Evan Cheng14b32e12007-12-11 01:46:18 +00003660 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003661
Evan Cheng14b32e12007-12-11 01:46:18 +00003662 // If this element is in the right place after this shuffle, then
3663 // remember it.
3664 if ((int)(EltIdx / 4) == BestHighQuad)
3665 InOrder.set(i);
3666 }
3667 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003668
Evan Cheng14b32e12007-12-11 01:46:18 +00003669 if (AnyOutOrder) {
Dale Johannesenace16102009-02-03 19:33:06 +00003670 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl,
3671 MaskVT, &MaskVec[0], 8);
3672 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3673 NewV, NewV, Mask);
Evan Cheng14b32e12007-12-11 01:46:18 +00003674 }
3675 }
3676
3677 // The other elements are put in the right place using pextrw and pinsrw.
3678 for (unsigned i = 0; i != 8; ++i) {
3679 if (InOrder[i])
3680 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003681 SDValue Elt = MaskElts[i];
Bill Wendlingae0218c2008-08-21 22:36:36 +00003682 if (Elt.getOpcode() == ISD::UNDEF)
3683 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003684 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003685 SDValue ExtOp = (EltIdx < 8)
Dale Johannesenace16102009-02-03 19:33:06 +00003686 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003687 DAG.getConstant(EltIdx, PtrVT))
Dale Johannesenace16102009-02-03 19:33:06 +00003688 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003689 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003690 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng14b32e12007-12-11 01:46:18 +00003691 DAG.getConstant(i, PtrVT));
3692 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003693
Evan Cheng14b32e12007-12-11 01:46:18 +00003694 return NewV;
3695 }
3696
Bill Wendlinge85dc492008-08-21 22:35:37 +00003697 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3698 // few as possible. First, let's find out how many elements are already in the
3699 // right order.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003700 unsigned V1InOrder = 0;
3701 unsigned V1FromV1 = 0;
3702 unsigned V2InOrder = 0;
3703 unsigned V2FromV2 = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003704 SmallVector<SDValue, 8> V1Elts;
3705 SmallVector<SDValue, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003706 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003707 SDValue Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003708 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003709 V1Elts.push_back(Elt);
3710 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003711 ++V1InOrder;
3712 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003713 continue;
3714 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003715 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003716 if (EltIdx == i) {
3717 V1Elts.push_back(Elt);
3718 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3719 ++V1InOrder;
3720 } else if (EltIdx == i+8) {
3721 V1Elts.push_back(Elt);
3722 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3723 ++V2InOrder;
3724 } else if (EltIdx < 8) {
3725 V1Elts.push_back(Elt);
Mon P Wange91a0002009-01-28 23:11:14 +00003726 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003727 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003728 } else {
Mon P Wang62c75ea2008-12-23 04:03:27 +00003729 V1Elts.push_back(Elt);
Evan Cheng14b32e12007-12-11 01:46:18 +00003730 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3731 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003732 }
3733 }
3734
3735 if (V2InOrder > V1InOrder) {
Dale Johannesenace16102009-02-03 19:33:06 +00003736 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003737 std::swap(V1, V2);
3738 std::swap(V1Elts, V2Elts);
3739 std::swap(V1FromV1, V2FromV2);
3740 }
3741
Evan Cheng14b32e12007-12-11 01:46:18 +00003742 if ((V1FromV1 + V1InOrder) != 8) {
3743 // Some elements are from V2.
3744 if (V1FromV1) {
3745 // If there are elements that are from V1 but out of place,
3746 // then first sort them in place
Dan Gohman475871a2008-07-27 21:46:04 +00003747 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003748 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003749 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003750 if (Elt.getOpcode() == ISD::UNDEF) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003751 MaskVec.push_back(DAG.getUNDEF(MaskEVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003752 continue;
3753 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003754 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003755 if (EltIdx >= 8)
Dale Johannesene8d72302009-02-06 23:05:02 +00003756 MaskVec.push_back(DAG.getUNDEF(MaskEVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003757 else
3758 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3759 }
Dale Johannesenace16102009-02-03 19:33:06 +00003760 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], 8);
3761 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003762 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003763
3764 NewV = V1;
3765 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003766 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003767 if (Elt.getOpcode() == ISD::UNDEF)
3768 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003769 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003770 if (EltIdx < 8)
3771 continue;
Dale Johannesenace16102009-02-03 19:33:06 +00003772 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003773 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003774 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng14b32e12007-12-11 01:46:18 +00003775 DAG.getConstant(i, PtrVT));
3776 }
3777 return NewV;
3778 } else {
3779 // All elements are from V1.
3780 NewV = V1;
3781 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003782 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003783 if (Elt.getOpcode() == ISD::UNDEF)
3784 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003785 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dale Johannesenace16102009-02-03 19:33:06 +00003786 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003787 DAG.getConstant(EltIdx, PtrVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003788 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng14b32e12007-12-11 01:46:18 +00003789 DAG.getConstant(i, PtrVT));
3790 }
3791 return NewV;
3792 }
3793}
3794
Evan Cheng7a831ce2007-12-15 03:00:47 +00003795/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3796/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3797/// done when every pair / quad of shuffle mask elements point to elements in
3798/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003799/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3800static
Dan Gohman475871a2008-07-27 21:46:04 +00003801SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003802 MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00003803 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003804 TargetLowering &TLI, DebugLoc dl) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003805 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003806 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003807 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003808 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003809 MVT NewVT = MaskVT;
3810 switch (VT.getSimpleVT()) {
3811 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003812 case MVT::v4f32: NewVT = MVT::v2f64; break;
3813 case MVT::v4i32: NewVT = MVT::v2i64; break;
3814 case MVT::v8i16: NewVT = MVT::v4i32; break;
3815 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003816 }
3817
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003818 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003819 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003820 NewVT = MVT::v2i64;
3821 else
3822 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003823 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003824 unsigned Scale = NumElems / NewWidth;
Dan Gohman475871a2008-07-27 21:46:04 +00003825 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003826 for (unsigned i = 0; i < NumElems; i += Scale) {
3827 unsigned StartIdx = ~0U;
3828 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003829 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng14b32e12007-12-11 01:46:18 +00003830 if (Elt.getOpcode() == ISD::UNDEF)
3831 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003832 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003833 if (StartIdx == ~0U)
3834 StartIdx = EltIdx - (EltIdx % Scale);
3835 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003836 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003837 }
3838 if (StartIdx == ~0U)
Dale Johannesene8d72302009-02-06 23:05:02 +00003839 MaskVec.push_back(DAG.getUNDEF(MaskEltVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003840 else
Duncan Sandsd038e042008-07-21 10:20:31 +00003841 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003842 }
3843
Dale Johannesenace16102009-02-03 19:33:06 +00003844 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3845 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
3846 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NewVT, V1, V2,
3847 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Cheng7a831ce2007-12-15 03:00:47 +00003848 &MaskVec[0], MaskVec.size()));
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,
3854 SDValue SrcOp, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003855 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,
3882 DAG.getNode(ISD::BIT_CONVERT, dl,
3883 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
3889LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
Dale Johannesenace16102009-02-03 19:33:06 +00003890 SDValue PermMask, MVT VT, SelectionDAG &DAG,
3891 DebugLoc dl) {
Evan Chengace3c172008-07-22 21:13:36 +00003892 MVT MaskVT = PermMask.getValueType();
3893 MVT MaskEVT = MaskVT.getVectorElementType();
3894 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003895 Locs.resize(4);
Dale Johannesene8d72302009-02-06 23:05:02 +00003896 SmallVector<SDValue, 8> Mask1(4, DAG.getUNDEF(MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003897 unsigned NumHi = 0;
3898 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003899 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003900 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003901 if (Elt.getOpcode() == ISD::UNDEF) {
3902 Locs[i] = std::make_pair(-1, -1);
3903 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003904 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmand0859942008-08-04 23:09:15 +00003905 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengace3c172008-07-22 21:13:36 +00003906 if (Val < 4) {
3907 Locs[i] = std::make_pair(0, NumLo);
3908 Mask1[NumLo] = Elt;
3909 NumLo++;
3910 } else {
3911 Locs[i] = std::make_pair(1, NumHi);
3912 if (2+NumHi < 4)
3913 Mask1[2+NumHi] = Elt;
3914 NumHi++;
3915 }
3916 }
3917 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003918
Evan Chengace3c172008-07-22 21:13:36 +00003919 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003920 // If no more than two elements come from either vector. This can be
3921 // implemented with two shuffles. First shuffle gather the elements.
3922 // The second shuffle, which takes the first shuffle as both of its
3923 // vector operands, put the elements into the right order.
Dale Johannesenace16102009-02-03 19:33:06 +00003924 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3925 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00003926 &Mask1[0], Mask1.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003927
Dale Johannesene8d72302009-02-06 23:05:02 +00003928 SmallVector<SDValue, 8> Mask2(4, DAG.getUNDEF(MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003929 for (unsigned i = 0; i != 4; ++i) {
3930 if (Locs[i].first == -1)
3931 continue;
3932 else {
3933 unsigned Idx = (i < 2) ? 0 : 4;
3934 Idx += Locs[i].first * 2 + Locs[i].second;
3935 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3936 }
3937 }
3938
Dale Johannesenace16102009-02-03 19:33:06 +00003939 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1,
3940 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00003941 &Mask2[0], Mask2.size()));
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.
Dale Johannesenace16102009-02-03 19:33:06 +00003952 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
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) {
Dan Gohman475871a2008-07-27 21:46:04 +00003959 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003960 if (Elt.getOpcode() == ISD::UNDEF)
3961 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003962 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003963 if (Val >= 4)
3964 break;
3965 }
3966
3967 Mask1[0] = PermMask.getOperand(HiIndex);
Dale Johannesene8d72302009-02-06 23:05:02 +00003968 Mask1[1] = DAG.getUNDEF(MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003969 Mask1[2] = PermMask.getOperand(HiIndex^1);
Dale Johannesene8d72302009-02-06 23:05:02 +00003970 Mask1[3] = DAG.getUNDEF(MaskEVT);
Dale Johannesenace16102009-02-03 19:33:06 +00003971 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
Dale Johannesened2eee62009-02-06 01:31:28 +00003972 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &Mask1[0], 4));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003973
3974 if (HiIndex >= 2) {
3975 Mask1[0] = PermMask.getOperand(0);
3976 Mask1[1] = PermMask.getOperand(1);
3977 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3978 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
Dale Johannesenace16102009-02-03 19:33:06 +00003979 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3980 DAG.getNode(ISD::BUILD_VECTOR, dl,
3981 MaskVT, &Mask1[0], 4));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003982 } else {
3983 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3984 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3985 Mask1[2] = PermMask.getOperand(2);
3986 Mask1[3] = PermMask.getOperand(3);
3987 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003988 Mask1[2] =
3989 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3990 MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003991 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003992 Mask1[3] =
3993 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3994 MaskEVT);
Dale Johannesenace16102009-02-03 19:33:06 +00003995 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V2, V1,
3996 DAG.getNode(ISD::BUILD_VECTOR, dl,
3997 MaskVT, &Mask1[0], 4));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003998 }
Evan Chengace3c172008-07-22 21:13:36 +00003999 }
4000
4001 // Break it into (shuffle shuffle_hi, shuffle_lo).
4002 Locs.clear();
Dale Johannesene8d72302009-02-06 23:05:02 +00004003 SmallVector<SDValue,8> LoMask(4, DAG.getUNDEF(MaskEVT));
4004 SmallVector<SDValue,8> HiMask(4, DAG.getUNDEF(MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00004005 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004006 unsigned MaskIdx = 0;
4007 unsigned LoIdx = 0;
4008 unsigned HiIdx = 2;
4009 for (unsigned i = 0; i != 4; ++i) {
4010 if (i == 2) {
4011 MaskPtr = &HiMask;
4012 MaskIdx = 1;
4013 LoIdx = 0;
4014 HiIdx = 2;
4015 }
Dan Gohman475871a2008-07-27 21:46:04 +00004016 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00004017 if (Elt.getOpcode() == ISD::UNDEF) {
4018 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004019 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004020 Locs[i] = std::make_pair(MaskIdx, LoIdx);
4021 (*MaskPtr)[LoIdx] = Elt;
4022 LoIdx++;
4023 } else {
4024 Locs[i] = std::make_pair(MaskIdx, HiIdx);
4025 (*MaskPtr)[HiIdx] = Elt;
4026 HiIdx++;
4027 }
4028 }
4029
Dale Johannesenace16102009-02-03 19:33:06 +00004030 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4031 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00004032 &LoMask[0], LoMask.size()));
Dale Johannesenace16102009-02-03 19:33:06 +00004033 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4034 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00004035 &HiMask[0], HiMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00004036 SmallVector<SDValue, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004037 for (unsigned i = 0; i != 4; ++i) {
4038 if (Locs[i].first == -1) {
Dale Johannesene8d72302009-02-06 23:05:02 +00004039 MaskOps.push_back(DAG.getUNDEF(MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00004040 } else {
4041 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4042 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4043 }
4044 }
Dale Johannesenace16102009-02-03 19:33:06 +00004045 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, LoShuffle, HiShuffle,
4046 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00004047 &MaskOps[0], MaskOps.size()));
4048}
4049
Dan Gohman475871a2008-07-27 21:46:04 +00004050SDValue
4051X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4052 SDValue V1 = Op.getOperand(0);
4053 SDValue V2 = Op.getOperand(1);
4054 SDValue PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004055 MVT VT = Op.getValueType();
Dale Johannesenace16102009-02-03 19:33:06 +00004056 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004057 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004058 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004059 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4060 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004061 bool V1IsSplat = false;
4062 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063
Gabor Greifba36cb52008-08-28 21:40:38 +00004064 if (isUndefShuffle(Op.getNode()))
Dale Johannesene8d72302009-02-06 23:05:02 +00004065 return DAG.getUNDEF(VT);
Evan Cheng8cf723d2006-09-08 01:50:06 +00004066
Gabor Greifba36cb52008-08-28 21:40:38 +00004067 if (isZeroShuffle(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004068 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004069
Gabor Greifba36cb52008-08-28 21:40:38 +00004070 if (isIdentityMask(PermMask.getNode()))
Evan Cheng49892af2007-06-19 00:02:56 +00004071 return V1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004072 else if (isIdentityMask(PermMask.getNode(), true))
Evan Cheng49892af2007-06-19 00:02:56 +00004073 return V2;
4074
Evan Cheng4dcc8a32008-09-25 23:35:16 +00004075 // Canonicalize movddup shuffles.
4076 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Cheng882cdfd2008-10-06 21:13:08 +00004077 VT.getSizeInBits() == 128 &&
Evan Cheng4dcc8a32008-09-25 23:35:16 +00004078 X86::isMOVDDUPMask(PermMask.getNode()))
4079 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4080
Gabor Greifba36cb52008-08-28 21:40:38 +00004081 if (isSplatMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004082 if (isMMX || NumElems < 4) return Op;
4083 // Promote it to a v4{if}32 splat.
4084 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004085 }
4086
Evan Cheng7a831ce2007-12-15 03:00:47 +00004087 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4088 // do it!
4089 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004090 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG,
4091 *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004092 if (NewOp.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00004093 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4094 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00004095 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4096 // FIXME: Figure out a cleaner way to do this.
4097 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004098 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004099 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesenace16102009-02-03 19:33:06 +00004100 DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004101 if (NewOp.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004102 SDValue NewV1 = NewOp.getOperand(0);
4103 SDValue NewV2 = NewOp.getOperand(1);
4104 SDValue NewMask = NewOp.getOperand(2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004105 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004106 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00004107 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget,
4108 dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004109 }
4110 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004111 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004112 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesenace16102009-02-03 19:33:06 +00004113 DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004114 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chengd880b972008-05-09 21:53:03 +00004115 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Dale Johannesenace16102009-02-03 19:33:06 +00004116 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004117 }
4118 }
4119
Evan Chengf26ffe92008-05-29 08:22:04 +00004120 // Check if this can be converted into a logical shift.
4121 bool isLeft = false;
4122 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004123 SDValue ShVal;
Evan Chengf26ffe92008-05-29 08:22:04 +00004124 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4125 if (isShift && ShVal.hasOneUse()) {
4126 // If the shifted value has multiple uses, it may be cheaper to use
4127 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004128 MVT EVT = VT.getVectorElementType();
4129 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004130 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004131 }
4132
Gabor Greifba36cb52008-08-28 21:40:38 +00004133 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004134 if (V1IsUndef)
4135 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004136 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004137 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004138 if (!isMMX)
4139 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004140 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004141
Gabor Greifba36cb52008-08-28 21:40:38 +00004142 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4143 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4144 X86::isMOVHLPSMask(PermMask.getNode()) ||
4145 X86::isMOVHPMask(PermMask.getNode()) ||
4146 X86::isMOVLPMask(PermMask.getNode())))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004147 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004148
Gabor Greifba36cb52008-08-28 21:40:38 +00004149 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4150 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004151 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004152
Evan Chengf26ffe92008-05-29 08:22:04 +00004153 if (isShift) {
4154 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004155 MVT EVT = VT.getVectorElementType();
4156 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004157 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004158 }
4159
Evan Cheng9eca5e82006-10-25 21:49:50 +00004160 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004161 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4162 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004163 V1IsSplat = isSplatVector(V1.getNode());
4164 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattner8a594482007-11-25 00:24:49 +00004165
4166 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004167 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00004168 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004169 std::swap(V1IsSplat, V2IsSplat);
4170 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004171 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004172 }
4173
Evan Cheng7a831ce2007-12-15 03:00:47 +00004174 // FIXME: Figure out a cleaner way to do this.
Gabor Greifba36cb52008-08-28 21:40:38 +00004175 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Evan Cheng9bbbb982006-10-25 20:48:19 +00004176 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004177 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004178 if (V2IsSplat) {
4179 // V2 is a splat, so the mask may be malformed. That is, it may point
4180 // to any V2 element. The instruction selectior won't like this. Get
4181 // a corrected mask and commute to form a proper MOVS{S|D}.
Dale Johannesenace16102009-02-03 19:33:06 +00004182 SDValue NewMask = getMOVLMask(NumElems, DAG, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004183 if (NewMask.getNode() != PermMask.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00004184 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004185 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00004186 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00004187 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004188
Gabor Greifba36cb52008-08-28 21:40:38 +00004189 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4190 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4191 X86::isUNPCKLMask(PermMask.getNode()) ||
4192 X86::isUNPCKHMask(PermMask.getNode()))
Evan Chengd9b8e402006-10-16 06:36:00 +00004193 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004194
Evan Cheng9bbbb982006-10-25 20:48:19 +00004195 if (V2IsSplat) {
4196 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004197 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004198 // new vector_shuffle with the corrected mask.
Dan Gohman475871a2008-07-27 21:46:04 +00004199 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004200 if (NewMask.getNode() != PermMask.getNode()) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004201 if (X86::isUNPCKLMask(NewMask.getNode(), true)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004202 SDValue NewMask = getUnpacklMask(NumElems, DAG, dl);
4203 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004204 } else if (X86::isUNPCKHMask(NewMask.getNode(), true)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004205 SDValue NewMask = getUnpackhMask(NumElems, DAG, dl);
4206 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004207 }
4208 }
4209 }
4210
4211 // Normalize the node to match x86 shuffle ops if needed
Gabor Greifba36cb52008-08-28 21:40:38 +00004212 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004213 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4214
4215 if (Commuted) {
4216 // Commute is back and try unpck* again.
4217 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004218 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4219 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4220 X86::isUNPCKLMask(PermMask.getNode()) ||
4221 X86::isUNPCKHMask(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004222 return Op;
4223 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004224
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004225 // Try PSHUF* first, then SHUFP*.
4226 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4227 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greifba36cb52008-08-28 21:40:38 +00004228 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004229 if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesenace16102009-02-03 19:33:06 +00004230 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1,
Dale Johannesene8d72302009-02-06 23:05:02 +00004231 DAG.getUNDEF(VT), PermMask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004232 return Op;
4233 }
4234
4235 if (!isMMX) {
4236 if (Subtarget->hasSSE2() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004237 (X86::isPSHUFDMask(PermMask.getNode()) ||
4238 X86::isPSHUFHWMask(PermMask.getNode()) ||
4239 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004241 if (VT == MVT::v4f32) {
4242 RVT = MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004243 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT,
4244 DAG.getNode(ISD::BIT_CONVERT, dl, RVT, V1),
Dale Johannesene8d72302009-02-06 23:05:02 +00004245 DAG.getUNDEF(RVT), PermMask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004246 } else if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesenace16102009-02-03 19:33:06 +00004247 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT, V1,
Dale Johannesene8d72302009-02-06 23:05:02 +00004248 DAG.getUNDEF(RVT), PermMask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004249 if (RVT != VT)
Dale Johannesenace16102009-02-03 19:33:06 +00004250 Op = DAG.getNode(ISD::BIT_CONVERT, dl, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004251 return Op;
4252 }
4253
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004254 // Binary or unary shufps.
Gabor Greifba36cb52008-08-28 21:40:38 +00004255 if (X86::isSHUFPMask(PermMask.getNode()) ||
4256 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Evan Cheng0db9fe62006-04-25 20:13:52 +00004257 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004258 }
4259
Evan Cheng14b32e12007-12-11 01:46:18 +00004260 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4261 if (VT == MVT::v8i16) {
Dale Johannesenace16102009-02-03 19:33:06 +00004262 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004263 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004264 return NewOp;
4265 }
4266
Evan Chengace3c172008-07-22 21:13:36 +00004267 // Handle all 4 wide cases with a number of shuffles except for MMX.
4268 if (NumElems == 4 && !isMMX)
Dale Johannesenace16102009-02-03 19:33:06 +00004269 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004270
Dan Gohman475871a2008-07-27 21:46:04 +00004271 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004272}
4273
Dan Gohman475871a2008-07-27 21:46:04 +00004274SDValue
4275X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004276 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004277 MVT VT = Op.getValueType();
Dale Johannesenace16102009-02-03 19:33:06 +00004278 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004279 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004280 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004281 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004282 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004283 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004284 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004285 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004286 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4287 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4288 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004289 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4290 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4291 DAG.getNode(ISD::BIT_CONVERT, dl,
4292 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004293 Op.getOperand(0)),
4294 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004295 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004296 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004297 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004298 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004299 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004300 } else if (VT == MVT::f32) {
4301 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4302 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004303 // result has a single use which is a store or a bitcast to i32. And in
4304 // the case of a store, it's not worth it if the index is a constant 0,
4305 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004306 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004307 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004308 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004309 if ((User->getOpcode() != ISD::STORE ||
4310 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4311 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004312 (User->getOpcode() != ISD::BIT_CONVERT ||
4313 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004314 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004315 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4316 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
4317 Op.getOperand(0)),
4318 Op.getOperand(1));
4319 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004320 } else if (VT == MVT::i32) {
4321 // ExtractPS works with constant index.
4322 if (isa<ConstantSDNode>(Op.getOperand(1)))
4323 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004324 }
Dan Gohman475871a2008-07-27 21:46:04 +00004325 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004326}
4327
4328
Dan Gohman475871a2008-07-27 21:46:04 +00004329SDValue
4330X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004331 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004332 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333
Evan Cheng62a3f152008-03-24 21:52:23 +00004334 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004335 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004336 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004337 return Res;
4338 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004339
Duncan Sands83ec4b62008-06-06 12:08:01 +00004340 MVT VT = Op.getValueType();
Dale Johannesenace16102009-02-03 19:33:06 +00004341 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004342 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004343 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004345 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004346 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004347 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4348 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4349 DAG.getNode(ISD::BIT_CONVERT, dl,
4350 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004351 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004352 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004353 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004354 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004355 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004356 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004357 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004358 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004359 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004360 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004361 if (Idx == 0)
4362 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004363 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004364 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman475871a2008-07-27 21:46:04 +00004365 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004366 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004367 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004368 IdxVec.
Dale Johannesene8d72302009-02-06 23:05:02 +00004369 push_back(DAG.getUNDEF(MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004370 IdxVec.
Dale Johannesene8d72302009-02-06 23:05:02 +00004371 push_back(DAG.getUNDEF(MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004372 IdxVec.
Dale Johannesene8d72302009-02-06 23:05:02 +00004373 push_back(DAG.getUNDEF(MaskVT.getVectorElementType()));
Dale Johannesenace16102009-02-03 19:33:06 +00004374 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004375 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004376 SDValue Vec = Op.getOperand(0);
Dale Johannesenace16102009-02-03 19:33:06 +00004377 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
Dale Johannesene8d72302009-02-06 23:05:02 +00004378 Vec, DAG.getUNDEF(Vec.getValueType()), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004379 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004380 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004381 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004382 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4383 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4384 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004385 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004386 if (Idx == 0)
4387 return Op;
4388
4389 // UNPCKHPD the element to the lowest double word, then movsd.
4390 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4391 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd038e042008-07-21 10:20:31 +00004392 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SmallVector<SDValue, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004394 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004395 IdxVec.
Dale Johannesene8d72302009-02-06 23:05:02 +00004396 push_back(DAG.getUNDEF(MaskVT.getVectorElementType()));
Dale Johannesenace16102009-02-03 19:33:06 +00004397 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004398 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004399 SDValue Vec = Op.getOperand(0);
Dale Johannesenace16102009-02-03 19:33:06 +00004400 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
Dale Johannesene8d72302009-02-06 23:05:02 +00004401 Vec, DAG.getUNDEF(Vec.getValueType()),
Dale Johannesenace16102009-02-03 19:33:06 +00004402 Mask);
4403 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004404 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004405 }
4406
Dan Gohman475871a2008-07-27 21:46:04 +00004407 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408}
4409
Dan Gohman475871a2008-07-27 21:46:04 +00004410SDValue
4411X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004412 MVT VT = Op.getValueType();
4413 MVT EVT = VT.getVectorElementType();
Dale Johannesenace16102009-02-03 19:33:06 +00004414 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004415
Dan Gohman475871a2008-07-27 21:46:04 +00004416 SDValue N0 = Op.getOperand(0);
4417 SDValue N1 = Op.getOperand(1);
4418 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004419
Dan Gohmanef521f12008-08-14 22:53:18 +00004420 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4421 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004422 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004423 : X86ISD::PINSRW;
4424 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4425 // argument.
4426 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004427 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004428 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004429 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004430 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004431 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004432 // Bits [7:6] of the constant are the source select. This will always be
4433 // zero here. The DAG Combiner may combine an extract_elt index into these
4434 // bits. For example (insert (extract, 3), 2) could be matched by putting
4435 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4436 // Bits [5:4] of the constant are the destination select. This is the
4437 // value of the incoming immediate.
4438 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4439 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004440 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004441 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004442 } else if (EVT == MVT::i32) {
4443 // InsertPS works with constant index.
4444 if (isa<ConstantSDNode>(N2))
4445 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004446 }
Dan Gohman475871a2008-07-27 21:46:04 +00004447 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004448}
4449
Dan Gohman475871a2008-07-27 21:46:04 +00004450SDValue
4451X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004452 MVT VT = Op.getValueType();
4453 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004454
4455 if (Subtarget->hasSSE41())
4456 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4457
Evan Cheng794405e2007-12-12 07:55:34 +00004458 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004459 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004460
Dale Johannesenace16102009-02-03 19:33:06 +00004461 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004462 SDValue N0 = Op.getOperand(0);
4463 SDValue N1 = Op.getOperand(1);
4464 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004465
Duncan Sands83ec4b62008-06-06 12:08:01 +00004466 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004467 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4468 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004469 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004470 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004471 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004472 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004473 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004474 }
Dan Gohman475871a2008-07-27 21:46:04 +00004475 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004476}
4477
Dan Gohman475871a2008-07-27 21:46:04 +00004478SDValue
4479X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00004480 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004481 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004482 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4483 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4484 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004485 Op.getOperand(0))));
4486
Dale Johannesenace16102009-02-03 19:33:06 +00004487 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004488 MVT VT = MVT::v2i32;
4489 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004490 default: break;
4491 case MVT::v16i8:
4492 case MVT::v8i16:
4493 VT = MVT::v4i32;
4494 break;
4495 }
Dale Johannesenace16102009-02-03 19:33:06 +00004496 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4497 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004498}
4499
Bill Wendling056292f2008-09-16 21:48:12 +00004500// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4501// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4502// one of the above mentioned nodes. It has to be wrapped because otherwise
4503// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4504// be used to form addressing mode. These wrapped nodes will be selected
4505// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004506SDValue
4507X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004508 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004509 // FIXME there isn't really any debug info here, should come from the parent
4510 DebugLoc dl = CP->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004511 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Evan Chengd0ff02c2006-11-29 23:19:46 +00004512 getPointerTy(),
4513 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004514 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004515 // With PIC, the address is actually $g + Offset.
4516 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4517 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004518 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004519 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4520 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004521 }
4522
4523 return Result;
4524}
4525
Dan Gohman475871a2008-07-27 21:46:04 +00004526SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004527X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004528 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004529 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004530 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4531 bool ExtraLoadRequired =
4532 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4533
4534 // Create the TargetGlobalAddress node, folding in the constant
4535 // offset if it is legal.
4536 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004537 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004538 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4539 Offset = 0;
4540 } else
4541 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004542 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004543
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004544 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004545 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004546 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4547 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004548 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004549 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004550
4551 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4552 // load the value at address GV, not the value of GV itself. This means that
4553 // the GlobalAddress must be in the base or index register of the address, not
4554 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004555 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004556 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004557 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004558 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004559
Dan Gohman6520e202008-10-18 02:06:02 +00004560 // If there was a non-zero offset that we didn't fold, create an explicit
4561 // addition for it.
4562 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004563 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004564 DAG.getConstant(Offset, getPointerTy()));
4565
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 return Result;
4567}
4568
Evan Chengda43bcf2008-09-24 00:05:32 +00004569SDValue
4570X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4571 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004572 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen33c960f2009-02-04 20:06:27 +00004573 return LowerGlobalAddress(GV, Op.getNode()->getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004574}
4575
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004576// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004577static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004578LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004579 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004580 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004581 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4582 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004583 DAG.getNode(X86ISD::GlobalBaseReg,
4584 PtrVT), InFlag);
4585 InFlag = Chain.getValue(1);
4586
4587 // emit leal symbol@TLSGD(,%ebx,1), %eax
4588 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004589 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004590 GA->getValueType(0),
4591 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004592 SDValue Ops[] = { Chain, TGA, InFlag };
Dale Johannesene8d72302009-02-06 23:05:02 +00004593 SDValue Result = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004594 InFlag = Result.getValue(2);
4595 Chain = Result.getValue(1);
4596
4597 // call ___tls_get_addr. This function receives its argument in
4598 // the register EAX.
Dale Johannesendd64c412009-02-04 00:33:20 +00004599 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Result, InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004600 InFlag = Chain.getValue(1);
4601
4602 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004604 DAG.getTargetExternalSymbol("___tls_get_addr",
4605 PtrVT),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004606 DAG.getRegister(X86::EAX, PtrVT),
4607 DAG.getRegister(X86::EBX, PtrVT),
4608 InFlag };
Dale Johannesene8d72302009-02-06 23:05:02 +00004609 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops1, 5);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004610 InFlag = Chain.getValue(1);
4611
Dale Johannesendd64c412009-02-04 00:33:20 +00004612 return DAG.getCopyFromReg(Chain, dl, X86::EAX, PtrVT, InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004613}
4614
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004615// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004616static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004617LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004618 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004619 SDValue InFlag, Chain;
Dale Johannesendd64c412009-02-04 00:33:20 +00004620 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004621
4622 // emit leaq symbol@TLSGD(%rip), %rdi
4623 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004624 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004625 GA->getValueType(0),
4626 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004627 SDValue Ops[] = { DAG.getEntryNode(), TGA};
Dale Johannesene8d72302009-02-06 23:05:02 +00004628 SDValue Result = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004629 Chain = Result.getValue(1);
4630 InFlag = Result.getValue(2);
4631
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004632 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004633 // the register RDI.
Dale Johannesendd64c412009-02-04 00:33:20 +00004634 Chain = DAG.getCopyToReg(Chain, dl, X86::RDI, Result, InFlag);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004635 InFlag = Chain.getValue(1);
4636
4637 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004638 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004639 DAG.getTargetExternalSymbol("__tls_get_addr",
4640 PtrVT),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004641 DAG.getRegister(X86::RDI, PtrVT),
4642 InFlag };
Dale Johannesene8d72302009-02-06 23:05:02 +00004643 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops1, 4);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004644 InFlag = Chain.getValue(1);
4645
Dale Johannesendd64c412009-02-04 00:33:20 +00004646 return DAG.getCopyFromReg(Chain, dl, X86::RAX, PtrVT, InFlag);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004647}
4648
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004649// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4650// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004651static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004652 const MVT PtrVT) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004653 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004654 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +00004655 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004656 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4657 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004659 GA->getValueType(0),
4660 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004661 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004662
4663 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dale Johannesen33c960f2009-02-04 20:06:27 +00004664 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004665 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004666
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004667 // The address of the thread local variable is the add of the thread
4668 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004669 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004670}
4671
Dan Gohman475871a2008-07-27 21:46:04 +00004672SDValue
4673X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004674 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004675 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004676 assert(Subtarget->isTargetELF() &&
4677 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004678 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4679 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4680 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004681 if (Subtarget->is64Bit()) {
4682 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4683 } else {
4684 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4685 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4686 else
4687 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4688 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004689}
4690
Dan Gohman475871a2008-07-27 21:46:04 +00004691SDValue
4692X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004693 // FIXME there isn't really any debug info here
4694 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004695 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4696 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004697 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004698 // With PIC, the address is actually $g + Offset.
4699 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4700 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004701 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004702 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4703 Result);
4704 }
4705
4706 return Result;
4707}
4708
Dan Gohman475871a2008-07-27 21:46:04 +00004709SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004710 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004711 // FIXME there isn't really any debug into here
4712 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004713 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004714 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4715 // With PIC, the address is actually $g + Offset.
4716 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4717 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004718 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004719 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4720 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004721 }
4722
4723 return Result;
4724}
4725
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004726/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4727/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004728SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004729 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004730 MVT VT = Op.getValueType();
4731 unsigned VTBits = VT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004732 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004733 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004734 SDValue ShOpLo = Op.getOperand(0);
4735 SDValue ShOpHi = Op.getOperand(1);
4736 SDValue ShAmt = Op.getOperand(2);
4737 SDValue Tmp1 = isSRA ?
Dale Johannesenace16102009-02-03 19:33:06 +00004738 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4739 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004740 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004741
Dan Gohman475871a2008-07-27 21:46:04 +00004742 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004743 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004744 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4745 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004746 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004747 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4748 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004749 }
Evan Chenge3413162006-01-09 18:33:28 +00004750
Dale Johannesenace16102009-02-03 19:33:06 +00004751 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004752 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004753 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004754 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004755
Dan Gohman475871a2008-07-27 21:46:04 +00004756 SDValue Hi, Lo;
4757 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4758 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4759 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004760
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004761 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004762 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4763 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004764 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004765 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4766 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004767 }
4768
Dan Gohman475871a2008-07-27 21:46:04 +00004769 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004770 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004771}
Evan Chenga3195e82006-01-12 22:54:21 +00004772
Dan Gohman475871a2008-07-27 21:46:04 +00004773SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004774 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004775 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004776 "Unknown SINT_TO_FP to lower!");
4777
4778 // These are really Legal; caller falls through into that case.
4779 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004780 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004781 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4782 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004783 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004784
Dale Johannesenace16102009-02-03 19:33:06 +00004785 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004786 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004787 MachineFunction &MF = DAG.getMachineFunction();
4788 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004789 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004790 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004791 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004792 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004793
4794 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004795 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004796 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004797 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004798 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4799 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004800 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004801 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802 Ops.push_back(Chain);
4803 Ops.push_back(StackSlot);
4804 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004805 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004806 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004807
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004808 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004810 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811
4812 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4813 // shouldn't be necessary except that RFP cannot be live across
4814 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004815 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004816 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004818 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004819 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004820 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004822 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004823 Ops.push_back(DAG.getValueType(Op.getValueType()));
4824 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004825 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4826 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004827 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004828 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004829
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830 return Result;
4831}
4832
Bill Wendling8b8a6362009-01-17 03:56:04 +00004833// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4834SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4835 // This algorithm is not obvious. Here it is in C code, more or less:
4836 /*
4837 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4838 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4839 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004840
Bill Wendling8b8a6362009-01-17 03:56:04 +00004841 // Copy ints to xmm registers.
4842 __m128i xh = _mm_cvtsi32_si128( hi );
4843 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004844
Bill Wendling8b8a6362009-01-17 03:56:04 +00004845 // Combine into low half of a single xmm register.
4846 __m128i x = _mm_unpacklo_epi32( xh, xl );
4847 __m128d d;
4848 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004849
Bill Wendling8b8a6362009-01-17 03:56:04 +00004850 // Merge in appropriate exponents to give the integer bits the right
4851 // magnitude.
4852 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004853
Bill Wendling8b8a6362009-01-17 03:56:04 +00004854 // Subtract away the biases to deal with the IEEE-754 double precision
4855 // implicit 1.
4856 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004857
Bill Wendling8b8a6362009-01-17 03:56:04 +00004858 // All conversions up to here are exact. The correctly rounded result is
4859 // calculated using the current rounding mode using the following
4860 // horizontal add.
4861 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4862 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4863 // store doesn't really need to be here (except
4864 // maybe to zero the other double)
4865 return sd;
4866 }
4867 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004868
Dale Johannesenace16102009-02-03 19:33:06 +00004869 DebugLoc dl = Op.getNode()->getDebugLoc();
4870
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004871 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004872 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004873 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4874 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4875 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4876 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4877 Constant *C0 = ConstantVector::get(CV0);
4878 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4879
Bill Wendling8b8a6362009-01-17 03:56:04 +00004880 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004881 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4882 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4883 Constant *C1 = ConstantVector::get(CV1);
4884 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4885
4886 SmallVector<SDValue, 4> MaskVec;
4887 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4888 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4889 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4890 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004891 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4892 &MaskVec[0], MaskVec.size());
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004893 SmallVector<SDValue, 4> MaskVec2;
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004894 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4895 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004896 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32,
4897 &MaskVec2[0], MaskVec2.size());
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004898
Dale Johannesenace16102009-02-03 19:33:06 +00004899 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4900 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004901 Op.getOperand(0),
4902 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004903 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4904 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004905 Op.getOperand(0),
4906 DAG.getIntPtrConstant(0)));
Dale Johannesenace16102009-02-03 19:33:06 +00004907 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004908 XR1, XR2, UnpcklMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004909 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004910 PseudoSourceValue::getConstantPool(), 0,
4911 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004912 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004913 Unpck1, CLod0, UnpcklMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004914 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4915 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004916 PseudoSourceValue::getConstantPool(), 0,
4917 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004918 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004919
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004920 // Add the halves; easiest way is to swap them into another reg first.
Dale Johannesenace16102009-02-03 19:33:06 +00004921 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2f64,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004922 Sub, Sub, ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004923 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4924 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004925 DAG.getIntPtrConstant(0));
4926}
4927
Bill Wendling8b8a6362009-01-17 03:56:04 +00004928// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4929SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00004930 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004931 // FP constant to bias correct the final result.
4932 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4933 MVT::f64);
4934
4935 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004936 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4937 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004938 Op.getOperand(0),
4939 DAG.getIntPtrConstant(0)));
4940
Dale Johannesenace16102009-02-03 19:33:06 +00004941 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4942 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004943 DAG.getIntPtrConstant(0));
4944
4945 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004946 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4947 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4948 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004949 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004950 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4951 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004952 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004953 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4954 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004955 DAG.getIntPtrConstant(0));
4956
4957 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004958 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004959
4960 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004961 MVT DestVT = Op.getValueType();
4962
4963 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004964 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004965 DAG.getIntPtrConstant(0));
4966 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004967 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004968 }
4969
4970 // Handle final rounding.
4971 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004972}
4973
4974SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004975 SDValue N0 = Op.getOperand(0);
Dale Johannesenace16102009-02-03 19:33:06 +00004976 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004977
Evan Chenga06ec9e2009-01-19 08:08:22 +00004978 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4979 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4980 // the optimization here.
4981 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004982 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004983
4984 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004985 if (SrcVT == MVT::i64) {
4986 // We only handle SSE2 f64 target here; caller can handle the rest.
4987 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4988 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004989
Bill Wendling8b8a6362009-01-17 03:56:04 +00004990 return LowerUINT_TO_FP_i64(Op, DAG);
4991 } else if (SrcVT == MVT::i32) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004992 return LowerUINT_TO_FP_i32(Op, DAG);
4993 }
4994
4995 assert(0 && "Unknown UINT_TO_FP to lower!");
4996 return SDValue();
4997}
4998
Dan Gohman475871a2008-07-27 21:46:04 +00004999std::pair<SDValue,SDValue> X86TargetLowering::
5000FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00005001 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005002 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
5003 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005004 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005005
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005006 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00005007 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005008 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005009 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005010 if (Subtarget->is64Bit() &&
5011 Op.getValueType() == MVT::i64 &&
5012 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00005013 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005014
Evan Cheng87c89352007-10-15 20:11:21 +00005015 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5016 // stack slot.
5017 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005018 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00005019 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00005020 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005021 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005022 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005023 default: assert(0 && "Invalid FP_TO_SINT to lower!");
5024 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5025 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5026 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005027 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005028
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue Chain = DAG.getEntryNode();
5030 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005031 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005032 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005033 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00005034 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00005035 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005036 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005037 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5038 };
Dale Johannesenace16102009-02-03 19:33:06 +00005039 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005040 Chain = Value.getValue(1);
5041 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5042 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5043 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005044
Evan Cheng0db9fe62006-04-25 20:13:52 +00005045 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005046 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00005047 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005048
Chris Lattner27a6c732007-11-24 07:07:01 +00005049 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005050}
5051
Dan Gohman475871a2008-07-27 21:46:04 +00005052SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
5053 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
5054 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00005055 if (FIST.getNode() == 0) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00005056
5057 // Load the result.
Dale Johannesenace16102009-02-03 19:33:06 +00005058 return DAG.getLoad(Op.getValueType(), Op.getNode()->getDebugLoc(),
5059 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005060}
5061
Dan Gohman475871a2008-07-27 21:46:04 +00005062SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00005063 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005064 MVT VT = Op.getValueType();
5065 MVT EltVT = VT;
5066 if (VT.isVector())
5067 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005068 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005069 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005070 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005071 CV.push_back(C);
5072 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005073 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005074 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005075 CV.push_back(C);
5076 CV.push_back(C);
5077 CV.push_back(C);
5078 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005079 }
Dan Gohmand3006222007-07-27 17:16:43 +00005080 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005081 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005082 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005083 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005084 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005085 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005086}
5087
Dan Gohman475871a2008-07-27 21:46:04 +00005088SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00005089 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005090 MVT VT = Op.getValueType();
5091 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00005092 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005093 if (VT.isVector()) {
5094 EltVT = VT.getVectorElementType();
5095 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00005096 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005097 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005098 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005099 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005100 CV.push_back(C);
5101 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005102 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005103 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005104 CV.push_back(C);
5105 CV.push_back(C);
5106 CV.push_back(C);
5107 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005108 }
Dan Gohmand3006222007-07-27 17:16:43 +00005109 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005110 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005111 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005112 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005113 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005114 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005115 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5116 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5117 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
5118 Op.getOperand(0)),
5119 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005120 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005121 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005122 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005123}
5124
Dan Gohman475871a2008-07-27 21:46:04 +00005125SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5126 SDValue Op0 = Op.getOperand(0);
5127 SDValue Op1 = Op.getOperand(1);
Dale Johannesenace16102009-02-03 19:33:06 +00005128 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005129 MVT VT = Op.getValueType();
5130 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005131
5132 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005133 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005134 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005135 SrcVT = VT;
5136 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005137 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005138 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005139 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005140 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005141 }
5142
5143 // At this point the operands and the result should have the same
5144 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005145
Evan Cheng68c47cb2007-01-05 07:55:56 +00005146 // First get the sign bit of second operand.
5147 std::vector<Constant*> CV;
5148 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005149 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5150 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005151 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005152 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5153 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5154 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5155 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005156 }
Dan Gohmand3006222007-07-27 17:16:43 +00005157 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005158 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005159 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005160 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005161 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005162 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005163
5164 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005165 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005166 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005167 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5168 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005169 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005170 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5171 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005172 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005173 }
5174
Evan Cheng73d6cf12007-01-05 21:37:56 +00005175 // Clear first operand sign bit.
5176 CV.clear();
5177 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005178 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5179 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005180 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005181 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5182 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5183 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5184 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005185 }
Dan Gohmand3006222007-07-27 17:16:43 +00005186 C = ConstantVector::get(CV);
5187 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005188 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005189 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005190 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005191 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005192
5193 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005194 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005195}
5196
Dan Gohman475871a2008-07-27 21:46:04 +00005197SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005198 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005199 SDValue Op0 = Op.getOperand(0);
5200 SDValue Op1 = Op.getOperand(1);
Dale Johannesenace16102009-02-03 19:33:06 +00005201 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005202 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5203
Dan Gohmane5af2d32009-01-29 01:59:02 +00005204 // Lower (X & (1 << N)) == 0 to BT(X, N).
5205 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5206 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005207 if (Op0.getOpcode() == ISD::AND &&
5208 Op0.hasOneUse() &&
5209 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005210 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005211 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005212 SDValue LHS, RHS;
5213 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5214 if (ConstantSDNode *Op010C =
5215 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5216 if (Op010C->getZExtValue() == 1) {
5217 LHS = Op0.getOperand(0);
5218 RHS = Op0.getOperand(1).getOperand(1);
5219 }
5220 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5221 if (ConstantSDNode *Op000C =
5222 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5223 if (Op000C->getZExtValue() == 1) {
5224 LHS = Op0.getOperand(1);
5225 RHS = Op0.getOperand(0).getOperand(1);
5226 }
5227 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5228 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5229 SDValue AndLHS = Op0.getOperand(0);
5230 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5231 LHS = AndLHS.getOperand(0);
5232 RHS = AndLHS.getOperand(1);
5233 }
5234 }
Evan Cheng0488db92007-09-25 01:57:46 +00005235
Dan Gohmane5af2d32009-01-29 01:59:02 +00005236 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005237 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5238 // instruction. Since the shift amount is in-range-or-undefined, we know
5239 // that doing a bittest on the i16 value is ok. We extend to i32 because
5240 // the encoding for the i16 version is larger than the i32 version.
5241 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005242 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005243
5244 // If the operand types disagree, extend the shift amount to match. Since
5245 // BT ignores high bits (like shifts) we can use anyextend.
5246 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005247 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005248
Dale Johannesenace16102009-02-03 19:33:06 +00005249 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005250 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005251 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005252 DAG.getConstant(Cond, MVT::i8), BT);
5253 }
5254 }
5255
5256 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5257 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner43287082008-12-24 00:11:37 +00005258
Dale Johannesenace16102009-02-03 19:33:06 +00005259 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5260 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005261 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005262}
5263
Dan Gohman475871a2008-07-27 21:46:04 +00005264SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5265 SDValue Cond;
5266 SDValue Op0 = Op.getOperand(0);
5267 SDValue Op1 = Op.getOperand(1);
5268 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005269 MVT VT = Op.getValueType();
5270 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5271 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesenace16102009-02-03 19:33:06 +00005272 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005273
5274 if (isFP) {
5275 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005276 MVT VT0 = Op0.getValueType();
5277 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5278 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005279 bool Swap = false;
5280
5281 switch (SetCCOpcode) {
5282 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005283 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005284 case ISD::SETEQ: SSECC = 0; break;
5285 case ISD::SETOGT:
5286 case ISD::SETGT: Swap = true; // Fallthrough
5287 case ISD::SETLT:
5288 case ISD::SETOLT: SSECC = 1; break;
5289 case ISD::SETOGE:
5290 case ISD::SETGE: Swap = true; // Fallthrough
5291 case ISD::SETLE:
5292 case ISD::SETOLE: SSECC = 2; break;
5293 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005294 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005295 case ISD::SETNE: SSECC = 4; break;
5296 case ISD::SETULE: Swap = true;
5297 case ISD::SETUGE: SSECC = 5; break;
5298 case ISD::SETULT: Swap = true;
5299 case ISD::SETUGT: SSECC = 6; break;
5300 case ISD::SETO: SSECC = 7; break;
5301 }
5302 if (Swap)
5303 std::swap(Op0, Op1);
5304
Nate Begemanfb8ead02008-07-25 19:05:58 +00005305 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005306 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005307 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005308 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005309 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5310 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5311 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005312 }
5313 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005314 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005315 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5316 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5317 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005318 }
5319 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005320 }
5321 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005322 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005323 }
5324
5325 // We are handling one of the integer comparisons here. Since SSE only has
5326 // GT and EQ comparisons for integer, swapping operands and multiple
5327 // operations may be required for some comparisons.
5328 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5329 bool Swap = false, Invert = false, FlipSigns = false;
5330
5331 switch (VT.getSimpleVT()) {
5332 default: break;
5333 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5334 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5335 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5336 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5337 }
5338
5339 switch (SetCCOpcode) {
5340 default: break;
5341 case ISD::SETNE: Invert = true;
5342 case ISD::SETEQ: Opc = EQOpc; break;
5343 case ISD::SETLT: Swap = true;
5344 case ISD::SETGT: Opc = GTOpc; break;
5345 case ISD::SETGE: Swap = true;
5346 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5347 case ISD::SETULT: Swap = true;
5348 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5349 case ISD::SETUGE: Swap = true;
5350 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5351 }
5352 if (Swap)
5353 std::swap(Op0, Op1);
5354
5355 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5356 // bits of the inputs before performing those operations.
5357 if (FlipSigns) {
5358 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005359 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5360 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005361 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Dale Johannesenace16102009-02-03 19:33:06 +00005362 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00005363 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005364 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5365 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005366 }
5367
Dale Johannesenace16102009-02-03 19:33:06 +00005368 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005369
5370 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005371 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005372 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005373
Nate Begeman30a0de92008-07-17 16:51:19 +00005374 return Result;
5375}
Evan Cheng0488db92007-09-25 01:57:46 +00005376
Evan Cheng370e5342008-12-03 08:38:43 +00005377// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5378static bool isX86LogicalCmp(unsigned Opc) {
5379 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5380}
5381
Dan Gohman475871a2008-07-27 21:46:04 +00005382SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005383 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005384 SDValue Cond = Op.getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005385 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005387
Evan Cheng734503b2006-09-11 02:19:56 +00005388 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005389 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005390
Evan Cheng3f41d662007-10-08 22:16:29 +00005391 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5392 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005393 if (Cond.getOpcode() == X86ISD::SETCC) {
5394 CC = Cond.getOperand(0);
5395
Dan Gohman475871a2008-07-27 21:46:04 +00005396 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005397 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00005399
Evan Cheng3f41d662007-10-08 22:16:29 +00005400 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005401 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005402 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005403 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattner1956d152008-01-16 06:19:45 +00005404
Dan Gohmane5af2d32009-01-29 01:59:02 +00005405 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005406 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005407 addTest = false;
5408 }
5409 }
5410
5411 if (addTest) {
5412 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005413 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5414 DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005415 }
5416
Duncan Sands83ec4b62008-06-06 12:08:01 +00005417 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00005418 MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005420 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5421 // condition is true.
5422 Ops.push_back(Op.getOperand(2));
5423 Ops.push_back(Op.getOperand(1));
5424 Ops.push_back(CC);
5425 Ops.push_back(Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005426 return DAG.getNode(X86ISD::CMOV, dl, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005427}
5428
Evan Cheng370e5342008-12-03 08:38:43 +00005429// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5430// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5431// from the AND / OR.
5432static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5433 Opc = Op.getOpcode();
5434 if (Opc != ISD::OR && Opc != ISD::AND)
5435 return false;
5436 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5437 Op.getOperand(0).hasOneUse() &&
5438 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5439 Op.getOperand(1).hasOneUse());
5440}
5441
Evan Cheng961d6d42009-02-02 08:19:07 +00005442// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5443// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005444static bool isXor1OfSetCC(SDValue Op) {
5445 if (Op.getOpcode() != ISD::XOR)
5446 return false;
5447 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5448 if (N1C && N1C->getAPIntValue() == 1) {
5449 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5450 Op.getOperand(0).hasOneUse();
5451 }
5452 return false;
5453}
5454
Dan Gohman475871a2008-07-27 21:46:04 +00005455SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005456 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005457 SDValue Chain = Op.getOperand(0);
5458 SDValue Cond = Op.getOperand(1);
5459 SDValue Dest = Op.getOperand(2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005460 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005461 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005462
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005464 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005465#if 0
5466 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005467 else if (Cond.getOpcode() == X86ISD::ADD ||
5468 Cond.getOpcode() == X86ISD::SUB ||
5469 Cond.getOpcode() == X86ISD::SMUL ||
5470 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005471 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005472#endif
5473
Evan Cheng3f41d662007-10-08 22:16:29 +00005474 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5475 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005476 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005477 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005478
Dan Gohman475871a2008-07-27 21:46:04 +00005479 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005480 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005481 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5482 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005483 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005484 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005485 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005486 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005487 default: break;
5488 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005489 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005490 // These can only come from an arithmetic instruction with overflow,
5491 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005492 Cond = Cond.getNode()->getOperand(1);
5493 addTest = false;
5494 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005495 }
Evan Cheng0488db92007-09-25 01:57:46 +00005496 }
Evan Cheng370e5342008-12-03 08:38:43 +00005497 } else {
5498 unsigned CondOpc;
5499 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5500 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5501 unsigned Opc = Cmp.getOpcode();
5502 if (CondOpc == ISD::OR) {
5503 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5504 // two branches instead of an explicit OR instruction with a
5505 // separate test.
5506 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5507 isX86LogicalCmp(Opc)) {
5508 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005509 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005510 Chain, Dest, CC, Cmp);
5511 CC = Cond.getOperand(1).getOperand(0);
5512 Cond = Cmp;
5513 addTest = false;
5514 }
5515 } else { // ISD::AND
5516 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5517 // two branches instead of an explicit AND instruction with a
5518 // separate test. However, we only do this if this block doesn't
5519 // have a fall-through edge, because this requires an explicit
5520 // jmp when the condition is false.
5521 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5522 isX86LogicalCmp(Opc) &&
5523 Op.getNode()->hasOneUse()) {
5524 X86::CondCode CCode =
5525 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5526 CCode = X86::GetOppositeBranchCondition(CCode);
5527 CC = DAG.getConstant(CCode, MVT::i8);
5528 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5529 // Look for an unconditional branch following this conditional branch.
5530 // We need this because we need to reverse the successors in order
5531 // to implement FCMP_OEQ.
5532 if (User.getOpcode() == ISD::BR) {
5533 SDValue FalseBB = User.getOperand(1);
5534 SDValue NewBR =
5535 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5536 assert(NewBR == User);
5537 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005538
Dale Johannesene4d209d2009-02-03 20:21:25 +00005539 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005540 Chain, Dest, CC, Cmp);
5541 X86::CondCode CCode =
5542 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5543 CCode = X86::GetOppositeBranchCondition(CCode);
5544 CC = DAG.getConstant(CCode, MVT::i8);
5545 Cond = Cmp;
5546 addTest = false;
5547 }
5548 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005549 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005550 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5551 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5552 // It should be transformed during dag combiner except when the condition
5553 // is set by a arithmetics with overflow node.
5554 X86::CondCode CCode =
5555 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5556 CCode = X86::GetOppositeBranchCondition(CCode);
5557 CC = DAG.getConstant(CCode, MVT::i8);
5558 Cond = Cond.getOperand(0).getOperand(1);
5559 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005560 }
Evan Cheng0488db92007-09-25 01:57:46 +00005561 }
5562
5563 if (addTest) {
5564 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005565 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5566 DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005567 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005568 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005569 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005570}
5571
Anton Korobeynikove060b532007-04-17 19:34:00 +00005572
5573// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5574// Calls to _alloca is needed to probe the stack when allocating more than 4k
5575// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5576// that the guard pages used by the OS virtual memory manager are allocated in
5577// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005578SDValue
5579X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005580 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005581 assert(Subtarget->isTargetCygMing() &&
5582 "This should be used only on Cygwin/Mingw targets");
Dale Johannesene4d209d2009-02-03 20:21:25 +00005583 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005584
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005585 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue Chain = Op.getOperand(0);
5587 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005588 // FIXME: Ensure alignment here
5589
Dan Gohman475871a2008-07-27 21:46:04 +00005590 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005591
Duncan Sands83ec4b62008-06-06 12:08:01 +00005592 MVT IntPtr = getPointerTy();
5593 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005594
Chris Lattnere563bbc2008-10-11 22:08:30 +00005595 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005596
Dale Johannesendd64c412009-02-04 00:33:20 +00005597 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005598 Flag = Chain.getValue(1);
5599
5600 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005601 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005602 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005603 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005604 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005605 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005606 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005607 Flag = Chain.getValue(1);
5608
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005609 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005610 DAG.getIntPtrConstant(0, true),
5611 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005612 Flag);
5613
Dale Johannesendd64c412009-02-04 00:33:20 +00005614 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005615
Dan Gohman475871a2008-07-27 21:46:04 +00005616 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005617 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005618}
5619
Dan Gohman475871a2008-07-27 21:46:04 +00005620SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005621X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005622 SDValue Chain,
5623 SDValue Dst, SDValue Src,
5624 SDValue Size, unsigned Align,
5625 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005626 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005627 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005628
Bill Wendling6f287b22008-09-30 21:22:07 +00005629 // If not DWORD aligned or size is more than the threshold, call the library.
5630 // The libc version is likely to be faster for these cases. It can use the
5631 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005632 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005633 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005634 ConstantSize->getZExtValue() >
5635 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005636 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005637
5638 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005639 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005640
Bill Wendling6158d842008-10-01 00:59:58 +00005641 if (const char *bzeroEntry = V &&
5642 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5643 MVT IntPtr = getPointerTy();
5644 const Type *IntPtrTy = TD->getIntPtrType();
5645 TargetLowering::ArgListTy Args;
5646 TargetLowering::ArgListEntry Entry;
5647 Entry.Node = Dst;
5648 Entry.Ty = IntPtrTy;
5649 Args.push_back(Entry);
5650 Entry.Node = Size;
5651 Args.push_back(Entry);
5652 std::pair<SDValue,SDValue> CallResult =
5653 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5654 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005655 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005656 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005657 }
5658
Dan Gohman707e0182008-04-12 04:36:06 +00005659 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005660 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005661 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005662
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005663 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005664 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005665 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005666 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005667 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005668 unsigned BytesLeft = 0;
5669 bool TwoRepStos = false;
5670 if (ValC) {
5671 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005672 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005673
Evan Cheng0db9fe62006-04-25 20:13:52 +00005674 // If the value is a constant, then we can potentially use larger sets.
5675 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005676 case 2: // WORD aligned
5677 AVT = MVT::i16;
5678 ValReg = X86::AX;
5679 Val = (Val << 8) | Val;
5680 break;
5681 case 0: // DWORD aligned
5682 AVT = MVT::i32;
5683 ValReg = X86::EAX;
5684 Val = (Val << 8) | Val;
5685 Val = (Val << 16) | Val;
5686 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5687 AVT = MVT::i64;
5688 ValReg = X86::RAX;
5689 Val = (Val << 32) | Val;
5690 }
5691 break;
5692 default: // Byte aligned
5693 AVT = MVT::i8;
5694 ValReg = X86::AL;
5695 Count = DAG.getIntPtrConstant(SizeVal);
5696 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005697 }
5698
Duncan Sands8e4eb092008-06-08 20:54:56 +00005699 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005700 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005701 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5702 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005703 }
5704
Dale Johannesen0f502f62009-02-03 22:26:09 +00005705 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005706 InFlag);
5707 InFlag = Chain.getValue(1);
5708 } else {
5709 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005710 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005711 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005712 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005713 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005714
Dale Johannesen0f502f62009-02-03 22:26:09 +00005715 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5716 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005717 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005718 InFlag = Chain.getValue(1);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005719 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5720 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005721 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005722 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005723
Chris Lattnerd96d0722007-02-25 06:40:16 +00005724 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005725 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 Ops.push_back(Chain);
5727 Ops.push_back(DAG.getValueType(AVT));
5728 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005729 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005730
Evan Cheng0db9fe62006-04-25 20:13:52 +00005731 if (TwoRepStos) {
5732 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005733 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005734 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005735 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005736 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Dale Johannesen0f502f62009-02-03 22:26:09 +00005737 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
5738 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005739 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005740 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005741 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005742 Ops.clear();
5743 Ops.push_back(Chain);
5744 Ops.push_back(DAG.getValueType(MVT::i8));
5745 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005746 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005747 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005748 // Handle the last 1 - 7 bytes.
5749 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005750 MVT AddrVT = Dst.getValueType();
5751 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005752
Dale Johannesen0f502f62009-02-03 22:26:09 +00005753 Chain = DAG.getMemset(Chain, dl,
5754 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005755 DAG.getConstant(Offset, AddrVT)),
5756 Src,
5757 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005758 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005759 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005760
Dan Gohman707e0182008-04-12 04:36:06 +00005761 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005762 return Chain;
5763}
Evan Cheng11e15b32006-04-03 20:53:28 +00005764
Dan Gohman475871a2008-07-27 21:46:04 +00005765SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005766X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005767 SDValue Chain, SDValue Dst, SDValue Src,
5768 SDValue Size, unsigned Align,
5769 bool AlwaysInline,
5770 const Value *DstSV, uint64_t DstSVOff,
5771 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005772 // This requires the copy size to be a constant, preferrably
5773 // within a subtarget-specific limit.
5774 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5775 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005776 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005777 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005778 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005779 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005780
Evan Cheng1887c1c2008-08-21 21:00:15 +00005781 /// If not DWORD aligned, call the library.
5782 if ((Align & 3) != 0)
5783 return SDValue();
5784
5785 // DWORD aligned
5786 MVT AVT = MVT::i32;
5787 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005788 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005789
Duncan Sands83ec4b62008-06-06 12:08:01 +00005790 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005791 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005792 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005793 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005794
Dan Gohman475871a2008-07-27 21:46:04 +00005795 SDValue InFlag(0, 0);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005796 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5797 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005798 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005799 InFlag = Chain.getValue(1);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005800 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5801 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005802 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005803 InFlag = Chain.getValue(1);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005804 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
5805 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005806 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005807 InFlag = Chain.getValue(1);
5808
Chris Lattnerd96d0722007-02-25 06:40:16 +00005809 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005810 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005811 Ops.push_back(Chain);
5812 Ops.push_back(DAG.getValueType(AVT));
5813 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005814 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005815
Dan Gohman475871a2008-07-27 21:46:04 +00005816 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005817 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005818 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005819 // Handle the last 1 - 7 bytes.
5820 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005821 MVT DstVT = Dst.getValueType();
5822 MVT SrcVT = Src.getValueType();
5823 MVT SizeVT = Size.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005824 Results.push_back(DAG.getMemcpy(Chain, dl,
5825 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005826 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005827 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005828 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005829 DAG.getConstant(BytesLeft, SizeVT),
5830 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005831 DstSV, DstSVOff + Offset,
5832 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005833 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005834
Dale Johannesen0f502f62009-02-03 22:26:09 +00005835 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5836 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005837}
5838
Dan Gohman475871a2008-07-27 21:46:04 +00005839SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005840 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesene4d209d2009-02-03 20:21:25 +00005841 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005842
Evan Cheng25ab6902006-09-08 06:48:29 +00005843 if (!Subtarget->is64Bit()) {
5844 // vastart just stores the address of the VarArgsFrameIndex slot into the
5845 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005846 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005847 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005848 }
5849
5850 // __va_list_tag:
5851 // gp_offset (0 - 6 * 8)
5852 // fp_offset (48 - 48 + 8 * 16)
5853 // overflow_arg_area (point to parameters coming in memory).
5854 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005855 SmallVector<SDValue, 8> MemOps;
5856 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005857 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005858 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005859 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005860 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005861 MemOps.push_back(Store);
5862
5863 // Store fp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005864 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5865 FIN, DAG.getIntPtrConstant(4));
5866 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005867 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005868 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005869 MemOps.push_back(Store);
5870
5871 // Store ptr to overflow_arg_area
Dale Johannesene4d209d2009-02-03 20:21:25 +00005872 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5873 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005874 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005875 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005876 MemOps.push_back(Store);
5877
5878 // Store ptr to reg_save_area.
Dale Johannesene4d209d2009-02-03 20:21:25 +00005879 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5880 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005881 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005882 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005883 MemOps.push_back(Store);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005884 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5885 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005886}
5887
Dan Gohman475871a2008-07-27 21:46:04 +00005888SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005889 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5890 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005891 SDValue Chain = Op.getOperand(0);
5892 SDValue SrcPtr = Op.getOperand(1);
5893 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005894
5895 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5896 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005897 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005898}
5899
Dan Gohman475871a2008-07-27 21:46:04 +00005900SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005901 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005902 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005903 SDValue Chain = Op.getOperand(0);
5904 SDValue DstPtr = Op.getOperand(1);
5905 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005906 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5907 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesendd64c412009-02-04 00:33:20 +00005908 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005909
Dale Johannesendd64c412009-02-04 00:33:20 +00005910 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005911 DAG.getIntPtrConstant(24), 8, false,
5912 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005913}
5914
Dan Gohman475871a2008-07-27 21:46:04 +00005915SDValue
5916X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00005917 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005918 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005919 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005920 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005921 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005922 case Intrinsic::x86_sse_comieq_ss:
5923 case Intrinsic::x86_sse_comilt_ss:
5924 case Intrinsic::x86_sse_comile_ss:
5925 case Intrinsic::x86_sse_comigt_ss:
5926 case Intrinsic::x86_sse_comige_ss:
5927 case Intrinsic::x86_sse_comineq_ss:
5928 case Intrinsic::x86_sse_ucomieq_ss:
5929 case Intrinsic::x86_sse_ucomilt_ss:
5930 case Intrinsic::x86_sse_ucomile_ss:
5931 case Intrinsic::x86_sse_ucomigt_ss:
5932 case Intrinsic::x86_sse_ucomige_ss:
5933 case Intrinsic::x86_sse_ucomineq_ss:
5934 case Intrinsic::x86_sse2_comieq_sd:
5935 case Intrinsic::x86_sse2_comilt_sd:
5936 case Intrinsic::x86_sse2_comile_sd:
5937 case Intrinsic::x86_sse2_comigt_sd:
5938 case Intrinsic::x86_sse2_comige_sd:
5939 case Intrinsic::x86_sse2_comineq_sd:
5940 case Intrinsic::x86_sse2_ucomieq_sd:
5941 case Intrinsic::x86_sse2_ucomilt_sd:
5942 case Intrinsic::x86_sse2_ucomile_sd:
5943 case Intrinsic::x86_sse2_ucomigt_sd:
5944 case Intrinsic::x86_sse2_ucomige_sd:
5945 case Intrinsic::x86_sse2_ucomineq_sd: {
5946 unsigned Opc = 0;
5947 ISD::CondCode CC = ISD::SETCC_INVALID;
5948 switch (IntNo) {
5949 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005950 case Intrinsic::x86_sse_comieq_ss:
5951 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005952 Opc = X86ISD::COMI;
5953 CC = ISD::SETEQ;
5954 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005955 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005956 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005957 Opc = X86ISD::COMI;
5958 CC = ISD::SETLT;
5959 break;
5960 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005961 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005962 Opc = X86ISD::COMI;
5963 CC = ISD::SETLE;
5964 break;
5965 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005966 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005967 Opc = X86ISD::COMI;
5968 CC = ISD::SETGT;
5969 break;
5970 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005971 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005972 Opc = X86ISD::COMI;
5973 CC = ISD::SETGE;
5974 break;
5975 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005976 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005977 Opc = X86ISD::COMI;
5978 CC = ISD::SETNE;
5979 break;
5980 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005981 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005982 Opc = X86ISD::UCOMI;
5983 CC = ISD::SETEQ;
5984 break;
5985 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005986 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005987 Opc = X86ISD::UCOMI;
5988 CC = ISD::SETLT;
5989 break;
5990 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005991 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005992 Opc = X86ISD::UCOMI;
5993 CC = ISD::SETLE;
5994 break;
5995 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005996 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005997 Opc = X86ISD::UCOMI;
5998 CC = ISD::SETGT;
5999 break;
6000 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006001 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006002 Opc = X86ISD::UCOMI;
6003 CC = ISD::SETGE;
6004 break;
6005 case Intrinsic::x86_sse_ucomineq_ss:
6006 case Intrinsic::x86_sse2_ucomineq_sd:
6007 Opc = X86ISD::UCOMI;
6008 CC = ISD::SETNE;
6009 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006010 }
Evan Cheng734503b2006-09-11 02:19:56 +00006011
Dan Gohman475871a2008-07-27 21:46:04 +00006012 SDValue LHS = Op.getOperand(1);
6013 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006014 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006015 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6016 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00006017 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006018 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006019 }
Evan Cheng5759f972008-05-04 09:15:50 +00006020
6021 // Fix vector shift instructions where the last operand is a non-immediate
6022 // i32 value.
6023 case Intrinsic::x86_sse2_pslli_w:
6024 case Intrinsic::x86_sse2_pslli_d:
6025 case Intrinsic::x86_sse2_pslli_q:
6026 case Intrinsic::x86_sse2_psrli_w:
6027 case Intrinsic::x86_sse2_psrli_d:
6028 case Intrinsic::x86_sse2_psrli_q:
6029 case Intrinsic::x86_sse2_psrai_w:
6030 case Intrinsic::x86_sse2_psrai_d:
6031 case Intrinsic::x86_mmx_pslli_w:
6032 case Intrinsic::x86_mmx_pslli_d:
6033 case Intrinsic::x86_mmx_pslli_q:
6034 case Intrinsic::x86_mmx_psrli_w:
6035 case Intrinsic::x86_mmx_psrli_d:
6036 case Intrinsic::x86_mmx_psrli_q:
6037 case Intrinsic::x86_mmx_psrai_w:
6038 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006039 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006040 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006041 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006042
6043 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006044 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006045 switch (IntNo) {
6046 case Intrinsic::x86_sse2_pslli_w:
6047 NewIntNo = Intrinsic::x86_sse2_psll_w;
6048 break;
6049 case Intrinsic::x86_sse2_pslli_d:
6050 NewIntNo = Intrinsic::x86_sse2_psll_d;
6051 break;
6052 case Intrinsic::x86_sse2_pslli_q:
6053 NewIntNo = Intrinsic::x86_sse2_psll_q;
6054 break;
6055 case Intrinsic::x86_sse2_psrli_w:
6056 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6057 break;
6058 case Intrinsic::x86_sse2_psrli_d:
6059 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6060 break;
6061 case Intrinsic::x86_sse2_psrli_q:
6062 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6063 break;
6064 case Intrinsic::x86_sse2_psrai_w:
6065 NewIntNo = Intrinsic::x86_sse2_psra_w;
6066 break;
6067 case Intrinsic::x86_sse2_psrai_d:
6068 NewIntNo = Intrinsic::x86_sse2_psra_d;
6069 break;
6070 default: {
6071 ShAmtVT = MVT::v2i32;
6072 switch (IntNo) {
6073 case Intrinsic::x86_mmx_pslli_w:
6074 NewIntNo = Intrinsic::x86_mmx_psll_w;
6075 break;
6076 case Intrinsic::x86_mmx_pslli_d:
6077 NewIntNo = Intrinsic::x86_mmx_psll_d;
6078 break;
6079 case Intrinsic::x86_mmx_pslli_q:
6080 NewIntNo = Intrinsic::x86_mmx_psll_q;
6081 break;
6082 case Intrinsic::x86_mmx_psrli_w:
6083 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6084 break;
6085 case Intrinsic::x86_mmx_psrli_d:
6086 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6087 break;
6088 case Intrinsic::x86_mmx_psrli_q:
6089 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6090 break;
6091 case Intrinsic::x86_mmx_psrai_w:
6092 NewIntNo = Intrinsic::x86_mmx_psra_w;
6093 break;
6094 case Intrinsic::x86_mmx_psrai_d:
6095 NewIntNo = Intrinsic::x86_mmx_psra_d;
6096 break;
6097 default: abort(); // Can't reach here.
6098 }
6099 break;
6100 }
6101 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006102 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006103 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6104 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6105 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006106 DAG.getConstant(NewIntNo, MVT::i32),
6107 Op.getOperand(1), ShAmt);
6108 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006109 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006110}
Evan Cheng72261582005-12-20 06:22:03 +00006111
Dan Gohman475871a2008-07-27 21:46:04 +00006112SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006113 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006114 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006115
6116 if (Depth > 0) {
6117 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6118 SDValue Offset =
6119 DAG.getConstant(TD->getPointerSize(),
6120 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006121 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6122 DAG.getNode(ISD::ADD, dl, getPointerTy(),
6123 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006124 NULL, 0);
6125 }
6126
6127 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006128 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006129 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6130 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006131}
6132
Dan Gohman475871a2008-07-27 21:46:04 +00006133SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006134 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6135 MFI->setFrameAddressIsTaken(true);
6136 MVT VT = Op.getValueType();
Dale Johannesendd64c412009-02-04 00:33:20 +00006137 DebugLoc dl = Op.getNode()->getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006138 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6139 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006140 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006141 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006142 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006143 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006144}
6145
Dan Gohman475871a2008-07-27 21:46:04 +00006146SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006147 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006148 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006149}
6150
Dan Gohman475871a2008-07-27 21:46:04 +00006151SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006152{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006153 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006154 SDValue Chain = Op.getOperand(0);
6155 SDValue Offset = Op.getOperand(1);
6156 SDValue Handler = Op.getOperand(2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006157 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006158
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006159 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6160 getPointerTy());
6161 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006162
Dale Johannesene4d209d2009-02-03 20:21:25 +00006163 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006164 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006165 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6166 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006167 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006168 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006169
Dale Johannesene4d209d2009-02-03 20:21:25 +00006170 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006171 MVT::Other,
6172 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006173}
6174
Dan Gohman475871a2008-07-27 21:46:04 +00006175SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006176 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006177 SDValue Root = Op.getOperand(0);
6178 SDValue Trmp = Op.getOperand(1); // trampoline
6179 SDValue FPtr = Op.getOperand(2); // nested function
6180 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesene4d209d2009-02-03 20:21:25 +00006181 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006182
Dan Gohman69de1932008-02-06 22:27:42 +00006183 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006184
Duncan Sands339e14f2008-01-16 22:55:25 +00006185 const X86InstrInfo *TII =
6186 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6187
Duncan Sandsb116fac2007-07-27 20:02:49 +00006188 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006189 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006190
6191 // Large code-model.
6192
6193 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6194 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6195
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006196 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6197 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006198
6199 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6200
6201 // Load the pointer to the nested function into R11.
6202 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006203 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006204 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6205 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006206
Dale Johannesene4d209d2009-02-03 20:21:25 +00006207 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6208 DAG.getConstant(2, MVT::i64));
6209 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006210
6211 // Load the 'nest' parameter value into R10.
6212 // R10 is specified in X86CallingConv.td
6213 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Dale Johannesene4d209d2009-02-03 20:21:25 +00006214 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6215 DAG.getConstant(10, MVT::i64));
6216 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6217 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006218
Dale Johannesene4d209d2009-02-03 20:21:25 +00006219 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6220 DAG.getConstant(12, MVT::i64));
6221 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006222
6223 // Jump to the nested function.
6224 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Dale Johannesene4d209d2009-02-03 20:21:25 +00006225 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6226 DAG.getConstant(20, MVT::i64));
6227 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6228 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006229
6230 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Dale Johannesene4d209d2009-02-03 20:21:25 +00006231 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6232 DAG.getConstant(22, MVT::i64));
6233 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006234 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006235
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006237 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6238 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006239 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006240 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006241 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6242 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006243 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006244
6245 switch (CC) {
6246 default:
6247 assert(0 && "Unsupported calling convention");
6248 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006249 case CallingConv::X86_StdCall: {
6250 // Pass 'nest' parameter in ECX.
6251 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006252 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006253
6254 // Check that ECX wasn't needed by an 'inreg' parameter.
6255 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006256 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006257
Chris Lattner58d74912008-03-12 17:45:29 +00006258 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006259 unsigned InRegCount = 0;
6260 unsigned Idx = 1;
6261
6262 for (FunctionType::param_iterator I = FTy->param_begin(),
6263 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006264 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006265 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006266 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006267
6268 if (InRegCount > 2) {
6269 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6270 abort();
6271 }
6272 }
6273 break;
6274 }
6275 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006276 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006277 // Pass 'nest' parameter in EAX.
6278 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006279 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006280 break;
6281 }
6282
Dan Gohman475871a2008-07-27 21:46:04 +00006283 SDValue OutChains[4];
6284 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006285
Dale Johannesene4d209d2009-02-03 20:21:25 +00006286 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6287 DAG.getConstant(10, MVT::i32));
6288 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006289
Duncan Sands339e14f2008-01-16 22:55:25 +00006290 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006291 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006292 OutChains[0] = DAG.getStore(Root, dl,
6293 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006294 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006295
Dale Johannesene4d209d2009-02-03 20:21:25 +00006296 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6297 DAG.getConstant(1, MVT::i32));
6298 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006299
Duncan Sands339e14f2008-01-16 22:55:25 +00006300 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006301 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6302 DAG.getConstant(5, MVT::i32));
6303 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006304 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006305
Dale Johannesene4d209d2009-02-03 20:21:25 +00006306 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6307 DAG.getConstant(6, MVT::i32));
6308 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006309
Dan Gohman475871a2008-07-27 21:46:04 +00006310 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006311 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6312 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006313 }
6314}
6315
Dan Gohman475871a2008-07-27 21:46:04 +00006316SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006317 /*
6318 The rounding mode is in bits 11:10 of FPSR, and has the following
6319 settings:
6320 00 Round to nearest
6321 01 Round to -inf
6322 10 Round to +inf
6323 11 Round to 0
6324
6325 FLT_ROUNDS, on the other hand, expects the following:
6326 -1 Undefined
6327 0 Round to 0
6328 1 Round to nearest
6329 2 Round to +inf
6330 3 Round to -inf
6331
6332 To perform the conversion, we do:
6333 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6334 */
6335
6336 MachineFunction &MF = DAG.getMachineFunction();
6337 const TargetMachine &TM = MF.getTarget();
6338 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6339 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006340 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006341 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006342
6343 // Save FP Control Word to stack slot
6344 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006345 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006346
Dale Johannesene4d209d2009-02-03 20:21:25 +00006347 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006348 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006349
6350 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006351 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006352
6353 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006355 DAG.getNode(ISD::SRL, dl, MVT::i16,
6356 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006357 CWD, DAG.getConstant(0x800, MVT::i16)),
6358 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006359 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006360 DAG.getNode(ISD::SRL, dl, MVT::i16,
6361 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006362 CWD, DAG.getConstant(0x400, MVT::i16)),
6363 DAG.getConstant(9, MVT::i8));
6364
Dan Gohman475871a2008-07-27 21:46:04 +00006365 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006366 DAG.getNode(ISD::AND, dl, MVT::i16,
6367 DAG.getNode(ISD::ADD, dl, MVT::i16,
6368 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006369 DAG.getConstant(1, MVT::i16)),
6370 DAG.getConstant(3, MVT::i16));
6371
6372
Duncan Sands83ec4b62008-06-06 12:08:01 +00006373 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006374 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6375}
6376
Dan Gohman475871a2008-07-27 21:46:04 +00006377SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006378 MVT VT = Op.getValueType();
6379 MVT OpVT = VT;
6380 unsigned NumBits = VT.getSizeInBits();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006381 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006382
6383 Op = Op.getOperand(0);
6384 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006385 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006386 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006387 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006388 }
Evan Cheng18efe262007-12-14 02:13:44 +00006389
Evan Cheng152804e2007-12-14 08:30:15 +00006390 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6391 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006392 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006393
6394 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006395 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006396 Ops.push_back(Op);
6397 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6398 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6399 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006400 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006401
6402 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006403 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006404
Evan Cheng18efe262007-12-14 02:13:44 +00006405 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006406 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006407 return Op;
6408}
6409
Dan Gohman475871a2008-07-27 21:46:04 +00006410SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006411 MVT VT = Op.getValueType();
6412 MVT OpVT = VT;
6413 unsigned NumBits = VT.getSizeInBits();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006414 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006415
6416 Op = Op.getOperand(0);
6417 if (VT == MVT::i8) {
6418 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006419 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006420 }
Evan Cheng152804e2007-12-14 08:30:15 +00006421
6422 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6423 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006424 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006425
6426 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006427 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006428 Ops.push_back(Op);
6429 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6430 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6431 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006432 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006433
Evan Cheng18efe262007-12-14 02:13:44 +00006434 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006435 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006436 return Op;
6437}
6438
Mon P Wangaf9b9522008-12-18 21:42:19 +00006439SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6440 MVT VT = Op.getValueType();
6441 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesene4d209d2009-02-03 20:21:25 +00006442 DebugLoc dl = Op.getNode()->getDebugLoc();
Mon P Wangaf9b9522008-12-18 21:42:19 +00006443
6444 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6445 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6446 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6447 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6448 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6449 //
6450 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6451 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6452 // return AloBlo + AloBhi + AhiBlo;
6453
6454 SDValue A = Op.getOperand(0);
6455 SDValue B = Op.getOperand(1);
6456
Dale Johannesene4d209d2009-02-03 20:21:25 +00006457 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006458 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6459 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006460 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006461 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6462 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006463 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006464 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6465 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006466 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006467 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6468 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006469 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006470 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6471 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006472 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006473 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6474 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006475 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006476 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6477 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006478 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6479 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006480 return Res;
6481}
6482
6483
Bill Wendling74c37652008-12-09 22:08:41 +00006484SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6485 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6486 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006487 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6488 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006489 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006490 SDValue LHS = N->getOperand(0);
6491 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006492 unsigned BaseOp = 0;
6493 unsigned Cond = 0;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006494 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006495
6496 switch (Op.getOpcode()) {
6497 default: assert(0 && "Unknown ovf instruction!");
6498 case ISD::SADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006499 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006500 Cond = X86::COND_O;
6501 break;
6502 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006503 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006504 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006505 break;
6506 case ISD::SSUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006507 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006508 Cond = X86::COND_O;
6509 break;
6510 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006511 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006512 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006513 break;
6514 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006515 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006516 Cond = X86::COND_O;
6517 break;
6518 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006519 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006520 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006521 break;
6522 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006523
Bill Wendling61edeb52008-12-02 01:06:39 +00006524 // Also sets EFLAGS.
6525 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006526 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006527
Bill Wendling61edeb52008-12-02 01:06:39 +00006528 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006529 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006530 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006531
Bill Wendling61edeb52008-12-02 01:06:39 +00006532 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6533 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006534}
6535
Dan Gohman475871a2008-07-27 21:46:04 +00006536SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006537 MVT T = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006538 DebugLoc dl = Op.getNode()->getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006539 unsigned Reg = 0;
6540 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006541 switch(T.getSimpleVT()) {
6542 default:
6543 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006544 case MVT::i8: Reg = X86::AL; size = 1; break;
6545 case MVT::i16: Reg = X86::AX; size = 2; break;
6546 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006547 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006548 assert(Subtarget->is64Bit() && "Node not type legal!");
6549 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006550 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006551 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006552 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006553 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006554 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006555 Op.getOperand(1),
6556 Op.getOperand(3),
6557 DAG.getTargetConstant(size, MVT::i8),
6558 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006559 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006560 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00006561 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006562 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006563 return cpOut;
6564}
6565
Duncan Sands1607f052008-12-01 11:39:25 +00006566SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006567 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006568 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006569 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006570 SDValue TheChain = Op.getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006571 DebugLoc dl = Op.getNode()->getDebugLoc();
6572 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006573 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6574 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006575 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006576 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006577 DAG.getConstant(32, MVT::i8));
6578 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006579 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006580 rdx.getValue(1)
6581 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006582 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006583}
6584
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006585SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6586 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006587 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006588 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006589 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006590 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006591 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006592 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006593 Node->getOperand(0),
6594 Node->getOperand(1), negOp,
6595 cast<AtomicSDNode>(Node)->getSrcValue(),
6596 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006597}
6598
Evan Cheng0db9fe62006-04-25 20:13:52 +00006599/// LowerOperation - Provide custom lowering hooks for some operations.
6600///
Dan Gohman475871a2008-07-27 21:46:04 +00006601SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602 switch (Op.getOpcode()) {
6603 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006604 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6605 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006606 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6607 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6608 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6609 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6610 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6611 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6612 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006613 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006614 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006615 case ISD::SHL_PARTS:
6616 case ISD::SRA_PARTS:
6617 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6618 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006619 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006620 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6621 case ISD::FABS: return LowerFABS(Op, DAG);
6622 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006623 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006624 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006625 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006626 case ISD::SELECT: return LowerSELECT(Op, DAG);
6627 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006628 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006629 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006630 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006631 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006632 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006633 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006634 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006635 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006636 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6637 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006638 case ISD::FRAME_TO_ARGS_OFFSET:
6639 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006640 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006641 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006642 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006643 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006644 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6645 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006646 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006647 case ISD::SADDO:
6648 case ISD::UADDO:
6649 case ISD::SSUBO:
6650 case ISD::USUBO:
6651 case ISD::SMULO:
6652 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006653 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006654 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006655}
6656
Duncan Sands1607f052008-12-01 11:39:25 +00006657void X86TargetLowering::
6658ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6659 SelectionDAG &DAG, unsigned NewOp) {
6660 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006661 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006662 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6663
6664 SDValue Chain = Node->getOperand(0);
6665 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006666 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006667 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006668 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006669 Node->getOperand(2), DAG.getIntPtrConstant(1));
6670 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6671 // have a MemOperand. Pass the info through as a normal operand.
6672 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6673 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6674 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006675 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006676 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006677 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006678 Results.push_back(Result.getValue(2));
6679}
6680
Duncan Sands126d9072008-07-04 11:47:58 +00006681/// ReplaceNodeResults - Replace a node with an illegal result type
6682/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006683void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6684 SmallVectorImpl<SDValue>&Results,
6685 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006686 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006687 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006688 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006689 assert(false && "Do not know how to custom type legalize this operation!");
6690 return;
6691 case ISD::FP_TO_SINT: {
6692 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6693 SDValue FIST = Vals.first, StackSlot = Vals.second;
6694 if (FIST.getNode() != 0) {
6695 MVT VT = N->getValueType(0);
6696 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006697 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006698 }
6699 return;
6700 }
6701 case ISD::READCYCLECOUNTER: {
6702 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6703 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006704 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006705 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
6706 rd.getValue(1));
6707 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006708 eax.getValue(2));
6709 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6710 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006711 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006712 Results.push_back(edx.getValue(1));
6713 return;
6714 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006715 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006716 MVT T = N->getValueType(0);
6717 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6718 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006719 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006720 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006721 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006722 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006723 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6724 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006725 cpInL.getValue(1));
6726 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006727 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006728 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006729 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006730 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006731 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006732 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006733 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006734 swapInL.getValue(1));
6735 SDValue Ops[] = { swapInH.getValue(0),
6736 N->getOperand(1),
6737 swapInH.getValue(1) };
6738 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006739 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006740 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6741 MVT::i32, Result.getValue(1));
6742 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6743 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006744 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006745 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006746 Results.push_back(cpOutH.getValue(1));
6747 return;
6748 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006749 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006750 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6751 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006752 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006753 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6754 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006755 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006756 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6757 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006758 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006759 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6760 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006761 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006762 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6763 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006764 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006765 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6766 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006767 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006768 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6769 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006770 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771}
6772
Evan Cheng72261582005-12-20 06:22:03 +00006773const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6774 switch (Opcode) {
6775 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006776 case X86ISD::BSF: return "X86ISD::BSF";
6777 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006778 case X86ISD::SHLD: return "X86ISD::SHLD";
6779 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006780 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006781 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006782 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006783 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006784 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006785 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006786 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6787 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6788 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006789 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006790 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006791 case X86ISD::CALL: return "X86ISD::CALL";
6792 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6793 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006794 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006795 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006796 case X86ISD::COMI: return "X86ISD::COMI";
6797 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006798 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006799 case X86ISD::CMOV: return "X86ISD::CMOV";
6800 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006801 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006802 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6803 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006804 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006805 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006806 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006807 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006808 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6809 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006810 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00006811 case X86ISD::FMAX: return "X86ISD::FMAX";
6812 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006813 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6814 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006815 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6816 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006817 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006818 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006819 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006820 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6821 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006822 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6823 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6824 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6825 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6826 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6827 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006828 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6829 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006830 case X86ISD::VSHL: return "X86ISD::VSHL";
6831 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006832 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6833 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6834 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6835 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6836 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6837 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6838 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6839 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6840 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6841 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006842 case X86ISD::ADD: return "X86ISD::ADD";
6843 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006844 case X86ISD::SMUL: return "X86ISD::SMUL";
6845 case X86ISD::UMUL: return "X86ISD::UMUL";
Evan Cheng72261582005-12-20 06:22:03 +00006846 }
6847}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006848
Chris Lattnerc9addb72007-03-30 23:15:24 +00006849// isLegalAddressingMode - Return true if the addressing mode represented
6850// by AM is legal for this target, for a load/store of the specified type.
6851bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6852 const Type *Ty) const {
6853 // X86 supports extremely general addressing modes.
6854
6855 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6856 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6857 return false;
6858
6859 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006860 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006861 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6862 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006863 // If BaseGV requires a register, we cannot also have a BaseReg.
6864 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6865 AM.HasBaseReg)
6866 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006867
6868 // X86-64 only supports addr of globals in small code model.
6869 if (Subtarget->is64Bit()) {
6870 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6871 return false;
6872 // If lower 4G is not available, then we must use rip-relative addressing.
6873 if (AM.BaseOffs || AM.Scale > 1)
6874 return false;
6875 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006876 }
6877
6878 switch (AM.Scale) {
6879 case 0:
6880 case 1:
6881 case 2:
6882 case 4:
6883 case 8:
6884 // These scales always work.
6885 break;
6886 case 3:
6887 case 5:
6888 case 9:
6889 // These scales are formed with basereg+scalereg. Only accept if there is
6890 // no basereg yet.
6891 if (AM.HasBaseReg)
6892 return false;
6893 break;
6894 default: // Other stuff never works.
6895 return false;
6896 }
6897
6898 return true;
6899}
6900
6901
Evan Cheng2bd122c2007-10-26 01:56:11 +00006902bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6903 if (!Ty1->isInteger() || !Ty2->isInteger())
6904 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006905 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6906 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006907 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006908 return false;
6909 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006910}
6911
Duncan Sands83ec4b62008-06-06 12:08:01 +00006912bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6913 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006914 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006915 unsigned NumBits1 = VT1.getSizeInBits();
6916 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006917 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006918 return false;
6919 return Subtarget->is64Bit() || NumBits1 < 64;
6920}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006921
Evan Cheng60c07e12006-07-05 22:17:51 +00006922/// isShuffleMaskLegal - Targets can use this to indicate that they only
6923/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6924/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6925/// are assumed to be legal.
6926bool
Dan Gohman475871a2008-07-27 21:46:04 +00006927X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006928 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006929 if (VT.getSizeInBits() == 64) return false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006930 return (Mask.getNode()->getNumOperands() <= 4 ||
6931 isIdentityMask(Mask.getNode()) ||
6932 isIdentityMask(Mask.getNode(), true) ||
6933 isSplatMask(Mask.getNode()) ||
6934 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6935 X86::isUNPCKLMask(Mask.getNode()) ||
6936 X86::isUNPCKHMask(Mask.getNode()) ||
6937 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6938 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Evan Cheng60c07e12006-07-05 22:17:51 +00006939}
6940
Dan Gohman7d8143f2008-04-09 20:09:42 +00006941bool
Dan Gohman475871a2008-07-27 21:46:04 +00006942X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006943 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006944 unsigned NumElts = BVOps.size();
6945 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006946 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006947 if (NumElts == 2) return true;
6948 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006949 return (isMOVLMask(&BVOps[0], 4) ||
6950 isCommutedMOVL(&BVOps[0], 4, true) ||
6951 isSHUFPMask(&BVOps[0], 4) ||
6952 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006953 }
6954 return false;
6955}
6956
6957//===----------------------------------------------------------------------===//
6958// X86 Scheduler Hooks
6959//===----------------------------------------------------------------------===//
6960
Mon P Wang63307c32008-05-05 19:05:59 +00006961// private utility function
6962MachineBasicBlock *
6963X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6964 MachineBasicBlock *MBB,
6965 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006966 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006967 unsigned LoadOpc,
6968 unsigned CXchgOpc,
6969 unsigned copyOpc,
6970 unsigned notOpc,
6971 unsigned EAXreg,
6972 TargetRegisterClass *RC,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006973 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006974 // For the atomic bitwise operator, we generate
6975 // thisMBB:
6976 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006977 // ld t1 = [bitinstr.addr]
6978 // op t2 = t1, [bitinstr.val]
6979 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006980 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6981 // bz newMBB
6982 // fallthrough -->nextMBB
6983 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6984 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006985 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006986 ++MBBIter;
6987
6988 /// First build the CFG
6989 MachineFunction *F = MBB->getParent();
6990 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006991 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6992 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6993 F->insert(MBBIter, newMBB);
6994 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006995
6996 // Move all successors to thisMBB to nextMBB
6997 nextMBB->transferSuccessors(thisMBB);
6998
6999 // Update thisMBB to fall through to newMBB
7000 thisMBB->addSuccessor(newMBB);
7001
7002 // newMBB jumps to itself and fall through to nextMBB
7003 newMBB->addSuccessor(nextMBB);
7004 newMBB->addSuccessor(newMBB);
7005
7006 // Insert instructions into newMBB based on incoming instruction
7007 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007008 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007009 MachineOperand& destOper = bInstr->getOperand(0);
7010 MachineOperand* argOpers[6];
7011 int numArgs = bInstr->getNumOperands() - 1;
7012 for (int i=0; i < numArgs; ++i)
7013 argOpers[i] = &bInstr->getOperand(i+1);
7014
7015 // x86 address has 4 operands: base, index, scale, and displacement
7016 int lastAddrIndx = 3; // [0,3]
7017 int valArgIndx = 4;
7018
Dale Johannesen140be2d2008-08-19 18:47:28 +00007019 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007020 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007021 for (int i=0; i <= lastAddrIndx; ++i)
7022 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007023
Dale Johannesen140be2d2008-08-19 18:47:28 +00007024 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007025 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007026 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007027 }
7028 else
7029 tt = t1;
7030
Dale Johannesen140be2d2008-08-19 18:47:28 +00007031 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007032 assert((argOpers[valArgIndx]->isReg() ||
7033 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007034 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007035 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007036 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007037 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007038 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007039 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007040 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007041
Dale Johannesene4d209d2009-02-03 20:21:25 +00007042 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007043 MIB.addReg(t1);
7044
Dale Johannesene4d209d2009-02-03 20:21:25 +00007045 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007046 for (int i=0; i <= lastAddrIndx; ++i)
7047 (*MIB).addOperand(*argOpers[i]);
7048 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007049 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7050 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7051
Dale Johannesene4d209d2009-02-03 20:21:25 +00007052 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007053 MIB.addReg(EAXreg);
Mon P Wang63307c32008-05-05 19:05:59 +00007054
7055 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007056 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007057
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007058 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007059 return nextMBB;
7060}
7061
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007062// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007063MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007064X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7065 MachineBasicBlock *MBB,
7066 unsigned regOpcL,
7067 unsigned regOpcH,
7068 unsigned immOpcL,
7069 unsigned immOpcH,
7070 bool invSrc) {
7071 // For the atomic bitwise operator, we generate
7072 // thisMBB (instructions are in pairs, except cmpxchg8b)
7073 // ld t1,t2 = [bitinstr.addr]
7074 // newMBB:
7075 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7076 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007077 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007078 // mov ECX, EBX <- t5, t6
7079 // mov EAX, EDX <- t1, t2
7080 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7081 // mov t3, t4 <- EAX, EDX
7082 // bz newMBB
7083 // result in out1, out2
7084 // fallthrough -->nextMBB
7085
7086 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7087 const unsigned LoadOpc = X86::MOV32rm;
7088 const unsigned copyOpc = X86::MOV32rr;
7089 const unsigned NotOpc = X86::NOT32r;
7090 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7091 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7092 MachineFunction::iterator MBBIter = MBB;
7093 ++MBBIter;
7094
7095 /// First build the CFG
7096 MachineFunction *F = MBB->getParent();
7097 MachineBasicBlock *thisMBB = MBB;
7098 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7099 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7100 F->insert(MBBIter, newMBB);
7101 F->insert(MBBIter, nextMBB);
7102
7103 // Move all successors to thisMBB to nextMBB
7104 nextMBB->transferSuccessors(thisMBB);
7105
7106 // Update thisMBB to fall through to newMBB
7107 thisMBB->addSuccessor(newMBB);
7108
7109 // newMBB jumps to itself and fall through to nextMBB
7110 newMBB->addSuccessor(nextMBB);
7111 newMBB->addSuccessor(newMBB);
7112
Dale Johannesene4d209d2009-02-03 20:21:25 +00007113 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007114 // Insert instructions into newMBB based on incoming instruction
7115 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
7116 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
7117 MachineOperand& dest1Oper = bInstr->getOperand(0);
7118 MachineOperand& dest2Oper = bInstr->getOperand(1);
7119 MachineOperand* argOpers[6];
7120 for (int i=0; i < 6; ++i)
7121 argOpers[i] = &bInstr->getOperand(i+2);
7122
7123 // x86 address has 4 operands: base, index, scale, and displacement
7124 int lastAddrIndx = 3; // [0,3]
7125
7126 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007127 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007128 for (int i=0; i <= lastAddrIndx; ++i)
7129 (*MIB).addOperand(*argOpers[i]);
7130 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007131 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007132 // add 4 to displacement.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007133 for (int i=0; i <= lastAddrIndx-1; ++i)
7134 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007135 MachineOperand newOp3 = *(argOpers[3]);
7136 if (newOp3.isImm())
7137 newOp3.setImm(newOp3.getImm()+4);
7138 else
7139 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007140 (*MIB).addOperand(newOp3);
7141
7142 // t3/4 are defined later, at the bottom of the loop
7143 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7144 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007145 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007146 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007147 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007148 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7149
7150 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7151 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7152 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007153 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7154 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007155 } else {
7156 tt1 = t1;
7157 tt2 = t2;
7158 }
7159
Dan Gohmand735b802008-10-03 15:45:36 +00007160 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007161 "invalid operand");
7162 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7163 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007164 if (argOpers[4]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007165 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007166 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007167 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007168 if (regOpcL != X86::MOV32rr)
7169 MIB.addReg(tt1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007170 (*MIB).addOperand(*argOpers[4]);
Dan Gohmand735b802008-10-03 15:45:36 +00007171 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7172 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7173 if (argOpers[5]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007174 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007175 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007176 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007177 if (regOpcH != X86::MOV32rr)
7178 MIB.addReg(tt2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007179 (*MIB).addOperand(*argOpers[5]);
7180
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007182 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007183 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007184 MIB.addReg(t2);
7185
Dale Johannesene4d209d2009-02-03 20:21:25 +00007186 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007187 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007188 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007189 MIB.addReg(t6);
7190
Dale Johannesene4d209d2009-02-03 20:21:25 +00007191 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007192 for (int i=0; i <= lastAddrIndx; ++i)
7193 (*MIB).addOperand(*argOpers[i]);
7194
7195 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7196 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7197
Dale Johannesene4d209d2009-02-03 20:21:25 +00007198 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007199 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007200 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007201 MIB.addReg(X86::EDX);
7202
7203 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007204 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007205
7206 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7207 return nextMBB;
7208}
7209
7210// private utility function
7211MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007212X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7213 MachineBasicBlock *MBB,
7214 unsigned cmovOpc) {
7215 // For the atomic min/max operator, we generate
7216 // thisMBB:
7217 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007218 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00007219 // mov t2 = [min/max.val]
7220 // cmp t1, t2
7221 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007222 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007223 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7224 // bz newMBB
7225 // fallthrough -->nextMBB
7226 //
7227 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7228 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007229 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007230 ++MBBIter;
7231
7232 /// First build the CFG
7233 MachineFunction *F = MBB->getParent();
7234 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007235 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7236 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7237 F->insert(MBBIter, newMBB);
7238 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007239
7240 // Move all successors to thisMBB to nextMBB
7241 nextMBB->transferSuccessors(thisMBB);
7242
7243 // Update thisMBB to fall through to newMBB
7244 thisMBB->addSuccessor(newMBB);
7245
7246 // newMBB jumps to newMBB and fall through to nextMBB
7247 newMBB->addSuccessor(nextMBB);
7248 newMBB->addSuccessor(newMBB);
7249
Dale Johannesene4d209d2009-02-03 20:21:25 +00007250 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007251 // Insert instructions into newMBB based on incoming instruction
7252 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7253 MachineOperand& destOper = mInstr->getOperand(0);
7254 MachineOperand* argOpers[6];
7255 int numArgs = mInstr->getNumOperands() - 1;
7256 for (int i=0; i < numArgs; ++i)
7257 argOpers[i] = &mInstr->getOperand(i+1);
7258
7259 // x86 address has 4 operands: base, index, scale, and displacement
7260 int lastAddrIndx = 3; // [0,3]
7261 int valArgIndx = 4;
7262
Mon P Wangab3e7472008-05-05 22:56:23 +00007263 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007264 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007265 for (int i=0; i <= lastAddrIndx; ++i)
7266 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007267
Mon P Wang63307c32008-05-05 19:05:59 +00007268 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007269 assert((argOpers[valArgIndx]->isReg() ||
7270 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007271 "invalid operand");
Mon P Wang63307c32008-05-05 19:05:59 +00007272
7273 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007274 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007275 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007276 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007277 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007278 (*MIB).addOperand(*argOpers[valArgIndx]);
7279
Dale Johannesene4d209d2009-02-03 20:21:25 +00007280 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007281 MIB.addReg(t1);
7282
Dale Johannesene4d209d2009-02-03 20:21:25 +00007283 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007284 MIB.addReg(t1);
7285 MIB.addReg(t2);
7286
7287 // Generate movc
7288 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007289 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007290 MIB.addReg(t2);
7291 MIB.addReg(t1);
7292
7293 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007294 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007295 for (int i=0; i <= lastAddrIndx; ++i)
7296 (*MIB).addOperand(*argOpers[i]);
7297 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007298 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7299 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00007300
Dale Johannesene4d209d2009-02-03 20:21:25 +00007301 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007302 MIB.addReg(X86::EAX);
7303
7304 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007305 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007306
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007307 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007308 return nextMBB;
7309}
7310
7311
Evan Cheng60c07e12006-07-05 22:17:51 +00007312MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007313X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7314 MachineBasicBlock *BB) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007315 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007316 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007317 switch (MI->getOpcode()) {
7318 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007319 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007320 case X86::CMOV_FR32:
7321 case X86::CMOV_FR64:
7322 case X86::CMOV_V4F32:
7323 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007324 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007325 // To "insert" a SELECT_CC instruction, we actually have to insert the
7326 // diamond control-flow pattern. The incoming instruction knows the
7327 // destination vreg to set, the condition code register to branch on, the
7328 // true/false values to select between, and a branch opcode to use.
7329 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007330 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007331 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007332
Evan Cheng60c07e12006-07-05 22:17:51 +00007333 // thisMBB:
7334 // ...
7335 // TrueVal = ...
7336 // cmpTY ccX, r1, r2
7337 // bCC copy1MBB
7338 // fallthrough --> copy0MBB
7339 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007340 MachineFunction *F = BB->getParent();
7341 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7342 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007343 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007344 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007345 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007346 F->insert(It, copy0MBB);
7347 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007348 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007349 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007350 sinkMBB->transferSuccessors(BB);
7351
7352 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007353 BB->addSuccessor(copy0MBB);
7354 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007355
Evan Cheng60c07e12006-07-05 22:17:51 +00007356 // copy0MBB:
7357 // %FalseValue = ...
7358 // # fallthrough to sinkMBB
7359 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007360
Evan Cheng60c07e12006-07-05 22:17:51 +00007361 // Update machine-CFG edges
7362 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007363
Evan Cheng60c07e12006-07-05 22:17:51 +00007364 // sinkMBB:
7365 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7366 // ...
7367 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007368 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007369 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7370 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7371
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007372 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007373 return BB;
7374 }
7375
Dale Johannesen849f2142007-07-03 00:53:03 +00007376 case X86::FP32_TO_INT16_IN_MEM:
7377 case X86::FP32_TO_INT32_IN_MEM:
7378 case X86::FP32_TO_INT64_IN_MEM:
7379 case X86::FP64_TO_INT16_IN_MEM:
7380 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007381 case X86::FP64_TO_INT64_IN_MEM:
7382 case X86::FP80_TO_INT16_IN_MEM:
7383 case X86::FP80_TO_INT32_IN_MEM:
7384 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007385 // Change the floating point control register to use "round towards zero"
7386 // mode when truncating to an integer value.
7387 MachineFunction *F = BB->getParent();
7388 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007389 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007390
7391 // Load the old value of the high byte of the control word...
7392 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007393 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007394 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
7395 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007396
7397 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007398 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007399 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007400
7401 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007402 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007403
7404 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007405 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007406 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007407
7408 // Get the X86 opcode to use.
7409 unsigned Opc;
7410 switch (MI->getOpcode()) {
7411 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007412 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7413 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7414 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7415 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7416 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7417 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007418 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7419 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7420 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007421 }
7422
7423 X86AddressMode AM;
7424 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007425 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007426 AM.BaseType = X86AddressMode::RegBase;
7427 AM.Base.Reg = Op.getReg();
7428 } else {
7429 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007430 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007431 }
7432 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007433 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007434 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007435 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007436 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007437 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007438 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007439 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007440 AM.GV = Op.getGlobal();
7441 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007442 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007443 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007444 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007445 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007446
7447 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007448 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007449
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007450 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007451 return BB;
7452 }
Mon P Wang63307c32008-05-05 19:05:59 +00007453 case X86::ATOMAND32:
7454 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007455 X86::AND32ri, X86::MOV32rm,
7456 X86::LCMPXCHG32, X86::MOV32rr,
7457 X86::NOT32r, X86::EAX,
7458 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007459 case X86::ATOMOR32:
7460 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007461 X86::OR32ri, X86::MOV32rm,
7462 X86::LCMPXCHG32, X86::MOV32rr,
7463 X86::NOT32r, X86::EAX,
7464 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007465 case X86::ATOMXOR32:
7466 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007467 X86::XOR32ri, X86::MOV32rm,
7468 X86::LCMPXCHG32, X86::MOV32rr,
7469 X86::NOT32r, X86::EAX,
7470 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007471 case X86::ATOMNAND32:
7472 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007473 X86::AND32ri, X86::MOV32rm,
7474 X86::LCMPXCHG32, X86::MOV32rr,
7475 X86::NOT32r, X86::EAX,
7476 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007477 case X86::ATOMMIN32:
7478 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7479 case X86::ATOMMAX32:
7480 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7481 case X86::ATOMUMIN32:
7482 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7483 case X86::ATOMUMAX32:
7484 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007485
7486 case X86::ATOMAND16:
7487 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7488 X86::AND16ri, X86::MOV16rm,
7489 X86::LCMPXCHG16, X86::MOV16rr,
7490 X86::NOT16r, X86::AX,
7491 X86::GR16RegisterClass);
7492 case X86::ATOMOR16:
7493 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7494 X86::OR16ri, X86::MOV16rm,
7495 X86::LCMPXCHG16, X86::MOV16rr,
7496 X86::NOT16r, X86::AX,
7497 X86::GR16RegisterClass);
7498 case X86::ATOMXOR16:
7499 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7500 X86::XOR16ri, X86::MOV16rm,
7501 X86::LCMPXCHG16, X86::MOV16rr,
7502 X86::NOT16r, X86::AX,
7503 X86::GR16RegisterClass);
7504 case X86::ATOMNAND16:
7505 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7506 X86::AND16ri, X86::MOV16rm,
7507 X86::LCMPXCHG16, X86::MOV16rr,
7508 X86::NOT16r, X86::AX,
7509 X86::GR16RegisterClass, true);
7510 case X86::ATOMMIN16:
7511 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7512 case X86::ATOMMAX16:
7513 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7514 case X86::ATOMUMIN16:
7515 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7516 case X86::ATOMUMAX16:
7517 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7518
7519 case X86::ATOMAND8:
7520 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7521 X86::AND8ri, X86::MOV8rm,
7522 X86::LCMPXCHG8, X86::MOV8rr,
7523 X86::NOT8r, X86::AL,
7524 X86::GR8RegisterClass);
7525 case X86::ATOMOR8:
7526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7527 X86::OR8ri, X86::MOV8rm,
7528 X86::LCMPXCHG8, X86::MOV8rr,
7529 X86::NOT8r, X86::AL,
7530 X86::GR8RegisterClass);
7531 case X86::ATOMXOR8:
7532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7533 X86::XOR8ri, X86::MOV8rm,
7534 X86::LCMPXCHG8, X86::MOV8rr,
7535 X86::NOT8r, X86::AL,
7536 X86::GR8RegisterClass);
7537 case X86::ATOMNAND8:
7538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7539 X86::AND8ri, X86::MOV8rm,
7540 X86::LCMPXCHG8, X86::MOV8rr,
7541 X86::NOT8r, X86::AL,
7542 X86::GR8RegisterClass, true);
7543 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007544 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007545 case X86::ATOMAND64:
7546 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7547 X86::AND64ri32, X86::MOV64rm,
7548 X86::LCMPXCHG64, X86::MOV64rr,
7549 X86::NOT64r, X86::RAX,
7550 X86::GR64RegisterClass);
7551 case X86::ATOMOR64:
7552 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7553 X86::OR64ri32, X86::MOV64rm,
7554 X86::LCMPXCHG64, X86::MOV64rr,
7555 X86::NOT64r, X86::RAX,
7556 X86::GR64RegisterClass);
7557 case X86::ATOMXOR64:
7558 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7559 X86::XOR64ri32, X86::MOV64rm,
7560 X86::LCMPXCHG64, X86::MOV64rr,
7561 X86::NOT64r, X86::RAX,
7562 X86::GR64RegisterClass);
7563 case X86::ATOMNAND64:
7564 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7565 X86::AND64ri32, X86::MOV64rm,
7566 X86::LCMPXCHG64, X86::MOV64rr,
7567 X86::NOT64r, X86::RAX,
7568 X86::GR64RegisterClass, true);
7569 case X86::ATOMMIN64:
7570 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7571 case X86::ATOMMAX64:
7572 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7573 case X86::ATOMUMIN64:
7574 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7575 case X86::ATOMUMAX64:
7576 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007577
7578 // This group does 64-bit operations on a 32-bit host.
7579 case X86::ATOMAND6432:
7580 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7581 X86::AND32rr, X86::AND32rr,
7582 X86::AND32ri, X86::AND32ri,
7583 false);
7584 case X86::ATOMOR6432:
7585 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7586 X86::OR32rr, X86::OR32rr,
7587 X86::OR32ri, X86::OR32ri,
7588 false);
7589 case X86::ATOMXOR6432:
7590 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7591 X86::XOR32rr, X86::XOR32rr,
7592 X86::XOR32ri, X86::XOR32ri,
7593 false);
7594 case X86::ATOMNAND6432:
7595 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7596 X86::AND32rr, X86::AND32rr,
7597 X86::AND32ri, X86::AND32ri,
7598 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007599 case X86::ATOMADD6432:
7600 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7601 X86::ADD32rr, X86::ADC32rr,
7602 X86::ADD32ri, X86::ADC32ri,
7603 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007604 case X86::ATOMSUB6432:
7605 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7606 X86::SUB32rr, X86::SBB32rr,
7607 X86::SUB32ri, X86::SBB32ri,
7608 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007609 case X86::ATOMSWAP6432:
7610 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7611 X86::MOV32rr, X86::MOV32rr,
7612 X86::MOV32ri, X86::MOV32ri,
7613 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007614 }
7615}
7616
7617//===----------------------------------------------------------------------===//
7618// X86 Optimization Hooks
7619//===----------------------------------------------------------------------===//
7620
Dan Gohman475871a2008-07-27 21:46:04 +00007621void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007622 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007623 APInt &KnownZero,
7624 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007625 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007626 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007627 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007628 assert((Opc >= ISD::BUILTIN_OP_END ||
7629 Opc == ISD::INTRINSIC_WO_CHAIN ||
7630 Opc == ISD::INTRINSIC_W_CHAIN ||
7631 Opc == ISD::INTRINSIC_VOID) &&
7632 "Should use MaskedValueIsZero if you don't know whether Op"
7633 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007634
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007635 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007636 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007637 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007638 case X86ISD::ADD:
7639 case X86ISD::SUB:
7640 case X86ISD::SMUL:
7641 case X86ISD::UMUL:
7642 // These nodes' second result is a boolean.
7643 if (Op.getResNo() == 0)
7644 break;
7645 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007646 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007647 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7648 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007649 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007650 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007651}
Chris Lattner259e97c2006-01-31 19:43:35 +00007652
Evan Cheng206ee9d2006-07-07 08:33:52 +00007653/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007654/// node is a GlobalAddress + offset.
7655bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7656 GlobalValue* &GA, int64_t &Offset) const{
7657 if (N->getOpcode() == X86ISD::Wrapper) {
7658 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007659 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007660 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007661 return true;
7662 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007663 }
Evan Chengad4196b2008-05-12 19:56:52 +00007664 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007665}
7666
Evan Chengad4196b2008-05-12 19:56:52 +00007667static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7668 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007669 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007670 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007671 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007672 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007673 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007674 return false;
7675}
7676
Dan Gohman475871a2008-07-27 21:46:04 +00007677static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007678 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00007679 SDNode *&Base,
7680 SelectionDAG &DAG, MachineFrameInfo *MFI,
7681 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007682 Base = NULL;
7683 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007684 SDValue Idx = PermMask.getOperand(i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00007685 if (Idx.getOpcode() == ISD::UNDEF) {
7686 if (!Base)
7687 return false;
7688 continue;
7689 }
7690
Dan Gohman475871a2008-07-27 21:46:04 +00007691 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007692 if (!Elt.getNode() ||
7693 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007694 return false;
7695 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007696 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007697 if (Base->getOpcode() == ISD::UNDEF)
7698 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007699 continue;
7700 }
7701 if (Elt.getOpcode() == ISD::UNDEF)
7702 continue;
7703
Gabor Greifba36cb52008-08-28 21:40:38 +00007704 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007705 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007706 return false;
7707 }
7708 return true;
7709}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007710
7711/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7712/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7713/// if the load addresses are consecutive, non-overlapping, and in the right
7714/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00007715static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00007716 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007717 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007718 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007719 MVT VT = N->getValueType(0);
7720 MVT EVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00007721 SDValue PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00007722 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007723 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00007724 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7725 DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007726 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007727
Dan Gohmand3006222007-07-27 17:16:43 +00007728 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007729 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007730 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7731 LD->getSrcValue(), LD->getSrcValueOffset(),
7732 LD->isVolatile());
7733 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7734 LD->getSrcValue(), LD->getSrcValueOffset(),
7735 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007736}
7737
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007738/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007739static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007740 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007741 const X86Subtarget *Subtarget,
7742 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007743 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007744 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007745
Evan Chengd880b972008-05-09 21:53:03 +00007746 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007747 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007748 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007749
Duncan Sands83ec4b62008-06-06 12:08:01 +00007750 MVT VT = N->getValueType(0);
7751 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00007752 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7753 // We are looking for load i64 and zero extend. We want to transform
7754 // it before legalizer has a chance to expand it. Also look for i64
7755 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00007756 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007757 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00007758 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00007759 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007760 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007761
7762 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00007763 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00007764 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007765 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00007766 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007767 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007768 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00007769 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007770 }
Evan Chengd880b972008-05-09 21:53:03 +00007771
7772 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007773 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00007774
7775 // Load must not be an extload.
7776 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00007777 return SDValue();
Mon P Wang7ad9b512009-01-30 07:07:40 +00007778
7779 // Load type should legal type so we don't have to legalize it.
7780 if (!TLI.isTypeLegal(VT))
7781 return SDValue();
7782
Evan Cheng8a186ae2008-09-24 23:26:36 +00007783 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7784 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007785 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007786 TargetLowering::TargetLoweringOpt TLO(DAG);
7787 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7788 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng8a186ae2008-09-24 23:26:36 +00007789 return ResNode;
Evan Chengd880b972008-05-09 21:53:03 +00007790}
7791
Chris Lattner83e6c992006-10-04 06:57:07 +00007792/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007793static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner83e6c992006-10-04 06:57:07 +00007794 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007795 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007796 SDValue Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007797
Chris Lattner83e6c992006-10-04 06:57:07 +00007798 // If we have SSE[12] support, try to form min/max nodes.
7799 if (Subtarget->hasSSE2() &&
7800 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7801 if (Cond.getOpcode() == ISD::SETCC) {
7802 // Get the LHS/RHS of the select.
Dan Gohman475871a2008-07-27 21:46:04 +00007803 SDValue LHS = N->getOperand(1);
7804 SDValue RHS = N->getOperand(2);
Chris Lattner83e6c992006-10-04 06:57:07 +00007805 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007806
Evan Cheng8ca29322006-11-10 21:43:37 +00007807 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00007808 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00007809 switch (CC) {
7810 default: break;
7811 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7812 case ISD::SETULE:
7813 case ISD::SETLE:
7814 if (!UnsafeFPMath) break;
7815 // FALL THROUGH.
7816 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7817 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00007818 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007819 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007820
Chris Lattner1907a7b2006-10-05 04:11:26 +00007821 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7822 case ISD::SETUGT:
7823 case ISD::SETGT:
7824 if (!UnsafeFPMath) break;
7825 // FALL THROUGH.
7826 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7827 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00007828 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007829 break;
7830 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007831 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00007832 switch (CC) {
7833 default: break;
7834 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7835 case ISD::SETUGT:
7836 case ISD::SETGT:
7837 if (!UnsafeFPMath) break;
7838 // FALL THROUGH.
7839 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7840 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00007841 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007842 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007843
Chris Lattner1907a7b2006-10-05 04:11:26 +00007844 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7845 case ISD::SETULE:
7846 case ISD::SETLE:
7847 if (!UnsafeFPMath) break;
7848 // FALL THROUGH.
7849 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7850 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00007851 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007852 break;
7853 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007854 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007855
Evan Cheng8ca29322006-11-10 21:43:37 +00007856 if (Opcode)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007857 return DAG.getNode(Opcode, dl, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007858 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007859
Chris Lattner83e6c992006-10-04 06:57:07 +00007860 }
7861
Dan Gohman475871a2008-07-27 21:46:04 +00007862 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007863}
7864
Nate Begeman740ab032009-01-26 00:52:55 +00007865/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7866/// when possible.
7867static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7868 const X86Subtarget *Subtarget) {
7869 // On X86 with SSE2 support, we can transform this to a vector shift if
7870 // all elements are shifted by the same amount. We can't do this in legalize
7871 // because the a constant vector is typically transformed to a constant pool
7872 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007873 if (!Subtarget->hasSSE2())
7874 return SDValue();
7875
Nate Begeman740ab032009-01-26 00:52:55 +00007876 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007877 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7878 return SDValue();
7879
Mon P Wang3becd092009-01-28 08:12:05 +00007880 SDValue ShAmtOp = N->getOperand(1);
7881 MVT EltVT = VT.getVectorElementType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007882 DebugLoc dl = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00007883 SDValue BaseShAmt;
7884 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7885 unsigned NumElts = VT.getVectorNumElements();
7886 unsigned i = 0;
7887 for (; i != NumElts; ++i) {
7888 SDValue Arg = ShAmtOp.getOperand(i);
7889 if (Arg.getOpcode() == ISD::UNDEF) continue;
7890 BaseShAmt = Arg;
7891 break;
7892 }
7893 for (; i != NumElts; ++i) {
7894 SDValue Arg = ShAmtOp.getOperand(i);
7895 if (Arg.getOpcode() == ISD::UNDEF) continue;
7896 if (Arg != BaseShAmt) {
7897 return SDValue();
7898 }
7899 }
7900 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7901 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007902 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ShAmtOp,
Mon P Wang3becd092009-01-28 08:12:05 +00007903 DAG.getIntPtrConstant(0));
7904 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007905 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00007906
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007907 if (EltVT.bitsGT(MVT::i32))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007908 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007909 else if (EltVT.bitsLT(MVT::i32))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007910 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00007911
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007912 // The shift amount is identical so we can do a vector shift.
7913 SDValue ValOp = N->getOperand(0);
7914 switch (N->getOpcode()) {
7915 default:
7916 assert(0 && "Unknown shift opcode!");
7917 break;
7918 case ISD::SHL:
7919 if (VT == MVT::v2i64)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007920 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007921 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7922 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007923 if (VT == MVT::v4i32)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007924 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007925 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7926 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007927 if (VT == MVT::v8i16)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007928 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007929 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7930 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007931 break;
7932 case ISD::SRA:
7933 if (VT == MVT::v4i32)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007934 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007935 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7936 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007937 if (VT == MVT::v8i16)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007938 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007939 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7940 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007941 break;
7942 case ISD::SRL:
7943 if (VT == MVT::v2i64)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007944 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007945 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7946 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007947 if (VT == MVT::v4i32)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007948 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007949 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7950 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007951 if (VT == MVT::v8i16)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007952 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007953 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7954 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007955 break;
Nate Begeman740ab032009-01-26 00:52:55 +00007956 }
7957 return SDValue();
7958}
7959
Chris Lattner149a4e52008-02-22 02:09:43 +00007960/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007961static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00007962 const X86Subtarget *Subtarget) {
7963 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7964 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00007965 // A preferable solution to the general problem is to figure out the right
7966 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00007967 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007968 if (St->getValue().getValueType().isVector() &&
7969 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00007970 isa<LoadSDNode>(St->getValue()) &&
7971 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7972 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007973 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007974 LoadSDNode *Ld = 0;
7975 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00007976 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00007977 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007978 // Must be a store of a load. We currently handle two cases: the load
7979 // is a direct child, and it's under an intervening TokenFactor. It is
7980 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00007981 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00007982 Ld = cast<LoadSDNode>(St->getChain());
7983 else if (St->getValue().hasOneUse() &&
7984 ChainVal->getOpcode() == ISD::TokenFactor) {
7985 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007986 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00007987 TokenFactorIndex = i;
7988 Ld = cast<LoadSDNode>(St->getValue());
7989 } else
7990 Ops.push_back(ChainVal->getOperand(i));
7991 }
7992 }
7993 if (Ld) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 DebugLoc dl = N->getDebugLoc();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007995 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7996 if (Subtarget->is64Bit()) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007997 SDValue NewLd = DAG.getLoad(MVT::i64, dl, Ld->getChain(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00007998 Ld->getBasePtr(), Ld->getSrcValue(),
7999 Ld->getSrcValueOffset(), Ld->isVolatile(),
8000 Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00008001 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008002 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00008003 Ops.push_back(NewChain);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008004 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008005 Ops.size());
8006 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008007 return DAG.getStore(NewChain, dl, NewLd, St->getBasePtr(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00008008 St->getSrcValue(), St->getSrcValueOffset(),
8009 St->isVolatile(), St->getAlignment());
8010 }
8011
8012 // Otherwise, lower to two 32-bit copies.
Dan Gohman475871a2008-07-27 21:46:04 +00008013 SDValue LoAddr = Ld->getBasePtr();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008014 SDValue HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008015 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00008016
Dale Johannesene4d209d2009-02-03 20:21:25 +00008017 SDValue LoLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), LoAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00008018 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8019 Ld->isVolatile(), Ld->getAlignment());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008020 SDValue HiLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00008021 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8022 Ld->isVolatile(),
8023 MinAlign(Ld->getAlignment(), 4));
8024
Dan Gohman475871a2008-07-27 21:46:04 +00008025 SDValue NewChain = LoLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008026 if (TokenFactorIndex != -1) {
8027 Ops.push_back(LoLd);
8028 Ops.push_back(HiLd);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008029 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008030 Ops.size());
8031 }
8032
8033 LoAddr = St->getBasePtr();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008034 HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008035 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00008036
Dale Johannesene4d209d2009-02-03 20:21:25 +00008037 SDValue LoSt = DAG.getStore(NewChain, dl, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00008038 St->getSrcValue(), St->getSrcValueOffset(),
8039 St->isVolatile(), St->getAlignment());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008040 SDValue HiSt = DAG.getStore(NewChain, dl, HiLd, HiAddr,
Gabor Greif327ef032008-08-28 23:19:51 +00008041 St->getSrcValue(),
8042 St->getSrcValueOffset() + 4,
Dale Johannesen079f2a62008-02-25 19:20:14 +00008043 St->isVolatile(),
8044 MinAlign(St->getAlignment(), 4));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008045 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008046 }
Chris Lattner149a4e52008-02-22 02:09:43 +00008047 }
Dan Gohman475871a2008-07-27 21:46:04 +00008048 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008049}
8050
Chris Lattner6cf73262008-01-25 06:14:17 +00008051/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8052/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008053static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008054 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8055 // F[X]OR(0.0, x) -> x
8056 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008057 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8058 if (C->getValueAPF().isPosZero())
8059 return N->getOperand(1);
8060 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8061 if (C->getValueAPF().isPosZero())
8062 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008063 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008064}
8065
8066/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008067static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008068 // FAND(0.0, x) -> 0.0
8069 // FAND(x, 0.0) -> 0.0
8070 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8071 if (C->getValueAPF().isPosZero())
8072 return N->getOperand(0);
8073 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8074 if (C->getValueAPF().isPosZero())
8075 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008076 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008077}
8078
Dan Gohmane5af2d32009-01-29 01:59:02 +00008079static SDValue PerformBTCombine(SDNode *N,
8080 SelectionDAG &DAG,
8081 TargetLowering::DAGCombinerInfo &DCI) {
8082 // BT ignores high bits in the bit index operand.
8083 SDValue Op1 = N->getOperand(1);
8084 if (Op1.hasOneUse()) {
8085 unsigned BitWidth = Op1.getValueSizeInBits();
8086 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8087 APInt KnownZero, KnownOne;
8088 TargetLowering::TargetLoweringOpt TLO(DAG);
8089 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8090 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8091 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8092 DCI.CommitTargetLoweringOpt(TLO);
8093 }
8094 return SDValue();
8095}
Chris Lattner83e6c992006-10-04 06:57:07 +00008096
Dan Gohman475871a2008-07-27 21:46:04 +00008097SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008098 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008099 SelectionDAG &DAG = DCI.DAG;
8100 switch (N->getOpcode()) {
8101 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008102 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8103 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008104 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008105 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Nate Begeman740ab032009-01-26 00:52:55 +00008106 case ISD::SHL:
8107 case ISD::SRA:
8108 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008109 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008110 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008111 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8112 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008113 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008114 }
8115
Dan Gohman475871a2008-07-27 21:46:04 +00008116 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008117}
8118
Evan Cheng60c07e12006-07-05 22:17:51 +00008119//===----------------------------------------------------------------------===//
8120// X86 Inline Assembly Support
8121//===----------------------------------------------------------------------===//
8122
Chris Lattnerf4dff842006-07-11 02:54:03 +00008123/// getConstraintType - Given a constraint letter, return the type of
8124/// constraint it is for this target.
8125X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008126X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8127 if (Constraint.size() == 1) {
8128 switch (Constraint[0]) {
8129 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008130 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008131 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008132 case 'r':
8133 case 'R':
8134 case 'l':
8135 case 'q':
8136 case 'Q':
8137 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008138 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008139 case 'Y':
8140 return C_RegisterClass;
8141 default:
8142 break;
8143 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008144 }
Chris Lattner4234f572007-03-25 02:14:49 +00008145 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008146}
8147
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008148/// LowerXConstraint - try to replace an X constraint, which matches anything,
8149/// with another that has more specific requirements based on the type of the
8150/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008151const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008152LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008153 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8154 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008155 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008156 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008157 return "Y";
8158 if (Subtarget->hasSSE1())
8159 return "x";
8160 }
8161
8162 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008163}
8164
Chris Lattner48884cd2007-08-25 00:47:38 +00008165/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8166/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008167void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008168 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008169 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008170 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008171 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008172 SDValue Result(0, 0);
Chris Lattner48884cd2007-08-25 00:47:38 +00008173
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008174 switch (Constraint) {
8175 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008176 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008177 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008178 if (C->getZExtValue() <= 31) {
8179 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008180 break;
8181 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008182 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008183 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008184 case 'J':
8185 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8186 if (C->getZExtValue() <= 63) {
8187 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8188 break;
8189 }
8190 }
8191 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008192 case 'N':
8193 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008194 if (C->getZExtValue() <= 255) {
8195 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008196 break;
8197 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008198 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008199 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008200 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008201 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008202 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008203 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008204 break;
8205 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008206
Chris Lattnerdc43a882007-05-03 16:52:29 +00008207 // If we are in non-pic codegen mode, we allow the address of a global (with
8208 // an optional displacement) to be used with 'i'.
8209 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8210 int64_t Offset = 0;
8211
8212 // Match either (GA) or (GA+C)
8213 if (GA) {
8214 Offset = GA->getOffset();
8215 } else if (Op.getOpcode() == ISD::ADD) {
8216 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8217 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8218 if (C && GA) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008219 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00008220 } else {
8221 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8222 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8223 if (C && GA)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008224 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00008225 else
8226 C = 0, GA = 0;
8227 }
8228 }
8229
8230 if (GA) {
Evan Chengda43bcf2008-09-24 00:05:32 +00008231 if (hasMemory)
Dale Johannesen33c960f2009-02-04 20:06:27 +00008232 Op = LowerGlobalAddress(GA->getGlobal(), Op.getNode()->getDebugLoc(),
8233 Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00008234 else
8235 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8236 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00008237 Result = Op;
8238 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008239 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008240
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008241 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00008242 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008243 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008244 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008245
Gabor Greifba36cb52008-08-28 21:40:38 +00008246 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008247 Ops.push_back(Result);
8248 return;
8249 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008250 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8251 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008252}
8253
Chris Lattner259e97c2006-01-31 19:43:35 +00008254std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008255getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008256 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008257 if (Constraint.size() == 1) {
8258 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008259 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008260 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008261 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8262 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008263 if (VT == MVT::i32)
8264 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8265 else if (VT == MVT::i16)
8266 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8267 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008268 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008269 else if (VT == MVT::i64)
8270 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8271 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008272 }
8273 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008274
Chris Lattner1efa40f2006-02-22 00:56:39 +00008275 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008276}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008277
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008278std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008279X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008280 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008281 // First, see if this is a constraint that directly corresponds to an LLVM
8282 // register class.
8283 if (Constraint.size() == 1) {
8284 // GCC Constraint Letters
8285 switch (Constraint[0]) {
8286 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008287 case 'r': // GENERAL_REGS
8288 case 'R': // LEGACY_REGS
8289 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008290 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008291 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008292 if (VT == MVT::i16)
8293 return std::make_pair(0U, X86::GR16RegisterClass);
8294 if (VT == MVT::i32 || !Subtarget->is64Bit())
8295 return std::make_pair(0U, X86::GR32RegisterClass);
8296 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008297 case 'f': // FP Stack registers.
8298 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8299 // value to the correct fpstack register class.
8300 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8301 return std::make_pair(0U, X86::RFP32RegisterClass);
8302 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8303 return std::make_pair(0U, X86::RFP64RegisterClass);
8304 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008305 case 'y': // MMX_REGS if MMX allowed.
8306 if (!Subtarget->hasMMX()) break;
8307 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008308 case 'Y': // SSE_REGS if SSE2 allowed
8309 if (!Subtarget->hasSSE2()) break;
8310 // FALL THROUGH.
8311 case 'x': // SSE_REGS if SSE1 allowed
8312 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008313
8314 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008315 default: break;
8316 // Scalar SSE types.
8317 case MVT::f32:
8318 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008319 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008320 case MVT::f64:
8321 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008322 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008323 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008324 case MVT::v16i8:
8325 case MVT::v8i16:
8326 case MVT::v4i32:
8327 case MVT::v2i64:
8328 case MVT::v4f32:
8329 case MVT::v2f64:
8330 return std::make_pair(0U, X86::VR128RegisterClass);
8331 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008332 break;
8333 }
8334 }
8335
Chris Lattnerf76d1802006-07-31 23:26:50 +00008336 // Use the default implementation in TargetLowering to convert the register
8337 // constraint into a member of a register class.
8338 std::pair<unsigned, const TargetRegisterClass*> Res;
8339 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008340
8341 // Not found as a standard register?
8342 if (Res.second == 0) {
8343 // GCC calls "st(0)" just plain "st".
8344 if (StringsEqualNoCase("{st}", Constraint)) {
8345 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008346 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008347 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008348 // 'A' means EAX + EDX.
8349 if (Constraint == "A") {
8350 Res.first = X86::EAX;
8351 Res.second = X86::GRADRegisterClass;
8352 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008353 return Res;
8354 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008355
Chris Lattnerf76d1802006-07-31 23:26:50 +00008356 // Otherwise, check to see if this is a register class of the wrong value
8357 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8358 // turn into {ax},{dx}.
8359 if (Res.second->hasType(VT))
8360 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008361
Chris Lattnerf76d1802006-07-31 23:26:50 +00008362 // All of the single-register GCC register classes map their values onto
8363 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8364 // really want an 8-bit or 32-bit register, map to the appropriate register
8365 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008366 if (Res.second == X86::GR16RegisterClass) {
8367 if (VT == MVT::i8) {
8368 unsigned DestReg = 0;
8369 switch (Res.first) {
8370 default: break;
8371 case X86::AX: DestReg = X86::AL; break;
8372 case X86::DX: DestReg = X86::DL; break;
8373 case X86::CX: DestReg = X86::CL; break;
8374 case X86::BX: DestReg = X86::BL; break;
8375 }
8376 if (DestReg) {
8377 Res.first = DestReg;
8378 Res.second = Res.second = X86::GR8RegisterClass;
8379 }
8380 } else if (VT == MVT::i32) {
8381 unsigned DestReg = 0;
8382 switch (Res.first) {
8383 default: break;
8384 case X86::AX: DestReg = X86::EAX; break;
8385 case X86::DX: DestReg = X86::EDX; break;
8386 case X86::CX: DestReg = X86::ECX; break;
8387 case X86::BX: DestReg = X86::EBX; break;
8388 case X86::SI: DestReg = X86::ESI; break;
8389 case X86::DI: DestReg = X86::EDI; break;
8390 case X86::BP: DestReg = X86::EBP; break;
8391 case X86::SP: DestReg = X86::ESP; break;
8392 }
8393 if (DestReg) {
8394 Res.first = DestReg;
8395 Res.second = Res.second = X86::GR32RegisterClass;
8396 }
8397 } else if (VT == MVT::i64) {
8398 unsigned DestReg = 0;
8399 switch (Res.first) {
8400 default: break;
8401 case X86::AX: DestReg = X86::RAX; break;
8402 case X86::DX: DestReg = X86::RDX; break;
8403 case X86::CX: DestReg = X86::RCX; break;
8404 case X86::BX: DestReg = X86::RBX; break;
8405 case X86::SI: DestReg = X86::RSI; break;
8406 case X86::DI: DestReg = X86::RDI; break;
8407 case X86::BP: DestReg = X86::RBP; break;
8408 case X86::SP: DestReg = X86::RSP; break;
8409 }
8410 if (DestReg) {
8411 Res.first = DestReg;
8412 Res.second = Res.second = X86::GR64RegisterClass;
8413 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008414 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008415 } else if (Res.second == X86::FR32RegisterClass ||
8416 Res.second == X86::FR64RegisterClass ||
8417 Res.second == X86::VR128RegisterClass) {
8418 // Handle references to XMM physical registers that got mapped into the
8419 // wrong class. This can happen with constraints like {xmm0} where the
8420 // target independent register mapper will just pick the first match it can
8421 // find, ignoring the required type.
8422 if (VT == MVT::f32)
8423 Res.second = X86::FR32RegisterClass;
8424 else if (VT == MVT::f64)
8425 Res.second = X86::FR64RegisterClass;
8426 else if (X86::VR128RegisterClass->hasType(VT))
8427 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008428 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008429
Chris Lattnerf76d1802006-07-31 23:26:50 +00008430 return Res;
8431}
Mon P Wang0c397192008-10-30 08:01:45 +00008432
8433//===----------------------------------------------------------------------===//
8434// X86 Widen vector type
8435//===----------------------------------------------------------------------===//
8436
8437/// getWidenVectorType: given a vector type, returns the type to widen
8438/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8439/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008440/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008441/// scalarizing vs using the wider vector type.
8442
Dan Gohmanc13cf132009-01-15 17:34:08 +00008443MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008444 assert(VT.isVector());
8445 if (isTypeLegal(VT))
8446 return VT;
8447
8448 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8449 // type based on element type. This would speed up our search (though
8450 // it may not be worth it since the size of the list is relatively
8451 // small).
8452 MVT EltVT = VT.getVectorElementType();
8453 unsigned NElts = VT.getVectorNumElements();
8454
8455 // On X86, it make sense to widen any vector wider than 1
8456 if (NElts <= 1)
8457 return MVT::Other;
8458
8459 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8460 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8461 MVT SVT = (MVT::SimpleValueType)nVT;
8462
8463 if (isTypeLegal(SVT) &&
8464 SVT.getVectorElementType() == EltVT &&
8465 SVT.getVectorNumElements() > NElts)
8466 return SVT;
8467 }
8468 return MVT::Other;
8469}