blob: e465cf3e8468b60cd104e96092d8b558518125e9 [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,
1627 DAG.getNode(ISD::UNDEF, dl, MVT::v2i64), Arg,
1628 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) {
1793 Ops.push_back(Chain);
Chris Lattnere563bbc2008-10-11 22:08:30 +00001794 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1795 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greifba36cb52008-08-28 21:40:38 +00001796 if (InFlag.getNode())
Gordon Henriksen86737662008-01-05 16:56:59 +00001797 Ops.push_back(InFlag);
1798 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1799 InFlag = Chain.getValue(1);
1800
1801 // Returns a chain & a flag for retval copy to use.
1802 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1803 Ops.clear();
1804 }
1805
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001806 Ops.push_back(Chain);
1807 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001808
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 if (IsTailCall)
1810 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001811
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 // Add argument registers to the end of the list so that they are known live
1813 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001814 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1815 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1816 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001817
Evan Cheng586ccac2008-03-18 23:36:35 +00001818 // Add an implicit use GOT pointer in EBX.
1819 if (!IsTailCall && !Is64Bit &&
1820 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1821 Subtarget->isPICStyleGOT())
1822 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1823
1824 // Add an implicit use of AL for x86 vararg functions.
1825 if (Is64Bit && isVarArg)
1826 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1827
Gabor Greifba36cb52008-08-28 21:40:38 +00001828 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001829 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001830
Gordon Henriksen86737662008-01-05 16:56:59 +00001831 if (IsTailCall) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001832 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001833 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001834 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001835 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001836
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 }
1839
Dale Johannesenace16102009-02-03 19:33:06 +00001840 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001841 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001842
Chris Lattner2d297092006-05-23 18:50:38 +00001843 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001844 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001845 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001846 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001847 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001848 // If this is is a call to a struct-return function, the callee
1849 // pops the hidden struct pointer, so we have to push it back.
1850 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001851 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001853 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001854
Gordon Henriksenae636f82008-01-03 16:47:34 +00001855 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001856 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001857 DAG.getIntPtrConstant(NumBytes, true),
1858 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1859 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001860 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001861 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001862
Chris Lattner3085e152007-02-25 08:59:22 +00001863 // Handle result values, copying them out of physregs into vregs that we
1864 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001865 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001866 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001867}
1868
Evan Cheng25ab6902006-09-08 06:48:29 +00001869
1870//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001871// Fast Calling Convention (tail call) implementation
1872//===----------------------------------------------------------------------===//
1873
1874// Like std call, callee cleans arguments, convention except that ECX is
1875// reserved for storing the tail called function address. Only 2 registers are
1876// free for argument passing (inreg). Tail call optimization is performed
1877// provided:
1878// * tailcallopt is enabled
1879// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001880// On X86_64 architecture with GOT-style position independent code only local
1881// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001882// To keep the stack aligned according to platform abi the function
1883// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1884// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001885// If a tail called function callee has more arguments than the caller the
1886// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001887// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001888// original REtADDR, but before the saved framepointer or the spilled registers
1889// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1890// stack layout:
1891// arg1
1892// arg2
1893// RETADDR
1894// [ new RETADDR
1895// move area ]
1896// (possible EBP)
1897// ESI
1898// EDI
1899// local1 ..
1900
1901/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1902/// for a 16 byte align requirement.
1903unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1904 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001905 MachineFunction &MF = DAG.getMachineFunction();
1906 const TargetMachine &TM = MF.getTarget();
1907 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1908 unsigned StackAlignment = TFI.getStackAlignment();
1909 uint64_t AlignMask = StackAlignment - 1;
1910 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001911 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001912 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1913 // Number smaller than 12 so just add the difference.
1914 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1915 } else {
1916 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1917 Offset = ((~AlignMask) & Offset) + StackAlignment +
1918 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001919 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001920 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001921}
1922
1923/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001924/// following the call is a return. A function is eligible if caller/callee
1925/// calling conventions match, currently only fastcc supports tail calls, and
1926/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001927bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001929 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001930 if (!PerformTailCallOpt)
1931 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001932
Dan Gohman095cc292008-09-13 01:54:27 +00001933 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001934 MachineFunction &MF = DAG.getMachineFunction();
1935 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001936 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001937 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001938 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001939 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001940 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001941 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001942 return true;
1943
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001944 // Can only do local tail calls (in same module, hidden or protected) on
1945 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001946 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1947 return G->getGlobal()->hasHiddenVisibility()
1948 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001949 }
1950 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001951
1952 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001953}
1954
Dan Gohman3df24e62008-09-03 23:12:08 +00001955FastISel *
1956X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001957 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001958 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001959 DenseMap<const Value *, unsigned> &vm,
1960 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001961 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001962 DenseMap<const AllocaInst *, int> &am
1963#ifndef NDEBUG
1964 , SmallSet<Instruction*, 8> &cil
1965#endif
1966 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001967 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001968#ifndef NDEBUG
1969 , cil
1970#endif
1971 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001972}
1973
1974
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001975//===----------------------------------------------------------------------===//
1976// Other Lowering Hooks
1977//===----------------------------------------------------------------------===//
1978
1979
Dan Gohman475871a2008-07-27 21:46:04 +00001980SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001981 MachineFunction &MF = DAG.getMachineFunction();
1982 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1983 int ReturnAddrIndex = FuncInfo->getRAIndex();
1984
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001985 if (ReturnAddrIndex == 0) {
1986 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00001987 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001988 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001989 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001990 }
1991
Evan Cheng25ab6902006-09-08 06:48:29 +00001992 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001993}
1994
1995
Chris Lattner1c39d4c2008-12-24 23:53:05 +00001996/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1997/// specific condition code, returning the condition code and the LHS/RHS of the
1998/// comparison to make.
1999static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2000 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002001 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002002 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2003 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2004 // X > -1 -> X == 0, jump !sign.
2005 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002006 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002007 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2008 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002009 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002010 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002011 // X < 1 -> X <= 0
2012 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002013 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002014 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002015 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002016
Evan Chengd9558e02006-01-06 00:43:03 +00002017 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002018 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002019 case ISD::SETEQ: return X86::COND_E;
2020 case ISD::SETGT: return X86::COND_G;
2021 case ISD::SETGE: return X86::COND_GE;
2022 case ISD::SETLT: return X86::COND_L;
2023 case ISD::SETLE: return X86::COND_LE;
2024 case ISD::SETNE: return X86::COND_NE;
2025 case ISD::SETULT: return X86::COND_B;
2026 case ISD::SETUGT: return X86::COND_A;
2027 case ISD::SETULE: return X86::COND_BE;
2028 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002029 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002030 }
2031
2032 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002033
Chris Lattner4c78e022008-12-23 23:42:27 +00002034 // If LHS is a foldable load, but RHS is not, flip the condition.
2035 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2036 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2037 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2038 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002039 }
2040
Chris Lattner4c78e022008-12-23 23:42:27 +00002041 switch (SetCCOpcode) {
2042 default: break;
2043 case ISD::SETOLT:
2044 case ISD::SETOLE:
2045 case ISD::SETUGT:
2046 case ISD::SETUGE:
2047 std::swap(LHS, RHS);
2048 break;
2049 }
2050
2051 // On a floating point condition, the flags are set as follows:
2052 // ZF PF CF op
2053 // 0 | 0 | 0 | X > Y
2054 // 0 | 0 | 1 | X < Y
2055 // 1 | 0 | 0 | X == Y
2056 // 1 | 1 | 1 | unordered
2057 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002058 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002059 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002060 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002061 case ISD::SETOLT: // flipped
2062 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002063 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002064 case ISD::SETOLE: // flipped
2065 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002066 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002067 case ISD::SETUGT: // flipped
2068 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002069 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002070 case ISD::SETUGE: // flipped
2071 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002072 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002073 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002074 case ISD::SETNE: return X86::COND_NE;
2075 case ISD::SETUO: return X86::COND_P;
2076 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002077 }
Evan Chengd9558e02006-01-06 00:43:03 +00002078}
2079
Evan Cheng4a460802006-01-11 00:33:36 +00002080/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2081/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002082/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002083static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002084 switch (X86CC) {
2085 default:
2086 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002087 case X86::COND_B:
2088 case X86::COND_BE:
2089 case X86::COND_E:
2090 case X86::COND_P:
2091 case X86::COND_A:
2092 case X86::COND_AE:
2093 case X86::COND_NE:
2094 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002095 return true;
2096 }
2097}
2098
Evan Cheng5ced1d82006-04-06 23:23:56 +00002099/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002100/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman475871a2008-07-27 21:46:04 +00002101static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002102 if (Op.getOpcode() == ISD::UNDEF)
2103 return true;
2104
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002105 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002106 return (Val >= Low && Val < Hi);
2107}
2108
2109/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2110/// true if Op is undef or if its value equal to the specified value.
Dan Gohman475871a2008-07-27 21:46:04 +00002111static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Evan Chengc5cdff22006-04-07 21:53:05 +00002112 if (Op.getOpcode() == ISD::UNDEF)
2113 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002114 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002115}
2116
Evan Cheng0188ecb2006-03-22 18:59:22 +00002117/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2118/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2119bool X86::isPSHUFDMask(SDNode *N) {
2120 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2121
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002122 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002123 return false;
2124
2125 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002126 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002127 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002128 if (Arg.getOpcode() == ISD::UNDEF) continue;
2129 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002130 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002131 return false;
2132 }
2133
2134 return true;
2135}
2136
2137/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002138/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002139bool X86::isPSHUFHWMask(SDNode *N) {
2140 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2141
2142 if (N->getNumOperands() != 8)
2143 return false;
2144
2145 // Lower quadword copied in order.
2146 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002147 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002148 if (Arg.getOpcode() == ISD::UNDEF) continue;
2149 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002150 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002151 return false;
2152 }
2153
2154 // Upper quadword shuffled.
2155 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002156 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002157 if (Arg.getOpcode() == ISD::UNDEF) continue;
2158 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002159 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002160 if (Val < 4 || Val > 7)
2161 return false;
2162 }
2163
2164 return true;
2165}
2166
2167/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002168/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002169bool X86::isPSHUFLWMask(SDNode *N) {
2170 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2171
2172 if (N->getNumOperands() != 8)
2173 return false;
2174
2175 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002176 for (unsigned i = 4; i != 8; ++i)
2177 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002178 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002179
2180 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002181 for (unsigned i = 0; i != 4; ++i)
2182 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002183 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002184
2185 return true;
2186}
2187
Evan Cheng14aed5e2006-03-24 01:18:28 +00002188/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2189/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Dan Gohmane7852d02009-01-26 04:35:06 +00002190template<class SDOperand>
2191static bool isSHUFPMask(SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002192 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002193
Evan Cheng39623da2006-04-20 08:58:49 +00002194 unsigned Half = NumElems / 2;
2195 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002196 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002197 return false;
2198 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002199 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002200 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002201
2202 return true;
2203}
2204
Evan Cheng39623da2006-04-20 08:58:49 +00002205bool X86::isSHUFPMask(SDNode *N) {
2206 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002207 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002208}
2209
Evan Cheng213d2cf2007-05-17 18:45:50 +00002210/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002211/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2212/// half elements to come from vector 1 (which would equal the dest.) and
2213/// the upper half to come from vector 2.
Dan Gohmane7852d02009-01-26 04:35:06 +00002214template<class SDOperand>
2215static bool isCommutedSHUFP(SDOperand *Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002216 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002217
Chris Lattner5a88b832007-02-25 07:10:00 +00002218 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002219 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002220 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002221 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002222 for (unsigned i = Half; i < NumOps; ++i)
2223 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002224 return false;
2225 return true;
2226}
2227
2228static bool isCommutedSHUFP(SDNode *N) {
2229 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002230 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002231}
2232
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002233/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2234/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2235bool X86::isMOVHLPSMask(SDNode *N) {
2236 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2237
Evan Cheng2064a2b2006-03-28 06:50:32 +00002238 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002239 return false;
2240
Evan Cheng2064a2b2006-03-28 06:50:32 +00002241 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002242 return isUndefOrEqual(N->getOperand(0), 6) &&
2243 isUndefOrEqual(N->getOperand(1), 7) &&
2244 isUndefOrEqual(N->getOperand(2), 2) &&
2245 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002246}
2247
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002248/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2249/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2250/// <2, 3, 2, 3>
2251bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2252 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2253
2254 if (N->getNumOperands() != 4)
2255 return false;
2256
2257 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2258 return isUndefOrEqual(N->getOperand(0), 2) &&
2259 isUndefOrEqual(N->getOperand(1), 3) &&
2260 isUndefOrEqual(N->getOperand(2), 2) &&
2261 isUndefOrEqual(N->getOperand(3), 3);
2262}
2263
Evan Cheng5ced1d82006-04-06 23:23:56 +00002264/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2265/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2266bool X86::isMOVLPMask(SDNode *N) {
2267 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2268
2269 unsigned NumElems = N->getNumOperands();
2270 if (NumElems != 2 && NumElems != 4)
2271 return false;
2272
Evan Chengc5cdff22006-04-07 21:53:05 +00002273 for (unsigned i = 0; i < NumElems/2; ++i)
2274 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2275 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002276
Evan Chengc5cdff22006-04-07 21:53:05 +00002277 for (unsigned i = NumElems/2; i < NumElems; ++i)
2278 if (!isUndefOrEqual(N->getOperand(i), i))
2279 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002280
2281 return true;
2282}
2283
2284/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002285/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2286/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002287bool X86::isMOVHPMask(SDNode *N) {
2288 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2289
2290 unsigned NumElems = N->getNumOperands();
2291 if (NumElems != 2 && NumElems != 4)
2292 return false;
2293
Evan Chengc5cdff22006-04-07 21:53:05 +00002294 for (unsigned i = 0; i < NumElems/2; ++i)
2295 if (!isUndefOrEqual(N->getOperand(i), i))
2296 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002297
2298 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002299 SDValue Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002300 if (!isUndefOrEqual(Arg, i + NumElems))
2301 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002302 }
2303
2304 return true;
2305}
2306
Evan Cheng0038e592006-03-28 00:39:58 +00002307/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2308/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Dan Gohmane7852d02009-01-26 04:35:06 +00002309template<class SDOperand>
2310bool static isUNPCKLMask(SDOperand *Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002311 bool V2IsSplat = false) {
2312 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002313 return false;
2314
Chris Lattner5a88b832007-02-25 07:10:00 +00002315 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002316 SDValue BitI = Elts[i];
2317 SDValue BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002318 if (!isUndefOrEqual(BitI, j))
2319 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002320 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002321 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002322 return false;
2323 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002324 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002325 return false;
2326 }
Evan Cheng0038e592006-03-28 00:39:58 +00002327 }
2328
2329 return true;
2330}
2331
Evan Cheng39623da2006-04-20 08:58:49 +00002332bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2333 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002334 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002335}
2336
Evan Cheng4fcb9222006-03-28 02:43:26 +00002337/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2338/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Dan Gohmane7852d02009-01-26 04:35:06 +00002339template<class SDOperand>
2340bool static isUNPCKHMask(SDOperand *Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002341 bool V2IsSplat = false) {
2342 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002343 return false;
2344
Chris Lattner5a88b832007-02-25 07:10:00 +00002345 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SDValue BitI = Elts[i];
2347 SDValue BitI1 = Elts[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002348 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002349 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002350 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002351 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002352 return false;
2353 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002354 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002355 return false;
2356 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002357 }
2358
2359 return true;
2360}
2361
Evan Cheng39623da2006-04-20 08:58:49 +00002362bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2363 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002364 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002365}
2366
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002367/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2368/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2369/// <0, 0, 1, 1>
2370bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2371 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2372
2373 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002374 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002375 return false;
2376
2377 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002378 SDValue BitI = N->getOperand(i);
2379 SDValue BitI1 = N->getOperand(i+1);
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002380
Evan Chengc5cdff22006-04-07 21:53:05 +00002381 if (!isUndefOrEqual(BitI, j))
2382 return false;
2383 if (!isUndefOrEqual(BitI1, j))
2384 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002385 }
2386
2387 return true;
2388}
2389
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002390/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2391/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2392/// <2, 2, 3, 3>
2393bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2394 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2395
2396 unsigned NumElems = N->getNumOperands();
2397 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2398 return false;
2399
2400 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002401 SDValue BitI = N->getOperand(i);
2402 SDValue BitI1 = N->getOperand(i + 1);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002403
2404 if (!isUndefOrEqual(BitI, j))
2405 return false;
2406 if (!isUndefOrEqual(BitI1, j))
2407 return false;
2408 }
2409
2410 return true;
2411}
2412
Evan Cheng017dcc62006-04-21 01:05:10 +00002413/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2414/// specifies a shuffle of elements that is suitable for input to MOVSS,
2415/// MOVSD, and MOVD, i.e. setting the lowest element.
Dan Gohmane7852d02009-01-26 04:35:06 +00002416template<class SDOperand>
2417static bool isMOVLMask(SDOperand *Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002418 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002419 return false;
2420
Chris Lattner5a88b832007-02-25 07:10:00 +00002421 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002422 return false;
2423
Chris Lattner5a88b832007-02-25 07:10:00 +00002424 for (unsigned i = 1; i < NumElts; ++i) {
2425 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002426 return false;
2427 }
2428
2429 return true;
2430}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002431
Evan Cheng017dcc62006-04-21 01:05:10 +00002432bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002433 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002434 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002435}
2436
Evan Cheng017dcc62006-04-21 01:05:10 +00002437/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2438/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002439/// element of vector 2 and the other elements to come from vector 1 in order.
Dan Gohmane7852d02009-01-26 04:35:06 +00002440template<class SDOperand>
2441static bool isCommutedMOVL(SDOperand *Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002442 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002443 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002444 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002445 return false;
2446
2447 if (!isUndefOrEqual(Ops[0], 0))
2448 return false;
2449
Chris Lattner5a88b832007-02-25 07:10:00 +00002450 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002451 SDValue Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002452 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2453 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2454 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002455 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002456 }
2457
2458 return true;
2459}
2460
Evan Cheng8cf723d2006-09-08 01:50:06 +00002461static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2462 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002463 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002464 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2465 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002466}
2467
Evan Chengd9539472006-04-14 21:59:03 +00002468/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2469/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2470bool X86::isMOVSHDUPMask(SDNode *N) {
2471 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2472
2473 if (N->getNumOperands() != 4)
2474 return false;
2475
2476 // Expect 1, 1, 3, 3
2477 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002478 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002479 if (Arg.getOpcode() == ISD::UNDEF) continue;
2480 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002481 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002482 if (Val != 1) return false;
2483 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002484
2485 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002486 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002487 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002488 if (Arg.getOpcode() == ISD::UNDEF) continue;
2489 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002490 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002491 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002492 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002493 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002494
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002495 // Don't use movshdup if it can be done with a shufps.
2496 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002497}
2498
2499/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2500/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2501bool X86::isMOVSLDUPMask(SDNode *N) {
2502 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2503
2504 if (N->getNumOperands() != 4)
2505 return false;
2506
2507 // Expect 0, 0, 2, 2
2508 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002509 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002510 if (Arg.getOpcode() == ISD::UNDEF) continue;
2511 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002512 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002513 if (Val != 0) return false;
2514 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002515
2516 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002517 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002518 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002519 if (Arg.getOpcode() == ISD::UNDEF) continue;
2520 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002521 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002522 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002523 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002524 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002525
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002526 // Don't use movshdup if it can be done with a shufps.
2527 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002528}
2529
Evan Cheng49892af2007-06-19 00:02:56 +00002530/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2531/// specifies a identity operation on the LHS or RHS.
2532static bool isIdentityMask(SDNode *N, bool RHS = false) {
2533 unsigned NumElems = N->getNumOperands();
2534 for (unsigned i = 0; i < NumElems; ++i)
2535 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2536 return false;
2537 return true;
2538}
2539
Evan Chengb9df0ca2006-03-22 02:53:00 +00002540/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2541/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002542static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002543 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2544
Evan Chengb9df0ca2006-03-22 02:53:00 +00002545 // This is a splat operation if each element of the permute is the same, and
2546 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002547 unsigned NumElems = N->getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002548 SDValue ElementBase;
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002549 unsigned i = 0;
2550 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002551 SDValue Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002552 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002553 ElementBase = Elt;
2554 break;
2555 }
2556 }
2557
Gabor Greifba36cb52008-08-28 21:40:38 +00002558 if (!ElementBase.getNode())
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002559 return false;
2560
2561 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002562 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002563 if (Arg.getOpcode() == ISD::UNDEF) continue;
2564 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002565 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002566 }
2567
2568 // Make sure it is a splat of the first vector operand.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002569 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002570}
2571
Mon P Wang62c75ea2008-12-23 04:03:27 +00002572/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2573/// we want to splat.
2574static SDValue getSplatMaskEltNo(SDNode *N) {
2575 assert(isSplatMask(N) && "Not a splat mask");
2576 unsigned NumElems = N->getNumOperands();
2577 SDValue ElementBase;
2578 unsigned i = 0;
2579 for (; i != NumElems; ++i) {
2580 SDValue Elt = N->getOperand(i);
2581 if (isa<ConstantSDNode>(Elt))
2582 return Elt;
2583 }
2584 assert(0 && " No splat value found!");
2585 return SDValue();
2586}
2587
2588
Evan Chengc575ca22006-04-17 20:43:08 +00002589/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2590/// a splat of a single element and it's a 2 or 4 element mask.
2591bool X86::isSplatMask(SDNode *N) {
2592 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2593
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002594 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002595 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2596 return false;
2597 return ::isSplatMask(N);
2598}
2599
Evan Chengf686d9b2006-10-27 21:08:32 +00002600/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2601/// specifies a splat of zero element.
2602bool X86::isSplatLoMask(SDNode *N) {
2603 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2604
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002605 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002606 if (!isUndefOrEqual(N->getOperand(i), 0))
2607 return false;
2608 return true;
2609}
2610
Evan Cheng0b457f02008-09-25 20:50:48 +00002611/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2612/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2613bool X86::isMOVDDUPMask(SDNode *N) {
2614 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2615
2616 unsigned e = N->getNumOperands() / 2;
2617 for (unsigned i = 0; i < e; ++i)
2618 if (!isUndefOrEqual(N->getOperand(i), i))
2619 return false;
2620 for (unsigned i = 0; i < e; ++i)
2621 if (!isUndefOrEqual(N->getOperand(e+i), i))
2622 return false;
2623 return true;
2624}
2625
Evan Cheng63d33002006-03-22 08:01:21 +00002626/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2627/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2628/// instructions.
2629unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002630 unsigned NumOperands = N->getNumOperands();
2631 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2632 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002633 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002634 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002635 SDValue Arg = N->getOperand(NumOperands-i-1);
Evan Chengef698ca2006-03-31 00:30:29 +00002636 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002637 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002638 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002639 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002640 if (i != NumOperands - 1)
2641 Mask <<= Shift;
2642 }
Evan Cheng63d33002006-03-22 08:01:21 +00002643
2644 return Mask;
2645}
2646
Evan Cheng506d3df2006-03-29 23:07:14 +00002647/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2648/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2649/// instructions.
2650unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2651 unsigned Mask = 0;
2652 // 8 nodes, but we only care about the last 4.
2653 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002654 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002655 SDValue Arg = N->getOperand(i);
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002656 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002657 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002658 Mask |= (Val - 4);
2659 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002660 if (i != 4)
2661 Mask <<= 2;
2662 }
2663
2664 return Mask;
2665}
2666
2667/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2668/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2669/// instructions.
2670unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2671 unsigned Mask = 0;
2672 // 8 nodes, but we only care about the first 4.
2673 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002674 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002675 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002676 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002677 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002678 Mask |= Val;
2679 if (i != 0)
2680 Mask <<= 2;
2681 }
2682
2683 return Mask;
2684}
2685
Evan Chengc21a0532006-04-05 01:47:37 +00002686/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2687/// specifies a 8 element shuffle that can be broken into a pair of
2688/// PSHUFHW and PSHUFLW.
2689static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2690 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2691
2692 if (N->getNumOperands() != 8)
2693 return false;
2694
2695 // Lower quadword shuffled.
2696 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002697 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002698 if (Arg.getOpcode() == ISD::UNDEF) continue;
2699 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002700 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002701 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002702 return false;
2703 }
2704
2705 // Upper quadword shuffled.
2706 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002707 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002708 if (Arg.getOpcode() == ISD::UNDEF) continue;
2709 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002710 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengc21a0532006-04-05 01:47:37 +00002711 if (Val < 4 || Val > 7)
2712 return false;
2713 }
2714
2715 return true;
2716}
2717
Chris Lattner8a594482007-11-25 00:24:49 +00002718/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002719/// values in ther permute mask.
Dan Gohman475871a2008-07-27 21:46:04 +00002720static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2721 SDValue &V2, SDValue &Mask,
Evan Cheng9eca5e82006-10-25 21:49:50 +00002722 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002723 MVT VT = Op.getValueType();
2724 MVT MaskVT = Mask.getValueType();
2725 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002726 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002727 SmallVector<SDValue, 8> MaskVec;
Dale Johannesenace16102009-02-03 19:33:06 +00002728 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002729
2730 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002731 SDValue Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002732 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesenace16102009-02-03 19:33:06 +00002733 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Cheng80d428c2006-04-19 22:48:17 +00002734 continue;
2735 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002736 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002737 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002738 if (Val < NumElems)
2739 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2740 else
2741 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2742 }
2743
Evan Cheng9eca5e82006-10-25 21:49:50 +00002744 std::swap(V1, V2);
Dale Johannesenace16102009-02-03 19:33:06 +00002745 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
2746 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002747}
2748
Evan Cheng779ccea2007-12-07 21:30:01 +00002749/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2750/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002751static
Dale Johannesenace16102009-02-03 19:33:06 +00002752SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002753 MVT MaskVT = Mask.getValueType();
2754 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002755 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002756 SmallVector<SDValue, 8> MaskVec;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002757 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002758 SDValue Arg = Mask.getOperand(i);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002759 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesenace16102009-02-03 19:33:06 +00002760 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, EltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002761 continue;
2762 }
2763 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002764 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002765 if (Val < NumElems)
2766 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2767 else
2768 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2769 }
Dale Johannesenace16102009-02-03 19:33:06 +00002770 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], NumElems);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002771}
2772
2773
Evan Cheng533a0aa2006-04-19 20:35:22 +00002774/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2775/// match movhlps. The lower half elements should come from upper half of
2776/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002777/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002778static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2779 unsigned NumElems = Mask->getNumOperands();
2780 if (NumElems != 4)
2781 return false;
2782 for (unsigned i = 0, e = 2; i != e; ++i)
2783 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2784 return false;
2785 for (unsigned i = 2; i != 4; ++i)
2786 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2787 return false;
2788 return true;
2789}
2790
Evan Cheng5ced1d82006-04-06 23:23:56 +00002791/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002792/// is promoted to a vector. It also returns the LoadSDNode by reference if
2793/// required.
2794static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002795 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2796 return false;
2797 N = N->getOperand(0).getNode();
2798 if (!ISD::isNON_EXTLoad(N))
2799 return false;
2800 if (LD)
2801 *LD = cast<LoadSDNode>(N);
2802 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002803}
2804
Evan Cheng533a0aa2006-04-19 20:35:22 +00002805/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2806/// match movlp{s|d}. The lower half elements should come from lower half of
2807/// V1 (and in order), and the upper half elements should come from the upper
2808/// half of V2 (and in order). And since V1 will become the source of the
2809/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002810static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002811 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002812 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002813 // Is V2 is a vector load, don't do this transformation. We will try to use
2814 // load folding shufps op.
2815 if (ISD::isNON_EXTLoad(V2))
2816 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002817
Evan Cheng533a0aa2006-04-19 20:35:22 +00002818 unsigned NumElems = Mask->getNumOperands();
2819 if (NumElems != 2 && NumElems != 4)
2820 return false;
2821 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2822 if (!isUndefOrEqual(Mask->getOperand(i), i))
2823 return false;
2824 for (unsigned i = NumElems/2; i != NumElems; ++i)
2825 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2826 return false;
2827 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002828}
2829
Evan Cheng39623da2006-04-20 08:58:49 +00002830/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2831/// all the same.
2832static bool isSplatVector(SDNode *N) {
2833 if (N->getOpcode() != ISD::BUILD_VECTOR)
2834 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002835
Dan Gohman475871a2008-07-27 21:46:04 +00002836 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002837 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2838 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002839 return false;
2840 return true;
2841}
2842
Evan Cheng8cf723d2006-09-08 01:50:06 +00002843/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2844/// to an undef.
2845static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002846 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002847 return false;
2848
Dan Gohman475871a2008-07-27 21:46:04 +00002849 SDValue V1 = N->getOperand(0);
2850 SDValue V2 = N->getOperand(1);
2851 SDValue Mask = N->getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002852 unsigned NumElems = Mask.getNumOperands();
2853 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002854 SDValue Arg = Mask.getOperand(i);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002855 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002856 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8cf723d2006-09-08 01:50:06 +00002857 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2858 return false;
2859 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2860 return false;
2861 }
2862 }
2863 return true;
2864}
2865
Evan Cheng213d2cf2007-05-17 18:45:50 +00002866/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2867/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002868static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002869 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002870 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002871 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002872 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002873}
2874
2875/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2876/// to an zero vector.
2877static bool isZeroShuffle(SDNode *N) {
2878 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2879 return false;
2880
Dan Gohman475871a2008-07-27 21:46:04 +00002881 SDValue V1 = N->getOperand(0);
2882 SDValue V2 = N->getOperand(1);
2883 SDValue Mask = N->getOperand(2);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002884 unsigned NumElems = Mask.getNumOperands();
2885 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002886 SDValue Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002887 if (Arg.getOpcode() == ISD::UNDEF)
2888 continue;
2889
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002890 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattner8a594482007-11-25 00:24:49 +00002891 if (Idx < NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002892 unsigned Opc = V1.getNode()->getOpcode();
2893 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002894 continue;
2895 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002896 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002897 return false;
2898 } else if (Idx >= NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002899 unsigned Opc = V2.getNode()->getOpcode();
2900 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002901 continue;
2902 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002903 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattner8a594482007-11-25 00:24:49 +00002904 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002905 }
2906 }
2907 return true;
2908}
2909
2910/// getZeroVector - Returns a vector of specified type with all zero elements.
2911///
Dale Johannesenace16102009-02-03 19:33:06 +00002912static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2913 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002914 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002915
2916 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2917 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002918 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002919 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002920 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00002921 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002922 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002923 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00002924 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002925 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002926 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Dale Johannesenace16102009-02-03 19:33:06 +00002927 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002928 }
Dale Johannesenace16102009-02-03 19:33:06 +00002929 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002930}
2931
Chris Lattner8a594482007-11-25 00:24:49 +00002932/// getOnesVector - Returns a vector of specified type with all bits set.
2933///
Dale Johannesenace16102009-02-03 19:33:06 +00002934static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002935 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002936
2937 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2938 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002939 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2940 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002941 if (VT.getSizeInBits() == 64) // MMX
Dale Johannesenace16102009-02-03 19:33:06 +00002942 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002943 else // SSE
Dale Johannesenace16102009-02-03 19:33:06 +00002944 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
2945 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002946}
2947
2948
Evan Cheng39623da2006-04-20 08:58:49 +00002949/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2950/// that point to V2 points to its first element.
Dan Gohman475871a2008-07-27 21:46:04 +00002951static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002952 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2953
2954 bool Changed = false;
Dan Gohman475871a2008-07-27 21:46:04 +00002955 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002956 unsigned NumElems = Mask.getNumOperands();
2957 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002958 SDValue Arg = Mask.getOperand(i);
Evan Cheng39623da2006-04-20 08:58:49 +00002959 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002960 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng39623da2006-04-20 08:58:49 +00002961 if (Val > NumElems) {
2962 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2963 Changed = true;
2964 }
2965 }
2966 MaskVec.push_back(Arg);
2967 }
2968
2969 if (Changed)
Dale Johannesenace16102009-02-03 19:33:06 +00002970 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getNode()->getDebugLoc(),
2971 Mask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002972 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002973 return Mask;
2974}
2975
Evan Cheng017dcc62006-04-21 01:05:10 +00002976/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2977/// operation of specified width.
Dale Johannesenace16102009-02-03 19:33:06 +00002978static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002979 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2980 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002981
Dan Gohman475871a2008-07-27 21:46:04 +00002982 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002983 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2984 for (unsigned i = 1; i != NumElems; ++i)
2985 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Dale Johannesenace16102009-02-03 19:33:06 +00002986 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
2987 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002988}
2989
Evan Chengc575ca22006-04-17 20:43:08 +00002990/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2991/// of specified width.
Dale Johannesenace16102009-02-03 19:33:06 +00002992static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG,
2993 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002994 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2995 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002996 SmallVector<SDValue, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002997 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2998 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2999 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
3000 }
Dale Johannesenace16102009-02-03 19:33:06 +00003001 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3002 &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00003003}
3004
Evan Cheng39623da2006-04-20 08:58:49 +00003005/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
3006/// of specified width.
Dale Johannesenace16102009-02-03 19:33:06 +00003007static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG,
3008 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003009 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3010 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00003011 unsigned Half = NumElems/2;
Dan Gohman475871a2008-07-27 21:46:04 +00003012 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00003013 for (unsigned i = 0; i != Half; ++i) {
3014 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
3015 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
3016 }
Dale Johannesenace16102009-02-03 19:33:06 +00003017 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3018 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00003019}
3020
Chris Lattner62098042008-03-09 01:05:04 +00003021/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
3022/// element #0 of a vector with the specified index, leaving the rest of the
3023/// elements in place.
Dan Gohman475871a2008-07-27 21:46:04 +00003024static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Dale Johannesenace16102009-02-03 19:33:06 +00003025 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003026 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3027 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003028 SmallVector<SDValue, 8> MaskVec;
Chris Lattner62098042008-03-09 01:05:04 +00003029 // Element #0 of the result gets the elt we are replacing.
3030 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
3031 for (unsigned i = 1; i != NumElems; ++i)
3032 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003033 return DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3034 &MaskVec[0], MaskVec.size());
Chris Lattner62098042008-03-09 01:05:04 +00003035}
3036
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003037/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman475871a2008-07-27 21:46:04 +00003038static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003039 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
3040 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003041 if (PVT == VT)
3042 return Op;
Dan Gohman475871a2008-07-27 21:46:04 +00003043 SDValue V1 = Op.getOperand(0);
3044 SDValue Mask = Op.getOperand(2);
Mon P Wang62c75ea2008-12-23 04:03:27 +00003045 unsigned MaskNumElems = Mask.getNumOperands();
3046 unsigned NumElems = MaskNumElems;
Dale Johannesenace16102009-02-03 19:33:06 +00003047 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003048 // Special handling of v4f32 -> v4i32.
3049 if (VT != MVT::v4f32) {
Mon P Wang62c75ea2008-12-23 04:03:27 +00003050 // Find which element we want to splat.
3051 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3052 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3053 // unpack elements to the correct location
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003054 while (NumElems > 4) {
Mon P Wang62c75ea2008-12-23 04:03:27 +00003055 if (EltNo < NumElems/2) {
Dale Johannesenace16102009-02-03 19:33:06 +00003056 Mask = getUnpacklMask(MaskNumElems, DAG, dl);
Mon P Wang62c75ea2008-12-23 04:03:27 +00003057 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00003058 Mask = getUnpackhMask(MaskNumElems, DAG, dl);
Mon P Wang62c75ea2008-12-23 04:03:27 +00003059 EltNo -= NumElems/2;
3060 }
Dale Johannesenace16102009-02-03 19:33:06 +00003061 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1, Mask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003062 NumElems >>= 1;
3063 }
Mon P Wang62c75ea2008-12-23 04:03:27 +00003064 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00003065 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengc575ca22006-04-17 20:43:08 +00003066 }
Evan Chengc575ca22006-04-17 20:43:08 +00003067
Dale Johannesenace16102009-02-03 19:33:06 +00003068 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3069 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003070 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00003071 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Chengc575ca22006-04-17 20:43:08 +00003072}
3073
Evan Cheng0b457f02008-09-25 20:50:48 +00003074/// isVectorLoad - Returns true if the node is a vector load, a scalar
3075/// load that's promoted to vector, or a load bitcasted.
3076static bool isVectorLoad(SDValue Op) {
3077 assert(Op.getValueType().isVector() && "Expected a vector type");
3078 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3079 Op.getOpcode() == ISD::BIT_CONVERT) {
3080 return isa<LoadSDNode>(Op.getOperand(0));
3081 }
3082 return isa<LoadSDNode>(Op);
3083}
3084
3085
3086/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3087///
3088static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3089 SelectionDAG &DAG, bool HasSSE3) {
3090 // If we have sse3 and shuffle has more than one use or input is a load, then
3091 // use movddup. Otherwise, use movlhps.
3092 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3093 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3094 MVT VT = Op.getValueType();
3095 if (VT == PVT)
3096 return Op;
Dale Johannesenace16102009-02-03 19:33:06 +00003097 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0b457f02008-09-25 20:50:48 +00003098 unsigned NumElems = PVT.getVectorNumElements();
3099 if (NumElems == 2) {
3100 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00003101 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng0b457f02008-09-25 20:50:48 +00003102 } else {
3103 assert(NumElems == 4);
3104 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3105 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
Dale Johannesenace16102009-02-03 19:33:06 +00003106 Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
3107 Cst0, Cst1, Cst0, Cst1);
Evan Cheng0b457f02008-09-25 20:50:48 +00003108 }
3109
Dale Johannesenace16102009-02-03 19:33:06 +00003110 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
3111 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, PVT, V1,
3112 DAG.getNode(ISD::UNDEF, dl, PVT), Mask);
3113 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuffle);
Evan Cheng0b457f02008-09-25 20:50:48 +00003114}
3115
Evan Chengba05f722006-04-21 23:03:30 +00003116/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003117/// vector of zero or undef vector. This produces a shuffle where the low
3118/// element of V2 is swizzled into the zero/undef vector, landing at element
3119/// 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 +00003120static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003121 bool isZero, bool HasSSE2,
3122 SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00003123 DebugLoc dl = V2.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003124 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003125 SDValue V1 = isZero
Dale Johannesenace16102009-02-03 19:33:06 +00003126 ? getZeroVector(VT, HasSSE2, DAG, dl) : DAG.getNode(ISD::UNDEF, dl, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003127 unsigned NumElems = V2.getValueType().getVectorNumElements();
3128 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3129 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003130 SmallVector<SDValue, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003131 for (unsigned i = 0; i != NumElems; ++i)
3132 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3133 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3134 else
3135 MaskVec.push_back(DAG.getConstant(i, EVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003136 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003137 &MaskVec[0], MaskVec.size());
Dale Johannesenace16102009-02-03 19:33:06 +00003138 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003139}
3140
Evan Chengf26ffe92008-05-29 08:22:04 +00003141/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3142/// a shuffle that is zero.
3143static
Dan Gohman475871a2008-07-27 21:46:04 +00003144unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengf26ffe92008-05-29 08:22:04 +00003145 unsigned NumElems, bool Low,
3146 SelectionDAG &DAG) {
3147 unsigned NumZeros = 0;
3148 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003149 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman475871a2008-07-27 21:46:04 +00003150 SDValue Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00003151 if (Idx.getOpcode() == ISD::UNDEF) {
3152 ++NumZeros;
3153 continue;
3154 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003155 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3156 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003157 ++NumZeros;
3158 else
3159 break;
3160 }
3161 return NumZeros;
3162}
3163
3164/// isVectorShift - Returns true if the shuffle can be implemented as a
3165/// logical left or right shift of a vector.
Dan Gohman475871a2008-07-27 21:46:04 +00003166static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3167 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003168 unsigned NumElems = Mask.getNumOperands();
3169
3170 isLeft = true;
3171 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3172 if (!NumZeros) {
3173 isLeft = false;
3174 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3175 if (!NumZeros)
3176 return false;
3177 }
3178
3179 bool SeenV1 = false;
3180 bool SeenV2 = false;
3181 for (unsigned i = NumZeros; i < NumElems; ++i) {
3182 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman475871a2008-07-27 21:46:04 +00003183 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengf26ffe92008-05-29 08:22:04 +00003184 if (Idx.getOpcode() == ISD::UNDEF)
3185 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003186 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengf26ffe92008-05-29 08:22:04 +00003187 if (Index < NumElems)
3188 SeenV1 = true;
3189 else {
3190 Index -= NumElems;
3191 SeenV2 = true;
3192 }
3193 if (Index != Val)
3194 return false;
3195 }
3196 if (SeenV1 && SeenV2)
3197 return false;
3198
3199 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3200 ShAmt = NumZeros;
3201 return true;
3202}
3203
3204
Evan Chengc78d3b42006-04-24 18:01:45 +00003205/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3206///
Dan Gohman475871a2008-07-27 21:46:04 +00003207static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003208 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003209 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003210 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003211 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003212
Dale Johannesenace16102009-02-03 19:33:06 +00003213 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003214 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003215 bool First = true;
3216 for (unsigned i = 0; i < 16; ++i) {
3217 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3218 if (ThisIsNonZero && First) {
3219 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003220 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003221 else
Dale Johannesenace16102009-02-03 19:33:06 +00003222 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003223 First = false;
3224 }
3225
3226 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003227 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003228 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3229 if (LastIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003230 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
3231 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003232 }
3233 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003234 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3235 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00003236 ThisElt, DAG.getConstant(8, MVT::i8));
3237 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003238 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003239 } else
3240 ThisElt = LastElt;
3241
Gabor Greifba36cb52008-08-28 21:40:38 +00003242 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003243 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003244 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003245 }
3246 }
3247
Dale Johannesenace16102009-02-03 19:33:06 +00003248 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003249}
3250
Bill Wendlinga348c562007-03-22 18:42:45 +00003251/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003252///
Dan Gohman475871a2008-07-27 21:46:04 +00003253static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003254 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003255 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003256 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003257 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003258
Dale Johannesenace16102009-02-03 19:33:06 +00003259 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003260 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003261 bool First = true;
3262 for (unsigned i = 0; i < 8; ++i) {
3263 bool isNonZero = (NonZeros & (1 << i)) != 0;
3264 if (isNonZero) {
3265 if (First) {
3266 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003267 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003268 else
Dale Johannesenace16102009-02-03 19:33:06 +00003269 V = DAG.getNode(ISD::UNDEF, dl, MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003270 First = false;
3271 }
Dale Johannesenace16102009-02-03 19:33:06 +00003272 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
3273 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003274 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003275 }
3276 }
3277
3278 return V;
3279}
3280
Evan Chengf26ffe92008-05-29 08:22:04 +00003281/// getVShift - Return a vector logical shift node.
3282///
Dan Gohman475871a2008-07-27 21:46:04 +00003283static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003284 unsigned NumBits, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003285 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003286 bool isMMX = VT.getSizeInBits() == 64;
3287 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003288 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003289 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3290 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3291 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003292 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003293}
3294
Dan Gohman475871a2008-07-27 21:46:04 +00003295SDValue
3296X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00003297 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003298 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003299 if (ISD::isBuildVectorAllZeros(Op.getNode())
3300 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003301 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3302 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3303 // eliminated on x86-32 hosts.
3304 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3305 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003306
Gabor Greifba36cb52008-08-28 21:40:38 +00003307 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003308 return getOnesVector(Op.getValueType(), DAG, dl);
3309 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003310 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003311
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 MVT VT = Op.getValueType();
3313 MVT EVT = VT.getVectorElementType();
3314 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003315
3316 unsigned NumElems = Op.getNumOperands();
3317 unsigned NumZero = 0;
3318 unsigned NumNonZero = 0;
3319 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003320 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003321 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003322 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003323 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003324 if (Elt.getOpcode() == ISD::UNDEF)
3325 continue;
3326 Values.insert(Elt);
3327 if (Elt.getOpcode() != ISD::Constant &&
3328 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003329 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003330 if (isZeroNode(Elt))
3331 NumZero++;
3332 else {
3333 NonZeros |= (1 << i);
3334 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003335 }
3336 }
3337
Dan Gohman7f321562007-06-25 16:23:39 +00003338 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003339 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesenace16102009-02-03 19:33:06 +00003340 return DAG.getNode(ISD::UNDEF, dl, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003341 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003342
Chris Lattner67f453a2008-03-09 05:42:06 +00003343 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003344 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003345 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003347
Chris Lattner62098042008-03-09 01:05:04 +00003348 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3349 // the value are obviously zero, truncate the value to i32 and do the
3350 // insertion that way. Only do this if the value is non-constant or if the
3351 // value is a constant being inserted into element 0. It is cheaper to do
3352 // a constant pool load than it is to do a movd + shuffle.
3353 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3354 (!IsAllConstants || Idx == 0)) {
3355 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3356 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003357 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3358 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003359
3360 // Truncate the value (which may itself be a constant) to i32, and
3361 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003362 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3363 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003364 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3365 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003366
3367 // Now we have our 32-bit value zero extended in the low element of
3368 // a vector. If Idx != 0, swizzle it into place.
3369 if (Idx != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003370 SDValue Ops[] = {
Dale Johannesenace16102009-02-03 19:33:06 +00003371 Item, DAG.getNode(ISD::UNDEF, dl, Item.getValueType()),
3372 getSwapEltZeroMask(VecElts, Idx, DAG, dl)
Chris Lattner62098042008-03-09 01:05:04 +00003373 };
Dale Johannesenace16102009-02-03 19:33:06 +00003374 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VecVT, Ops, 3);
Chris Lattner62098042008-03-09 01:05:04 +00003375 }
Dale Johannesenace16102009-02-03 19:33:06 +00003376 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003377 }
3378 }
3379
Chris Lattner19f79692008-03-08 22:59:52 +00003380 // If we have a constant or non-constant insertion into the low element of
3381 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3382 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3383 // depending on what the source datatype is. Because we can only get here
3384 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3385 if (Idx == 0 &&
3386 // Don't do this for i64 values on x86-32.
3387 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003388 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003389 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003390 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3391 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003392 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003393
3394 // Is it a vector logical left shift?
3395 if (NumElems == 2 && Idx == 1 &&
3396 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003397 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003398 return getVShift(true, VT,
3399 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003400 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003401 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003402
3403 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003404 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003405
Chris Lattner19f79692008-03-08 22:59:52 +00003406 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3407 // is a non-constant being inserted into an element other than the low one,
3408 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3409 // movd/movss) to move this into the low element, then shuffle it into
3410 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003411 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003412 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003413
Evan Cheng0db9fe62006-04-25 20:13:52 +00003414 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003415 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3416 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003417 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3418 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003419 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003420 for (unsigned i = 0; i < NumElems; i++)
3421 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003422 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003423 &MaskVec[0], MaskVec.size());
Dale Johannesenace16102009-02-03 19:33:06 +00003424 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, Item,
Evan Cheng0db9fe62006-04-25 20:13:52 +00003425 DAG.getNode(ISD::UNDEF, VT), Mask);
3426 }
3427 }
3428
Chris Lattner67f453a2008-03-09 05:42:06 +00003429 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3430 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003431 return SDValue();
Chris Lattner67f453a2008-03-09 05:42:06 +00003432
Dan Gohmana3941172007-07-24 22:55:08 +00003433 // A vector full of immediates; various special cases are already
3434 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003435 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003436 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003437
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003438 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003439 if (EVTBits == 64) {
3440 if (NumNonZero == 1) {
3441 // One half is zero or undef.
3442 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003443 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003444 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003445 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3446 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003447 }
Dan Gohman475871a2008-07-27 21:46:04 +00003448 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003449 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003450
3451 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003452 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003453 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003454 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003455 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003456 }
3457
Bill Wendling826f36f2007-03-28 00:57:11 +00003458 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003460 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003461 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003462 }
3463
3464 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003465 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003466 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003467 if (NumElems == 4 && NumZero > 0) {
3468 for (unsigned i = 0; i < 4; ++i) {
3469 bool isZero = !(NonZeros & (1 << i));
3470 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003471 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003472 else
Dale Johannesenace16102009-02-03 19:33:06 +00003473 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003474 }
3475
3476 for (unsigned i = 0; i < 2; ++i) {
3477 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3478 default: break;
3479 case 0:
3480 V[i] = V[i*2]; // Must be a zero vector.
3481 break;
3482 case 1:
Dale Johannesenace16102009-02-03 19:33:06 +00003483 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2+1], V[i*2],
3484 getMOVLMask(NumElems, DAG, dl));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003485 break;
3486 case 2:
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 getMOVLMask(NumElems, DAG, dl));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003489 break;
3490 case 3:
Dale Johannesenace16102009-02-03 19:33:06 +00003491 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i*2], V[i*2+1],
3492 getUnpacklMask(NumElems, DAG, dl));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003493 break;
3494 }
3495 }
3496
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3498 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003499 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003500 bool Reverse = (NonZeros & 0x3) == 2;
3501 for (unsigned i = 0; i < 2; ++i)
3502 if (Reverse)
3503 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3504 else
3505 MaskVec.push_back(DAG.getConstant(i, EVT));
3506 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3507 for (unsigned i = 0; i < 2; ++i)
3508 if (Reverse)
3509 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3510 else
3511 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003512 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00003513 &MaskVec[0], MaskVec.size());
Dale Johannesenace16102009-02-03 19:33:06 +00003514 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[0], V[1], ShufMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003515 }
3516
3517 if (Values.size() > 2) {
3518 // Expand into a number of unpckl*.
3519 // e.g. for v4f32
3520 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3521 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3522 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dale Johannesenace16102009-02-03 19:33:06 +00003523 SDValue UnpckMask = getUnpacklMask(NumElems, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003524 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003525 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003526 NumElems >>= 1;
3527 while (NumElems != 0) {
3528 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003529 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V[i], V[i + NumElems],
Evan Cheng0db9fe62006-04-25 20:13:52 +00003530 UnpckMask);
3531 NumElems >>= 1;
3532 }
3533 return V[0];
3534 }
3535
Dan Gohman475871a2008-07-27 21:46:04 +00003536 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003537}
3538
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003539static
Dan Gohman475871a2008-07-27 21:46:04 +00003540SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendlinge85dc492008-08-21 22:35:37 +00003541 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003542 TargetLowering &TLI, DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00003543 SDValue NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3545 MVT MaskEVT = MaskVT.getVectorElementType();
3546 MVT PtrVT = TLI.getPointerTy();
Gabor Greifba36cb52008-08-28 21:40:38 +00003547 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3548 PermMask.getNode()->op_end());
Evan Cheng14b32e12007-12-11 01:46:18 +00003549
3550 // First record which half of which vector the low elements come from.
3551 SmallVector<unsigned, 4> LowQuad(4);
3552 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003553 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003554 if (Elt.getOpcode() == ISD::UNDEF)
3555 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003556 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003557 int QuadIdx = EltIdx / 4;
3558 ++LowQuad[QuadIdx];
3559 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003560
Evan Cheng14b32e12007-12-11 01:46:18 +00003561 int BestLowQuad = -1;
3562 unsigned MaxQuad = 1;
3563 for (unsigned i = 0; i < 4; ++i) {
3564 if (LowQuad[i] > MaxQuad) {
3565 BestLowQuad = i;
3566 MaxQuad = LowQuad[i];
3567 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003568 }
3569
Evan Cheng14b32e12007-12-11 01:46:18 +00003570 // Record which half of which vector the high elements come from.
3571 SmallVector<unsigned, 4> HighQuad(4);
3572 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003573 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003574 if (Elt.getOpcode() == ISD::UNDEF)
3575 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003576 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003577 int QuadIdx = EltIdx / 4;
3578 ++HighQuad[QuadIdx];
3579 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003580
Evan Cheng14b32e12007-12-11 01:46:18 +00003581 int BestHighQuad = -1;
3582 MaxQuad = 1;
3583 for (unsigned i = 0; i < 4; ++i) {
3584 if (HighQuad[i] > MaxQuad) {
3585 BestHighQuad = i;
3586 MaxQuad = HighQuad[i];
3587 }
3588 }
3589
3590 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3591 if (BestLowQuad != -1 || BestHighQuad != -1) {
3592 // First sort the 4 chunks in order using shufpd.
Dan Gohman475871a2008-07-27 21:46:04 +00003593 SmallVector<SDValue, 8> MaskVec;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003594
Evan Cheng14b32e12007-12-11 01:46:18 +00003595 if (BestLowQuad != -1)
3596 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3597 else
3598 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003599
Evan Cheng14b32e12007-12-11 01:46:18 +00003600 if (BestHighQuad != -1)
3601 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3602 else
3603 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003604
Dale Johannesenace16102009-02-03 19:33:06 +00003605 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, &MaskVec[0],2);
3606 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2i64,
3607 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3608 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), Mask);
3609 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003610
3611 // Now sort high and low parts separately.
3612 BitVector InOrder(8);
3613 if (BestLowQuad != -1) {
3614 // Sort lower half in order using PSHUFLW.
3615 MaskVec.clear();
3616 bool AnyOutOrder = false;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003617
Evan Cheng14b32e12007-12-11 01:46:18 +00003618 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003619 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003620 if (Elt.getOpcode() == ISD::UNDEF) {
3621 MaskVec.push_back(Elt);
3622 InOrder.set(i);
3623 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003624 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003625 if (EltIdx != i)
3626 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003627
Evan Cheng14b32e12007-12-11 01:46:18 +00003628 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003629
Evan Cheng14b32e12007-12-11 01:46:18 +00003630 // If this element is in the right place after this shuffle, then
3631 // remember it.
3632 if ((int)(EltIdx / 4) == BestLowQuad)
3633 InOrder.set(i);
3634 }
3635 }
3636 if (AnyOutOrder) {
3637 for (unsigned i = 4; i != 8; ++i)
3638 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003639 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
3640 &MaskVec[0], 8);
3641 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3642 NewV, NewV, Mask);
Evan Cheng14b32e12007-12-11 01:46:18 +00003643 }
3644 }
3645
3646 if (BestHighQuad != -1) {
3647 // Sort high half in order using PSHUFHW if possible.
3648 MaskVec.clear();
Bill Wendlinge85dc492008-08-21 22:35:37 +00003649
Evan Cheng14b32e12007-12-11 01:46:18 +00003650 for (unsigned i = 0; i != 4; ++i)
3651 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003652
Evan Cheng14b32e12007-12-11 01:46:18 +00003653 bool AnyOutOrder = false;
3654 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003655 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003656 if (Elt.getOpcode() == ISD::UNDEF) {
3657 MaskVec.push_back(Elt);
3658 InOrder.set(i);
3659 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003660 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003661 if (EltIdx != i)
3662 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003663
Evan Cheng14b32e12007-12-11 01:46:18 +00003664 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003665
Evan Cheng14b32e12007-12-11 01:46:18 +00003666 // If this element is in the right place after this shuffle, then
3667 // remember it.
3668 if ((int)(EltIdx / 4) == BestHighQuad)
3669 InOrder.set(i);
3670 }
3671 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003672
Evan Cheng14b32e12007-12-11 01:46:18 +00003673 if (AnyOutOrder) {
Dale Johannesenace16102009-02-03 19:33:06 +00003674 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl,
3675 MaskVT, &MaskVec[0], 8);
3676 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16,
3677 NewV, NewV, Mask);
Evan Cheng14b32e12007-12-11 01:46:18 +00003678 }
3679 }
3680
3681 // The other elements are put in the right place using pextrw and pinsrw.
3682 for (unsigned i = 0; i != 8; ++i) {
3683 if (InOrder[i])
3684 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003685 SDValue Elt = MaskElts[i];
Bill Wendlingae0218c2008-08-21 22:36:36 +00003686 if (Elt.getOpcode() == ISD::UNDEF)
3687 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003688 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003689 SDValue ExtOp = (EltIdx < 8)
Dale Johannesenace16102009-02-03 19:33:06 +00003690 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003691 DAG.getConstant(EltIdx, PtrVT))
Dale Johannesenace16102009-02-03 19:33:06 +00003692 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003693 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003694 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng14b32e12007-12-11 01:46:18 +00003695 DAG.getConstant(i, PtrVT));
3696 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003697
Evan Cheng14b32e12007-12-11 01:46:18 +00003698 return NewV;
3699 }
3700
Bill Wendlinge85dc492008-08-21 22:35:37 +00003701 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3702 // few as possible. First, let's find out how many elements are already in the
3703 // right order.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003704 unsigned V1InOrder = 0;
3705 unsigned V1FromV1 = 0;
3706 unsigned V2InOrder = 0;
3707 unsigned V2FromV2 = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003708 SmallVector<SDValue, 8> V1Elts;
3709 SmallVector<SDValue, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003710 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003711 SDValue Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003712 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003713 V1Elts.push_back(Elt);
3714 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003715 ++V1InOrder;
3716 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003717 continue;
3718 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003719 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003720 if (EltIdx == i) {
3721 V1Elts.push_back(Elt);
3722 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3723 ++V1InOrder;
3724 } else if (EltIdx == i+8) {
3725 V1Elts.push_back(Elt);
3726 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3727 ++V2InOrder;
3728 } else if (EltIdx < 8) {
3729 V1Elts.push_back(Elt);
Mon P Wange91a0002009-01-28 23:11:14 +00003730 V2Elts.push_back(DAG.getConstant(EltIdx+8, MaskEVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003731 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003732 } else {
Mon P Wang62c75ea2008-12-23 04:03:27 +00003733 V1Elts.push_back(Elt);
Evan Cheng14b32e12007-12-11 01:46:18 +00003734 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3735 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003736 }
3737 }
3738
3739 if (V2InOrder > V1InOrder) {
Dale Johannesenace16102009-02-03 19:33:06 +00003740 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003741 std::swap(V1, V2);
3742 std::swap(V1Elts, V2Elts);
3743 std::swap(V1FromV1, V2FromV2);
3744 }
3745
Evan Cheng14b32e12007-12-11 01:46:18 +00003746 if ((V1FromV1 + V1InOrder) != 8) {
3747 // Some elements are from V2.
3748 if (V1FromV1) {
3749 // If there are elements that are from V1 but out of place,
3750 // then first sort them in place
Dan Gohman475871a2008-07-27 21:46:04 +00003751 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003752 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003753 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003754 if (Elt.getOpcode() == ISD::UNDEF) {
Dale Johannesenace16102009-02-03 19:33:06 +00003755 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003756 continue;
3757 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003758 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003759 if (EltIdx >= 8)
Dale Johannesenace16102009-02-03 19:33:06 +00003760 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003761 else
3762 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3763 }
Dale Johannesenace16102009-02-03 19:33:06 +00003764 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &MaskVec[0], 8);
3765 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003766 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003767
3768 NewV = V1;
3769 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003770 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003771 if (Elt.getOpcode() == ISD::UNDEF)
3772 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003773 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003774 if (EltIdx < 8)
3775 continue;
Dale Johannesenace16102009-02-03 19:33:06 +00003776 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003777 DAG.getConstant(EltIdx - 8, PtrVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003778 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng14b32e12007-12-11 01:46:18 +00003779 DAG.getConstant(i, PtrVT));
3780 }
3781 return NewV;
3782 } else {
3783 // All elements are from V1.
3784 NewV = V1;
3785 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003786 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003787 if (Elt.getOpcode() == ISD::UNDEF)
3788 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003789 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dale Johannesenace16102009-02-03 19:33:06 +00003790 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003791 DAG.getConstant(EltIdx, PtrVT));
Dale Johannesenace16102009-02-03 19:33:06 +00003792 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Evan Cheng14b32e12007-12-11 01:46:18 +00003793 DAG.getConstant(i, PtrVT));
3794 }
3795 return NewV;
3796 }
3797}
3798
Evan Cheng7a831ce2007-12-15 03:00:47 +00003799/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3800/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3801/// done when every pair / quad of shuffle mask elements point to elements in
3802/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003803/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3804static
Dan Gohman475871a2008-07-27 21:46:04 +00003805SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003806 MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SDValue PermMask, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003808 TargetLowering &TLI, DebugLoc dl) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003809 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003810 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003811 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003812 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003813 MVT NewVT = MaskVT;
3814 switch (VT.getSimpleVT()) {
3815 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003816 case MVT::v4f32: NewVT = MVT::v2f64; break;
3817 case MVT::v4i32: NewVT = MVT::v2i64; break;
3818 case MVT::v8i16: NewVT = MVT::v4i32; break;
3819 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003820 }
3821
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003822 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003823 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003824 NewVT = MVT::v2i64;
3825 else
3826 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003827 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003828 unsigned Scale = NumElems / NewWidth;
Dan Gohman475871a2008-07-27 21:46:04 +00003829 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003830 for (unsigned i = 0; i < NumElems; i += Scale) {
3831 unsigned StartIdx = ~0U;
3832 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003833 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng14b32e12007-12-11 01:46:18 +00003834 if (Elt.getOpcode() == ISD::UNDEF)
3835 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003836 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003837 if (StartIdx == ~0U)
3838 StartIdx = EltIdx - (EltIdx % Scale);
3839 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003840 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003841 }
3842 if (StartIdx == ~0U)
Dale Johannesenace16102009-02-03 19:33:06 +00003843 MaskVec.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEltVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003844 else
Duncan Sandsd038e042008-07-21 10:20:31 +00003845 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003846 }
3847
Dale Johannesenace16102009-02-03 19:33:06 +00003848 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3849 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
3850 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, NewVT, V1, V2,
3851 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Cheng7a831ce2007-12-15 03:00:47 +00003852 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003853}
3854
Evan Chengd880b972008-05-09 21:53:03 +00003855/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003856///
Dan Gohman475871a2008-07-27 21:46:04 +00003857static SDValue getVZextMovL(MVT VT, MVT OpVT,
3858 SDValue SrcOp, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003859 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003860 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3861 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003862 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003863 LD = dyn_cast<LoadSDNode>(SrcOp);
3864 if (!LD) {
3865 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3866 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003867 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003868 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3869 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3870 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3871 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3872 // PR2108
3873 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003874 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3875 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3876 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3877 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003878 SrcOp.getOperand(0)
3879 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003880 }
3881 }
3882 }
3883
Dale Johannesenace16102009-02-03 19:33:06 +00003884 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3885 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3886 DAG.getNode(ISD::BIT_CONVERT, dl,
3887 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003888}
3889
Evan Chengace3c172008-07-22 21:13:36 +00003890/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3891/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003892static SDValue
3893LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
Dale Johannesenace16102009-02-03 19:33:06 +00003894 SDValue PermMask, MVT VT, SelectionDAG &DAG,
3895 DebugLoc dl) {
Evan Chengace3c172008-07-22 21:13:36 +00003896 MVT MaskVT = PermMask.getValueType();
3897 MVT MaskEVT = MaskVT.getVectorElementType();
3898 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003899 Locs.resize(4);
Dale Johannesenace16102009-02-03 19:33:06 +00003900 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003901 unsigned NumHi = 0;
3902 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003903 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003904 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003905 if (Elt.getOpcode() == ISD::UNDEF) {
3906 Locs[i] = std::make_pair(-1, -1);
3907 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003908 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmand0859942008-08-04 23:09:15 +00003909 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengace3c172008-07-22 21:13:36 +00003910 if (Val < 4) {
3911 Locs[i] = std::make_pair(0, NumLo);
3912 Mask1[NumLo] = Elt;
3913 NumLo++;
3914 } else {
3915 Locs[i] = std::make_pair(1, NumHi);
3916 if (2+NumHi < 4)
3917 Mask1[2+NumHi] = Elt;
3918 NumHi++;
3919 }
3920 }
3921 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003922
Evan Chengace3c172008-07-22 21:13:36 +00003923 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003924 // If no more than two elements come from either vector. This can be
3925 // implemented with two shuffles. First shuffle gather the elements.
3926 // The second shuffle, which takes the first shuffle as both of its
3927 // vector operands, put the elements into the right order.
Dale Johannesenace16102009-02-03 19:33:06 +00003928 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3929 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00003930 &Mask1[0], Mask1.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003931
Dale Johannesenace16102009-02-03 19:33:06 +00003932 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003933 for (unsigned i = 0; i != 4; ++i) {
3934 if (Locs[i].first == -1)
3935 continue;
3936 else {
3937 unsigned Idx = (i < 2) ? 0 : 4;
3938 Idx += Locs[i].first * 2 + Locs[i].second;
3939 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3940 }
3941 }
3942
Dale Johannesenace16102009-02-03 19:33:06 +00003943 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V1,
3944 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00003945 &Mask2[0], Mask2.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003946 } else if (NumLo == 3 || NumHi == 3) {
3947 // Otherwise, we must have three elements from one vector, call it X, and
3948 // one element from the other, call it Y. First, use a shufps to build an
3949 // intermediate vector with the one element from Y and the element from X
3950 // that will be in the same half in the final destination (the indexes don't
3951 // matter). Then, use a shufps to build the final vector, taking the half
3952 // containing the element from Y from the intermediate, and the other half
3953 // from X.
3954 if (NumHi == 3) {
3955 // Normalize it so the 3 elements come from V1.
Dale Johannesenace16102009-02-03 19:33:06 +00003956 PermMask = CommuteVectorShuffleMask(PermMask, DAG, dl);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003957 std::swap(V1, V2);
3958 }
3959
3960 // Find the element from V2.
3961 unsigned HiIndex;
3962 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman475871a2008-07-27 21:46:04 +00003963 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003964 if (Elt.getOpcode() == ISD::UNDEF)
3965 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003966 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003967 if (Val >= 4)
3968 break;
3969 }
3970
3971 Mask1[0] = PermMask.getOperand(HiIndex);
Dale Johannesenace16102009-02-03 19:33:06 +00003972 Mask1[1] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003973 Mask1[2] = PermMask.getOperand(HiIndex^1);
Dale Johannesenace16102009-02-03 19:33:06 +00003974 Mask1[3] = DAG.getNode(ISD::UNDEF, dl, MaskEVT);
3975 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
Dale Johannesened2eee62009-02-06 01:31:28 +00003976 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT, &Mask1[0], 4));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003977
3978 if (HiIndex >= 2) {
3979 Mask1[0] = PermMask.getOperand(0);
3980 Mask1[1] = PermMask.getOperand(1);
3981 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3982 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
Dale Johannesenace16102009-02-03 19:33:06 +00003983 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
3984 DAG.getNode(ISD::BUILD_VECTOR, dl,
3985 MaskVT, &Mask1[0], 4));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003986 } else {
3987 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3988 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3989 Mask1[2] = PermMask.getOperand(2);
3990 Mask1[3] = PermMask.getOperand(3);
3991 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003992 Mask1[2] =
3993 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3994 MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003995 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003996 Mask1[3] =
3997 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3998 MaskEVT);
Dale Johannesenace16102009-02-03 19:33:06 +00003999 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V2, V1,
4000 DAG.getNode(ISD::BUILD_VECTOR, dl,
4001 MaskVT, &Mask1[0], 4));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004002 }
Evan Chengace3c172008-07-22 21:13:36 +00004003 }
4004
4005 // Break it into (shuffle shuffle_hi, shuffle_lo).
4006 Locs.clear();
Dan Gohman475871a2008-07-27 21:46:04 +00004007 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4008 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
4009 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004010 unsigned MaskIdx = 0;
4011 unsigned LoIdx = 0;
4012 unsigned HiIdx = 2;
4013 for (unsigned i = 0; i != 4; ++i) {
4014 if (i == 2) {
4015 MaskPtr = &HiMask;
4016 MaskIdx = 1;
4017 LoIdx = 0;
4018 HiIdx = 2;
4019 }
Dan Gohman475871a2008-07-27 21:46:04 +00004020 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00004021 if (Elt.getOpcode() == ISD::UNDEF) {
4022 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004023 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004024 Locs[i] = std::make_pair(MaskIdx, LoIdx);
4025 (*MaskPtr)[LoIdx] = Elt;
4026 LoIdx++;
4027 } else {
4028 Locs[i] = std::make_pair(MaskIdx, HiIdx);
4029 (*MaskPtr)[HiIdx] = Elt;
4030 HiIdx++;
4031 }
4032 }
4033
Dale Johannesenace16102009-02-03 19:33:06 +00004034 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4035 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00004036 &LoMask[0], LoMask.size()));
Dale Johannesenace16102009-02-03 19:33:06 +00004037 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2,
4038 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00004039 &HiMask[0], HiMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00004040 SmallVector<SDValue, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004041 for (unsigned i = 0; i != 4; ++i) {
4042 if (Locs[i].first == -1) {
Dale Johannesenace16102009-02-03 19:33:06 +00004043 MaskOps.push_back(DAG.getNode(ISD::UNDEF, dl, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00004044 } else {
4045 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
4046 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
4047 }
4048 }
Dale Johannesenace16102009-02-03 19:33:06 +00004049 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, LoShuffle, HiShuffle,
4050 DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Evan Chengace3c172008-07-22 21:13:36 +00004051 &MaskOps[0], MaskOps.size()));
4052}
4053
Dan Gohman475871a2008-07-27 21:46:04 +00004054SDValue
4055X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
4056 SDValue V1 = Op.getOperand(0);
4057 SDValue V2 = Op.getOperand(1);
4058 SDValue PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059 MVT VT = Op.getValueType();
Dale Johannesenace16102009-02-03 19:33:06 +00004060 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004061 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004062 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4064 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004065 bool V1IsSplat = false;
4066 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004067
Gabor Greifba36cb52008-08-28 21:40:38 +00004068 if (isUndefShuffle(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004069 return DAG.getNode(ISD::UNDEF, dl, VT);
Evan Cheng8cf723d2006-09-08 01:50:06 +00004070
Gabor Greifba36cb52008-08-28 21:40:38 +00004071 if (isZeroShuffle(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004072 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004073
Gabor Greifba36cb52008-08-28 21:40:38 +00004074 if (isIdentityMask(PermMask.getNode()))
Evan Cheng49892af2007-06-19 00:02:56 +00004075 return V1;
Gabor Greifba36cb52008-08-28 21:40:38 +00004076 else if (isIdentityMask(PermMask.getNode(), true))
Evan Cheng49892af2007-06-19 00:02:56 +00004077 return V2;
4078
Evan Cheng4dcc8a32008-09-25 23:35:16 +00004079 // Canonicalize movddup shuffles.
4080 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Cheng882cdfd2008-10-06 21:13:08 +00004081 VT.getSizeInBits() == 128 &&
Evan Cheng4dcc8a32008-09-25 23:35:16 +00004082 X86::isMOVDDUPMask(PermMask.getNode()))
4083 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4084
Gabor Greifba36cb52008-08-28 21:40:38 +00004085 if (isSplatMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004086 if (isMMX || NumElems < 4) return Op;
4087 // Promote it to a v4{if}32 splat.
4088 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004089 }
4090
Evan Cheng7a831ce2007-12-15 03:00:47 +00004091 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4092 // do it!
4093 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004094 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG,
4095 *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004096 if (NewOp.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00004097 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4098 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00004099 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4100 // FIXME: Figure out a cleaner way to do this.
4101 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004102 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004103 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesenace16102009-02-03 19:33:06 +00004104 DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004105 if (NewOp.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004106 SDValue NewV1 = NewOp.getOperand(0);
4107 SDValue NewV2 = NewOp.getOperand(1);
4108 SDValue NewMask = NewOp.getOperand(2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004109 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004110 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00004111 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget,
4112 dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004113 }
4114 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004115 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004116 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Dale Johannesenace16102009-02-03 19:33:06 +00004117 DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004118 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chengd880b972008-05-09 21:53:03 +00004119 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Dale Johannesenace16102009-02-03 19:33:06 +00004120 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004121 }
4122 }
4123
Evan Chengf26ffe92008-05-29 08:22:04 +00004124 // Check if this can be converted into a logical shift.
4125 bool isLeft = false;
4126 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SDValue ShVal;
Evan Chengf26ffe92008-05-29 08:22:04 +00004128 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4129 if (isShift && ShVal.hasOneUse()) {
4130 // If the shifted value has multiple uses, it may be cheaper to use
4131 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004132 MVT EVT = VT.getVectorElementType();
4133 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004134 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004135 }
4136
Gabor Greifba36cb52008-08-28 21:40:38 +00004137 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004138 if (V1IsUndef)
4139 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004140 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004141 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004142 if (!isMMX)
4143 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004144 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004145
Gabor Greifba36cb52008-08-28 21:40:38 +00004146 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4147 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4148 X86::isMOVHLPSMask(PermMask.getNode()) ||
4149 X86::isMOVHPMask(PermMask.getNode()) ||
4150 X86::isMOVLPMask(PermMask.getNode())))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004151 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004152
Gabor Greifba36cb52008-08-28 21:40:38 +00004153 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4154 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004155 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004156
Evan Chengf26ffe92008-05-29 08:22:04 +00004157 if (isShift) {
4158 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004159 MVT EVT = VT.getVectorElementType();
4160 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004161 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004162 }
4163
Evan Cheng9eca5e82006-10-25 21:49:50 +00004164 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004165 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4166 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004167 V1IsSplat = isSplatVector(V1.getNode());
4168 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattner8a594482007-11-25 00:24:49 +00004169
4170 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004171 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00004172 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004173 std::swap(V1IsSplat, V2IsSplat);
4174 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004175 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004176 }
4177
Evan Cheng7a831ce2007-12-15 03:00:47 +00004178 // FIXME: Figure out a cleaner way to do this.
Gabor Greifba36cb52008-08-28 21:40:38 +00004179 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Evan Cheng9bbbb982006-10-25 20:48:19 +00004180 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004181 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004182 if (V2IsSplat) {
4183 // V2 is a splat, so the mask may be malformed. That is, it may point
4184 // to any V2 element. The instruction selectior won't like this. Get
4185 // a corrected mask and commute to form a proper MOVS{S|D}.
Dale Johannesenace16102009-02-03 19:33:06 +00004186 SDValue NewMask = getMOVLMask(NumElems, DAG, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004187 if (NewMask.getNode() != PermMask.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00004188 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004189 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00004190 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00004191 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004192
Gabor Greifba36cb52008-08-28 21:40:38 +00004193 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4194 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4195 X86::isUNPCKLMask(PermMask.getNode()) ||
4196 X86::isUNPCKHMask(PermMask.getNode()))
Evan Chengd9b8e402006-10-16 06:36:00 +00004197 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004198
Evan Cheng9bbbb982006-10-25 20:48:19 +00004199 if (V2IsSplat) {
4200 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004201 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004202 // new vector_shuffle with the corrected mask.
Dan Gohman475871a2008-07-27 21:46:04 +00004203 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004204 if (NewMask.getNode() != PermMask.getNode()) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004205 if (X86::isUNPCKLMask(NewMask.getNode(), true)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004206 SDValue NewMask = getUnpacklMask(NumElems, DAG, dl);
4207 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Mon P Wang7bcaefa2009-02-04 01:16:59 +00004208 } else if (X86::isUNPCKHMask(NewMask.getNode(), true)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004209 SDValue NewMask = getUnpackhMask(NumElems, DAG, dl);
4210 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004211 }
4212 }
4213 }
4214
4215 // Normalize the node to match x86 shuffle ops if needed
Gabor Greifba36cb52008-08-28 21:40:38 +00004216 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004217 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4218
4219 if (Commuted) {
4220 // Commute is back and try unpck* again.
4221 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004222 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4223 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4224 X86::isUNPCKLMask(PermMask.getNode()) ||
4225 X86::isUNPCKHMask(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004226 return Op;
4227 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004228
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004229 // Try PSHUF* first, then SHUFP*.
4230 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4231 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greifba36cb52008-08-28 21:40:38 +00004232 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004233 if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesenace16102009-02-03 19:33:06 +00004234 return DAG.getNode(ISD::VECTOR_SHUFFLE, dl, VT, V1,
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004235 DAG.getNode(ISD::UNDEF, VT), PermMask);
4236 return Op;
4237 }
4238
4239 if (!isMMX) {
4240 if (Subtarget->hasSSE2() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004241 (X86::isPSHUFDMask(PermMask.getNode()) ||
4242 X86::isPSHUFHWMask(PermMask.getNode()) ||
4243 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004244 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004245 if (VT == MVT::v4f32) {
4246 RVT = MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004247 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT,
4248 DAG.getNode(ISD::BIT_CONVERT, dl, RVT, V1),
4249 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004250 } else if (V2.getOpcode() != ISD::UNDEF)
Dale Johannesenace16102009-02-03 19:33:06 +00004251 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, RVT, V1,
4252 DAG.getNode(ISD::UNDEF, dl, RVT), PermMask);
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004253 if (RVT != VT)
Dale Johannesenace16102009-02-03 19:33:06 +00004254 Op = DAG.getNode(ISD::BIT_CONVERT, dl, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004255 return Op;
4256 }
4257
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004258 // Binary or unary shufps.
Gabor Greifba36cb52008-08-28 21:40:38 +00004259 if (X86::isSHUFPMask(PermMask.getNode()) ||
4260 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Evan Cheng0db9fe62006-04-25 20:13:52 +00004261 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004262 }
4263
Evan Cheng14b32e12007-12-11 01:46:18 +00004264 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4265 if (VT == MVT::v8i16) {
Dale Johannesenace16102009-02-03 19:33:06 +00004266 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004267 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004268 return NewOp;
4269 }
4270
Evan Chengace3c172008-07-22 21:13:36 +00004271 // Handle all 4 wide cases with a number of shuffles except for MMX.
4272 if (NumElems == 4 && !isMMX)
Dale Johannesenace16102009-02-03 19:33:06 +00004273 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004274
Dan Gohman475871a2008-07-27 21:46:04 +00004275 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004276}
4277
Dan Gohman475871a2008-07-27 21:46:04 +00004278SDValue
4279X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004280 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004281 MVT VT = Op.getValueType();
Dale Johannesenace16102009-02-03 19:33:06 +00004282 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004283 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004284 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004285 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004286 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004287 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004288 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004289 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004290 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4291 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4292 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004293 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4294 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4295 DAG.getNode(ISD::BIT_CONVERT, dl,
4296 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004297 Op.getOperand(0)),
4298 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004299 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004300 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004301 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004302 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004303 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004304 } else if (VT == MVT::f32) {
4305 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4306 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004307 // result has a single use which is a store or a bitcast to i32. And in
4308 // the case of a store, it's not worth it if the index is a constant 0,
4309 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004310 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004311 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004312 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004313 if ((User->getOpcode() != ISD::STORE ||
4314 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4315 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004316 (User->getOpcode() != ISD::BIT_CONVERT ||
4317 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004318 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004319 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4320 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
4321 Op.getOperand(0)),
4322 Op.getOperand(1));
4323 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004324 } else if (VT == MVT::i32) {
4325 // ExtractPS works with constant index.
4326 if (isa<ConstantSDNode>(Op.getOperand(1)))
4327 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004328 }
Dan Gohman475871a2008-07-27 21:46:04 +00004329 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004330}
4331
4332
Dan Gohman475871a2008-07-27 21:46:04 +00004333SDValue
4334X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004336 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004337
Evan Cheng62a3f152008-03-24 21:52:23 +00004338 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004339 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004340 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004341 return Res;
4342 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004343
Duncan Sands83ec4b62008-06-06 12:08:01 +00004344 MVT VT = Op.getValueType();
Dale Johannesenace16102009-02-03 19:33:06 +00004345 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004346 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004347 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004348 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004349 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004350 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004351 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4352 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4353 DAG.getNode(ISD::BIT_CONVERT, dl,
4354 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004355 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004356 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004357 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004358 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004359 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004360 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004361 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004362 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004363 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004364 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004365 if (Idx == 0)
4366 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004367 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004368 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman475871a2008-07-27 21:46:04 +00004369 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004370 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004371 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004372 IdxVec.
Dale Johannesenace16102009-02-03 19:33:06 +00004373 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004374 IdxVec.
Dale Johannesenace16102009-02-03 19:33:06 +00004375 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004376 IdxVec.
Dale Johannesenace16102009-02-03 19:33:06 +00004377 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4378 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004379 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004380 SDValue Vec = Op.getOperand(0);
Dale Johannesenace16102009-02-03 19:33:06 +00004381 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4382 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4383 Mask);
4384 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004385 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004386 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004387 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4388 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4389 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004390 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004391 if (Idx == 0)
4392 return Op;
4393
4394 // UNPCKHPD the element to the lowest double word, then movsd.
4395 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4396 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd038e042008-07-21 10:20:31 +00004397 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman475871a2008-07-27 21:46:04 +00004398 SmallVector<SDValue, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004399 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004400 IdxVec.
Dale Johannesenace16102009-02-03 19:33:06 +00004401 push_back(DAG.getNode(ISD::UNDEF, dl, MaskVT.getVectorElementType()));
4402 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004403 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004404 SDValue Vec = Op.getOperand(0);
Dale Johannesenace16102009-02-03 19:33:06 +00004405 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, Vec.getValueType(),
4406 Vec, DAG.getNode(ISD::UNDEF, dl, Vec.getValueType()),
4407 Mask);
4408 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004409 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004410 }
4411
Dan Gohman475871a2008-07-27 21:46:04 +00004412 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413}
4414
Dan Gohman475871a2008-07-27 21:46:04 +00004415SDValue
4416X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004417 MVT VT = Op.getValueType();
4418 MVT EVT = VT.getVectorElementType();
Dale Johannesenace16102009-02-03 19:33:06 +00004419 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004420
Dan Gohman475871a2008-07-27 21:46:04 +00004421 SDValue N0 = Op.getOperand(0);
4422 SDValue N1 = Op.getOperand(1);
4423 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004424
Dan Gohmanef521f12008-08-14 22:53:18 +00004425 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4426 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004427 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004428 : X86ISD::PINSRW;
4429 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4430 // argument.
4431 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004432 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004433 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004434 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004435 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004436 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004437 // Bits [7:6] of the constant are the source select. This will always be
4438 // zero here. The DAG Combiner may combine an extract_elt index into these
4439 // bits. For example (insert (extract, 3), 2) could be matched by putting
4440 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4441 // Bits [5:4] of the constant are the destination select. This is the
4442 // value of the incoming immediate.
4443 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4444 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004445 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004446 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004447 } else if (EVT == MVT::i32) {
4448 // InsertPS works with constant index.
4449 if (isa<ConstantSDNode>(N2))
4450 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004451 }
Dan Gohman475871a2008-07-27 21:46:04 +00004452 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004453}
4454
Dan Gohman475871a2008-07-27 21:46:04 +00004455SDValue
4456X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004457 MVT VT = Op.getValueType();
4458 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004459
4460 if (Subtarget->hasSSE41())
4461 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4462
Evan Cheng794405e2007-12-12 07:55:34 +00004463 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004464 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004465
Dale Johannesenace16102009-02-03 19:33:06 +00004466 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004467 SDValue N0 = Op.getOperand(0);
4468 SDValue N1 = Op.getOperand(1);
4469 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004470
Duncan Sands83ec4b62008-06-06 12:08:01 +00004471 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004472 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4473 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004474 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004475 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004476 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004477 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004478 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479 }
Dan Gohman475871a2008-07-27 21:46:04 +00004480 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481}
4482
Dan Gohman475871a2008-07-27 21:46:04 +00004483SDValue
4484X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00004485 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004486 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004487 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4488 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4489 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004490 Op.getOperand(0))));
4491
Dale Johannesenace16102009-02-03 19:33:06 +00004492 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004493 MVT VT = MVT::v2i32;
4494 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004495 default: break;
4496 case MVT::v16i8:
4497 case MVT::v8i16:
4498 VT = MVT::v4i32;
4499 break;
4500 }
Dale Johannesenace16102009-02-03 19:33:06 +00004501 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4502 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004503}
4504
Bill Wendling056292f2008-09-16 21:48:12 +00004505// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4506// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4507// one of the above mentioned nodes. It has to be wrapped because otherwise
4508// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4509// be used to form addressing mode. These wrapped nodes will be selected
4510// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004511SDValue
4512X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004513 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004514 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Evan Chengd0ff02c2006-11-29 23:19:46 +00004515 getPointerTy(),
4516 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004517 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004518 // With PIC, the address is actually $g + Offset.
4519 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4520 !Subtarget->isPICStyleRIPRel()) {
4521 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4522 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4523 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004524 }
4525
4526 return Result;
4527}
4528
Dan Gohman475871a2008-07-27 21:46:04 +00004529SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004530X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004531 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004532 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004533 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4534 bool ExtraLoadRequired =
4535 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4536
4537 // Create the TargetGlobalAddress node, folding in the constant
4538 // offset if it is legal.
4539 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004540 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004541 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4542 Offset = 0;
4543 } else
4544 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004545 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004546
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004547 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004548 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004549 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4550 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004551 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004553
4554 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4555 // load the value at address GV, not the value of GV itself. This means that
4556 // the GlobalAddress must be in the base or index register of the address, not
4557 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004558 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004559 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004560 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004561 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004562
Dan Gohman6520e202008-10-18 02:06:02 +00004563 // If there was a non-zero offset that we didn't fold, create an explicit
4564 // addition for it.
4565 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004566 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004567 DAG.getConstant(Offset, getPointerTy()));
4568
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 return Result;
4570}
4571
Evan Chengda43bcf2008-09-24 00:05:32 +00004572SDValue
4573X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4574 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004575 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen33c960f2009-02-04 20:06:27 +00004576 return LowerGlobalAddress(GV, Op.getNode()->getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004577}
4578
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004579// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004580static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004581LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004582 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004583 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004584 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4585 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004586 DAG.getNode(X86ISD::GlobalBaseReg,
4587 PtrVT), InFlag);
4588 InFlag = Chain.getValue(1);
4589
4590 // emit leal symbol@TLSGD(,%ebx,1), %eax
4591 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004592 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004593 GA->getValueType(0),
4594 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004595 SDValue Ops[] = { Chain, TGA, InFlag };
4596 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004597 InFlag = Result.getValue(2);
4598 Chain = Result.getValue(1);
4599
4600 // call ___tls_get_addr. This function receives its argument in
4601 // the register EAX.
Dale Johannesendd64c412009-02-04 00:33:20 +00004602 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Result, InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004603 InFlag = Chain.getValue(1);
4604
4605 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004606 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004607 DAG.getTargetExternalSymbol("___tls_get_addr",
4608 PtrVT),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004609 DAG.getRegister(X86::EAX, PtrVT),
4610 DAG.getRegister(X86::EBX, PtrVT),
4611 InFlag };
4612 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4613 InFlag = Chain.getValue(1);
4614
Dale Johannesendd64c412009-02-04 00:33:20 +00004615 return DAG.getCopyFromReg(Chain, dl, X86::EAX, PtrVT, InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004616}
4617
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004618// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004619static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004620LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004621 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004622 SDValue InFlag, Chain;
Dale Johannesendd64c412009-02-04 00:33:20 +00004623 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004624
4625 // emit leaq symbol@TLSGD(%rip), %rdi
4626 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004627 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004628 GA->getValueType(0),
4629 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004630 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4631 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004632 Chain = Result.getValue(1);
4633 InFlag = Result.getValue(2);
4634
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004635 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004636 // the register RDI.
Dale Johannesendd64c412009-02-04 00:33:20 +00004637 Chain = DAG.getCopyToReg(Chain, dl, X86::RDI, Result, InFlag);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004638 InFlag = Chain.getValue(1);
4639
4640 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004641 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004642 DAG.getTargetExternalSymbol("__tls_get_addr",
4643 PtrVT),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004644 DAG.getRegister(X86::RDI, PtrVT),
4645 InFlag };
4646 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4647 InFlag = Chain.getValue(1);
4648
Dale Johannesendd64c412009-02-04 00:33:20 +00004649 return DAG.getCopyFromReg(Chain, dl, X86::RAX, PtrVT, InFlag);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004650}
4651
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004652// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4653// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004654static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004655 const MVT PtrVT) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004656 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004657 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004659 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4660 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004661 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004662 GA->getValueType(0),
4663 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004664 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004665
4666 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dale Johannesen33c960f2009-02-04 20:06:27 +00004667 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004668 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004669
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004670 // The address of the thread local variable is the add of the thread
4671 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004672 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004673}
4674
Dan Gohman475871a2008-07-27 21:46:04 +00004675SDValue
4676X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004677 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004678 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004679 assert(Subtarget->isTargetELF() &&
4680 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004681 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4682 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4683 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004684 if (Subtarget->is64Bit()) {
4685 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4686 } else {
4687 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4688 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4689 else
4690 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4691 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004692}
4693
Dan Gohman475871a2008-07-27 21:46:04 +00004694SDValue
4695X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendling056292f2008-09-16 21:48:12 +00004696 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4697 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004698 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004699 // With PIC, the address is actually $g + Offset.
4700 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4701 !Subtarget->isPICStyleRIPRel()) {
4702 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4703 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4704 Result);
4705 }
4706
4707 return Result;
4708}
4709
Dan Gohman475871a2008-07-27 21:46:04 +00004710SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004711 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004712 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004713 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4714 // With PIC, the address is actually $g + Offset.
4715 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4716 !Subtarget->isPICStyleRIPRel()) {
4717 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4718 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4719 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004720 }
4721
4722 return Result;
4723}
4724
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004725/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4726/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004727SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004728 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004729 MVT VT = Op.getValueType();
4730 unsigned VTBits = VT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004731 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004732 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004733 SDValue ShOpLo = Op.getOperand(0);
4734 SDValue ShOpHi = Op.getOperand(1);
4735 SDValue ShAmt = Op.getOperand(2);
4736 SDValue Tmp1 = isSRA ?
Dale Johannesenace16102009-02-03 19:33:06 +00004737 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4738 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004739 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004740
Dan Gohman475871a2008-07-27 21:46:04 +00004741 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004742 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004743 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4744 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004745 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004746 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4747 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004748 }
Evan Chenge3413162006-01-09 18:33:28 +00004749
Dale Johannesenace16102009-02-03 19:33:06 +00004750 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004751 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004752 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004753 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004754
Dan Gohman475871a2008-07-27 21:46:04 +00004755 SDValue Hi, Lo;
4756 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4757 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4758 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004759
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004760 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004761 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4762 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004763 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004764 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4765 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004766 }
4767
Dan Gohman475871a2008-07-27 21:46:04 +00004768 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004769 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004770}
Evan Chenga3195e82006-01-12 22:54:21 +00004771
Dan Gohman475871a2008-07-27 21:46:04 +00004772SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004773 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004774 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004775 "Unknown SINT_TO_FP to lower!");
4776
4777 // These are really Legal; caller falls through into that case.
4778 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004779 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004780 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4781 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004782 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004783
Dale Johannesenace16102009-02-03 19:33:06 +00004784 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004785 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 MachineFunction &MF = DAG.getMachineFunction();
4787 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004788 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004789 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004790 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004791 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792
4793 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004794 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004795 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004796 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004797 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4798 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004799 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004800 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801 Ops.push_back(Chain);
4802 Ops.push_back(StackSlot);
4803 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004804 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004805 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004806
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004807 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004808 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004809 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004810
4811 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4812 // shouldn't be necessary except that RFP cannot be live across
4813 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004814 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004815 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004816 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004817 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004818 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004819 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004820 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004821 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004822 Ops.push_back(DAG.getValueType(Op.getValueType()));
4823 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004824 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4825 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004826 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004827 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004828
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829 return Result;
4830}
4831
Bill Wendling8b8a6362009-01-17 03:56:04 +00004832// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4833SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4834 // This algorithm is not obvious. Here it is in C code, more or less:
4835 /*
4836 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4837 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4838 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004839
Bill Wendling8b8a6362009-01-17 03:56:04 +00004840 // Copy ints to xmm registers.
4841 __m128i xh = _mm_cvtsi32_si128( hi );
4842 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004843
Bill Wendling8b8a6362009-01-17 03:56:04 +00004844 // Combine into low half of a single xmm register.
4845 __m128i x = _mm_unpacklo_epi32( xh, xl );
4846 __m128d d;
4847 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004848
Bill Wendling8b8a6362009-01-17 03:56:04 +00004849 // Merge in appropriate exponents to give the integer bits the right
4850 // magnitude.
4851 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004852
Bill Wendling8b8a6362009-01-17 03:56:04 +00004853 // Subtract away the biases to deal with the IEEE-754 double precision
4854 // implicit 1.
4855 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004856
Bill Wendling8b8a6362009-01-17 03:56:04 +00004857 // All conversions up to here are exact. The correctly rounded result is
4858 // calculated using the current rounding mode using the following
4859 // horizontal add.
4860 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4861 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4862 // store doesn't really need to be here (except
4863 // maybe to zero the other double)
4864 return sd;
4865 }
4866 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004867
Dale Johannesenace16102009-02-03 19:33:06 +00004868 DebugLoc dl = Op.getNode()->getDebugLoc();
4869
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004870 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004871 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004872 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4873 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4874 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4875 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4876 Constant *C0 = ConstantVector::get(CV0);
4877 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4878
Bill Wendling8b8a6362009-01-17 03:56:04 +00004879 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004880 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4881 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4882 Constant *C1 = ConstantVector::get(CV1);
4883 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4884
4885 SmallVector<SDValue, 4> MaskVec;
4886 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4887 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4888 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4889 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004890 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
4891 &MaskVec[0], MaskVec.size());
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004892 SmallVector<SDValue, 4> MaskVec2;
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004893 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4894 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004895 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32,
4896 &MaskVec2[0], MaskVec2.size());
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004897
Dale Johannesenace16102009-02-03 19:33:06 +00004898 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4899 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004900 Op.getOperand(0),
4901 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004902 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4903 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004904 Op.getOperand(0),
4905 DAG.getIntPtrConstant(0)));
Dale Johannesenace16102009-02-03 19:33:06 +00004906 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004907 XR1, XR2, UnpcklMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004908 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004909 PseudoSourceValue::getConstantPool(), 0,
4910 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004911 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v4i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004912 Unpck1, CLod0, UnpcklMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004913 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4914 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004915 PseudoSourceValue::getConstantPool(), 0,
4916 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004917 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004918
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004919 // Add the halves; easiest way is to swap them into another reg first.
Dale Johannesenace16102009-02-03 19:33:06 +00004920 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, dl, MVT::v2f64,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004921 Sub, Sub, ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004922 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4923 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004924 DAG.getIntPtrConstant(0));
4925}
4926
Bill Wendling8b8a6362009-01-17 03:56:04 +00004927// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4928SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00004929 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004930 // FP constant to bias correct the final result.
4931 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4932 MVT::f64);
4933
4934 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004935 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4936 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004937 Op.getOperand(0),
4938 DAG.getIntPtrConstant(0)));
4939
Dale Johannesenace16102009-02-03 19:33:06 +00004940 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4941 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004942 DAG.getIntPtrConstant(0));
4943
4944 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004945 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4946 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4947 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004948 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004949 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4950 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004951 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004952 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4953 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004954 DAG.getIntPtrConstant(0));
4955
4956 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004957 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004958
4959 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004960 MVT DestVT = Op.getValueType();
4961
4962 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004963 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004964 DAG.getIntPtrConstant(0));
4965 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004966 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004967 }
4968
4969 // Handle final rounding.
4970 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004971}
4972
4973SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004974 SDValue N0 = Op.getOperand(0);
Dale Johannesenace16102009-02-03 19:33:06 +00004975 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004976
Evan Chenga06ec9e2009-01-19 08:08:22 +00004977 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4978 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4979 // the optimization here.
4980 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004981 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004982
4983 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004984 if (SrcVT == MVT::i64) {
4985 // We only handle SSE2 f64 target here; caller can handle the rest.
4986 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4987 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004988
Bill Wendling8b8a6362009-01-17 03:56:04 +00004989 return LowerUINT_TO_FP_i64(Op, DAG);
4990 } else if (SrcVT == MVT::i32) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004991 return LowerUINT_TO_FP_i32(Op, DAG);
4992 }
4993
4994 assert(0 && "Unknown UINT_TO_FP to lower!");
4995 return SDValue();
4996}
4997
Dan Gohman475871a2008-07-27 21:46:04 +00004998std::pair<SDValue,SDValue> X86TargetLowering::
4999FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00005000 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005001 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
5002 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005003 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005004
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005005 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00005006 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005007 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005008 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005009 if (Subtarget->is64Bit() &&
5010 Op.getValueType() == MVT::i64 &&
5011 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00005012 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005013
Evan Cheng87c89352007-10-15 20:11:21 +00005014 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5015 // stack slot.
5016 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005017 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00005018 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00005019 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005020 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005021 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005022 default: assert(0 && "Invalid FP_TO_SINT to lower!");
5023 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5024 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5025 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005026 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005027
Dan Gohman475871a2008-07-27 21:46:04 +00005028 SDValue Chain = DAG.getEntryNode();
5029 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005030 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005031 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005032 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00005033 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00005034 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005035 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005036 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5037 };
Dale Johannesenace16102009-02-03 19:33:06 +00005038 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005039 Chain = Value.getValue(1);
5040 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5041 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5042 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005043
Evan Cheng0db9fe62006-04-25 20:13:52 +00005044 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00005046 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005047
Chris Lattner27a6c732007-11-24 07:07:01 +00005048 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005049}
5050
Dan Gohman475871a2008-07-27 21:46:04 +00005051SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
5052 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
5053 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00005054 if (FIST.getNode() == 0) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00005055
5056 // Load the result.
Dale Johannesenace16102009-02-03 19:33:06 +00005057 return DAG.getLoad(Op.getValueType(), Op.getNode()->getDebugLoc(),
5058 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005059}
5060
Dan Gohman475871a2008-07-27 21:46:04 +00005061SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00005062 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005063 MVT VT = Op.getValueType();
5064 MVT EltVT = VT;
5065 if (VT.isVector())
5066 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005068 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005069 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005070 CV.push_back(C);
5071 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005073 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005074 CV.push_back(C);
5075 CV.push_back(C);
5076 CV.push_back(C);
5077 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005078 }
Dan Gohmand3006222007-07-27 17:16:43 +00005079 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005080 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005081 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005082 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005083 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005084 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005085}
5086
Dan Gohman475871a2008-07-27 21:46:04 +00005087SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00005088 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005089 MVT VT = Op.getValueType();
5090 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00005091 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005092 if (VT.isVector()) {
5093 EltVT = VT.getVectorElementType();
5094 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00005095 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005096 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005097 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005098 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005099 CV.push_back(C);
5100 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005101 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005102 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005103 CV.push_back(C);
5104 CV.push_back(C);
5105 CV.push_back(C);
5106 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005107 }
Dan Gohmand3006222007-07-27 17:16:43 +00005108 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005109 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005110 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005111 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005112 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005113 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005114 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5115 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5116 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
5117 Op.getOperand(0)),
5118 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005119 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005120 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005121 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122}
5123
Dan Gohman475871a2008-07-27 21:46:04 +00005124SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
5125 SDValue Op0 = Op.getOperand(0);
5126 SDValue Op1 = Op.getOperand(1);
Dale Johannesenace16102009-02-03 19:33:06 +00005127 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005128 MVT VT = Op.getValueType();
5129 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005130
5131 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005132 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005133 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005134 SrcVT = VT;
5135 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005136 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005137 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005138 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005139 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005140 }
5141
5142 // At this point the operands and the result should have the same
5143 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005144
Evan Cheng68c47cb2007-01-05 07:55:56 +00005145 // First get the sign bit of second operand.
5146 std::vector<Constant*> CV;
5147 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005148 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
5149 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005150 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005151 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
5152 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5153 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5154 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005155 }
Dan Gohmand3006222007-07-27 17:16:43 +00005156 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005157 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005158 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005159 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005160 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005161 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005162
5163 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005164 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005165 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005166 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5167 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005168 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005169 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5170 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005171 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005172 }
5173
Evan Cheng73d6cf12007-01-05 21:37:56 +00005174 // Clear first operand sign bit.
5175 CV.clear();
5176 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00005177 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5178 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005179 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00005180 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5181 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5182 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5183 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005184 }
Dan Gohmand3006222007-07-27 17:16:43 +00005185 C = ConstantVector::get(CV);
5186 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dale Johannesenace16102009-02-03 19:33:06 +00005187 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005188 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005189 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005190 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005191
5192 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005193 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005194}
5195
Dan Gohman475871a2008-07-27 21:46:04 +00005196SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005197 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SDValue Op0 = Op.getOperand(0);
5199 SDValue Op1 = Op.getOperand(1);
Dale Johannesenace16102009-02-03 19:33:06 +00005200 DebugLoc dl = Op.getNode()->getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005201 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5202
Dan Gohmane5af2d32009-01-29 01:59:02 +00005203 // Lower (X & (1 << N)) == 0 to BT(X, N).
5204 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5205 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005206 if (Op0.getOpcode() == ISD::AND &&
5207 Op0.hasOneUse() &&
5208 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005209 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005210 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005211 SDValue LHS, RHS;
5212 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5213 if (ConstantSDNode *Op010C =
5214 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5215 if (Op010C->getZExtValue() == 1) {
5216 LHS = Op0.getOperand(0);
5217 RHS = Op0.getOperand(1).getOperand(1);
5218 }
5219 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5220 if (ConstantSDNode *Op000C =
5221 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5222 if (Op000C->getZExtValue() == 1) {
5223 LHS = Op0.getOperand(1);
5224 RHS = Op0.getOperand(0).getOperand(1);
5225 }
5226 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5227 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5228 SDValue AndLHS = Op0.getOperand(0);
5229 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5230 LHS = AndLHS.getOperand(0);
5231 RHS = AndLHS.getOperand(1);
5232 }
5233 }
Evan Cheng0488db92007-09-25 01:57:46 +00005234
Dan Gohmane5af2d32009-01-29 01:59:02 +00005235 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005236 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5237 // instruction. Since the shift amount is in-range-or-undefined, we know
5238 // that doing a bittest on the i16 value is ok. We extend to i32 because
5239 // the encoding for the i16 version is larger than the i32 version.
5240 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005241 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005242
5243 // If the operand types disagree, extend the shift amount to match. Since
5244 // BT ignores high bits (like shifts) we can use anyextend.
5245 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005246 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005247
Dale Johannesenace16102009-02-03 19:33:06 +00005248 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005249 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005250 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005251 DAG.getConstant(Cond, MVT::i8), BT);
5252 }
5253 }
5254
5255 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5256 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner43287082008-12-24 00:11:37 +00005257
Dale Johannesenace16102009-02-03 19:33:06 +00005258 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5259 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005260 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005261}
5262
Dan Gohman475871a2008-07-27 21:46:04 +00005263SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5264 SDValue Cond;
5265 SDValue Op0 = Op.getOperand(0);
5266 SDValue Op1 = Op.getOperand(1);
5267 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005268 MVT VT = Op.getValueType();
5269 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5270 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesenace16102009-02-03 19:33:06 +00005271 DebugLoc dl = Op.getNode()->getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005272
5273 if (isFP) {
5274 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005275 MVT VT0 = Op0.getValueType();
5276 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5277 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005278 bool Swap = false;
5279
5280 switch (SetCCOpcode) {
5281 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005282 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005283 case ISD::SETEQ: SSECC = 0; break;
5284 case ISD::SETOGT:
5285 case ISD::SETGT: Swap = true; // Fallthrough
5286 case ISD::SETLT:
5287 case ISD::SETOLT: SSECC = 1; break;
5288 case ISD::SETOGE:
5289 case ISD::SETGE: Swap = true; // Fallthrough
5290 case ISD::SETLE:
5291 case ISD::SETOLE: SSECC = 2; break;
5292 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005293 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005294 case ISD::SETNE: SSECC = 4; break;
5295 case ISD::SETULE: Swap = true;
5296 case ISD::SETUGE: SSECC = 5; break;
5297 case ISD::SETULT: Swap = true;
5298 case ISD::SETUGT: SSECC = 6; break;
5299 case ISD::SETO: SSECC = 7; break;
5300 }
5301 if (Swap)
5302 std::swap(Op0, Op1);
5303
Nate Begemanfb8ead02008-07-25 19:05:58 +00005304 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005305 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005306 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005308 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5309 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5310 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005311 }
5312 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005313 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005314 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5315 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5316 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005317 }
5318 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005319 }
5320 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005321 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005322 }
5323
5324 // We are handling one of the integer comparisons here. Since SSE only has
5325 // GT and EQ comparisons for integer, swapping operands and multiple
5326 // operations may be required for some comparisons.
5327 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5328 bool Swap = false, Invert = false, FlipSigns = false;
5329
5330 switch (VT.getSimpleVT()) {
5331 default: break;
5332 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5333 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5334 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5335 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5336 }
5337
5338 switch (SetCCOpcode) {
5339 default: break;
5340 case ISD::SETNE: Invert = true;
5341 case ISD::SETEQ: Opc = EQOpc; break;
5342 case ISD::SETLT: Swap = true;
5343 case ISD::SETGT: Opc = GTOpc; break;
5344 case ISD::SETGE: Swap = true;
5345 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5346 case ISD::SETULT: Swap = true;
5347 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5348 case ISD::SETUGE: Swap = true;
5349 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5350 }
5351 if (Swap)
5352 std::swap(Op0, Op1);
5353
5354 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5355 // bits of the inputs before performing those operations.
5356 if (FlipSigns) {
5357 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005358 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5359 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005360 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Dale Johannesenace16102009-02-03 19:33:06 +00005361 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00005362 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005363 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5364 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005365 }
5366
Dale Johannesenace16102009-02-03 19:33:06 +00005367 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005368
5369 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005370 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005371 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005372
Nate Begeman30a0de92008-07-17 16:51:19 +00005373 return Result;
5374}
Evan Cheng0488db92007-09-25 01:57:46 +00005375
Evan Cheng370e5342008-12-03 08:38:43 +00005376// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5377static bool isX86LogicalCmp(unsigned Opc) {
5378 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5379}
5380
Dan Gohman475871a2008-07-27 21:46:04 +00005381SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005382 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005383 SDValue Cond = Op.getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005384 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005385 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005386
Evan Cheng734503b2006-09-11 02:19:56 +00005387 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005388 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005389
Evan Cheng3f41d662007-10-08 22:16:29 +00005390 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5391 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005392 if (Cond.getOpcode() == X86ISD::SETCC) {
5393 CC = Cond.getOperand(0);
5394
Dan Gohman475871a2008-07-27 21:46:04 +00005395 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005396 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005397 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00005398
Evan Cheng3f41d662007-10-08 22:16:29 +00005399 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005400 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005401 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005402 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattner1956d152008-01-16 06:19:45 +00005403
Dan Gohmane5af2d32009-01-29 01:59:02 +00005404 if ((isX86LogicalCmp(Opc) && !IllegalFPCMov) || Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005405 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005406 addTest = false;
5407 }
5408 }
5409
5410 if (addTest) {
5411 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005412 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5413 DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005414 }
5415
Duncan Sands83ec4b62008-06-06 12:08:01 +00005416 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00005417 MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005419 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5420 // condition is true.
5421 Ops.push_back(Op.getOperand(2));
5422 Ops.push_back(Op.getOperand(1));
5423 Ops.push_back(CC);
5424 Ops.push_back(Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005425 return DAG.getNode(X86ISD::CMOV, dl, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005426}
5427
Evan Cheng370e5342008-12-03 08:38:43 +00005428// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5429// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5430// from the AND / OR.
5431static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5432 Opc = Op.getOpcode();
5433 if (Opc != ISD::OR && Opc != ISD::AND)
5434 return false;
5435 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5436 Op.getOperand(0).hasOneUse() &&
5437 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5438 Op.getOperand(1).hasOneUse());
5439}
5440
Evan Cheng961d6d42009-02-02 08:19:07 +00005441// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5442// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005443static bool isXor1OfSetCC(SDValue Op) {
5444 if (Op.getOpcode() != ISD::XOR)
5445 return false;
5446 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5447 if (N1C && N1C->getAPIntValue() == 1) {
5448 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5449 Op.getOperand(0).hasOneUse();
5450 }
5451 return false;
5452}
5453
Dan Gohman475871a2008-07-27 21:46:04 +00005454SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005455 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue Chain = Op.getOperand(0);
5457 SDValue Cond = Op.getOperand(1);
5458 SDValue Dest = Op.getOperand(2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005459 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005460 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005461
Evan Cheng0db9fe62006-04-25 20:13:52 +00005462 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005463 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005464#if 0
5465 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005466 else if (Cond.getOpcode() == X86ISD::ADD ||
5467 Cond.getOpcode() == X86ISD::SUB ||
5468 Cond.getOpcode() == X86ISD::SMUL ||
5469 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005470 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005471#endif
5472
Evan Cheng3f41d662007-10-08 22:16:29 +00005473 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5474 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005475 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005476 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477
Dan Gohman475871a2008-07-27 21:46:04 +00005478 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005479 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005480 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5481 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005482 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005483 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005484 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005485 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005486 default: break;
5487 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005488 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005489 // These can only come from an arithmetic instruction with overflow,
5490 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005491 Cond = Cond.getNode()->getOperand(1);
5492 addTest = false;
5493 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005494 }
Evan Cheng0488db92007-09-25 01:57:46 +00005495 }
Evan Cheng370e5342008-12-03 08:38:43 +00005496 } else {
5497 unsigned CondOpc;
5498 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5499 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5500 unsigned Opc = Cmp.getOpcode();
5501 if (CondOpc == ISD::OR) {
5502 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5503 // two branches instead of an explicit OR instruction with a
5504 // separate test.
5505 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5506 isX86LogicalCmp(Opc)) {
5507 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005508 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005509 Chain, Dest, CC, Cmp);
5510 CC = Cond.getOperand(1).getOperand(0);
5511 Cond = Cmp;
5512 addTest = false;
5513 }
5514 } else { // ISD::AND
5515 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5516 // two branches instead of an explicit AND instruction with a
5517 // separate test. However, we only do this if this block doesn't
5518 // have a fall-through edge, because this requires an explicit
5519 // jmp when the condition is false.
5520 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5521 isX86LogicalCmp(Opc) &&
5522 Op.getNode()->hasOneUse()) {
5523 X86::CondCode CCode =
5524 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5525 CCode = X86::GetOppositeBranchCondition(CCode);
5526 CC = DAG.getConstant(CCode, MVT::i8);
5527 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5528 // Look for an unconditional branch following this conditional branch.
5529 // We need this because we need to reverse the successors in order
5530 // to implement FCMP_OEQ.
5531 if (User.getOpcode() == ISD::BR) {
5532 SDValue FalseBB = User.getOperand(1);
5533 SDValue NewBR =
5534 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5535 assert(NewBR == User);
5536 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005537
Dale Johannesene4d209d2009-02-03 20:21:25 +00005538 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005539 Chain, Dest, CC, Cmp);
5540 X86::CondCode CCode =
5541 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5542 CCode = X86::GetOppositeBranchCondition(CCode);
5543 CC = DAG.getConstant(CCode, MVT::i8);
5544 Cond = Cmp;
5545 addTest = false;
5546 }
5547 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005548 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005549 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5550 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5551 // It should be transformed during dag combiner except when the condition
5552 // is set by a arithmetics with overflow node.
5553 X86::CondCode CCode =
5554 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5555 CCode = X86::GetOppositeBranchCondition(CCode);
5556 CC = DAG.getConstant(CCode, MVT::i8);
5557 Cond = Cond.getOperand(0).getOperand(1);
5558 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005559 }
Evan Cheng0488db92007-09-25 01:57:46 +00005560 }
5561
5562 if (addTest) {
5563 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005564 Cond= DAG.getNode(X86ISD::CMP, dl, MVT::i32, Cond,
5565 DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005566 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005567 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005568 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005569}
5570
Anton Korobeynikove060b532007-04-17 19:34:00 +00005571
5572// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5573// Calls to _alloca is needed to probe the stack when allocating more than 4k
5574// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5575// that the guard pages used by the OS virtual memory manager are allocated in
5576// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005577SDValue
5578X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005579 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005580 assert(Subtarget->isTargetCygMing() &&
5581 "This should be used only on Cygwin/Mingw targets");
Dale Johannesene4d209d2009-02-03 20:21:25 +00005582 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005583
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005584 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005585 SDValue Chain = Op.getOperand(0);
5586 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005587 // FIXME: Ensure alignment here
5588
Dan Gohman475871a2008-07-27 21:46:04 +00005589 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005590
Duncan Sands83ec4b62008-06-06 12:08:01 +00005591 MVT IntPtr = getPointerTy();
5592 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005593
Chris Lattnere563bbc2008-10-11 22:08:30 +00005594 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005595
Dale Johannesendd64c412009-02-04 00:33:20 +00005596 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005597 Flag = Chain.getValue(1);
5598
5599 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005600 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005601 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005602 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005603 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005604 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005605 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005606 Flag = Chain.getValue(1);
5607
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005608 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005609 DAG.getIntPtrConstant(0, true),
5610 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005611 Flag);
5612
Dale Johannesendd64c412009-02-04 00:33:20 +00005613 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005614
Dan Gohman475871a2008-07-27 21:46:04 +00005615 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005616 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005617}
5618
Dan Gohman475871a2008-07-27 21:46:04 +00005619SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005620X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005621 SDValue Chain,
5622 SDValue Dst, SDValue Src,
5623 SDValue Size, unsigned Align,
5624 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005625 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005626 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005627
Bill Wendling6f287b22008-09-30 21:22:07 +00005628 // If not DWORD aligned or size is more than the threshold, call the library.
5629 // The libc version is likely to be faster for these cases. It can use the
5630 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005631 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005632 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005633 ConstantSize->getZExtValue() >
5634 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005635 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005636
5637 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005638 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005639
Bill Wendling6158d842008-10-01 00:59:58 +00005640 if (const char *bzeroEntry = V &&
5641 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5642 MVT IntPtr = getPointerTy();
5643 const Type *IntPtrTy = TD->getIntPtrType();
5644 TargetLowering::ArgListTy Args;
5645 TargetLowering::ArgListEntry Entry;
5646 Entry.Node = Dst;
5647 Entry.Ty = IntPtrTy;
5648 Args.push_back(Entry);
5649 Entry.Node = Size;
5650 Args.push_back(Entry);
5651 std::pair<SDValue,SDValue> CallResult =
5652 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5653 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005654 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005655 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005656 }
5657
Dan Gohman707e0182008-04-12 04:36:06 +00005658 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005659 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005660 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005661
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005662 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005663 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005664 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005665 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005666 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005667 unsigned BytesLeft = 0;
5668 bool TwoRepStos = false;
5669 if (ValC) {
5670 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005671 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005672
Evan Cheng0db9fe62006-04-25 20:13:52 +00005673 // If the value is a constant, then we can potentially use larger sets.
5674 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005675 case 2: // WORD aligned
5676 AVT = MVT::i16;
5677 ValReg = X86::AX;
5678 Val = (Val << 8) | Val;
5679 break;
5680 case 0: // DWORD aligned
5681 AVT = MVT::i32;
5682 ValReg = X86::EAX;
5683 Val = (Val << 8) | Val;
5684 Val = (Val << 16) | Val;
5685 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5686 AVT = MVT::i64;
5687 ValReg = X86::RAX;
5688 Val = (Val << 32) | Val;
5689 }
5690 break;
5691 default: // Byte aligned
5692 AVT = MVT::i8;
5693 ValReg = X86::AL;
5694 Count = DAG.getIntPtrConstant(SizeVal);
5695 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005696 }
5697
Duncan Sands8e4eb092008-06-08 20:54:56 +00005698 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005699 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005700 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5701 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005702 }
5703
Dale Johannesen0f502f62009-02-03 22:26:09 +00005704 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705 InFlag);
5706 InFlag = Chain.getValue(1);
5707 } else {
5708 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005709 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005710 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005711 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005712 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005713
Dale Johannesen0f502f62009-02-03 22:26:09 +00005714 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5715 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005716 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717 InFlag = Chain.getValue(1);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005718 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5719 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005720 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005721 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005722
Chris Lattnerd96d0722007-02-25 06:40:16 +00005723 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005724 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005725 Ops.push_back(Chain);
5726 Ops.push_back(DAG.getValueType(AVT));
5727 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005728 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005729
Evan Cheng0db9fe62006-04-25 20:13:52 +00005730 if (TwoRepStos) {
5731 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005732 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005733 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005734 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005735 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Dale Johannesen0f502f62009-02-03 22:26:09 +00005736 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
5737 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005738 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005739 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005740 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005741 Ops.clear();
5742 Ops.push_back(Chain);
5743 Ops.push_back(DAG.getValueType(MVT::i8));
5744 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005745 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005746 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005747 // Handle the last 1 - 7 bytes.
5748 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005749 MVT AddrVT = Dst.getValueType();
5750 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005751
Dale Johannesen0f502f62009-02-03 22:26:09 +00005752 Chain = DAG.getMemset(Chain, dl,
5753 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005754 DAG.getConstant(Offset, AddrVT)),
5755 Src,
5756 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005757 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005758 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005759
Dan Gohman707e0182008-04-12 04:36:06 +00005760 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005761 return Chain;
5762}
Evan Cheng11e15b32006-04-03 20:53:28 +00005763
Dan Gohman475871a2008-07-27 21:46:04 +00005764SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005765X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005766 SDValue Chain, SDValue Dst, SDValue Src,
5767 SDValue Size, unsigned Align,
5768 bool AlwaysInline,
5769 const Value *DstSV, uint64_t DstSVOff,
5770 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005771 // This requires the copy size to be a constant, preferrably
5772 // within a subtarget-specific limit.
5773 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5774 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005775 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005776 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005777 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005778 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005779
Evan Cheng1887c1c2008-08-21 21:00:15 +00005780 /// If not DWORD aligned, call the library.
5781 if ((Align & 3) != 0)
5782 return SDValue();
5783
5784 // DWORD aligned
5785 MVT AVT = MVT::i32;
5786 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005787 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005788
Duncan Sands83ec4b62008-06-06 12:08:01 +00005789 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005790 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005791 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005792 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005793
Dan Gohman475871a2008-07-27 21:46:04 +00005794 SDValue InFlag(0, 0);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005795 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
5796 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005797 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005798 InFlag = Chain.getValue(1);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005799 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
5800 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005801 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005802 InFlag = Chain.getValue(1);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005803 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
5804 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005805 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005806 InFlag = Chain.getValue(1);
5807
Chris Lattnerd96d0722007-02-25 06:40:16 +00005808 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005809 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005810 Ops.push_back(Chain);
5811 Ops.push_back(DAG.getValueType(AVT));
5812 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005813 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005814
Dan Gohman475871a2008-07-27 21:46:04 +00005815 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005816 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005817 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005818 // Handle the last 1 - 7 bytes.
5819 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005820 MVT DstVT = Dst.getValueType();
5821 MVT SrcVT = Src.getValueType();
5822 MVT SizeVT = Size.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005823 Results.push_back(DAG.getMemcpy(Chain, dl,
5824 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005825 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005826 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005827 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005828 DAG.getConstant(BytesLeft, SizeVT),
5829 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005830 DstSV, DstSVOff + Offset,
5831 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005832 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005833
Dale Johannesen0f502f62009-02-03 22:26:09 +00005834 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5835 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005836}
5837
Dan Gohman475871a2008-07-27 21:46:04 +00005838SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005839 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesene4d209d2009-02-03 20:21:25 +00005840 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005841
Evan Cheng25ab6902006-09-08 06:48:29 +00005842 if (!Subtarget->is64Bit()) {
5843 // vastart just stores the address of the VarArgsFrameIndex slot into the
5844 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005845 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005846 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005847 }
5848
5849 // __va_list_tag:
5850 // gp_offset (0 - 6 * 8)
5851 // fp_offset (48 - 48 + 8 * 16)
5852 // overflow_arg_area (point to parameters coming in memory).
5853 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005854 SmallVector<SDValue, 8> MemOps;
5855 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005856 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005857 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005858 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005859 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005860 MemOps.push_back(Store);
5861
5862 // Store fp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005863 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5864 FIN, DAG.getIntPtrConstant(4));
5865 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005866 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005867 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005868 MemOps.push_back(Store);
5869
5870 // Store ptr to overflow_arg_area
Dale Johannesene4d209d2009-02-03 20:21:25 +00005871 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5872 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005873 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005874 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005875 MemOps.push_back(Store);
5876
5877 // Store ptr to reg_save_area.
Dale Johannesene4d209d2009-02-03 20:21:25 +00005878 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5879 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005880 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005881 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005882 MemOps.push_back(Store);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005883 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
5884 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005885}
5886
Dan Gohman475871a2008-07-27 21:46:04 +00005887SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005888 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5889 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005890 SDValue Chain = Op.getOperand(0);
5891 SDValue SrcPtr = Op.getOperand(1);
5892 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005893
5894 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5895 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005896 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005897}
5898
Dan Gohman475871a2008-07-27 21:46:04 +00005899SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005900 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005901 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005902 SDValue Chain = Op.getOperand(0);
5903 SDValue DstPtr = Op.getOperand(1);
5904 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005905 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5906 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesendd64c412009-02-04 00:33:20 +00005907 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005908
Dale Johannesendd64c412009-02-04 00:33:20 +00005909 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005910 DAG.getIntPtrConstant(24), 8, false,
5911 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005912}
5913
Dan Gohman475871a2008-07-27 21:46:04 +00005914SDValue
5915X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00005916 DebugLoc dl = Op.getNode()->getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005917 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005918 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005919 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005920 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005921 case Intrinsic::x86_sse_comieq_ss:
5922 case Intrinsic::x86_sse_comilt_ss:
5923 case Intrinsic::x86_sse_comile_ss:
5924 case Intrinsic::x86_sse_comigt_ss:
5925 case Intrinsic::x86_sse_comige_ss:
5926 case Intrinsic::x86_sse_comineq_ss:
5927 case Intrinsic::x86_sse_ucomieq_ss:
5928 case Intrinsic::x86_sse_ucomilt_ss:
5929 case Intrinsic::x86_sse_ucomile_ss:
5930 case Intrinsic::x86_sse_ucomigt_ss:
5931 case Intrinsic::x86_sse_ucomige_ss:
5932 case Intrinsic::x86_sse_ucomineq_ss:
5933 case Intrinsic::x86_sse2_comieq_sd:
5934 case Intrinsic::x86_sse2_comilt_sd:
5935 case Intrinsic::x86_sse2_comile_sd:
5936 case Intrinsic::x86_sse2_comigt_sd:
5937 case Intrinsic::x86_sse2_comige_sd:
5938 case Intrinsic::x86_sse2_comineq_sd:
5939 case Intrinsic::x86_sse2_ucomieq_sd:
5940 case Intrinsic::x86_sse2_ucomilt_sd:
5941 case Intrinsic::x86_sse2_ucomile_sd:
5942 case Intrinsic::x86_sse2_ucomigt_sd:
5943 case Intrinsic::x86_sse2_ucomige_sd:
5944 case Intrinsic::x86_sse2_ucomineq_sd: {
5945 unsigned Opc = 0;
5946 ISD::CondCode CC = ISD::SETCC_INVALID;
5947 switch (IntNo) {
5948 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005949 case Intrinsic::x86_sse_comieq_ss:
5950 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005951 Opc = X86ISD::COMI;
5952 CC = ISD::SETEQ;
5953 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005954 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005955 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005956 Opc = X86ISD::COMI;
5957 CC = ISD::SETLT;
5958 break;
5959 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005960 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005961 Opc = X86ISD::COMI;
5962 CC = ISD::SETLE;
5963 break;
5964 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005965 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005966 Opc = X86ISD::COMI;
5967 CC = ISD::SETGT;
5968 break;
5969 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005970 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005971 Opc = X86ISD::COMI;
5972 CC = ISD::SETGE;
5973 break;
5974 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005975 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005976 Opc = X86ISD::COMI;
5977 CC = ISD::SETNE;
5978 break;
5979 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005980 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005981 Opc = X86ISD::UCOMI;
5982 CC = ISD::SETEQ;
5983 break;
5984 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005985 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005986 Opc = X86ISD::UCOMI;
5987 CC = ISD::SETLT;
5988 break;
5989 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005990 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005991 Opc = X86ISD::UCOMI;
5992 CC = ISD::SETLE;
5993 break;
5994 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005995 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005996 Opc = X86ISD::UCOMI;
5997 CC = ISD::SETGT;
5998 break;
5999 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006000 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006001 Opc = X86ISD::UCOMI;
6002 CC = ISD::SETGE;
6003 break;
6004 case Intrinsic::x86_sse_ucomineq_ss:
6005 case Intrinsic::x86_sse2_ucomineq_sd:
6006 Opc = X86ISD::UCOMI;
6007 CC = ISD::SETNE;
6008 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006009 }
Evan Cheng734503b2006-09-11 02:19:56 +00006010
Dan Gohman475871a2008-07-27 21:46:04 +00006011 SDValue LHS = Op.getOperand(1);
6012 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006013 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006014 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6015 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00006016 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006017 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006018 }
Evan Cheng5759f972008-05-04 09:15:50 +00006019
6020 // Fix vector shift instructions where the last operand is a non-immediate
6021 // i32 value.
6022 case Intrinsic::x86_sse2_pslli_w:
6023 case Intrinsic::x86_sse2_pslli_d:
6024 case Intrinsic::x86_sse2_pslli_q:
6025 case Intrinsic::x86_sse2_psrli_w:
6026 case Intrinsic::x86_sse2_psrli_d:
6027 case Intrinsic::x86_sse2_psrli_q:
6028 case Intrinsic::x86_sse2_psrai_w:
6029 case Intrinsic::x86_sse2_psrai_d:
6030 case Intrinsic::x86_mmx_pslli_w:
6031 case Intrinsic::x86_mmx_pslli_d:
6032 case Intrinsic::x86_mmx_pslli_q:
6033 case Intrinsic::x86_mmx_psrli_w:
6034 case Intrinsic::x86_mmx_psrli_d:
6035 case Intrinsic::x86_mmx_psrli_q:
6036 case Intrinsic::x86_mmx_psrai_w:
6037 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006038 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006039 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006040 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006041
6042 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006043 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006044 switch (IntNo) {
6045 case Intrinsic::x86_sse2_pslli_w:
6046 NewIntNo = Intrinsic::x86_sse2_psll_w;
6047 break;
6048 case Intrinsic::x86_sse2_pslli_d:
6049 NewIntNo = Intrinsic::x86_sse2_psll_d;
6050 break;
6051 case Intrinsic::x86_sse2_pslli_q:
6052 NewIntNo = Intrinsic::x86_sse2_psll_q;
6053 break;
6054 case Intrinsic::x86_sse2_psrli_w:
6055 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6056 break;
6057 case Intrinsic::x86_sse2_psrli_d:
6058 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6059 break;
6060 case Intrinsic::x86_sse2_psrli_q:
6061 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6062 break;
6063 case Intrinsic::x86_sse2_psrai_w:
6064 NewIntNo = Intrinsic::x86_sse2_psra_w;
6065 break;
6066 case Intrinsic::x86_sse2_psrai_d:
6067 NewIntNo = Intrinsic::x86_sse2_psra_d;
6068 break;
6069 default: {
6070 ShAmtVT = MVT::v2i32;
6071 switch (IntNo) {
6072 case Intrinsic::x86_mmx_pslli_w:
6073 NewIntNo = Intrinsic::x86_mmx_psll_w;
6074 break;
6075 case Intrinsic::x86_mmx_pslli_d:
6076 NewIntNo = Intrinsic::x86_mmx_psll_d;
6077 break;
6078 case Intrinsic::x86_mmx_pslli_q:
6079 NewIntNo = Intrinsic::x86_mmx_psll_q;
6080 break;
6081 case Intrinsic::x86_mmx_psrli_w:
6082 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6083 break;
6084 case Intrinsic::x86_mmx_psrli_d:
6085 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6086 break;
6087 case Intrinsic::x86_mmx_psrli_q:
6088 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6089 break;
6090 case Intrinsic::x86_mmx_psrai_w:
6091 NewIntNo = Intrinsic::x86_mmx_psra_w;
6092 break;
6093 case Intrinsic::x86_mmx_psrai_d:
6094 NewIntNo = Intrinsic::x86_mmx_psra_d;
6095 break;
6096 default: abort(); // Can't reach here.
6097 }
6098 break;
6099 }
6100 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006101 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006102 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6103 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6104 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006105 DAG.getConstant(NewIntNo, MVT::i32),
6106 Op.getOperand(1), ShAmt);
6107 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006108 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006109}
Evan Cheng72261582005-12-20 06:22:03 +00006110
Dan Gohman475871a2008-07-27 21:46:04 +00006111SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006112 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006113 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006114
6115 if (Depth > 0) {
6116 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6117 SDValue Offset =
6118 DAG.getConstant(TD->getPointerSize(),
6119 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006120 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6121 DAG.getNode(ISD::ADD, dl, getPointerTy(),
6122 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006123 NULL, 0);
6124 }
6125
6126 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006127 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006128 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
6129 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006130}
6131
Dan Gohman475871a2008-07-27 21:46:04 +00006132SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006133 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6134 MFI->setFrameAddressIsTaken(true);
6135 MVT VT = Op.getValueType();
Dale Johannesendd64c412009-02-04 00:33:20 +00006136 DebugLoc dl = Op.getNode()->getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006137 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6138 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006139 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006140 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006141 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006142 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006143}
6144
Dan Gohman475871a2008-07-27 21:46:04 +00006145SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006146 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006147 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006148}
6149
Dan Gohman475871a2008-07-27 21:46:04 +00006150SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006151{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006152 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006153 SDValue Chain = Op.getOperand(0);
6154 SDValue Offset = Op.getOperand(1);
6155 SDValue Handler = Op.getOperand(2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006156 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006157
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006158 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6159 getPointerTy());
6160 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006161
Dale Johannesene4d209d2009-02-03 20:21:25 +00006162 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006163 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006164 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6165 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006166 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006167 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006168
Dale Johannesene4d209d2009-02-03 20:21:25 +00006169 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006170 MVT::Other,
6171 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006172}
6173
Dan Gohman475871a2008-07-27 21:46:04 +00006174SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006175 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006176 SDValue Root = Op.getOperand(0);
6177 SDValue Trmp = Op.getOperand(1); // trampoline
6178 SDValue FPtr = Op.getOperand(2); // nested function
6179 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesene4d209d2009-02-03 20:21:25 +00006180 DebugLoc dl = Op.getNode()->getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006181
Dan Gohman69de1932008-02-06 22:27:42 +00006182 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006183
Duncan Sands339e14f2008-01-16 22:55:25 +00006184 const X86InstrInfo *TII =
6185 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6186
Duncan Sandsb116fac2007-07-27 20:02:49 +00006187 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006188 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006189
6190 // Large code-model.
6191
6192 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6193 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6194
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006195 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6196 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006197
6198 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6199
6200 // Load the pointer to the nested function into R11.
6201 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006202 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006203 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6204 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006205
Dale Johannesene4d209d2009-02-03 20:21:25 +00006206 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6207 DAG.getConstant(2, MVT::i64));
6208 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006209
6210 // Load the 'nest' parameter value into R10.
6211 // R10 is specified in X86CallingConv.td
6212 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Dale Johannesene4d209d2009-02-03 20:21:25 +00006213 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6214 DAG.getConstant(10, MVT::i64));
6215 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6216 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006217
Dale Johannesene4d209d2009-02-03 20:21:25 +00006218 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6219 DAG.getConstant(12, MVT::i64));
6220 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006221
6222 // Jump to the nested function.
6223 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Dale Johannesene4d209d2009-02-03 20:21:25 +00006224 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6225 DAG.getConstant(20, MVT::i64));
6226 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6227 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006228
6229 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Dale Johannesene4d209d2009-02-03 20:21:25 +00006230 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6231 DAG.getConstant(22, MVT::i64));
6232 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006233 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006234
Dan Gohman475871a2008-07-27 21:46:04 +00006235 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006236 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6237 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006238 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006239 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006240 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6241 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006242 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006243
6244 switch (CC) {
6245 default:
6246 assert(0 && "Unsupported calling convention");
6247 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006248 case CallingConv::X86_StdCall: {
6249 // Pass 'nest' parameter in ECX.
6250 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006251 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006252
6253 // Check that ECX wasn't needed by an 'inreg' parameter.
6254 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006255 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006256
Chris Lattner58d74912008-03-12 17:45:29 +00006257 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006258 unsigned InRegCount = 0;
6259 unsigned Idx = 1;
6260
6261 for (FunctionType::param_iterator I = FTy->param_begin(),
6262 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006263 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006264 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006265 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006266
6267 if (InRegCount > 2) {
6268 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6269 abort();
6270 }
6271 }
6272 break;
6273 }
6274 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006275 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006276 // Pass 'nest' parameter in EAX.
6277 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006278 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006279 break;
6280 }
6281
Dan Gohman475871a2008-07-27 21:46:04 +00006282 SDValue OutChains[4];
6283 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006284
Dale Johannesene4d209d2009-02-03 20:21:25 +00006285 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6286 DAG.getConstant(10, MVT::i32));
6287 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006288
Duncan Sands339e14f2008-01-16 22:55:25 +00006289 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006290 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006291 OutChains[0] = DAG.getStore(Root, dl,
6292 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006293 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006294
Dale Johannesene4d209d2009-02-03 20:21:25 +00006295 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6296 DAG.getConstant(1, MVT::i32));
6297 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006298
Duncan Sands339e14f2008-01-16 22:55:25 +00006299 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006300 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6301 DAG.getConstant(5, MVT::i32));
6302 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006303 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006304
Dale Johannesene4d209d2009-02-03 20:21:25 +00006305 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6306 DAG.getConstant(6, MVT::i32));
6307 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006308
Dan Gohman475871a2008-07-27 21:46:04 +00006309 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006310 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6311 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006312 }
6313}
6314
Dan Gohman475871a2008-07-27 21:46:04 +00006315SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006316 /*
6317 The rounding mode is in bits 11:10 of FPSR, and has the following
6318 settings:
6319 00 Round to nearest
6320 01 Round to -inf
6321 10 Round to +inf
6322 11 Round to 0
6323
6324 FLT_ROUNDS, on the other hand, expects the following:
6325 -1 Undefined
6326 0 Round to 0
6327 1 Round to nearest
6328 2 Round to +inf
6329 3 Round to -inf
6330
6331 To perform the conversion, we do:
6332 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6333 */
6334
6335 MachineFunction &MF = DAG.getMachineFunction();
6336 const TargetMachine &TM = MF.getTarget();
6337 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6338 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006339 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006340 DebugLoc dl = Op.getNode()->getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006341
6342 // Save FP Control Word to stack slot
6343 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006344 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006345
Dale Johannesene4d209d2009-02-03 20:21:25 +00006346 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006347 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006348
6349 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006350 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006351
6352 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006353 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006354 DAG.getNode(ISD::SRL, dl, MVT::i16,
6355 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006356 CWD, DAG.getConstant(0x800, MVT::i16)),
6357 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006358 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006359 DAG.getNode(ISD::SRL, dl, MVT::i16,
6360 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006361 CWD, DAG.getConstant(0x400, MVT::i16)),
6362 DAG.getConstant(9, MVT::i8));
6363
Dan Gohman475871a2008-07-27 21:46:04 +00006364 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006365 DAG.getNode(ISD::AND, dl, MVT::i16,
6366 DAG.getNode(ISD::ADD, dl, MVT::i16,
6367 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006368 DAG.getConstant(1, MVT::i16)),
6369 DAG.getConstant(3, MVT::i16));
6370
6371
Duncan Sands83ec4b62008-06-06 12:08:01 +00006372 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006373 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6374}
6375
Dan Gohman475871a2008-07-27 21:46:04 +00006376SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006377 MVT VT = Op.getValueType();
6378 MVT OpVT = VT;
6379 unsigned NumBits = VT.getSizeInBits();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006380 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006381
6382 Op = Op.getOperand(0);
6383 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006384 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006385 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006386 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006387 }
Evan Cheng18efe262007-12-14 02:13:44 +00006388
Evan Cheng152804e2007-12-14 08:30:15 +00006389 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6390 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006391 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006392
6393 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006394 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006395 Ops.push_back(Op);
6396 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6397 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6398 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006399 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006400
6401 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006402 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006403
Evan Cheng18efe262007-12-14 02:13:44 +00006404 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006405 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006406 return Op;
6407}
6408
Dan Gohman475871a2008-07-27 21:46:04 +00006409SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006410 MVT VT = Op.getValueType();
6411 MVT OpVT = VT;
6412 unsigned NumBits = VT.getSizeInBits();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006413 DebugLoc dl = Op.getNode()->getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006414
6415 Op = Op.getOperand(0);
6416 if (VT == MVT::i8) {
6417 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006418 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006419 }
Evan Cheng152804e2007-12-14 08:30:15 +00006420
6421 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6422 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006423 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006424
6425 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006426 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006427 Ops.push_back(Op);
6428 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6429 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6430 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006431 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006432
Evan Cheng18efe262007-12-14 02:13:44 +00006433 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006434 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006435 return Op;
6436}
6437
Mon P Wangaf9b9522008-12-18 21:42:19 +00006438SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6439 MVT VT = Op.getValueType();
6440 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesene4d209d2009-02-03 20:21:25 +00006441 DebugLoc dl = Op.getNode()->getDebugLoc();
Mon P Wangaf9b9522008-12-18 21:42:19 +00006442
6443 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6444 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6445 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6446 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6447 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6448 //
6449 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6450 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6451 // return AloBlo + AloBhi + AhiBlo;
6452
6453 SDValue A = Op.getOperand(0);
6454 SDValue B = Op.getOperand(1);
6455
Dale Johannesene4d209d2009-02-03 20:21:25 +00006456 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006457 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6458 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006459 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006460 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6461 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006462 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006463 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6464 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006465 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006466 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6467 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006468 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006469 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6470 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006471 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006472 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6473 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006474 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006475 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6476 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006477 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6478 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006479 return Res;
6480}
6481
6482
Bill Wendling74c37652008-12-09 22:08:41 +00006483SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6484 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6485 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006486 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6487 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006488 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006489 SDValue LHS = N->getOperand(0);
6490 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006491 unsigned BaseOp = 0;
6492 unsigned Cond = 0;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006493 DebugLoc dl = Op.getNode()->getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006494
6495 switch (Op.getOpcode()) {
6496 default: assert(0 && "Unknown ovf instruction!");
6497 case ISD::SADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006498 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006499 Cond = X86::COND_O;
6500 break;
6501 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006502 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006503 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006504 break;
6505 case ISD::SSUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006506 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006507 Cond = X86::COND_O;
6508 break;
6509 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006510 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006511 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006512 break;
6513 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006514 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006515 Cond = X86::COND_O;
6516 break;
6517 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006518 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006519 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006520 break;
6521 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006522
Bill Wendling61edeb52008-12-02 01:06:39 +00006523 // Also sets EFLAGS.
6524 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006525 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006526
Bill Wendling61edeb52008-12-02 01:06:39 +00006527 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006528 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006529 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006530
Bill Wendling61edeb52008-12-02 01:06:39 +00006531 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6532 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006533}
6534
Dan Gohman475871a2008-07-27 21:46:04 +00006535SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006536 MVT T = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006537 DebugLoc dl = Op.getNode()->getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006538 unsigned Reg = 0;
6539 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006540 switch(T.getSimpleVT()) {
6541 default:
6542 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006543 case MVT::i8: Reg = X86::AL; size = 1; break;
6544 case MVT::i16: Reg = X86::AX; size = 2; break;
6545 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006546 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006547 assert(Subtarget->is64Bit() && "Node not type legal!");
6548 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006549 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006550 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006551 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006552 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006553 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006554 Op.getOperand(1),
6555 Op.getOperand(3),
6556 DAG.getTargetConstant(size, MVT::i8),
6557 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006558 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006559 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Dan Gohman475871a2008-07-27 21:46:04 +00006560 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006561 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006562 return cpOut;
6563}
6564
Duncan Sands1607f052008-12-01 11:39:25 +00006565SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006566 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006567 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006568 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006569 SDValue TheChain = Op.getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006570 DebugLoc dl = Op.getNode()->getDebugLoc();
6571 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006572 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6573 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006574 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006575 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006576 DAG.getConstant(32, MVT::i8));
6577 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006578 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006579 rdx.getValue(1)
6580 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006581 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006582}
6583
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006584SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6585 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006586 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006587 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006588 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006589 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006590 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006591 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006592 Node->getOperand(0),
6593 Node->getOperand(1), negOp,
6594 cast<AtomicSDNode>(Node)->getSrcValue(),
6595 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006596}
6597
Evan Cheng0db9fe62006-04-25 20:13:52 +00006598/// LowerOperation - Provide custom lowering hooks for some operations.
6599///
Dan Gohman475871a2008-07-27 21:46:04 +00006600SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006601 switch (Op.getOpcode()) {
6602 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006603 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6604 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6606 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6607 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6608 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6609 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6610 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6611 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006612 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006613 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006614 case ISD::SHL_PARTS:
6615 case ISD::SRA_PARTS:
6616 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6617 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006618 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006619 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6620 case ISD::FABS: return LowerFABS(Op, DAG);
6621 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006622 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006623 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006624 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006625 case ISD::SELECT: return LowerSELECT(Op, DAG);
6626 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006627 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006628 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006629 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006630 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006631 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006632 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006633 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006634 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006635 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6636 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006637 case ISD::FRAME_TO_ARGS_OFFSET:
6638 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006639 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006640 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006641 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006642 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006643 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6644 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006645 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006646 case ISD::SADDO:
6647 case ISD::UADDO:
6648 case ISD::SSUBO:
6649 case ISD::USUBO:
6650 case ISD::SMULO:
6651 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006652 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006653 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006654}
6655
Duncan Sands1607f052008-12-01 11:39:25 +00006656void X86TargetLowering::
6657ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6658 SelectionDAG &DAG, unsigned NewOp) {
6659 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006660 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006661 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6662
6663 SDValue Chain = Node->getOperand(0);
6664 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006665 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006666 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006667 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006668 Node->getOperand(2), DAG.getIntPtrConstant(1));
6669 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6670 // have a MemOperand. Pass the info through as a normal operand.
6671 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6672 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6673 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006674 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006675 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006676 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006677 Results.push_back(Result.getValue(2));
6678}
6679
Duncan Sands126d9072008-07-04 11:47:58 +00006680/// ReplaceNodeResults - Replace a node with an illegal result type
6681/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006682void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6683 SmallVectorImpl<SDValue>&Results,
6684 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006685 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006686 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006687 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006688 assert(false && "Do not know how to custom type legalize this operation!");
6689 return;
6690 case ISD::FP_TO_SINT: {
6691 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6692 SDValue FIST = Vals.first, StackSlot = Vals.second;
6693 if (FIST.getNode() != 0) {
6694 MVT VT = N->getValueType(0);
6695 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006696 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006697 }
6698 return;
6699 }
6700 case ISD::READCYCLECOUNTER: {
6701 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6702 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006703 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006704 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
6705 rd.getValue(1));
6706 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006707 eax.getValue(2));
6708 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6709 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006710 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006711 Results.push_back(edx.getValue(1));
6712 return;
6713 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006714 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006715 MVT T = N->getValueType(0);
6716 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6717 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006718 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006719 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006720 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006721 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006722 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6723 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006724 cpInL.getValue(1));
6725 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006726 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006727 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006728 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006729 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006730 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006731 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006732 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006733 swapInL.getValue(1));
6734 SDValue Ops[] = { swapInH.getValue(0),
6735 N->getOperand(1),
6736 swapInH.getValue(1) };
6737 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006738 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006739 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6740 MVT::i32, Result.getValue(1));
6741 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6742 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006743 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006744 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006745 Results.push_back(cpOutH.getValue(1));
6746 return;
6747 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006748 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006749 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6750 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006751 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006752 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6753 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006754 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006755 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6756 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006757 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006758 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6759 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006760 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006761 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6762 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006763 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006764 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6765 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006766 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006767 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6768 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006769 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006770}
6771
Evan Cheng72261582005-12-20 06:22:03 +00006772const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6773 switch (Opcode) {
6774 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006775 case X86ISD::BSF: return "X86ISD::BSF";
6776 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006777 case X86ISD::SHLD: return "X86ISD::SHLD";
6778 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006779 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006780 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006781 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006782 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006783 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006784 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006785 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6786 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6787 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006788 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006789 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006790 case X86ISD::CALL: return "X86ISD::CALL";
6791 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6792 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006793 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006794 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006795 case X86ISD::COMI: return "X86ISD::COMI";
6796 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006797 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006798 case X86ISD::CMOV: return "X86ISD::CMOV";
6799 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006800 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006801 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6802 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006803 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006804 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006805 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006806 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006807 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6808 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006809 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00006810 case X86ISD::FMAX: return "X86ISD::FMAX";
6811 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006812 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6813 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006814 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6815 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006816 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006817 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006818 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006819 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6820 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006821 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6822 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6823 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6824 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6825 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6826 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006827 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6828 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006829 case X86ISD::VSHL: return "X86ISD::VSHL";
6830 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006831 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6832 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6833 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6834 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6835 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6836 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6837 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6838 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6839 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6840 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006841 case X86ISD::ADD: return "X86ISD::ADD";
6842 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006843 case X86ISD::SMUL: return "X86ISD::SMUL";
6844 case X86ISD::UMUL: return "X86ISD::UMUL";
Evan Cheng72261582005-12-20 06:22:03 +00006845 }
6846}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006847
Chris Lattnerc9addb72007-03-30 23:15:24 +00006848// isLegalAddressingMode - Return true if the addressing mode represented
6849// by AM is legal for this target, for a load/store of the specified type.
6850bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6851 const Type *Ty) const {
6852 // X86 supports extremely general addressing modes.
6853
6854 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6855 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6856 return false;
6857
6858 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006859 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006860 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6861 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006862 // If BaseGV requires a register, we cannot also have a BaseReg.
6863 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6864 AM.HasBaseReg)
6865 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006866
6867 // X86-64 only supports addr of globals in small code model.
6868 if (Subtarget->is64Bit()) {
6869 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6870 return false;
6871 // If lower 4G is not available, then we must use rip-relative addressing.
6872 if (AM.BaseOffs || AM.Scale > 1)
6873 return false;
6874 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006875 }
6876
6877 switch (AM.Scale) {
6878 case 0:
6879 case 1:
6880 case 2:
6881 case 4:
6882 case 8:
6883 // These scales always work.
6884 break;
6885 case 3:
6886 case 5:
6887 case 9:
6888 // These scales are formed with basereg+scalereg. Only accept if there is
6889 // no basereg yet.
6890 if (AM.HasBaseReg)
6891 return false;
6892 break;
6893 default: // Other stuff never works.
6894 return false;
6895 }
6896
6897 return true;
6898}
6899
6900
Evan Cheng2bd122c2007-10-26 01:56:11 +00006901bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6902 if (!Ty1->isInteger() || !Ty2->isInteger())
6903 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006904 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6905 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006906 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006907 return false;
6908 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006909}
6910
Duncan Sands83ec4b62008-06-06 12:08:01 +00006911bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6912 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006913 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006914 unsigned NumBits1 = VT1.getSizeInBits();
6915 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006916 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006917 return false;
6918 return Subtarget->is64Bit() || NumBits1 < 64;
6919}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006920
Evan Cheng60c07e12006-07-05 22:17:51 +00006921/// isShuffleMaskLegal - Targets can use this to indicate that they only
6922/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6923/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6924/// are assumed to be legal.
6925bool
Dan Gohman475871a2008-07-27 21:46:04 +00006926X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006927 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006928 if (VT.getSizeInBits() == 64) return false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006929 return (Mask.getNode()->getNumOperands() <= 4 ||
6930 isIdentityMask(Mask.getNode()) ||
6931 isIdentityMask(Mask.getNode(), true) ||
6932 isSplatMask(Mask.getNode()) ||
6933 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6934 X86::isUNPCKLMask(Mask.getNode()) ||
6935 X86::isUNPCKHMask(Mask.getNode()) ||
6936 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6937 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Evan Cheng60c07e12006-07-05 22:17:51 +00006938}
6939
Dan Gohman7d8143f2008-04-09 20:09:42 +00006940bool
Dan Gohman475871a2008-07-27 21:46:04 +00006941X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006942 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006943 unsigned NumElts = BVOps.size();
6944 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006945 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006946 if (NumElts == 2) return true;
6947 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006948 return (isMOVLMask(&BVOps[0], 4) ||
6949 isCommutedMOVL(&BVOps[0], 4, true) ||
6950 isSHUFPMask(&BVOps[0], 4) ||
6951 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006952 }
6953 return false;
6954}
6955
6956//===----------------------------------------------------------------------===//
6957// X86 Scheduler Hooks
6958//===----------------------------------------------------------------------===//
6959
Mon P Wang63307c32008-05-05 19:05:59 +00006960// private utility function
6961MachineBasicBlock *
6962X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6963 MachineBasicBlock *MBB,
6964 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006965 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006966 unsigned LoadOpc,
6967 unsigned CXchgOpc,
6968 unsigned copyOpc,
6969 unsigned notOpc,
6970 unsigned EAXreg,
6971 TargetRegisterClass *RC,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006972 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006973 // For the atomic bitwise operator, we generate
6974 // thisMBB:
6975 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006976 // ld t1 = [bitinstr.addr]
6977 // op t2 = t1, [bitinstr.val]
6978 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006979 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6980 // bz newMBB
6981 // fallthrough -->nextMBB
6982 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6983 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006984 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006985 ++MBBIter;
6986
6987 /// First build the CFG
6988 MachineFunction *F = MBB->getParent();
6989 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006990 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6991 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6992 F->insert(MBBIter, newMBB);
6993 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006994
6995 // Move all successors to thisMBB to nextMBB
6996 nextMBB->transferSuccessors(thisMBB);
6997
6998 // Update thisMBB to fall through to newMBB
6999 thisMBB->addSuccessor(newMBB);
7000
7001 // newMBB jumps to itself and fall through to nextMBB
7002 newMBB->addSuccessor(nextMBB);
7003 newMBB->addSuccessor(newMBB);
7004
7005 // Insert instructions into newMBB based on incoming instruction
7006 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007007 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007008 MachineOperand& destOper = bInstr->getOperand(0);
7009 MachineOperand* argOpers[6];
7010 int numArgs = bInstr->getNumOperands() - 1;
7011 for (int i=0; i < numArgs; ++i)
7012 argOpers[i] = &bInstr->getOperand(i+1);
7013
7014 // x86 address has 4 operands: base, index, scale, and displacement
7015 int lastAddrIndx = 3; // [0,3]
7016 int valArgIndx = 4;
7017
Dale Johannesen140be2d2008-08-19 18:47:28 +00007018 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007019 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007020 for (int i=0; i <= lastAddrIndx; ++i)
7021 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007022
Dale Johannesen140be2d2008-08-19 18:47:28 +00007023 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007024 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007025 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007026 }
7027 else
7028 tt = t1;
7029
Dale Johannesen140be2d2008-08-19 18:47:28 +00007030 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007031 assert((argOpers[valArgIndx]->isReg() ||
7032 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007033 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007034 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007035 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007036 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007037 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007038 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007039 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007040
Dale Johannesene4d209d2009-02-03 20:21:25 +00007041 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007042 MIB.addReg(t1);
7043
Dale Johannesene4d209d2009-02-03 20:21:25 +00007044 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007045 for (int i=0; i <= lastAddrIndx; ++i)
7046 (*MIB).addOperand(*argOpers[i]);
7047 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007048 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7049 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7050
Dale Johannesene4d209d2009-02-03 20:21:25 +00007051 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007052 MIB.addReg(EAXreg);
Mon P Wang63307c32008-05-05 19:05:59 +00007053
7054 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007055 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007056
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007057 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007058 return nextMBB;
7059}
7060
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007061// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007062MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007063X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7064 MachineBasicBlock *MBB,
7065 unsigned regOpcL,
7066 unsigned regOpcH,
7067 unsigned immOpcL,
7068 unsigned immOpcH,
7069 bool invSrc) {
7070 // For the atomic bitwise operator, we generate
7071 // thisMBB (instructions are in pairs, except cmpxchg8b)
7072 // ld t1,t2 = [bitinstr.addr]
7073 // newMBB:
7074 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7075 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007076 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007077 // mov ECX, EBX <- t5, t6
7078 // mov EAX, EDX <- t1, t2
7079 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7080 // mov t3, t4 <- EAX, EDX
7081 // bz newMBB
7082 // result in out1, out2
7083 // fallthrough -->nextMBB
7084
7085 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7086 const unsigned LoadOpc = X86::MOV32rm;
7087 const unsigned copyOpc = X86::MOV32rr;
7088 const unsigned NotOpc = X86::NOT32r;
7089 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7090 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7091 MachineFunction::iterator MBBIter = MBB;
7092 ++MBBIter;
7093
7094 /// First build the CFG
7095 MachineFunction *F = MBB->getParent();
7096 MachineBasicBlock *thisMBB = MBB;
7097 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7098 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7099 F->insert(MBBIter, newMBB);
7100 F->insert(MBBIter, nextMBB);
7101
7102 // Move all successors to thisMBB to nextMBB
7103 nextMBB->transferSuccessors(thisMBB);
7104
7105 // Update thisMBB to fall through to newMBB
7106 thisMBB->addSuccessor(newMBB);
7107
7108 // newMBB jumps to itself and fall through to nextMBB
7109 newMBB->addSuccessor(nextMBB);
7110 newMBB->addSuccessor(newMBB);
7111
Dale Johannesene4d209d2009-02-03 20:21:25 +00007112 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007113 // Insert instructions into newMBB based on incoming instruction
7114 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
7115 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
7116 MachineOperand& dest1Oper = bInstr->getOperand(0);
7117 MachineOperand& dest2Oper = bInstr->getOperand(1);
7118 MachineOperand* argOpers[6];
7119 for (int i=0; i < 6; ++i)
7120 argOpers[i] = &bInstr->getOperand(i+2);
7121
7122 // x86 address has 4 operands: base, index, scale, and displacement
7123 int lastAddrIndx = 3; // [0,3]
7124
7125 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007126 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007127 for (int i=0; i <= lastAddrIndx; ++i)
7128 (*MIB).addOperand(*argOpers[i]);
7129 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007130 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007131 // add 4 to displacement.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007132 for (int i=0; i <= lastAddrIndx-1; ++i)
7133 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007134 MachineOperand newOp3 = *(argOpers[3]);
7135 if (newOp3.isImm())
7136 newOp3.setImm(newOp3.getImm()+4);
7137 else
7138 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007139 (*MIB).addOperand(newOp3);
7140
7141 // t3/4 are defined later, at the bottom of the loop
7142 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7143 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007144 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007145 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007146 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007147 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7148
7149 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7150 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7151 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007152 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7153 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007154 } else {
7155 tt1 = t1;
7156 tt2 = t2;
7157 }
7158
Dan Gohmand735b802008-10-03 15:45:36 +00007159 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007160 "invalid operand");
7161 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7162 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007163 if (argOpers[4]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007164 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007165 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007166 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007167 if (regOpcL != X86::MOV32rr)
7168 MIB.addReg(tt1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007169 (*MIB).addOperand(*argOpers[4]);
Dan Gohmand735b802008-10-03 15:45:36 +00007170 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7171 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7172 if (argOpers[5]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007173 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007174 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007175 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007176 if (regOpcH != X86::MOV32rr)
7177 MIB.addReg(tt2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007178 (*MIB).addOperand(*argOpers[5]);
7179
Dale Johannesene4d209d2009-02-03 20:21:25 +00007180 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007181 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007182 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007183 MIB.addReg(t2);
7184
Dale Johannesene4d209d2009-02-03 20:21:25 +00007185 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007186 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007187 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007188 MIB.addReg(t6);
7189
Dale Johannesene4d209d2009-02-03 20:21:25 +00007190 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007191 for (int i=0; i <= lastAddrIndx; ++i)
7192 (*MIB).addOperand(*argOpers[i]);
7193
7194 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7195 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7196
Dale Johannesene4d209d2009-02-03 20:21:25 +00007197 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007198 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007199 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007200 MIB.addReg(X86::EDX);
7201
7202 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007203 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007204
7205 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7206 return nextMBB;
7207}
7208
7209// private utility function
7210MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007211X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7212 MachineBasicBlock *MBB,
7213 unsigned cmovOpc) {
7214 // For the atomic min/max operator, we generate
7215 // thisMBB:
7216 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007217 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00007218 // mov t2 = [min/max.val]
7219 // cmp t1, t2
7220 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007221 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007222 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7223 // bz newMBB
7224 // fallthrough -->nextMBB
7225 //
7226 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7227 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007228 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007229 ++MBBIter;
7230
7231 /// First build the CFG
7232 MachineFunction *F = MBB->getParent();
7233 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007234 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7235 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7236 F->insert(MBBIter, newMBB);
7237 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007238
7239 // Move all successors to thisMBB to nextMBB
7240 nextMBB->transferSuccessors(thisMBB);
7241
7242 // Update thisMBB to fall through to newMBB
7243 thisMBB->addSuccessor(newMBB);
7244
7245 // newMBB jumps to newMBB and fall through to nextMBB
7246 newMBB->addSuccessor(nextMBB);
7247 newMBB->addSuccessor(newMBB);
7248
Dale Johannesene4d209d2009-02-03 20:21:25 +00007249 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007250 // Insert instructions into newMBB based on incoming instruction
7251 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7252 MachineOperand& destOper = mInstr->getOperand(0);
7253 MachineOperand* argOpers[6];
7254 int numArgs = mInstr->getNumOperands() - 1;
7255 for (int i=0; i < numArgs; ++i)
7256 argOpers[i] = &mInstr->getOperand(i+1);
7257
7258 // x86 address has 4 operands: base, index, scale, and displacement
7259 int lastAddrIndx = 3; // [0,3]
7260 int valArgIndx = 4;
7261
Mon P Wangab3e7472008-05-05 22:56:23 +00007262 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007263 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007264 for (int i=0; i <= lastAddrIndx; ++i)
7265 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007266
Mon P Wang63307c32008-05-05 19:05:59 +00007267 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007268 assert((argOpers[valArgIndx]->isReg() ||
7269 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007270 "invalid operand");
Mon P Wang63307c32008-05-05 19:05:59 +00007271
7272 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007273 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007274 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007275 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007276 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007277 (*MIB).addOperand(*argOpers[valArgIndx]);
7278
Dale Johannesene4d209d2009-02-03 20:21:25 +00007279 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007280 MIB.addReg(t1);
7281
Dale Johannesene4d209d2009-02-03 20:21:25 +00007282 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007283 MIB.addReg(t1);
7284 MIB.addReg(t2);
7285
7286 // Generate movc
7287 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007288 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007289 MIB.addReg(t2);
7290 MIB.addReg(t1);
7291
7292 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007293 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007294 for (int i=0; i <= lastAddrIndx; ++i)
7295 (*MIB).addOperand(*argOpers[i]);
7296 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007297 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7298 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00007299
Dale Johannesene4d209d2009-02-03 20:21:25 +00007300 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007301 MIB.addReg(X86::EAX);
7302
7303 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007304 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007305
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007306 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007307 return nextMBB;
7308}
7309
7310
Evan Cheng60c07e12006-07-05 22:17:51 +00007311MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007312X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7313 MachineBasicBlock *BB) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007314 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007315 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007316 switch (MI->getOpcode()) {
7317 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007318 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007319 case X86::CMOV_FR32:
7320 case X86::CMOV_FR64:
7321 case X86::CMOV_V4F32:
7322 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007323 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007324 // To "insert" a SELECT_CC instruction, we actually have to insert the
7325 // diamond control-flow pattern. The incoming instruction knows the
7326 // destination vreg to set, the condition code register to branch on, the
7327 // true/false values to select between, and a branch opcode to use.
7328 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007329 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007330 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007331
Evan Cheng60c07e12006-07-05 22:17:51 +00007332 // thisMBB:
7333 // ...
7334 // TrueVal = ...
7335 // cmpTY ccX, r1, r2
7336 // bCC copy1MBB
7337 // fallthrough --> copy0MBB
7338 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007339 MachineFunction *F = BB->getParent();
7340 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7341 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007342 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007343 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007344 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007345 F->insert(It, copy0MBB);
7346 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007347 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007348 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007349 sinkMBB->transferSuccessors(BB);
7350
7351 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007352 BB->addSuccessor(copy0MBB);
7353 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007354
Evan Cheng60c07e12006-07-05 22:17:51 +00007355 // copy0MBB:
7356 // %FalseValue = ...
7357 // # fallthrough to sinkMBB
7358 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007359
Evan Cheng60c07e12006-07-05 22:17:51 +00007360 // Update machine-CFG edges
7361 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007362
Evan Cheng60c07e12006-07-05 22:17:51 +00007363 // sinkMBB:
7364 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7365 // ...
7366 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007367 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007368 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7369 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7370
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007371 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007372 return BB;
7373 }
7374
Dale Johannesen849f2142007-07-03 00:53:03 +00007375 case X86::FP32_TO_INT16_IN_MEM:
7376 case X86::FP32_TO_INT32_IN_MEM:
7377 case X86::FP32_TO_INT64_IN_MEM:
7378 case X86::FP64_TO_INT16_IN_MEM:
7379 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007380 case X86::FP64_TO_INT64_IN_MEM:
7381 case X86::FP80_TO_INT16_IN_MEM:
7382 case X86::FP80_TO_INT32_IN_MEM:
7383 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007384 // Change the floating point control register to use "round towards zero"
7385 // mode when truncating to an integer value.
7386 MachineFunction *F = BB->getParent();
7387 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007388 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007389
7390 // Load the old value of the high byte of the control word...
7391 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007392 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007393 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
7394 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007395
7396 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007397 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007398 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007399
7400 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007401 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007402
7403 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007404 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007405 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007406
7407 // Get the X86 opcode to use.
7408 unsigned Opc;
7409 switch (MI->getOpcode()) {
7410 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007411 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7412 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7413 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7414 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7415 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7416 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007417 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7418 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7419 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007420 }
7421
7422 X86AddressMode AM;
7423 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007424 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007425 AM.BaseType = X86AddressMode::RegBase;
7426 AM.Base.Reg = Op.getReg();
7427 } else {
7428 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007429 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007430 }
7431 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007432 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007433 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007434 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007435 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007436 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007437 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007438 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007439 AM.GV = Op.getGlobal();
7440 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007441 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007442 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007443 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007444 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007445
7446 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007447 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007448
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007449 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007450 return BB;
7451 }
Mon P Wang63307c32008-05-05 19:05:59 +00007452 case X86::ATOMAND32:
7453 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007454 X86::AND32ri, X86::MOV32rm,
7455 X86::LCMPXCHG32, X86::MOV32rr,
7456 X86::NOT32r, X86::EAX,
7457 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007458 case X86::ATOMOR32:
7459 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007460 X86::OR32ri, X86::MOV32rm,
7461 X86::LCMPXCHG32, X86::MOV32rr,
7462 X86::NOT32r, X86::EAX,
7463 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007464 case X86::ATOMXOR32:
7465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007466 X86::XOR32ri, X86::MOV32rm,
7467 X86::LCMPXCHG32, X86::MOV32rr,
7468 X86::NOT32r, X86::EAX,
7469 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007470 case X86::ATOMNAND32:
7471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007472 X86::AND32ri, X86::MOV32rm,
7473 X86::LCMPXCHG32, X86::MOV32rr,
7474 X86::NOT32r, X86::EAX,
7475 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007476 case X86::ATOMMIN32:
7477 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7478 case X86::ATOMMAX32:
7479 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7480 case X86::ATOMUMIN32:
7481 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7482 case X86::ATOMUMAX32:
7483 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007484
7485 case X86::ATOMAND16:
7486 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7487 X86::AND16ri, X86::MOV16rm,
7488 X86::LCMPXCHG16, X86::MOV16rr,
7489 X86::NOT16r, X86::AX,
7490 X86::GR16RegisterClass);
7491 case X86::ATOMOR16:
7492 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7493 X86::OR16ri, X86::MOV16rm,
7494 X86::LCMPXCHG16, X86::MOV16rr,
7495 X86::NOT16r, X86::AX,
7496 X86::GR16RegisterClass);
7497 case X86::ATOMXOR16:
7498 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7499 X86::XOR16ri, X86::MOV16rm,
7500 X86::LCMPXCHG16, X86::MOV16rr,
7501 X86::NOT16r, X86::AX,
7502 X86::GR16RegisterClass);
7503 case X86::ATOMNAND16:
7504 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7505 X86::AND16ri, X86::MOV16rm,
7506 X86::LCMPXCHG16, X86::MOV16rr,
7507 X86::NOT16r, X86::AX,
7508 X86::GR16RegisterClass, true);
7509 case X86::ATOMMIN16:
7510 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7511 case X86::ATOMMAX16:
7512 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7513 case X86::ATOMUMIN16:
7514 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7515 case X86::ATOMUMAX16:
7516 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7517
7518 case X86::ATOMAND8:
7519 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7520 X86::AND8ri, X86::MOV8rm,
7521 X86::LCMPXCHG8, X86::MOV8rr,
7522 X86::NOT8r, X86::AL,
7523 X86::GR8RegisterClass);
7524 case X86::ATOMOR8:
7525 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7526 X86::OR8ri, X86::MOV8rm,
7527 X86::LCMPXCHG8, X86::MOV8rr,
7528 X86::NOT8r, X86::AL,
7529 X86::GR8RegisterClass);
7530 case X86::ATOMXOR8:
7531 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7532 X86::XOR8ri, X86::MOV8rm,
7533 X86::LCMPXCHG8, X86::MOV8rr,
7534 X86::NOT8r, X86::AL,
7535 X86::GR8RegisterClass);
7536 case X86::ATOMNAND8:
7537 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7538 X86::AND8ri, X86::MOV8rm,
7539 X86::LCMPXCHG8, X86::MOV8rr,
7540 X86::NOT8r, X86::AL,
7541 X86::GR8RegisterClass, true);
7542 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007543 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007544 case X86::ATOMAND64:
7545 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7546 X86::AND64ri32, X86::MOV64rm,
7547 X86::LCMPXCHG64, X86::MOV64rr,
7548 X86::NOT64r, X86::RAX,
7549 X86::GR64RegisterClass);
7550 case X86::ATOMOR64:
7551 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7552 X86::OR64ri32, X86::MOV64rm,
7553 X86::LCMPXCHG64, X86::MOV64rr,
7554 X86::NOT64r, X86::RAX,
7555 X86::GR64RegisterClass);
7556 case X86::ATOMXOR64:
7557 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7558 X86::XOR64ri32, X86::MOV64rm,
7559 X86::LCMPXCHG64, X86::MOV64rr,
7560 X86::NOT64r, X86::RAX,
7561 X86::GR64RegisterClass);
7562 case X86::ATOMNAND64:
7563 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7564 X86::AND64ri32, X86::MOV64rm,
7565 X86::LCMPXCHG64, X86::MOV64rr,
7566 X86::NOT64r, X86::RAX,
7567 X86::GR64RegisterClass, true);
7568 case X86::ATOMMIN64:
7569 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7570 case X86::ATOMMAX64:
7571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7572 case X86::ATOMUMIN64:
7573 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7574 case X86::ATOMUMAX64:
7575 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007576
7577 // This group does 64-bit operations on a 32-bit host.
7578 case X86::ATOMAND6432:
7579 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7580 X86::AND32rr, X86::AND32rr,
7581 X86::AND32ri, X86::AND32ri,
7582 false);
7583 case X86::ATOMOR6432:
7584 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7585 X86::OR32rr, X86::OR32rr,
7586 X86::OR32ri, X86::OR32ri,
7587 false);
7588 case X86::ATOMXOR6432:
7589 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7590 X86::XOR32rr, X86::XOR32rr,
7591 X86::XOR32ri, X86::XOR32ri,
7592 false);
7593 case X86::ATOMNAND6432:
7594 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7595 X86::AND32rr, X86::AND32rr,
7596 X86::AND32ri, X86::AND32ri,
7597 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007598 case X86::ATOMADD6432:
7599 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7600 X86::ADD32rr, X86::ADC32rr,
7601 X86::ADD32ri, X86::ADC32ri,
7602 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007603 case X86::ATOMSUB6432:
7604 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7605 X86::SUB32rr, X86::SBB32rr,
7606 X86::SUB32ri, X86::SBB32ri,
7607 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007608 case X86::ATOMSWAP6432:
7609 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7610 X86::MOV32rr, X86::MOV32rr,
7611 X86::MOV32ri, X86::MOV32ri,
7612 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007613 }
7614}
7615
7616//===----------------------------------------------------------------------===//
7617// X86 Optimization Hooks
7618//===----------------------------------------------------------------------===//
7619
Dan Gohman475871a2008-07-27 21:46:04 +00007620void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007621 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007622 APInt &KnownZero,
7623 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007624 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007625 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007626 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007627 assert((Opc >= ISD::BUILTIN_OP_END ||
7628 Opc == ISD::INTRINSIC_WO_CHAIN ||
7629 Opc == ISD::INTRINSIC_W_CHAIN ||
7630 Opc == ISD::INTRINSIC_VOID) &&
7631 "Should use MaskedValueIsZero if you don't know whether Op"
7632 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007633
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007634 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007635 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007636 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007637 case X86ISD::ADD:
7638 case X86ISD::SUB:
7639 case X86ISD::SMUL:
7640 case X86ISD::UMUL:
7641 // These nodes' second result is a boolean.
7642 if (Op.getResNo() == 0)
7643 break;
7644 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007645 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007646 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7647 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007648 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007649 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007650}
Chris Lattner259e97c2006-01-31 19:43:35 +00007651
Evan Cheng206ee9d2006-07-07 08:33:52 +00007652/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007653/// node is a GlobalAddress + offset.
7654bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7655 GlobalValue* &GA, int64_t &Offset) const{
7656 if (N->getOpcode() == X86ISD::Wrapper) {
7657 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007658 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007659 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007660 return true;
7661 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007662 }
Evan Chengad4196b2008-05-12 19:56:52 +00007663 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007664}
7665
Evan Chengad4196b2008-05-12 19:56:52 +00007666static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7667 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007668 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007669 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007670 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007671 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007672 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007673 return false;
7674}
7675
Dan Gohman475871a2008-07-27 21:46:04 +00007676static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007677 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00007678 SDNode *&Base,
7679 SelectionDAG &DAG, MachineFrameInfo *MFI,
7680 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007681 Base = NULL;
7682 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007683 SDValue Idx = PermMask.getOperand(i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00007684 if (Idx.getOpcode() == ISD::UNDEF) {
7685 if (!Base)
7686 return false;
7687 continue;
7688 }
7689
Dan Gohman475871a2008-07-27 21:46:04 +00007690 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007691 if (!Elt.getNode() ||
7692 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007693 return false;
7694 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007695 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007696 if (Base->getOpcode() == ISD::UNDEF)
7697 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007698 continue;
7699 }
7700 if (Elt.getOpcode() == ISD::UNDEF)
7701 continue;
7702
Gabor Greifba36cb52008-08-28 21:40:38 +00007703 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007704 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007705 return false;
7706 }
7707 return true;
7708}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007709
7710/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7711/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7712/// if the load addresses are consecutive, non-overlapping, and in the right
7713/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00007714static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00007715 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007716 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007717 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007718 MVT VT = N->getValueType(0);
7719 MVT EVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00007720 SDValue PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00007721 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007722 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00007723 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7724 DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007725 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007726
Dan Gohmand3006222007-07-27 17:16:43 +00007727 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007728 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007729 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7730 LD->getSrcValue(), LD->getSrcValueOffset(),
7731 LD->isVolatile());
7732 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7733 LD->getSrcValue(), LD->getSrcValueOffset(),
7734 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007735}
7736
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007737/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007738static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007739 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007740 const X86Subtarget *Subtarget,
7741 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007742 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007743 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007744
Evan Chengd880b972008-05-09 21:53:03 +00007745 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007746 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007747 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007748
Duncan Sands83ec4b62008-06-06 12:08:01 +00007749 MVT VT = N->getValueType(0);
7750 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00007751 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7752 // We are looking for load i64 and zero extend. We want to transform
7753 // it before legalizer has a chance to expand it. Also look for i64
7754 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00007755 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007756 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00007757 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00007758 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007759 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007760
7761 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00007762 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00007763 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007764 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00007765 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007766 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007767 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00007768 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007769 }
Evan Chengd880b972008-05-09 21:53:03 +00007770
7771 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007772 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00007773
7774 // Load must not be an extload.
7775 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00007776 return SDValue();
Mon P Wang7ad9b512009-01-30 07:07:40 +00007777
7778 // Load type should legal type so we don't have to legalize it.
7779 if (!TLI.isTypeLegal(VT))
7780 return SDValue();
7781
Evan Cheng8a186ae2008-09-24 23:26:36 +00007782 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7783 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007784 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007785 TargetLowering::TargetLoweringOpt TLO(DAG);
7786 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7787 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng8a186ae2008-09-24 23:26:36 +00007788 return ResNode;
Evan Chengd880b972008-05-09 21:53:03 +00007789}
7790
Chris Lattner83e6c992006-10-04 06:57:07 +00007791/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007792static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner83e6c992006-10-04 06:57:07 +00007793 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007794 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007795 SDValue Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007796
Chris Lattner83e6c992006-10-04 06:57:07 +00007797 // If we have SSE[12] support, try to form min/max nodes.
7798 if (Subtarget->hasSSE2() &&
7799 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7800 if (Cond.getOpcode() == ISD::SETCC) {
7801 // Get the LHS/RHS of the select.
Dan Gohman475871a2008-07-27 21:46:04 +00007802 SDValue LHS = N->getOperand(1);
7803 SDValue RHS = N->getOperand(2);
Chris Lattner83e6c992006-10-04 06:57:07 +00007804 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007805
Evan Cheng8ca29322006-11-10 21:43:37 +00007806 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00007807 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00007808 switch (CC) {
7809 default: break;
7810 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7811 case ISD::SETULE:
7812 case ISD::SETLE:
7813 if (!UnsafeFPMath) break;
7814 // FALL THROUGH.
7815 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7816 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00007817 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007818 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007819
Chris Lattner1907a7b2006-10-05 04:11:26 +00007820 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7821 case ISD::SETUGT:
7822 case ISD::SETGT:
7823 if (!UnsafeFPMath) break;
7824 // FALL THROUGH.
7825 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7826 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00007827 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007828 break;
7829 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007830 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00007831 switch (CC) {
7832 default: break;
7833 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7834 case ISD::SETUGT:
7835 case ISD::SETGT:
7836 if (!UnsafeFPMath) break;
7837 // FALL THROUGH.
7838 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7839 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00007840 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007841 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007842
Chris Lattner1907a7b2006-10-05 04:11:26 +00007843 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7844 case ISD::SETULE:
7845 case ISD::SETLE:
7846 if (!UnsafeFPMath) break;
7847 // FALL THROUGH.
7848 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7849 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00007850 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007851 break;
7852 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007853 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007854
Evan Cheng8ca29322006-11-10 21:43:37 +00007855 if (Opcode)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007856 return DAG.getNode(Opcode, dl, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007857 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007858
Chris Lattner83e6c992006-10-04 06:57:07 +00007859 }
7860
Dan Gohman475871a2008-07-27 21:46:04 +00007861 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007862}
7863
Nate Begeman740ab032009-01-26 00:52:55 +00007864/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7865/// when possible.
7866static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7867 const X86Subtarget *Subtarget) {
7868 // On X86 with SSE2 support, we can transform this to a vector shift if
7869 // all elements are shifted by the same amount. We can't do this in legalize
7870 // because the a constant vector is typically transformed to a constant pool
7871 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007872 if (!Subtarget->hasSSE2())
7873 return SDValue();
7874
Nate Begeman740ab032009-01-26 00:52:55 +00007875 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007876 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7877 return SDValue();
7878
Mon P Wang3becd092009-01-28 08:12:05 +00007879 SDValue ShAmtOp = N->getOperand(1);
7880 MVT EltVT = VT.getVectorElementType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007881 DebugLoc dl = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00007882 SDValue BaseShAmt;
7883 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7884 unsigned NumElts = VT.getVectorNumElements();
7885 unsigned i = 0;
7886 for (; i != NumElts; ++i) {
7887 SDValue Arg = ShAmtOp.getOperand(i);
7888 if (Arg.getOpcode() == ISD::UNDEF) continue;
7889 BaseShAmt = Arg;
7890 break;
7891 }
7892 for (; i != NumElts; ++i) {
7893 SDValue Arg = ShAmtOp.getOperand(i);
7894 if (Arg.getOpcode() == ISD::UNDEF) continue;
7895 if (Arg != BaseShAmt) {
7896 return SDValue();
7897 }
7898 }
7899 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7900 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007901 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, ShAmtOp,
Mon P Wang3becd092009-01-28 08:12:05 +00007902 DAG.getIntPtrConstant(0));
7903 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007904 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00007905
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007906 if (EltVT.bitsGT(MVT::i32))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007907 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007908 else if (EltVT.bitsLT(MVT::i32))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007909 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00007910
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007911 // The shift amount is identical so we can do a vector shift.
7912 SDValue ValOp = N->getOperand(0);
7913 switch (N->getOpcode()) {
7914 default:
7915 assert(0 && "Unknown shift opcode!");
7916 break;
7917 case ISD::SHL:
7918 if (VT == MVT::v2i64)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007919 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007920 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7921 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007922 if (VT == MVT::v4i32)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007923 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007924 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7925 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007926 if (VT == MVT::v8i16)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007927 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007928 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7929 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007930 break;
7931 case ISD::SRA:
7932 if (VT == MVT::v4i32)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007933 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007934 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7935 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007936 if (VT == MVT::v8i16)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007937 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007938 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7939 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007940 break;
7941 case ISD::SRL:
7942 if (VT == MVT::v2i64)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007943 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007944 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7945 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007946 if (VT == MVT::v4i32)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007947 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007948 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7949 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007950 if (VT == MVT::v8i16)
Dale Johannesene4d209d2009-02-03 20:21:25 +00007951 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00007952 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7953 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00007954 break;
Nate Begeman740ab032009-01-26 00:52:55 +00007955 }
7956 return SDValue();
7957}
7958
Chris Lattner149a4e52008-02-22 02:09:43 +00007959/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007960static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00007961 const X86Subtarget *Subtarget) {
7962 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7963 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00007964 // A preferable solution to the general problem is to figure out the right
7965 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00007966 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007967 if (St->getValue().getValueType().isVector() &&
7968 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00007969 isa<LoadSDNode>(St->getValue()) &&
7970 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7971 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007972 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007973 LoadSDNode *Ld = 0;
7974 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00007975 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00007976 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007977 // Must be a store of a load. We currently handle two cases: the load
7978 // is a direct child, and it's under an intervening TokenFactor. It is
7979 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00007980 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00007981 Ld = cast<LoadSDNode>(St->getChain());
7982 else if (St->getValue().hasOneUse() &&
7983 ChainVal->getOpcode() == ISD::TokenFactor) {
7984 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007985 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00007986 TokenFactorIndex = i;
7987 Ld = cast<LoadSDNode>(St->getValue());
7988 } else
7989 Ops.push_back(ChainVal->getOperand(i));
7990 }
7991 }
7992 if (Ld) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007993 DebugLoc dl = N->getDebugLoc();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007994 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7995 if (Subtarget->is64Bit()) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 SDValue NewLd = DAG.getLoad(MVT::i64, dl, Ld->getChain(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00007997 Ld->getBasePtr(), Ld->getSrcValue(),
7998 Ld->getSrcValueOffset(), Ld->isVolatile(),
7999 Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00008000 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008001 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00008002 Ops.push_back(NewChain);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008003 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008004 Ops.size());
8005 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00008006 return DAG.getStore(NewChain, dl, NewLd, St->getBasePtr(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00008007 St->getSrcValue(), St->getSrcValueOffset(),
8008 St->isVolatile(), St->getAlignment());
8009 }
8010
8011 // Otherwise, lower to two 32-bit copies.
Dan Gohman475871a2008-07-27 21:46:04 +00008012 SDValue LoAddr = Ld->getBasePtr();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008013 SDValue HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008014 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00008015
Dale Johannesene4d209d2009-02-03 20:21:25 +00008016 SDValue LoLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), LoAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00008017 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8018 Ld->isVolatile(), Ld->getAlignment());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008019 SDValue HiLd = DAG.getLoad(MVT::i32, dl, Ld->getChain(), HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00008020 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8021 Ld->isVolatile(),
8022 MinAlign(Ld->getAlignment(), 4));
8023
Dan Gohman475871a2008-07-27 21:46:04 +00008024 SDValue NewChain = LoLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008025 if (TokenFactorIndex != -1) {
8026 Ops.push_back(LoLd);
8027 Ops.push_back(HiLd);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008028 NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008029 Ops.size());
8030 }
8031
8032 LoAddr = St->getBasePtr();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008033 HiAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008034 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00008035
Dale Johannesene4d209d2009-02-03 20:21:25 +00008036 SDValue LoSt = DAG.getStore(NewChain, dl, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00008037 St->getSrcValue(), St->getSrcValueOffset(),
8038 St->isVolatile(), St->getAlignment());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008039 SDValue HiSt = DAG.getStore(NewChain, dl, HiLd, HiAddr,
Gabor Greif327ef032008-08-28 23:19:51 +00008040 St->getSrcValue(),
8041 St->getSrcValueOffset() + 4,
Dale Johannesen079f2a62008-02-25 19:20:14 +00008042 St->isVolatile(),
8043 MinAlign(St->getAlignment(), 4));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008044 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008045 }
Chris Lattner149a4e52008-02-22 02:09:43 +00008046 }
Dan Gohman475871a2008-07-27 21:46:04 +00008047 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008048}
8049
Chris Lattner6cf73262008-01-25 06:14:17 +00008050/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8051/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008052static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008053 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8054 // F[X]OR(0.0, x) -> x
8055 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008056 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8057 if (C->getValueAPF().isPosZero())
8058 return N->getOperand(1);
8059 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8060 if (C->getValueAPF().isPosZero())
8061 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008062 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008063}
8064
8065/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008066static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008067 // FAND(0.0, x) -> 0.0
8068 // FAND(x, 0.0) -> 0.0
8069 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8070 if (C->getValueAPF().isPosZero())
8071 return N->getOperand(0);
8072 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8073 if (C->getValueAPF().isPosZero())
8074 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008075 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008076}
8077
Dan Gohmane5af2d32009-01-29 01:59:02 +00008078static SDValue PerformBTCombine(SDNode *N,
8079 SelectionDAG &DAG,
8080 TargetLowering::DAGCombinerInfo &DCI) {
8081 // BT ignores high bits in the bit index operand.
8082 SDValue Op1 = N->getOperand(1);
8083 if (Op1.hasOneUse()) {
8084 unsigned BitWidth = Op1.getValueSizeInBits();
8085 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8086 APInt KnownZero, KnownOne;
8087 TargetLowering::TargetLoweringOpt TLO(DAG);
8088 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8089 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8090 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8091 DCI.CommitTargetLoweringOpt(TLO);
8092 }
8093 return SDValue();
8094}
Chris Lattner83e6c992006-10-04 06:57:07 +00008095
Dan Gohman475871a2008-07-27 21:46:04 +00008096SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008097 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008098 SelectionDAG &DAG = DCI.DAG;
8099 switch (N->getOpcode()) {
8100 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008101 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8102 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008103 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008104 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Nate Begeman740ab032009-01-26 00:52:55 +00008105 case ISD::SHL:
8106 case ISD::SRA:
8107 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008108 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008109 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008110 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8111 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008112 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008113 }
8114
Dan Gohman475871a2008-07-27 21:46:04 +00008115 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008116}
8117
Evan Cheng60c07e12006-07-05 22:17:51 +00008118//===----------------------------------------------------------------------===//
8119// X86 Inline Assembly Support
8120//===----------------------------------------------------------------------===//
8121
Chris Lattnerf4dff842006-07-11 02:54:03 +00008122/// getConstraintType - Given a constraint letter, return the type of
8123/// constraint it is for this target.
8124X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008125X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8126 if (Constraint.size() == 1) {
8127 switch (Constraint[0]) {
8128 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008129 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008130 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008131 case 'r':
8132 case 'R':
8133 case 'l':
8134 case 'q':
8135 case 'Q':
8136 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008137 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008138 case 'Y':
8139 return C_RegisterClass;
8140 default:
8141 break;
8142 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008143 }
Chris Lattner4234f572007-03-25 02:14:49 +00008144 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008145}
8146
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008147/// LowerXConstraint - try to replace an X constraint, which matches anything,
8148/// with another that has more specific requirements based on the type of the
8149/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008150const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008151LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008152 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8153 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008154 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008155 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008156 return "Y";
8157 if (Subtarget->hasSSE1())
8158 return "x";
8159 }
8160
8161 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008162}
8163
Chris Lattner48884cd2007-08-25 00:47:38 +00008164/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8165/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008166void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008167 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008168 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008169 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008170 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008171 SDValue Result(0, 0);
Chris Lattner48884cd2007-08-25 00:47:38 +00008172
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008173 switch (Constraint) {
8174 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008175 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008177 if (C->getZExtValue() <= 31) {
8178 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008179 break;
8180 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008181 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008182 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008183 case 'J':
8184 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8185 if (C->getZExtValue() <= 63) {
8186 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8187 break;
8188 }
8189 }
8190 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008191 case 'N':
8192 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008193 if (C->getZExtValue() <= 255) {
8194 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008195 break;
8196 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008197 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008198 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008199 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008200 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008201 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008202 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008203 break;
8204 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008205
Chris Lattnerdc43a882007-05-03 16:52:29 +00008206 // If we are in non-pic codegen mode, we allow the address of a global (with
8207 // an optional displacement) to be used with 'i'.
8208 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8209 int64_t Offset = 0;
8210
8211 // Match either (GA) or (GA+C)
8212 if (GA) {
8213 Offset = GA->getOffset();
8214 } else if (Op.getOpcode() == ISD::ADD) {
8215 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8216 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8217 if (C && GA) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008218 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00008219 } else {
8220 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8221 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8222 if (C && GA)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008223 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00008224 else
8225 C = 0, GA = 0;
8226 }
8227 }
8228
8229 if (GA) {
Evan Chengda43bcf2008-09-24 00:05:32 +00008230 if (hasMemory)
Dale Johannesen33c960f2009-02-04 20:06:27 +00008231 Op = LowerGlobalAddress(GA->getGlobal(), Op.getNode()->getDebugLoc(),
8232 Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00008233 else
8234 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8235 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00008236 Result = Op;
8237 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008238 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008239
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008240 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00008241 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008242 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008243 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008244
Gabor Greifba36cb52008-08-28 21:40:38 +00008245 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008246 Ops.push_back(Result);
8247 return;
8248 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008249 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8250 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008251}
8252
Chris Lattner259e97c2006-01-31 19:43:35 +00008253std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008254getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008255 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008256 if (Constraint.size() == 1) {
8257 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008258 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008259 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008260 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8261 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008262 if (VT == MVT::i32)
8263 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8264 else if (VT == MVT::i16)
8265 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8266 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008267 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008268 else if (VT == MVT::i64)
8269 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8270 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008271 }
8272 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008273
Chris Lattner1efa40f2006-02-22 00:56:39 +00008274 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008275}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008276
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008277std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008278X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008279 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008280 // First, see if this is a constraint that directly corresponds to an LLVM
8281 // register class.
8282 if (Constraint.size() == 1) {
8283 // GCC Constraint Letters
8284 switch (Constraint[0]) {
8285 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008286 case 'r': // GENERAL_REGS
8287 case 'R': // LEGACY_REGS
8288 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008289 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008290 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008291 if (VT == MVT::i16)
8292 return std::make_pair(0U, X86::GR16RegisterClass);
8293 if (VT == MVT::i32 || !Subtarget->is64Bit())
8294 return std::make_pair(0U, X86::GR32RegisterClass);
8295 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008296 case 'f': // FP Stack registers.
8297 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8298 // value to the correct fpstack register class.
8299 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8300 return std::make_pair(0U, X86::RFP32RegisterClass);
8301 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8302 return std::make_pair(0U, X86::RFP64RegisterClass);
8303 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008304 case 'y': // MMX_REGS if MMX allowed.
8305 if (!Subtarget->hasMMX()) break;
8306 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008307 case 'Y': // SSE_REGS if SSE2 allowed
8308 if (!Subtarget->hasSSE2()) break;
8309 // FALL THROUGH.
8310 case 'x': // SSE_REGS if SSE1 allowed
8311 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008312
8313 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008314 default: break;
8315 // Scalar SSE types.
8316 case MVT::f32:
8317 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008318 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008319 case MVT::f64:
8320 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008321 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008322 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008323 case MVT::v16i8:
8324 case MVT::v8i16:
8325 case MVT::v4i32:
8326 case MVT::v2i64:
8327 case MVT::v4f32:
8328 case MVT::v2f64:
8329 return std::make_pair(0U, X86::VR128RegisterClass);
8330 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008331 break;
8332 }
8333 }
8334
Chris Lattnerf76d1802006-07-31 23:26:50 +00008335 // Use the default implementation in TargetLowering to convert the register
8336 // constraint into a member of a register class.
8337 std::pair<unsigned, const TargetRegisterClass*> Res;
8338 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008339
8340 // Not found as a standard register?
8341 if (Res.second == 0) {
8342 // GCC calls "st(0)" just plain "st".
8343 if (StringsEqualNoCase("{st}", Constraint)) {
8344 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008345 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008346 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008347 // 'A' means EAX + EDX.
8348 if (Constraint == "A") {
8349 Res.first = X86::EAX;
8350 Res.second = X86::GRADRegisterClass;
8351 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008352 return Res;
8353 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008354
Chris Lattnerf76d1802006-07-31 23:26:50 +00008355 // Otherwise, check to see if this is a register class of the wrong value
8356 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8357 // turn into {ax},{dx}.
8358 if (Res.second->hasType(VT))
8359 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008360
Chris Lattnerf76d1802006-07-31 23:26:50 +00008361 // All of the single-register GCC register classes map their values onto
8362 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8363 // really want an 8-bit or 32-bit register, map to the appropriate register
8364 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008365 if (Res.second == X86::GR16RegisterClass) {
8366 if (VT == MVT::i8) {
8367 unsigned DestReg = 0;
8368 switch (Res.first) {
8369 default: break;
8370 case X86::AX: DestReg = X86::AL; break;
8371 case X86::DX: DestReg = X86::DL; break;
8372 case X86::CX: DestReg = X86::CL; break;
8373 case X86::BX: DestReg = X86::BL; break;
8374 }
8375 if (DestReg) {
8376 Res.first = DestReg;
8377 Res.second = Res.second = X86::GR8RegisterClass;
8378 }
8379 } else if (VT == MVT::i32) {
8380 unsigned DestReg = 0;
8381 switch (Res.first) {
8382 default: break;
8383 case X86::AX: DestReg = X86::EAX; break;
8384 case X86::DX: DestReg = X86::EDX; break;
8385 case X86::CX: DestReg = X86::ECX; break;
8386 case X86::BX: DestReg = X86::EBX; break;
8387 case X86::SI: DestReg = X86::ESI; break;
8388 case X86::DI: DestReg = X86::EDI; break;
8389 case X86::BP: DestReg = X86::EBP; break;
8390 case X86::SP: DestReg = X86::ESP; break;
8391 }
8392 if (DestReg) {
8393 Res.first = DestReg;
8394 Res.second = Res.second = X86::GR32RegisterClass;
8395 }
8396 } else if (VT == MVT::i64) {
8397 unsigned DestReg = 0;
8398 switch (Res.first) {
8399 default: break;
8400 case X86::AX: DestReg = X86::RAX; break;
8401 case X86::DX: DestReg = X86::RDX; break;
8402 case X86::CX: DestReg = X86::RCX; break;
8403 case X86::BX: DestReg = X86::RBX; break;
8404 case X86::SI: DestReg = X86::RSI; break;
8405 case X86::DI: DestReg = X86::RDI; break;
8406 case X86::BP: DestReg = X86::RBP; break;
8407 case X86::SP: DestReg = X86::RSP; break;
8408 }
8409 if (DestReg) {
8410 Res.first = DestReg;
8411 Res.second = Res.second = X86::GR64RegisterClass;
8412 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008413 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008414 } else if (Res.second == X86::FR32RegisterClass ||
8415 Res.second == X86::FR64RegisterClass ||
8416 Res.second == X86::VR128RegisterClass) {
8417 // Handle references to XMM physical registers that got mapped into the
8418 // wrong class. This can happen with constraints like {xmm0} where the
8419 // target independent register mapper will just pick the first match it can
8420 // find, ignoring the required type.
8421 if (VT == MVT::f32)
8422 Res.second = X86::FR32RegisterClass;
8423 else if (VT == MVT::f64)
8424 Res.second = X86::FR64RegisterClass;
8425 else if (X86::VR128RegisterClass->hasType(VT))
8426 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008427 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008428
Chris Lattnerf76d1802006-07-31 23:26:50 +00008429 return Res;
8430}
Mon P Wang0c397192008-10-30 08:01:45 +00008431
8432//===----------------------------------------------------------------------===//
8433// X86 Widen vector type
8434//===----------------------------------------------------------------------===//
8435
8436/// getWidenVectorType: given a vector type, returns the type to widen
8437/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8438/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008439/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008440/// scalarizing vs using the wider vector type.
8441
Dan Gohmanc13cf132009-01-15 17:34:08 +00008442MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008443 assert(VT.isVector());
8444 if (isTypeLegal(VT))
8445 return VT;
8446
8447 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8448 // type based on element type. This would speed up our search (though
8449 // it may not be worth it since the size of the list is relatively
8450 // small).
8451 MVT EltVT = VT.getVectorElementType();
8452 unsigned NElts = VT.getVectorNumElements();
8453
8454 // On X86, it make sense to widen any vector wider than 1
8455 if (NElts <= 1)
8456 return MVT::Other;
8457
8458 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8459 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8460 MVT SVT = (MVT::SimpleValueType)nVT;
8461
8462 if (isTypeLegal(SVT) &&
8463 SVT.getVectorElementType() == EltVT &&
8464 SVT.getVectorNumElements() > NElts)
8465 return SVT;
8466 }
8467 return MVT::Other;
8468}