blob: 527e19b608821dcb3882c998648b3c32042d3a25 [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengd82fae32010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "X86TargetMachine.h"
Chris Lattner8886dc22009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CallingConv.h"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
Chris Lattnerec7cfd42009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/GlobalVariable.h"
26#include "llvm/Function.h"
Chris Lattner7fce21c2009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Intrinsics.h"
Owen Anderson6361f972009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner25525cd2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner541d8902010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattner82411c42010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
Daniel Dunbarbb6c3dc2010-03-15 23:51:06 +000039#include "llvm/MC/MCExpr.h"
Chris Lattner82411c42010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattner82411c42010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng75184a92007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengd82fae32010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattner82411c42010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang1f292322008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattner82411c42010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendling024a32b2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattner82411c42010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052using namespace llvm;
Bill Wendling024a32b2010-03-12 19:20:40 +000053using namespace dwarf;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
Evan Chengd82fae32010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang1f292322008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000059
Dan Gohmane84197b2009-09-03 17:18:51 +000060// Disable16Bit - 16-bit operations typically have a larger encoding than
61// corresponding 32-bit instructions, and 16-bit code is slow on some
62// processors. This is an experimental flag to disable 16-bit operations
63// (which forces them to be Legalized to 32-bit operations).
64static cl::opt<bool>
65Disable16Bit("disable-16bit", cl::Hidden,
66 cl::desc("Disable use of 16-bit instructions"));
67
Evan Cheng2aea0b42008-04-25 19:11:04 +000068// Forward declarations.
Owen Andersonac9de032009-08-10 22:56:29 +000069static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +000070 SDValue V2);
Evan Cheng2aea0b42008-04-25 19:11:04 +000071
Chris Lattnerc4c40a92009-07-28 03:13:23 +000072static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
73 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
74 default: llvm_unreachable("unknown subtarget type");
75 case X86Subtarget::isDarwin:
Bill Wendling9a80c2e2010-03-15 19:04:37 +000076 if (TM.getSubtarget<X86Subtarget>().is64Bit())
77 return new X8664_MachoTargetObjectFile();
Anton Korobeynikovdf708fc2010-02-21 20:28:15 +000078 return new TargetLoweringObjectFileMachO();
Chris Lattnerc4c40a92009-07-28 03:13:23 +000079 case X86Subtarget::isELF:
Anton Korobeynikovd779bcb2010-02-15 22:35:59 +000080 if (TM.getSubtarget<X86Subtarget>().is64Bit())
81 return new X8664_ELFTargetObjectFile(TM);
82 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerc4c40a92009-07-28 03:13:23 +000083 case X86Subtarget::isMingw:
84 case X86Subtarget::isCygwin:
85 case X86Subtarget::isWindows:
86 return new TargetLoweringObjectFileCOFF();
87 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +000088}
89
Dan Gohmanb41dfba2008-05-14 01:58:56 +000090X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerc4c40a92009-07-28 03:13:23 +000091 : TargetLowering(TM, createTLOF(TM)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000093 X86ScalarSSEf64 = Subtarget->hasSSE2();
94 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000096
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000098 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 // Set up the TargetLowering object.
101
102 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000103 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +0000104 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 setSchedulingPreference(SchedulingForRegPressure);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 setStackPointerRegisterToSaveRestore(X86StackPtr);
107
108 if (Subtarget->isTargetDarwin()) {
109 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
110 setUseUnderscoreSetJmp(false);
111 setUseUnderscoreLongJmp(false);
112 } else if (Subtarget->isTargetMingw()) {
113 // MS runtime is weird: it exports _setjmp, but longjmp!
114 setUseUnderscoreSetJmp(true);
115 setUseUnderscoreLongJmp(false);
116 } else {
117 setUseUnderscoreSetJmp(true);
118 setUseUnderscoreLongJmp(true);
119 }
Scott Michel91099d62009-02-17 22:15:04 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // Set up the register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000122 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohmane84197b2009-09-03 17:18:51 +0000123 if (!Disable16Bit)
124 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000125 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000127 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000129 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130
Scott Michel91099d62009-02-17 22:15:04 +0000131 // We don't accept any truncstore of integer registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000132 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000133 if (!Disable16Bit)
134 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000135 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000136 if (!Disable16Bit)
137 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
139 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng71343822008-10-15 02:05:31 +0000140
141 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000142 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
143 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
144 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
145 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
150 // operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
152 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
153 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
155 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000156 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
157 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman8c3cb582009-05-23 09:59:16 +0000158 } else if (!UseSoftFloat) {
159 if (X86ScalarSSEf64) {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000160 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000161 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000162 }
Eli Friedman8c3cb582009-05-23 09:59:16 +0000163 // We have an algorithm for SSE2, and we turn this into a 64-bit
164 // FILD for other targets.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000165 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 }
167
168 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
169 // this operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
171 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000172
Devang Patel3c233642009-06-05 18:48:29 +0000173 if (!UseSoftFloat) {
Bill Wendling6b42d012009-03-13 08:41:47 +0000174 // SSE has no i16 to fp conversion, only i32
175 if (X86ScalarSSEf32) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000176 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000177 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000179 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
181 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000182 }
Dale Johannesen2fc20782007-09-14 22:26:36 +0000183 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000184 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
185 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 }
187
Dale Johannesen958b08b2007-09-19 23:55:34 +0000188 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
189 // are Legal, f80 is custom lowered.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
191 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192
193 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
194 // this operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000195 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
196 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000198 if (X86ScalarSSEf32) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000199 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000200 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
204 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 }
206
207 // Handle FP_TO_UINT by promoting the destination to a larger signed
208 // conversion.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000209 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
210 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
211 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
213 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
215 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman8c3cb582009-05-23 09:59:16 +0000216 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000217 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 // Expand FP_TO_UINT into a select.
219 // FIXME: We would like to use a Custom expander here eventually to do
220 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000221 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 else
Eli Friedman8c3cb582009-05-23 09:59:16 +0000223 // With SSE3 we can use fisttpll to convert to a signed i64; without
224 // SSE, we're stuck with a fistpll.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000225 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 }
227
228 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000229 if (!X86ScalarSSEf64) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000230 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
231 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 }
233
Dan Gohman8450d862008-02-18 19:34:53 +0000234 // Scalar integer divide and remainder are lowered to use operations that
235 // produce two results, to match the available instructions. This exposes
236 // the two-result form to trivial CSE, which is able to combine x/y and x%y
237 // into a single instruction.
238 //
239 // Scalar integer multiply-high is also lowered to use two-result
240 // operations, to match the available instructions. However, plain multiply
241 // (low) operations are left as Legal, as there are single-result
242 // instructions for this in x86. Using the two-result multiply instructions
243 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000244 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
248 setOperationAction(ISD::SREM , MVT::i8 , Expand);
249 setOperationAction(ISD::UREM , MVT::i8 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
254 setOperationAction(ISD::SREM , MVT::i16 , Expand);
255 setOperationAction(ISD::UREM , MVT::i16 , Expand);
256 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
257 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
258 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
259 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
260 setOperationAction(ISD::SREM , MVT::i32 , Expand);
261 setOperationAction(ISD::UREM , MVT::i32 , Expand);
262 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
263 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
264 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
265 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
266 setOperationAction(ISD::SREM , MVT::i64 , Expand);
267 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000268
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000269 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
270 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
271 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
272 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
276 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
277 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
278 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
279 setOperationAction(ISD::FREM , MVT::f32 , Expand);
280 setOperationAction(ISD::FREM , MVT::f64 , Expand);
281 setOperationAction(ISD::FREM , MVT::f80 , Expand);
282 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000283
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000284 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
285 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
287 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000288 if (Disable16Bit) {
289 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
290 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
291 } else {
292 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
293 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
294 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000295 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
296 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
297 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000299 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
300 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
301 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 }
303
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000304 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
305 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
307 // These should be promoted to a larger select which is supported.
Dan Gohman29b998f2009-08-27 00:14:12 +0000308 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 // X86 wants to expand cmov itself.
Dan Gohman29b998f2009-08-27 00:14:12 +0000310 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohmane84197b2009-09-03 17:18:51 +0000311 if (Disable16Bit)
312 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
313 else
314 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000315 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
316 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
317 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
318 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
319 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohmane84197b2009-09-03 17:18:51 +0000320 if (Disable16Bit)
321 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
322 else
323 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000324 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
325 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
326 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
327 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000329 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
330 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000332 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333
334 // Darwin ABI issue.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000335 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
336 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
337 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
338 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000339 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000340 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
341 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohman064403e2009-10-30 01:28:02 +0000342 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000344 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
345 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
346 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
347 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohman064403e2009-10-30 01:28:02 +0000348 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 }
350 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
352 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
353 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000354 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000355 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
356 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
357 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000358 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359
Evan Cheng8d51ab32008-03-10 19:38:10 +0000360 if (Subtarget->hasSSE1())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000361 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000362
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000363 if (!Subtarget->hasSSE2())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000364 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000365
Mon P Wang078a62d2008-05-05 19:05:59 +0000366 // Expand certain atomics
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
368 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
370 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000371
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000372 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000376
Dale Johannesenf160d802008-10-02 18:53:47 +0000377 if (!Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000378 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
381 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
382 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
383 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
384 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000385 }
386
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 // FIXME - use subtarget debug flags
388 if (!Subtarget->isTargetDarwin() &&
389 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000390 !Subtarget->isTargetCygMing()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000391 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000392 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000394 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
395 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
396 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
397 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 setExceptionPointerRegister(X86::RAX);
400 setExceptionSelectorRegister(X86::RDX);
401 } else {
402 setExceptionPointerRegister(X86::EAX);
403 setExceptionSelectorRegister(X86::EDX);
404 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000405 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
406 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000407
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000408 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000409
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000410 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000411
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000413 setOperationAction(ISD::VASTART , MVT::Other, Custom);
414 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000415 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000416 setOperationAction(ISD::VAARG , MVT::Other, Custom);
417 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000418 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000419 setOperationAction(ISD::VAARG , MVT::Other, Expand);
420 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000421 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000423 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
424 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 if (Subtarget->isTargetCygMing())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000428 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000430 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
Evan Cheng0b84fe12009-02-13 22:36:38 +0000432 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000433 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000435 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
436 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437
438 // Use ANDPD to simulate FABS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FABS , MVT::f64, Custom);
440 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 // Use XORP to simulate FNEG.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FNEG , MVT::f64, Custom);
444 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
448 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449
450 // We don't support sin/cos/fmod
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000451 setOperationAction(ISD::FSIN , MVT::f64, Expand);
452 setOperationAction(ISD::FCOS , MVT::f64, Expand);
453 setOperationAction(ISD::FSIN , MVT::f32, Expand);
454 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 // Expand FP immediates into loads from the stack, except for the special
457 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000458 addLegalFPImmediate(APFloat(+0.0)); // xorpd
459 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Cheng0b84fe12009-02-13 22:36:38 +0000460 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000461 // Use SSE for f32, x87 for f64.
462 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000463 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000465
466 // Use ANDPS to simulate FABS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000468
469 // Use XORP to simulate FNEG.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000471
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000472 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000473
474 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
476 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000477
478 // We don't support sin/cos/fmod
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000479 setOperationAction(ISD::FSIN , MVT::f32, Expand);
480 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000481
Nate Begemane2ba64f2008-02-14 08:57:00 +0000482 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000483 addLegalFPImmediate(APFloat(+0.0f)); // xorps
484 addLegalFPImmediate(APFloat(+0.0)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
488
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000489 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000490 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000492 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000493 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000494 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000496 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
497 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000499 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
500 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
501 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
502 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000503
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000505 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
506 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000508 addLegalFPImmediate(APFloat(+0.0)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000512 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
513 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
514 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
515 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 }
517
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000518 // Long double always uses X87.
Evan Chenge738dc32009-03-26 23:06:32 +0000519 if (!UseSoftFloat) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000520 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
521 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
522 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000523 {
524 bool ignored;
525 APFloat TmpFlt(+0.0);
526 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
527 &ignored);
528 addLegalFPImmediate(TmpFlt); // FLD0
529 TmpFlt.changeSign();
530 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
531 APFloat TmpFlt2(+1.0);
532 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
533 &ignored);
534 addLegalFPImmediate(TmpFlt2); // FLD1
535 TmpFlt2.changeSign();
536 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
537 }
Scott Michel91099d62009-02-17 22:15:04 +0000538
Evan Cheng0b84fe12009-02-13 22:36:38 +0000539 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
541 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000542 }
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000543 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000544
Dan Gohman2f7b1982007-10-11 23:21:31 +0000545 // Always use a library call for pow.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
547 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
548 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000549
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FLOG, MVT::f80, Expand);
551 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
552 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
553 setOperationAction(ISD::FEXP, MVT::f80, Expand);
554 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000555
Mon P Wanga5a239f2008-11-06 05:31:54 +0000556 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000557 // (for widening) or expand (for scalarization). Then we will selectively
558 // turn on ones that can be effectively codegen'd.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000559 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
560 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
561 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
576 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
577 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman9d501bd2009-12-11 21:31:27 +0000609 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohmanc6cfdd32009-12-14 23:40:38 +0000610 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
611 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
612 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
613 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
614 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
615 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
616 setTruncStoreAction((MVT::SimpleValueType)VT,
617 (MVT::SimpleValueType)InnerVT, Expand);
618 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
619 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
620 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 }
622
Evan Cheng0b84fe12009-02-13 22:36:38 +0000623 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
624 // with -msoft-float, disable use of MMX as well.
Evan Chenge738dc32009-03-26 23:06:32 +0000625 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000626 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
627 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
628 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
629 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
630 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000632 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
633 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
634 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
635 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000637 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
638 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
639 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
640 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000642 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
643 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000645 setOperationAction(ISD::AND, MVT::v8i8, Promote);
646 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
647 setOperationAction(ISD::AND, MVT::v4i16, Promote);
648 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
649 setOperationAction(ISD::AND, MVT::v2i32, Promote);
650 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
651 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000653 setOperationAction(ISD::OR, MVT::v8i8, Promote);
654 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
655 setOperationAction(ISD::OR, MVT::v4i16, Promote);
656 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
657 setOperationAction(ISD::OR, MVT::v2i32, Promote);
658 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
659 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000661 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
662 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
663 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
664 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
665 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
666 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
667 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000669 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
670 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
671 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
672 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
673 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
674 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
675 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
676 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
677 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000679 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
680 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
681 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
682 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
683 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000685 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
686 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
687 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
688 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
691 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
692 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000694
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000696
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000697 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
698 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
699 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
700 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
701 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
702 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 }
705
Evan Chenge738dc32009-03-26 23:06:32 +0000706 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000707 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
710 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
711 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
712 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
713 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
714 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
715 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
716 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
717 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
718 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
719 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
720 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
722
Evan Chenge738dc32009-03-26 23:06:32 +0000723 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000724 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000725
Bill Wendling042eda32009-03-11 22:30:01 +0000726 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
727 // registers cannot be used even for integer operations.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000728 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
729 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
730 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
731 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000733 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
734 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
735 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
736 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
737 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
738 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
739 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
740 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
741 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
742 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
743 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
744 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
745 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
746 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
747 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
748 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000750 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
751 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
752 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
753 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000754
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000755 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
756 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
757 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760
Mon P Wanga8ff0dd2010-01-24 00:05:03 +0000761 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
762 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
763 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
764 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
765 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
766
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000768 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
769 EVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000770 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000771 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000772 continue;
David Greenea5acb6e2009-06-29 16:47:10 +0000773 // Do not attempt to custom lower non-128-bit vectors
774 if (!VT.is128BitVector())
775 continue;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000776 setOperationAction(ISD::BUILD_VECTOR,
777 VT.getSimpleVT().SimpleTy, Custom);
778 setOperationAction(ISD::VECTOR_SHUFFLE,
779 VT.getSimpleVT().SimpleTy, Custom);
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
781 VT.getSimpleVT().SimpleTy, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 }
Bill Wendling042eda32009-03-11 22:30:01 +0000783
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000784 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
785 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
786 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
787 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
788 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendling042eda32009-03-11 22:30:01 +0000790
Nate Begeman4294c1f2008-02-12 22:51:28 +0000791 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000792 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
793 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000794 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795
796 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000797 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
798 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersonac9de032009-08-10 22:56:29 +0000799 EVT VT = SVT;
David Greenea5acb6e2009-06-29 16:47:10 +0000800
801 // Do not attempt to promote non-128-bit vectors
802 if (!VT.is128BitVector()) {
803 continue;
804 }
Eric Christopher00b717d2010-03-30 01:04:59 +0000805
Owen Andersona0c69eb2009-08-10 20:46:15 +0000806 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000807 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000808 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000809 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000810 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000811 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000812 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000813 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000814 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000815 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 }
817
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000818 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000819
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 // Custom lower v2i64 and v2f64 selects.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000821 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
822 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
823 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
824 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000825
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000826 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
827 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedmanc0521fb2009-06-06 03:57:58 +0000828 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000829 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
830 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedmanc0521fb2009-06-06 03:57:58 +0000831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000833
Nate Begemand77e59e2008-02-11 04:19:36 +0000834 if (Subtarget->hasSSE41()) {
835 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000836 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000837
838 // i8 and i16 vectors are custom , because the source register and source
839 // source memory operand types are not the same width. f32 vectors are
840 // custom since the immediate controlling the insert encodes additional
841 // information.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
843 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000846
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000847 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
848 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
849 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
850 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000851
852 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000853 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000855 }
856 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857
Nate Begeman03605a02008-07-17 16:51:19 +0000858 if (Subtarget->hasSSE42()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000859 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000860 }
Scott Michel91099d62009-02-17 22:15:04 +0000861
David Greenea5acb6e2009-06-29 16:47:10 +0000862 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000863 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
864 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
865 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
866 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greeneed1b3db2009-06-29 22:50:51 +0000867
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000868 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
869 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
870 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
871 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
872 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
873 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
874 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
875 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
876 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
877 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
878 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
879 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
880 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
881 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
882 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000883
884 // Operations to consider commented out -v16i16 v32i8
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000885 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
886 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
887 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
888 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
889 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
890 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
891 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
892 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
893 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
894 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
895 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
896 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
897 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
898 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000899
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000900 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
901 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
902 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
903 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000904
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000905 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
906 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
907 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
908 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
909 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000910
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000911 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
912 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
913 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
914 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
915 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
916 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000917
918#if 0
919 // Not sure we want to do this since there are no 256-bit integer
920 // operations in AVX
921
922 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
923 // This includes 256-bit vectors
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000924 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
925 EVT VT = (MVT::SimpleValueType)i;
David Greenea5acb6e2009-06-29 16:47:10 +0000926
927 // Do not attempt to custom lower non-power-of-2 vectors
928 if (!isPowerOf2_32(VT.getVectorNumElements()))
929 continue;
930
931 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
932 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
933 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
934 }
935
936 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopher3d82bbd2009-08-27 18:07:15 +0000939 }
David Greenea5acb6e2009-06-29 16:47:10 +0000940#endif
941
942#if 0
943 // Not sure we want to do this since there are no 256-bit integer
944 // operations in AVX
945
946 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
947 // Including 256-bit vectors
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000948 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
949 EVT VT = (MVT::SimpleValueType)i;
David Greenea5acb6e2009-06-29 16:47:10 +0000950
951 if (!VT.is256BitVector()) {
952 continue;
953 }
954 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000955 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000956 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000957 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000958 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000959 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000960 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000961 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000962 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000963 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000964 }
965
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000966 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greenea5acb6e2009-06-29 16:47:10 +0000967#endif
968 }
969
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 // We want to custom lower some of our intrinsics.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000971 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972
Bill Wendling7e04be62008-12-09 22:08:41 +0000973 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000974 setOperationAction(ISD::SADDO, MVT::i32, Custom);
975 setOperationAction(ISD::SADDO, MVT::i64, Custom);
976 setOperationAction(ISD::UADDO, MVT::i32, Custom);
977 setOperationAction(ISD::UADDO, MVT::i64, Custom);
978 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
979 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
980 setOperationAction(ISD::USUBO, MVT::i32, Custom);
981 setOperationAction(ISD::USUBO, MVT::i64, Custom);
982 setOperationAction(ISD::SMULO, MVT::i32, Custom);
983 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000984
Evan Cheng9c215602009-03-31 19:38:51 +0000985 if (!Subtarget->is64Bit()) {
986 // These libcalls are not available in 32-bit.
987 setLibcallName(RTLIB::SHL_I128, 0);
988 setLibcallName(RTLIB::SRL_I128, 0);
989 setLibcallName(RTLIB::SRA_I128, 0);
990 }
991
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 // We have target-specific dag combine patterns for the following nodes:
993 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohmanb115d052010-03-15 23:23:03 +0000994 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chenge9b9c672008-05-09 21:53:03 +0000995 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 setTargetDAGCombine(ISD::SELECT);
sampo025b75c2009-01-26 00:52:55 +0000997 setTargetDAGCombine(ISD::SHL);
998 setTargetDAGCombine(ISD::SRA);
999 setTargetDAGCombine(ISD::SRL);
Evan Cheng10957b82010-01-04 21:22:48 +00001000 setTargetDAGCombine(ISD::OR);
Chris Lattnerce84ae42008-02-22 02:09:43 +00001001 setTargetDAGCombine(ISD::STORE);
Owen Anderson58155b22009-06-29 18:04:45 +00001002 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Chengedeb1692009-12-16 00:53:11 +00001003 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng04ecee12009-03-28 05:57:29 +00001004 if (Subtarget->is64Bit())
1005 setTargetDAGCombine(ISD::MUL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006
1007 computeRegisterProperties();
1008
1009 // FIXME: These should be based on subtarget info. Plus, the values should
1010 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +00001011 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng0b592c02010-04-01 06:04:33 +00001012 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman97fab242008-06-30 21:00:56 +00001013 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Cheng45c1edb2008-02-28 00:43:03 +00001014 setPrefLoopAlignment(16);
Evan Cheng79566822009-05-13 21:42:09 +00001015 benefitFromCodePlacementOpt = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016}
1017
Scott Michel502151f2008-03-10 15:42:14 +00001018
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001019MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1020 return MVT::i8;
Scott Michel502151f2008-03-10 15:42:14 +00001021}
1022
1023
Evan Cheng5a67b812008-01-23 23:17:41 +00001024/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1025/// the desired ByVal argument alignment.
1026static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1027 if (MaxAlign == 16)
1028 return;
1029 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1030 if (VTy->getBitWidth() == 128)
1031 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +00001032 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1033 unsigned EltAlign = 0;
1034 getMaxByValAlign(ATy->getElementType(), EltAlign);
1035 if (EltAlign > MaxAlign)
1036 MaxAlign = EltAlign;
1037 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1038 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1039 unsigned EltAlign = 0;
1040 getMaxByValAlign(STy->getElementType(i), EltAlign);
1041 if (EltAlign > MaxAlign)
1042 MaxAlign = EltAlign;
1043 if (MaxAlign == 16)
1044 break;
1045 }
1046 }
1047 return;
1048}
1049
1050/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1051/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +00001052/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1053/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +00001054unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00001055 if (Subtarget->is64Bit()) {
1056 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001057 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00001058 if (TyAlign > 8)
1059 return TyAlign;
1060 return 8;
1061 }
1062
Evan Cheng5a67b812008-01-23 23:17:41 +00001063 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +00001064 if (Subtarget->hasSSE1())
1065 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +00001066 return Align;
1067}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068
Evan Cheng8c590372008-05-15 08:39:06 +00001069/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengbd550f62010-04-01 20:10:42 +00001070/// and store operations as a result of memset, memcpy, and memmove lowering.
1071/// If DstAlign is zero that means it's safe to destination alignment can
1072/// satisfy any constraint. Similarly if SrcAlign is zero it means there
1073/// isn't a need to check it against alignment requirement, probably because
Evan Cheng52ff54e2010-04-02 19:36:14 +00001074/// the source does not need to be loaded. If 'NonScalarIntSafe' is true, that
1075/// means it's safe to return a non-scalar-integer type, e.g. constant string
1076/// source or loaded from memory. It returns EVT::Other if SelectionDAG should
1077/// be responsible for determining it.
Owen Andersonac9de032009-08-10 22:56:29 +00001078EVT
Evan Cheng0b592c02010-04-01 06:04:33 +00001079X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1080 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng52ff54e2010-04-02 19:36:14 +00001081 bool NonScalarIntSafe,
Devang Patelc386c842009-06-05 21:57:13 +00001082 SelectionDAG &DAG) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001083 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1084 // linux. This is because the stack realignment code can't handle certain
1085 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patelc386c842009-06-05 21:57:13 +00001086 const Function *F = DAG.getMachineFunction().getFunction();
Evan Cheng52ff54e2010-04-02 19:36:14 +00001087 if (NonScalarIntSafe &&
1088 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng0b592c02010-04-01 06:04:33 +00001089 if (Size >= 16 &&
1090 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthd2bb6712010-04-02 01:31:24 +00001091 ((DstAlign == 0 || DstAlign >= 16) &&
1092 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng0b592c02010-04-01 06:04:33 +00001093 Subtarget->getStackAlignment() >= 16) {
1094 if (Subtarget->hasSSE2())
1095 return MVT::v4i32;
Evan Cheng52ff54e2010-04-02 19:36:14 +00001096 if (Subtarget->hasSSE1())
Evan Cheng0b592c02010-04-01 06:04:33 +00001097 return MVT::v4f32;
Evan Cheng52ff54e2010-04-02 19:36:14 +00001098 } else if (Size >= 8 &&
Evan Cheng281d37e2010-04-01 20:27:45 +00001099 !Subtarget->is64Bit() &&
Evan Cheng0b592c02010-04-01 06:04:33 +00001100 Subtarget->getStackAlignment() >= 8 &&
1101 Subtarget->hasSSE2())
1102 return MVT::f64;
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001103 }
Evan Cheng8c590372008-05-15 08:39:06 +00001104 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001105 return MVT::i64;
1106 return MVT::i32;
Evan Cheng8c590372008-05-15 08:39:06 +00001107}
1108
Chris Lattner25525cd2010-01-25 23:38:14 +00001109/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1110/// current function. The returned value is a member of the
1111/// MachineJumpTableInfo::JTEntryKind enum.
1112unsigned X86TargetLowering::getJumpTableEncoding() const {
1113 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1114 // symbol.
1115 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1116 Subtarget->isPICStyleGOT())
Chris Lattner82411c42010-01-26 05:02:42 +00001117 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner25525cd2010-01-25 23:38:14 +00001118
1119 // Otherwise, use the normal jump table encoding heuristics.
1120 return TargetLowering::getJumpTableEncoding();
1121}
1122
Chris Lattner541d8902010-01-26 06:28:43 +00001123/// getPICBaseSymbol - Return the X86-32 PIC base.
1124MCSymbol *
1125X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1126 MCContext &Ctx) const {
1127 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner3b197832010-03-30 18:10:53 +00001128 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1129 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner541d8902010-01-26 06:28:43 +00001130}
1131
1132
Chris Lattner82411c42010-01-26 05:02:42 +00001133const MCExpr *
1134X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1135 const MachineBasicBlock *MBB,
1136 unsigned uid,MCContext &Ctx) const{
1137 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1138 Subtarget->isPICStyleGOT());
1139 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1140 // entries.
Daniel Dunbarbb6c3dc2010-03-15 23:51:06 +00001141 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1142 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattner82411c42010-01-26 05:02:42 +00001143}
1144
Evan Cheng6fb06762007-11-09 01:32:10 +00001145/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1146/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +00001147SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner541d8902010-01-26 06:28:43 +00001148 SelectionDAG &DAG) const {
Chris Lattneraa7c6d22009-07-09 03:15:51 +00001149 if (!Subtarget->is64Bit())
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001150 // This doesn't have DebugLoc associated with it, but is not really the
1151 // same as a Register.
Chris Lattnerd2c680b2010-04-02 20:16:16 +00001152 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Cheng6fb06762007-11-09 01:32:10 +00001153 return Table;
1154}
1155
Chris Lattner541d8902010-01-26 06:28:43 +00001156/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1157/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1158/// MCExpr.
1159const MCExpr *X86TargetLowering::
1160getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1161 MCContext &Ctx) const {
1162 // X86-64 uses RIP relative addressing based on the jump table label.
1163 if (Subtarget->isPICStyleRIPRel())
1164 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1165
1166 // Otherwise, the reference is relative to the PIC base.
1167 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1168}
1169
Bill Wendling045f2632009-07-01 18:50:55 +00001170/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling25a8ae32009-06-30 22:38:32 +00001171unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman4f6b95c2009-08-18 00:20:06 +00001172 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling25a8ae32009-06-30 22:38:32 +00001173}
1174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175//===----------------------------------------------------------------------===//
1176// Return Value Calling Convention Implementation
1177//===----------------------------------------------------------------------===//
1178
1179#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001180
Kenneth Uildriks87d04262009-11-07 02:11:54 +00001181bool
1182X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1183 const SmallVectorImpl<EVT> &OutTys,
1184 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1185 SelectionDAG &DAG) {
1186 SmallVector<CCValAssign, 16> RVLocs;
1187 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1188 RVLocs, *DAG.getContext());
1189 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1190}
1191
Dan Gohman9178de12009-08-05 01:29:28 +00001192SDValue
1193X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001194 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001195 const SmallVectorImpl<ISD::OutputArg> &Outs,
1196 DebugLoc dl, SelectionDAG &DAG) {
Scott Michel91099d62009-02-17 22:15:04 +00001197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001199 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1200 RVLocs, *DAG.getContext());
1201 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +00001202
Evan Chengcf840d52010-02-04 02:40:39 +00001203 // Add the regs to the liveout set for the function.
1204 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1205 for (unsigned i = 0; i != RVLocs.size(); ++i)
1206 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1207 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michel91099d62009-02-17 22:15:04 +00001208
Dan Gohman8181bd12008-07-27 21:46:04 +00001209 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001210
Dan Gohman8181bd12008-07-27 21:46:04 +00001211 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +00001212 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1213 // Operand #1 = Bytes To Pop
Dan Gohmane84197b2009-09-03 17:18:51 +00001214 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michel91099d62009-02-17 22:15:04 +00001215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001217 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1218 CCValAssign &VA = RVLocs[i];
1219 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman9178de12009-08-05 01:29:28 +00001220 SDValue ValToCopy = Outs[i].Val;
Scott Michel91099d62009-02-17 22:15:04 +00001221
Chris Lattnerb56cc342008-03-11 03:23:40 +00001222 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1223 // the RET instruction and handled by the FP Stackifier.
Dan Gohman6c4be722009-02-04 17:28:58 +00001224 if (VA.getLocReg() == X86::ST0 ||
1225 VA.getLocReg() == X86::ST1) {
Chris Lattnerb56cc342008-03-11 03:23:40 +00001226 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1227 // change the value to the FP stack register class.
Dan Gohman6c4be722009-02-04 17:28:58 +00001228 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001229 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattnerb56cc342008-03-11 03:23:40 +00001230 RetOps.push_back(ValToCopy);
1231 // Don't emit a copytoreg.
1232 continue;
1233 }
Dale Johannesena585daf2008-06-24 22:01:44 +00001234
Evan Chengef356282009-02-23 09:03:22 +00001235 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1236 // which is returned in RAX / RDX.
Evan Chenge8db6e02009-02-22 08:05:12 +00001237 if (Subtarget->is64Bit()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001238 EVT ValVT = ValToCopy.getValueType();
Evan Chengef356282009-02-23 09:03:22 +00001239 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001240 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001241 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001242 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001243 }
Evan Chenge8db6e02009-02-22 08:05:12 +00001244 }
1245
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001246 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 Flag = Chain.getValue(1);
1248 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001249
1250 // The x86-64 ABI for returning structs by value requires that we copy
1251 // the sret argument into %rax for the return. We saved the argument into
1252 // a virtual register in the entry block, so now we copy the value out
1253 // and into %rax.
1254 if (Subtarget->is64Bit() &&
1255 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1256 MachineFunction &MF = DAG.getMachineFunction();
1257 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1258 unsigned Reg = FuncInfo->getSRetReturnReg();
1259 if (!Reg) {
Evan Chengcf840d52010-02-04 02:40:39 +00001260 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001261 FuncInfo->setSRetReturnReg(Reg);
1262 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001263 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001264
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001265 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001266 Flag = Chain.getValue(1);
Dan Gohman1c738f52009-10-12 16:36:12 +00001267
1268 // RAX now acts like a return value.
Evan Chengcf840d52010-02-04 02:40:39 +00001269 MRI.addLiveOut(X86::RAX);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001270 }
Scott Michel91099d62009-02-17 22:15:04 +00001271
Chris Lattnerb56cc342008-03-11 03:23:40 +00001272 RetOps[0] = Chain; // Update chain.
1273
1274 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001275 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001276 RetOps.push_back(Flag);
Scott Michel91099d62009-02-17 22:15:04 +00001277
1278 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001279 MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280}
1281
Dan Gohman9178de12009-08-05 01:29:28 +00001282/// LowerCallResult - Lower the result values of a call into the
1283/// appropriate copies out of appropriate physical registers.
1284///
1285SDValue
1286X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001287 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001288 const SmallVectorImpl<ISD::InputArg> &Ins,
1289 DebugLoc dl, SelectionDAG &DAG,
1290 SmallVectorImpl<SDValue> &InVals) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 // Assign locations to each value returned by this call.
1293 SmallVector<CCValAssign, 16> RVLocs;
Edwin Törökaf8e1332009-02-01 18:15:56 +00001294 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman9178de12009-08-05 01:29:28 +00001295 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Anderson175b6542009-07-22 00:24:57 +00001296 RVLocs, *DAG.getContext());
Dan Gohman9178de12009-08-05 01:29:28 +00001297 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +00001298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001300 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman6c4be722009-02-04 17:28:58 +00001301 CCValAssign &VA = RVLocs[i];
Owen Andersonac9de032009-08-10 22:56:29 +00001302 EVT CopyVT = VA.getValVT();
Scott Michel91099d62009-02-17 22:15:04 +00001303
Edwin Törökaf8e1332009-02-01 18:15:56 +00001304 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001305 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman9178de12009-08-05 01:29:28 +00001306 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Edwin Török2b331342009-07-08 19:04:27 +00001307 llvm_report_error("SSE register return with SSE disabled");
Edwin Törökaf8e1332009-02-01 18:15:56 +00001308 }
1309
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001310 // If this is a call to a function that returns an fp value on the floating
1311 // point stack, but where we prefer to use the value in xmm registers, copy
1312 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman6c4be722009-02-04 17:28:58 +00001313 if ((VA.getLocReg() == X86::ST0 ||
1314 VA.getLocReg() == X86::ST1) &&
1315 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001316 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 }
Scott Michel91099d62009-02-17 22:15:04 +00001318
Evan Cheng9cc600e2009-02-20 20:43:02 +00001319 SDValue Val;
1320 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Chengef356282009-02-23 09:03:22 +00001321 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1322 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1323 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001324 MVT::v2i64, InFlag).getValue(1);
Evan Chengef356282009-02-23 09:03:22 +00001325 Val = Chain.getValue(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001326 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1327 Val, DAG.getConstant(0, MVT::i64));
Evan Chengef356282009-02-23 09:03:22 +00001328 } else {
1329 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001330 MVT::i64, InFlag).getValue(1);
Evan Chengef356282009-02-23 09:03:22 +00001331 Val = Chain.getValue(0);
1332 }
Evan Cheng9cc600e2009-02-20 20:43:02 +00001333 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1334 } else {
1335 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1336 CopyVT, InFlag).getValue(1);
1337 Val = Chain.getValue(0);
1338 }
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001339 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001340
Dan Gohman6c4be722009-02-04 17:28:58 +00001341 if (CopyVT != VA.getValVT()) {
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001342 // Round the F80 the right size, which also moves to the appropriate xmm
1343 // register.
Dan Gohman6c4be722009-02-04 17:28:58 +00001344 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001345 // This truncation won't change the value.
1346 DAG.getIntPtrConstant(1));
1347 }
Scott Michel91099d62009-02-17 22:15:04 +00001348
Dan Gohman9178de12009-08-05 01:29:28 +00001349 InVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 }
Duncan Sands698842f2008-07-02 17:40:58 +00001351
Dan Gohman9178de12009-08-05 01:29:28 +00001352 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353}
1354
1355
1356//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001357// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358//===----------------------------------------------------------------------===//
1359// StdCall calling convention seems to be standard for many Windows' API
1360// routines and around. It differs from C calling convention just a little:
1361// callee should clean up the stack, not caller. Symbols should be also
1362// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001363// For info on fast calling convention see Fast Calling Convention (tail call)
1364// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365
Dan Gohman9178de12009-08-05 01:29:28 +00001366/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001367/// semantics.
Dan Gohman9178de12009-08-05 01:29:28 +00001368static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1369 if (Outs.empty())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001370 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001371
Dan Gohman9178de12009-08-05 01:29:28 +00001372 return Outs[0].Flags.isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001373}
1374
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001375/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001376/// return semantics.
Dan Gohman9178de12009-08-05 01:29:28 +00001377static bool
1378ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1379 if (Ins.empty())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001381
Dan Gohman9178de12009-08-05 01:29:28 +00001382 return Ins[0].Flags.isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383}
1384
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001385/// IsCalleePop - Determines whether the callee is required to pop its
1386/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel5838baa2009-09-02 08:44:58 +00001387bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen18ace102008-01-05 16:56:59 +00001388 if (IsVarArg)
1389 return false;
1390
Dan Gohman705e3f72008-09-13 01:54:27 +00001391 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001392 default:
1393 return false;
1394 case CallingConv::X86_StdCall:
1395 return !Subtarget->is64Bit();
1396 case CallingConv::X86_FastCall:
1397 return !Subtarget->is64Bit();
1398 case CallingConv::Fast:
Dan Gohmanea8579c2010-02-08 20:27:50 +00001399 return GuaranteedTailCallOpt;
Chris Lattnerac9a9392010-03-11 00:22:57 +00001400 case CallingConv::GHC:
1401 return GuaranteedTailCallOpt;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001402 }
1403}
1404
Dan Gohman705e3f72008-09-13 01:54:27 +00001405/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1406/// given CallingConvention value.
Sandeep Patel5838baa2009-09-02 08:44:58 +00001407CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001408 if (Subtarget->is64Bit()) {
Chris Lattnerac9a9392010-03-11 00:22:57 +00001409 if (CC == CallingConv::GHC)
1410 return CC_X86_64_GHC;
1411 else if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001412 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001413 else
1414 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001415 }
1416
Gordon Henriksen18ace102008-01-05 16:56:59 +00001417 if (CC == CallingConv::X86_FastCall)
1418 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001419 else if (CC == CallingConv::Fast)
1420 return CC_X86_32_FastCC;
Chris Lattnerac9a9392010-03-11 00:22:57 +00001421 else if (CC == CallingConv::GHC)
1422 return CC_X86_32_GHC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 else
1424 return CC_X86_32_C;
1425}
1426
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001427/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1428/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001429/// the specific parameter attribute. The copy will be passed as a byval
1430/// function parameter.
Scott Michel91099d62009-02-17 22:15:04 +00001431static SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00001432CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001433 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1434 DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001435 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001436 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wangf9f760e2010-04-02 18:43:02 +00001437 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001438}
1439
Chris Lattnerac9a9392010-03-11 00:22:57 +00001440/// IsTailCallConvention - Return true if the calling convention is one that
1441/// supports tail call optimization.
1442static bool IsTailCallConvention(CallingConv::ID CC) {
1443 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1444}
1445
Evan Cheng6b6ed592010-01-27 00:07:07 +00001446/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1447/// a tailcall target by changing its ABI.
1448static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattnerac9a9392010-03-11 00:22:57 +00001449 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng6b6ed592010-01-27 00:07:07 +00001450}
1451
Dan Gohman9178de12009-08-05 01:29:28 +00001452SDValue
1453X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001454 CallingConv::ID CallConv,
Dan Gohman9178de12009-08-05 01:29:28 +00001455 const SmallVectorImpl<ISD::InputArg> &Ins,
1456 DebugLoc dl, SelectionDAG &DAG,
1457 const CCValAssign &VA,
1458 MachineFrameInfo *MFI,
1459 unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001460 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman9178de12009-08-05 01:29:28 +00001461 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng6b6ed592010-01-27 00:07:07 +00001462 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001463 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001464 EVT ValVT;
1465
1466 // If value is passed by pointer we have address passed instead of the value
1467 // itself.
1468 if (VA.getLocInfo() == CCValAssign::Indirect)
1469 ValVT = VA.getLocVT();
1470 else
1471 ValVT = VA.getValVT();
Evan Cheng3e42a522008-01-10 02:24:25 +00001472
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001473 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michel91099d62009-02-17 22:15:04 +00001474 // changed with more analysis.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001475 // In case of tail call optimization mark all arguments mutable. Since they
1476 // could be overwritten by lowering of arguments in case of a tail call.
Evan Chengf36bebc2010-02-02 23:58:13 +00001477 if (Flags.isByVal()) {
1478 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1479 VA.getLocMemOffset(), isImmutable, false);
1480 return DAG.getFrameIndex(FI, getPointerTy());
1481 } else {
1482 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1483 VA.getLocMemOffset(), isImmutable, false);
1484 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1485 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene25160362010-02-15 16:53:33 +00001486 PseudoSourceValue::getFixedStack(FI), 0,
1487 false, false, 0);
Evan Chengf36bebc2010-02-02 23:58:13 +00001488 }
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001489}
1490
Dan Gohman8181bd12008-07-27 21:46:04 +00001491SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001492X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001493 CallingConv::ID CallConv,
Dan Gohman9178de12009-08-05 01:29:28 +00001494 bool isVarArg,
1495 const SmallVectorImpl<ISD::InputArg> &Ins,
1496 DebugLoc dl,
1497 SelectionDAG &DAG,
1498 SmallVectorImpl<SDValue> &InVals) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001500 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michel91099d62009-02-17 22:15:04 +00001501
Gordon Henriksen18ace102008-01-05 16:56:59 +00001502 const Function* Fn = MF.getFunction();
1503 if (Fn->hasExternalLinkage() &&
1504 Subtarget->isTargetCygMing() &&
1505 Fn->getName() == "main")
1506 FuncInfo->setForceFramePointer(true);
1507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001509 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001510 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001511
Chris Lattnerac9a9392010-03-11 00:22:57 +00001512 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1513 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 // Assign locations to all of the incoming arguments.
1516 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001517 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1518 ArgLocs, *DAG.getContext());
1519 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 unsigned LastVal = ~0U;
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001522 SDValue ArgValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1524 CCValAssign &VA = ArgLocs[i];
1525 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1526 // places.
1527 assert(VA.getValNo() != LastVal &&
1528 "Don't support value assigned to multiple locs yet");
1529 LastVal = VA.getValNo();
Scott Michel91099d62009-02-17 22:15:04 +00001530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 if (VA.isRegLoc()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001532 EVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001533 TargetRegisterClass *RC = NULL;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001534 if (RegVT == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 RC = X86::GR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001536 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001537 RC = X86::GR64RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001538 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001539 RC = X86::FR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001540 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001541 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001542 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001543 RC = X86::VR128RegisterClass;
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001544 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1545 RC = X86::VR64RegisterClass;
1546 else
Edwin Törökbd448e32009-07-14 16:55:14 +00001547 llvm_unreachable("Unknown argument type!");
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001548
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001549 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman9178de12009-08-05 01:29:28 +00001550 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michel91099d62009-02-17 22:15:04 +00001551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1553 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1554 // right size.
1555 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001556 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 DAG.getValueType(VA.getValVT()));
1558 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001559 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001561 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikova6ad5be2009-08-03 08:14:14 +00001562 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michel91099d62009-02-17 22:15:04 +00001563
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001564 if (VA.isExtInLoc()) {
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001565 // Handle MMX values passed in XMM regs.
1566 if (RegVT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001567 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1568 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001569 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1570 } else
1571 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001572 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573 } else {
1574 assert(VA.isMemLoc());
Dan Gohman9178de12009-08-05 01:29:28 +00001575 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576 }
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001577
1578 // If value is passed via pointer - do a load.
1579 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene25160362010-02-15 16:53:33 +00001580 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1581 false, false, 0);
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001582
Dan Gohman9178de12009-08-05 01:29:28 +00001583 InVals.push_back(ArgValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001585
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001586 // The x86-64 ABI for returning structs by value requires that we copy
1587 // the sret argument into %rax for the return. Save the argument into
1588 // a virtual register so that we can access it from the return points.
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001589 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001590 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1591 unsigned Reg = FuncInfo->getSRetReturnReg();
1592 if (!Reg) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001593 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001594 FuncInfo->setSRetReturnReg(Reg);
1595 }
Dan Gohman9178de12009-08-05 01:29:28 +00001596 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001597 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001598 }
1599
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng6b6ed592010-01-27 00:07:07 +00001601 // Align stack specially for tail calls.
1602 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001603 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001604
1605 // If the function takes variable number of arguments, make a frame index for
1606 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001607 if (isVarArg) {
Dan Gohman9178de12009-08-05 01:29:28 +00001608 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene6424ab92009-11-12 20:49:22 +00001609 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001610 }
1611 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001612 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1613
1614 // FIXME: We should really autogenerate these arrays
1615 static const unsigned GPR64ArgRegsWin64[] = {
1616 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001617 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001618 static const unsigned XMMArgRegsWin64[] = {
1619 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1620 };
1621 static const unsigned GPR64ArgRegs64Bit[] = {
1622 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1623 };
1624 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001625 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1626 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1627 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001628 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1629
1630 if (IsWin64) {
1631 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1632 GPR64ArgRegs = GPR64ArgRegsWin64;
1633 XMMArgRegs = XMMArgRegsWin64;
1634 } else {
1635 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1636 GPR64ArgRegs = GPR64ArgRegs64Bit;
1637 XMMArgRegs = XMMArgRegs64Bit;
1638 }
1639 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1640 TotalNumIntRegs);
1641 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1642 TotalNumXMMRegs);
1643
Devang Patelc386c842009-06-05 21:57:13 +00001644 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Cheng0b84fe12009-02-13 22:36:38 +00001645 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Edwin Törökaf8e1332009-02-01 18:15:56 +00001646 "SSE register cannot be used when SSE is disabled!");
Devang Patelc386c842009-06-05 21:57:13 +00001647 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Cheng0b84fe12009-02-13 22:36:38 +00001648 "SSE register cannot be used when SSE is disabled!");
Devang Patelc386c842009-06-05 21:57:13 +00001649 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Edwin Törökaf8e1332009-02-01 18:15:56 +00001650 // Kernel mode asks for SSE to be disabled, so don't push them
1651 // on the stack.
1652 TotalNumXMMRegs = 0;
Bill Wendling042eda32009-03-11 22:30:01 +00001653
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 // For X86-64, if there are vararg parameters that are passed via
1655 // registers, then we must store them to their spots on the stack so they
1656 // may be loaded by deferencing the result of va_next.
1657 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001658 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1659 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene6424ab92009-11-12 20:49:22 +00001660 TotalNumXMMRegs * 16, 16,
1661 false);
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001662
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001664 SmallVector<SDValue, 8> MemOps;
1665 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman34228bf2009-08-15 01:38:56 +00001666 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001667 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohman34228bf2009-08-15 01:38:56 +00001668 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1669 DAG.getIntPtrConstant(Offset));
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001670 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1671 X86::GR64RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001672 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman8181bd12008-07-27 21:46:04 +00001673 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001674 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Cheng174e2cf2009-10-18 18:16:27 +00001675 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
David Greene25160362010-02-15 16:53:33 +00001676 Offset, false, false, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001677 MemOps.push_back(Store);
Dan Gohman34228bf2009-08-15 01:38:56 +00001678 Offset += 8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001680
Dan Gohmanb9f06832009-08-16 21:24:25 +00001681 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1682 // Now store the XMM (fp + vector) parameter registers.
1683 SmallVector<SDValue, 11> SaveXMMOps;
1684 SaveXMMOps.push_back(Chain);
Dan Gohman34228bf2009-08-15 01:38:56 +00001685
Dan Gohmanb9f06832009-08-16 21:24:25 +00001686 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1687 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1688 SaveXMMOps.push_back(ALVal);
Dan Gohman34228bf2009-08-15 01:38:56 +00001689
Dan Gohmanb9f06832009-08-16 21:24:25 +00001690 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1691 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohman34228bf2009-08-15 01:38:56 +00001692
Dan Gohmanb9f06832009-08-16 21:24:25 +00001693 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1694 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1695 X86::VR128RegisterClass);
1696 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1697 SaveXMMOps.push_back(Val);
1698 }
1699 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1700 MVT::Other,
1701 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 }
Dan Gohmanb9f06832009-08-16 21:24:25 +00001703
1704 if (!MemOps.empty())
1705 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1706 &MemOps[0], MemOps.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001708 }
Scott Michel91099d62009-02-17 22:15:04 +00001709
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 // Some CCs need callee pop.
Dan Gohman9178de12009-08-05 01:29:28 +00001711 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001712 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 } else {
1714 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattnerac9a9392010-03-11 00:22:57 +00001716 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Scott Michel91099d62009-02-17 22:15:04 +00001717 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001718 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001719
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720 if (!Is64Bit) {
1721 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman9178de12009-08-05 01:29:28 +00001722 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1724 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725
Anton Korobeynikove844e472007-08-15 17:12:32 +00001726 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727
Dan Gohman9178de12009-08-05 01:29:28 +00001728 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729}
1730
Dan Gohman8181bd12008-07-27 21:46:04 +00001731SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001732X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1733 SDValue StackPtr, SDValue Arg,
1734 DebugLoc dl, SelectionDAG &DAG,
Evan Chengbc077bf2008-01-10 00:09:10 +00001735 const CCValAssign &VA,
Dan Gohman9178de12009-08-05 01:29:28 +00001736 ISD::ArgFlagsTy Flags) {
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +00001737 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +00001738 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001739 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesence0805b2009-02-03 19:33:06 +00001740 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001741 if (Flags.isByVal()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001742 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengbc077bf2008-01-10 00:09:10 +00001743 }
Dale Johannesence0805b2009-02-03 19:33:06 +00001744 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene25160362010-02-15 16:53:33 +00001745 PseudoSourceValue::getStack(), LocMemOffset,
1746 false, false, 0);
Evan Chengbc077bf2008-01-10 00:09:10 +00001747}
1748
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001749/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001750/// optimization is performed and it is required.
Scott Michel91099d62009-02-17 22:15:04 +00001751SDValue
1752X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Cheng00787d52010-01-26 19:04:47 +00001753 SDValue &OutRetAddr, SDValue Chain,
1754 bool IsTailCall, bool Is64Bit,
1755 int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001756 // Adjust the Return address stack slot.
Owen Andersonac9de032009-08-10 22:56:29 +00001757 EVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001758 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001759
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001760 // Load the "old" Return address.
David Greene25160362010-02-15 16:53:33 +00001761 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001762 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001763}
1764
1765/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1766/// optimization is performed and it is required (FPDiff!=0).
Scott Michel91099d62009-02-17 22:15:04 +00001767static SDValue
1768EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001769 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesence0805b2009-02-03 19:33:06 +00001770 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001771 // Store the return address to the appropriate stack slot.
1772 if (!FPDiff) return Chain;
1773 // Calculate the new stack slot for the return address.
1774 int SlotSize = Is64Bit ? 8 : 4;
Scott Michel91099d62009-02-17 22:15:04 +00001775 int NewReturnAddrFI =
Arnold Schwaighoferf7519522010-02-22 16:18:09 +00001776 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001777 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001778 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001779 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene25160362010-02-15 16:53:33 +00001780 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1781 false, false, 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001782 return Chain;
1783}
1784
Dan Gohman9178de12009-08-05 01:29:28 +00001785SDValue
Evan Chengff116f92010-02-02 23:55:14 +00001786X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001787 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng6b6ed592010-01-27 00:07:07 +00001788 bool &isTailCall,
Dan Gohman9178de12009-08-05 01:29:28 +00001789 const SmallVectorImpl<ISD::OutputArg> &Outs,
1790 const SmallVectorImpl<ISD::InputArg> &Ins,
1791 DebugLoc dl, SelectionDAG &DAG,
1792 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman9178de12009-08-05 01:29:28 +00001793 MachineFunction &MF = DAG.getMachineFunction();
1794 bool Is64Bit = Subtarget->is64Bit();
1795 bool IsStructRet = CallIsStructReturn(Outs);
Evan Chengf4919612010-02-05 02:21:12 +00001796 bool IsSibcall = false;
Dan Gohman9178de12009-08-05 01:29:28 +00001797
Evan Chengf4919612010-02-05 02:21:12 +00001798 if (isTailCall) {
Evan Cheng6b6ed592010-01-27 00:07:07 +00001799 // Check if it's really possible to do a tail call.
Evan Chengec290582010-03-15 18:54:48 +00001800 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1801 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Chengff116f92010-02-02 23:55:14 +00001802 Outs, Ins, DAG);
Evan Chengc54fa452010-02-06 03:28:46 +00001803
1804 // Sibcalls are automatically detected tailcalls which do not require
1805 // ABI changes.
Dan Gohmanea8579c2010-02-08 20:27:50 +00001806 if (!GuaranteedTailCallOpt && isTailCall)
Evan Chengf4919612010-02-05 02:21:12 +00001807 IsSibcall = true;
Evan Chengc54fa452010-02-06 03:28:46 +00001808
1809 if (isTailCall)
1810 ++NumTailCalls;
Evan Chengf4919612010-02-05 02:21:12 +00001811 }
Evan Cheng6b6ed592010-01-27 00:07:07 +00001812
Chris Lattnerac9a9392010-03-11 00:22:57 +00001813 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1814 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816 // Analyze operands of the call, assigning locations to each operand.
1817 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001818 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1819 ArgLocs, *DAG.getContext());
1820 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001821
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 // Get a count of how many bytes are to be pushed on the stack.
1823 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengc54fa452010-02-06 03:28:46 +00001824 if (IsSibcall)
Evan Chengc38381c2010-02-02 02:22:50 +00001825 // This is a sibcall. The memory operands are available in caller's
1826 // own caller's stack.
1827 NumBytes = 0;
Chris Lattnerac9a9392010-03-11 00:22:57 +00001828 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengc54fa452010-02-06 03:28:46 +00001829 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001830
Gordon Henriksen18ace102008-01-05 16:56:59 +00001831 int FPDiff = 0;
Evan Chengc54fa452010-02-06 03:28:46 +00001832 if (isTailCall && !IsSibcall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001833 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michel91099d62009-02-17 22:15:04 +00001834 unsigned NumBytesCallerPushed =
Gordon Henriksen18ace102008-01-05 16:56:59 +00001835 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1836 FPDiff = NumBytesCallerPushed - NumBytes;
1837
1838 // Set the delta of movement of the returnaddr stackslot.
1839 // But only set if delta is greater than previous delta.
1840 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1841 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1842 }
1843
Evan Chengc54fa452010-02-06 03:28:46 +00001844 if (!IsSibcall)
1845 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001846
Dan Gohman8181bd12008-07-27 21:46:04 +00001847 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001848 // Load return adress for tail calls.
Evan Chengc54fa452010-02-06 03:28:46 +00001849 if (isTailCall && FPDiff)
1850 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1851 Is64Bit, FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001852
Dan Gohman8181bd12008-07-27 21:46:04 +00001853 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1854 SmallVector<SDValue, 8> MemOpChains;
1855 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001857 // Walk the register/memloc assignments, inserting copies/loads. In the case
1858 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1860 CCValAssign &VA = ArgLocs[i];
Owen Andersonac9de032009-08-10 22:56:29 +00001861 EVT RegVT = VA.getLocVT();
Dan Gohman9178de12009-08-05 01:29:28 +00001862 SDValue Arg = Outs[i].Val;
1863 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman705e3f72008-09-13 01:54:27 +00001864 bool isByVal = Flags.isByVal();
Scott Michel91099d62009-02-17 22:15:04 +00001865
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001866 // Promote the value if needed.
1867 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001868 default: llvm_unreachable("Unknown loc info!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 case CCValAssign::Full: break;
1870 case CCValAssign::SExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001871 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 break;
1873 case CCValAssign::ZExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001874 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001875 break;
1876 case CCValAssign::AExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001877 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1878 // Special case: passing MMX values in XMM registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001879 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1880 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1881 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001882 } else
1883 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1884 break;
1885 case CCValAssign::BCvt:
1886 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887 break;
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001888 case CCValAssign::Indirect: {
1889 // Store the argument.
1890 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Cheng174e2cf2009-10-18 18:16:27 +00001891 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001892 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene25160362010-02-15 16:53:33 +00001893 PseudoSourceValue::getFixedStack(FI), 0,
1894 false, false, 0);
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001895 Arg = SpillSlot;
1896 break;
1897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 }
Scott Michel91099d62009-02-17 22:15:04 +00001899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900 if (VA.isRegLoc()) {
1901 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengc54fa452010-02-06 03:28:46 +00001902 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Chengf4919612010-02-05 02:21:12 +00001903 assert(VA.isMemLoc());
1904 if (StackPtr.getNode() == 0)
1905 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1906 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1907 dl, DAG, VA, Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908 }
1909 }
Scott Michel91099d62009-02-17 22:15:04 +00001910
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 if (!MemOpChains.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001912 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 &MemOpChains[0], MemOpChains.size());
1914
1915 // Build a sequence of copy-to-reg nodes chained together with token chain
1916 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001917 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001918 // Tail call byval lowering might overwrite argument registers so in case of
1919 // tail call optimization the copies to registers are lowered later.
Dan Gohman9178de12009-08-05 01:29:28 +00001920 if (!isTailCall)
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001921 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00001922 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001923 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001924 InFlag = Chain.getValue(1);
1925 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001926
Chris Lattnerf165d342009-07-09 04:24:46 +00001927 if (Subtarget->isPICStyleGOT()) {
Chris Lattner679cad52009-07-09 02:55:47 +00001928 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1929 // GOT pointer.
Dan Gohman9178de12009-08-05 01:29:28 +00001930 if (!isTailCall) {
Chris Lattner679cad52009-07-09 02:55:47 +00001931 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1932 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerd2c680b2010-04-02 20:16:16 +00001933 DebugLoc(), getPointerTy()),
Chris Lattner679cad52009-07-09 02:55:47 +00001934 InFlag);
1935 InFlag = Chain.getValue(1);
1936 } else {
1937 // If we are tail calling and generating PIC/GOT style code load the
1938 // address of the callee into ECX. The value in ecx is used as target of
1939 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1940 // for tail calls on PIC/GOT architectures. Normally we would just put the
1941 // address of GOT into ebx and then call target@PLT. But for tail calls
1942 // ebx would be restored (since ebx is callee saved) before jumping to the
1943 // target@PLT.
1944
1945 // Note: The actual moving to ECX is done further down.
1946 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1947 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1948 !G->getGlobal()->hasProtectedVisibility())
1949 Callee = LowerGlobalAddress(Callee, DAG);
1950 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner5d1f2572009-07-09 04:39:06 +00001951 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattner679cad52009-07-09 02:55:47 +00001952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001953 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001954
Gordon Henriksen18ace102008-01-05 16:56:59 +00001955 if (Is64Bit && isVarArg) {
1956 // From AMD64 ABI document:
1957 // For calls that may call functions that use varargs or stdargs
1958 // (prototype-less calls or calls to functions containing ellipsis (...) in
1959 // the declaration) %al is used as hidden argument to specify the number
1960 // of SSE registers used. The contents of %al do not need to match exactly
1961 // the number of registers, but must be an ubound on the number of SSE
1962 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001963
1964 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001965 // Count the number of XMM registers allocated.
1966 static const unsigned XMMArgRegs[] = {
1967 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1968 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1969 };
1970 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michel91099d62009-02-17 22:15:04 +00001971 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Edwin Törökaf8e1332009-02-01 18:15:56 +00001972 && "SSE registers cannot be used when SSE is disabled");
Scott Michel91099d62009-02-17 22:15:04 +00001973
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001974 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001975 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001976 InFlag = Chain.getValue(1);
1977 }
1978
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001979
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001980 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman9178de12009-08-05 01:29:28 +00001981 if (isTailCall) {
1982 // Force all the incoming stack arguments to be loaded from the stack
1983 // before any new outgoing arguments are stored to the stack, because the
1984 // outgoing stack slots may alias the incoming argument stack slots, and
1985 // the alias isn't otherwise explicit. This is slightly more conservative
1986 // than necessary, because it means that each store effectively depends
1987 // on every argument instead of just those arguments it would clobber.
1988 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1989
Dan Gohman8181bd12008-07-27 21:46:04 +00001990 SmallVector<SDValue, 8> MemOpChains2;
1991 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001992 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001993 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001994 InFlag = SDValue();
Dan Gohmanea8579c2010-02-08 20:27:50 +00001995 if (GuaranteedTailCallOpt) {
Evan Chengc38381c2010-02-02 02:22:50 +00001996 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1997 CCValAssign &VA = ArgLocs[i];
1998 if (VA.isRegLoc())
1999 continue;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00002000 assert(VA.isMemLoc());
Dan Gohman9178de12009-08-05 01:29:28 +00002001 SDValue Arg = Outs[i].Val;
2002 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002003 // Create frame index.
2004 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00002005 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene6424ab92009-11-12 20:49:22 +00002006 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002007 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00002008
Duncan Sandsc93fae32008-03-21 09:14:45 +00002009 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00002010 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00002011 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00002012 if (StackPtr.getNode() == 0)
Scott Michel91099d62009-02-17 22:15:04 +00002013 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002014 getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00002015 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002016
Dan Gohman9178de12009-08-05 01:29:28 +00002017 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2018 ArgChain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002019 Flags, DAG, dl));
Gordon Henriksen18ace102008-01-05 16:56:59 +00002020 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00002021 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00002022 MemOpChains2.push_back(
Dan Gohman9178de12009-08-05 01:29:28 +00002023 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene25160362010-02-15 16:53:33 +00002024 PseudoSourceValue::getFixedStack(FI), 0,
2025 false, false, 0));
Scott Michel91099d62009-02-17 22:15:04 +00002026 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00002027 }
2028 }
2029
2030 if (!MemOpChains2.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002031 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00002032 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00002033
Arnold Schwaighofera0032722008-04-30 09:16:33 +00002034 // Copy arguments to their registers.
2035 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00002036 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002037 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00002038 InFlag = Chain.getValue(1);
2039 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002040 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002041
Gordon Henriksen18ace102008-01-05 16:56:59 +00002042 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002043 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00002044 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002045 }
2046
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002047 bool WasGlobalOrExternal = false;
2048 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2049 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2050 // In the 64-bit large code model, we have to make all calls
2051 // through a register, since the call instruction's 32-bit
2052 // pc-relative offset may not be large enough to hold the whole
2053 // address.
2054 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2055 WasGlobalOrExternal = true;
2056 // If the callee is a GlobalAddress node (quite common, every direct call
2057 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2058 // it.
2059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 // We should use extra load for direct calls to dllimported functions in
2061 // non-JIT mode.
Chris Lattner48837612009-07-09 05:27:35 +00002062 GlobalValue *GV = G->getGlobal();
Chris Lattner180a7ee2009-07-10 05:48:03 +00002063 if (!GV->hasDLLImportLinkage()) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002064 unsigned char OpFlags = 0;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002065
Chris Lattner8e8afe42009-07-09 05:02:21 +00002066 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2067 // external symbols most go through the PLT in PIC mode. If the symbol
2068 // has hidden or protected visibility, or if it is static or local, then
2069 // we don't need to use the PLT - we can directly call it.
2070 if (Subtarget->isTargetELF() &&
2071 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner48837612009-07-09 05:27:35 +00002072 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002073 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00002074 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner48837612009-07-09 05:27:35 +00002075 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2076 Subtarget->getDarwinVers() < 9) {
2077 // PC-relative references to external symbols should go through $stub,
2078 // unless we're building with the leopard linker or later, which
2079 // automatically synthesizes these stubs.
2080 OpFlags = X86II::MO_DARWIN_STUB;
2081 }
Chris Lattner8e8afe42009-07-09 05:02:21 +00002082
Chris Lattner48837612009-07-09 05:27:35 +00002083 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner8e8afe42009-07-09 05:02:21 +00002084 G->getOffset(), OpFlags);
2085 }
Bill Wendlingfef06052008-09-16 21:48:12 +00002086 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002087 WasGlobalOrExternal = true;
Chris Lattner8e8afe42009-07-09 05:02:21 +00002088 unsigned char OpFlags = 0;
2089
2090 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2091 // symbols should go through the PLT.
2092 if (Subtarget->isTargetELF() &&
Chris Lattner48837612009-07-09 05:27:35 +00002093 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002094 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00002095 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner48837612009-07-09 05:27:35 +00002096 Subtarget->getDarwinVers() < 9) {
2097 // PC-relative references to external symbols should go through $stub,
2098 // unless we're building with the leopard linker or later, which
2099 // automatically synthesizes these stubs.
2100 OpFlags = X86II::MO_DARWIN_STUB;
2101 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002102
Chris Lattner8e8afe42009-07-09 05:02:21 +00002103 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2104 OpFlags);
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002105 }
2106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002107 // Returns a chain & a flag for retval copy to use.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002108 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00002109 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002110
Evan Chengc54fa452010-02-06 03:28:46 +00002111 if (!IsSibcall && isTailCall) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002112 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2113 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002114 InFlag = Chain.getValue(1);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002115 }
Scott Michel91099d62009-02-17 22:15:04 +00002116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 Ops.push_back(Chain);
2118 Ops.push_back(Callee);
2119
Dan Gohman9178de12009-08-05 01:29:28 +00002120 if (isTailCall)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002121 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122
Gordon Henriksen18ace102008-01-05 16:56:59 +00002123 // Add argument registers to the end of the list so that they are known live
2124 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00002125 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2126 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2127 RegsToPass[i].second.getValueType()));
Scott Michel91099d62009-02-17 22:15:04 +00002128
Evan Cheng8ba45e62008-03-18 23:36:35 +00002129 // Add an implicit use GOT pointer in EBX.
Dan Gohman9178de12009-08-05 01:29:28 +00002130 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng8ba45e62008-03-18 23:36:35 +00002131 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2132
2133 // Add an implicit use of AL for x86 vararg functions.
2134 if (Is64Bit && isVarArg)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002135 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng8ba45e62008-03-18 23:36:35 +00002136
Gabor Greif1c80d112008-08-28 21:40:38 +00002137 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002138 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002139
Dan Gohman9178de12009-08-05 01:29:28 +00002140 if (isTailCall) {
2141 // If this is the first return lowered for this function, add the regs
2142 // to the liveout set for the function.
2143 if (MF.getRegInfo().liveout_empty()) {
2144 SmallVector<CCValAssign, 16> RVLocs;
2145 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2146 *DAG.getContext());
2147 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2148 for (unsigned i = 0; i != RVLocs.size(); ++i)
2149 if (RVLocs[i].isRegLoc())
2150 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2151 }
Dan Gohman9178de12009-08-05 01:29:28 +00002152 return DAG.getNode(X86ISD::TC_RETURN, dl,
2153 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00002154 }
2155
Dale Johannesence0805b2009-02-03 19:33:06 +00002156 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 InFlag = Chain.getValue(1);
2158
2159 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00002160 unsigned NumBytesForCalleeToPush;
Dan Gohman9178de12009-08-05 01:29:28 +00002161 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen18ace102008-01-05 16:56:59 +00002162 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattnerac9a9392010-03-11 00:22:57 +00002163 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmandf1a7ff2010-02-10 16:03:48 +00002164 // If this is a call to a struct-return function, the callee
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 // pops the hidden struct pointer, so we have to push it back.
2166 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002167 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002168 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002169 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michel91099d62009-02-17 22:15:04 +00002170
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002171 // Returns a flag for retval copy to use.
Evan Chengc54fa452010-02-06 03:28:46 +00002172 if (!IsSibcall) {
2173 Chain = DAG.getCALLSEQ_END(Chain,
2174 DAG.getIntPtrConstant(NumBytes, true),
2175 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2176 true),
2177 InFlag);
2178 InFlag = Chain.getValue(1);
2179 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180
2181 // Handle result values, copying them out of physregs into vregs that we
2182 // return.
Dan Gohman9178de12009-08-05 01:29:28 +00002183 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2184 Ins, dl, DAG, InVals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185}
2186
2187
2188//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002189// Fast Calling Convention (tail call) implementation
2190//===----------------------------------------------------------------------===//
2191
2192// Like std call, callee cleans arguments, convention except that ECX is
2193// reserved for storing the tail called function address. Only 2 registers are
2194// free for argument passing (inreg). Tail call optimization is performed
2195// provided:
2196// * tailcallopt is enabled
2197// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00002198// On X86_64 architecture with GOT-style position independent code only local
2199// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00002200// To keep the stack aligned according to platform abi the function
2201// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2202// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002203// If a tail called function callee has more arguments than the caller the
2204// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00002205// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002206// original REtADDR, but before the saved framepointer or the spilled registers
2207// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2208// stack layout:
2209// arg1
2210// arg2
2211// RETADDR
Scott Michel91099d62009-02-17 22:15:04 +00002212// [ new RETADDR
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002213// move area ]
2214// (possible EBP)
2215// ESI
2216// EDI
2217// local1 ..
2218
2219/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2220/// for a 16 byte align requirement.
Scott Michel91099d62009-02-17 22:15:04 +00002221unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002222 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00002223 MachineFunction &MF = DAG.getMachineFunction();
2224 const TargetMachine &TM = MF.getTarget();
2225 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2226 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michel91099d62009-02-17 22:15:04 +00002227 uint64_t AlignMask = StackAlignment - 1;
Evan Chengded8f902008-09-07 09:07:23 +00002228 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00002229 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00002230 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2231 // Number smaller than 12 so just add the difference.
2232 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2233 } else {
2234 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michel91099d62009-02-17 22:15:04 +00002235 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chengded8f902008-09-07 09:07:23 +00002236 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002237 }
Evan Chengded8f902008-09-07 09:07:23 +00002238 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002239}
2240
Evan Chengf4919612010-02-05 02:21:12 +00002241/// MatchingStackOffset - Return true if the given stack call argument is
2242/// already available in the same position (relatively) of the caller's
2243/// incoming argument stack.
2244static
2245bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2246 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2247 const X86InstrInfo *TII) {
Evan Cheng3df6bd42010-03-05 08:38:04 +00002248 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2249 int FI = INT_MAX;
Evan Chengf4919612010-02-05 02:21:12 +00002250 if (Arg.getOpcode() == ISD::CopyFromReg) {
2251 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2252 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2253 return false;
2254 MachineInstr *Def = MRI->getVRegDef(VR);
2255 if (!Def)
2256 return false;
2257 if (!Flags.isByVal()) {
2258 if (!TII->isLoadFromStackSlot(Def, FI))
2259 return false;
2260 } else {
2261 unsigned Opcode = Def->getOpcode();
2262 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2263 Def->getOperand(1).isFI()) {
2264 FI = Def->getOperand(1).getIndex();
Evan Cheng3df6bd42010-03-05 08:38:04 +00002265 Bytes = Flags.getByValSize();
Evan Chengf4919612010-02-05 02:21:12 +00002266 } else
2267 return false;
2268 }
Evan Cheng3df6bd42010-03-05 08:38:04 +00002269 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2270 if (Flags.isByVal())
2271 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng53c69cb2010-03-05 19:55:55 +00002272 // dereferenced. e.g.
Evan Cheng3df6bd42010-03-05 08:38:04 +00002273 // define @foo(%struct.X* %A) {
2274 // tail call @bar(%struct.X* byval %A)
2275 // }
Evan Chengf4919612010-02-05 02:21:12 +00002276 return false;
2277 SDValue Ptr = Ld->getBasePtr();
2278 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2279 if (!FINode)
2280 return false;
2281 FI = FINode->getIndex();
Evan Cheng3df6bd42010-03-05 08:38:04 +00002282 } else
2283 return false;
Evan Chengf4919612010-02-05 02:21:12 +00002284
Evan Cheng3df6bd42010-03-05 08:38:04 +00002285 assert(FI != INT_MAX);
Evan Chengf4919612010-02-05 02:21:12 +00002286 if (!MFI->isFixedObjectIndex(FI))
2287 return false;
Evan Cheng3df6bd42010-03-05 08:38:04 +00002288 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Chengf4919612010-02-05 02:21:12 +00002289}
2290
Dan Gohman9178de12009-08-05 01:29:28 +00002291/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2292/// for tail call optimization. Targets which want to do tail call
2293/// optimization should implement this function.
2294bool
2295X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel5838baa2009-09-02 08:44:58 +00002296 CallingConv::ID CalleeCC,
Dan Gohman9178de12009-08-05 01:29:28 +00002297 bool isVarArg,
Evan Chengec290582010-03-15 18:54:48 +00002298 bool isCalleeStructRet,
2299 bool isCallerStructRet,
Evan Chengd82fae32010-01-27 06:25:16 +00002300 const SmallVectorImpl<ISD::OutputArg> &Outs,
2301 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman9178de12009-08-05 01:29:28 +00002302 SelectionDAG& DAG) const {
Chris Lattnerac9a9392010-03-11 00:22:57 +00002303 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengd82fae32010-01-27 06:25:16 +00002304 CalleeCC != CallingConv::C)
2305 return false;
2306
Evan Cheng3d424642010-01-29 06:45:59 +00002307 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng522dbc02010-03-26 16:26:03 +00002308 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng3d424642010-01-29 06:45:59 +00002309 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohmanea8579c2010-02-08 20:27:50 +00002310 if (GuaranteedTailCallOpt) {
Chris Lattnerac9a9392010-03-11 00:22:57 +00002311 if (IsTailCallConvention(CalleeCC) &&
Evan Chengca18ef22010-01-31 06:44:49 +00002312 CallerF->getCallingConv() == CalleeCC)
2313 return true;
2314 return false;
2315 }
2316
Evan Chengc38381c2010-02-02 02:22:50 +00002317 // Look for obvious safe cases to perform tail call optimization that does not
2318 // requite ABI changes. This is what gcc calls sibcall.
2319
Evan Cheng522dbc02010-03-26 16:26:03 +00002320 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2321 // emit a special epilogue.
2322 if (RegInfo->needsStackRealignment(MF))
2323 return false;
2324
Evan Cheng50ed8882010-03-26 02:13:13 +00002325 // Do not sibcall optimize vararg calls unless the call site is not passing any
2326 // arguments.
2327 if (isVarArg && !Outs.empty())
Evan Chengca18ef22010-01-31 06:44:49 +00002328 return false;
2329
Evan Chengec290582010-03-15 18:54:48 +00002330 // Also avoid sibcall optimization if either caller or callee uses struct
2331 // return semantics.
2332 if (isCalleeStructRet || isCallerStructRet)
2333 return false;
2334
Evan Chengd5b29562010-03-20 02:58:15 +00002335 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2336 // Therefore if it's not used by the call it is not safe to optimize this into
2337 // a sibcall.
2338 bool Unused = false;
2339 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2340 if (!Ins[i].Used) {
2341 Unused = true;
2342 break;
2343 }
2344 }
2345 if (Unused) {
2346 SmallVector<CCValAssign, 16> RVLocs;
2347 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2348 RVLocs, *DAG.getContext());
2349 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2350 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2351 CCValAssign &VA = RVLocs[i];
2352 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2353 return false;
2354 }
2355 }
2356
Evan Cheng73e1dbe2010-01-30 01:22:00 +00002357 // If the callee takes no arguments then go on to check the results of the
2358 // call.
2359 if (!Outs.empty()) {
2360 // Check if stack adjustment is needed. For now, do not do this if any
2361 // argument is passed on the stack.
2362 SmallVector<CCValAssign, 16> ArgLocs;
2363 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2364 ArgLocs, *DAG.getContext());
2365 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengc38381c2010-02-02 02:22:50 +00002366 if (CCInfo.getNextStackOffset()) {
2367 MachineFunction &MF = DAG.getMachineFunction();
2368 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2369 return false;
2370 if (Subtarget->isTargetWin64())
2371 // Win64 ABI has additional complications.
2372 return false;
2373
2374 // Check if the arguments are already laid out in the right way as
2375 // the caller's fixed stack objects.
2376 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Chengf4919612010-02-05 02:21:12 +00002377 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2378 const X86InstrInfo *TII =
2379 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengc38381c2010-02-02 02:22:50 +00002380 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2381 CCValAssign &VA = ArgLocs[i];
2382 EVT RegVT = VA.getLocVT();
2383 SDValue Arg = Outs[i].Val;
2384 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengc38381c2010-02-02 02:22:50 +00002385 if (VA.getLocInfo() == CCValAssign::Indirect)
2386 return false;
2387 if (!VA.isRegLoc()) {
Evan Chengf4919612010-02-05 02:21:12 +00002388 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2389 MFI, MRI, TII))
Evan Chengc38381c2010-02-02 02:22:50 +00002390 return false;
2391 }
2392 }
2393 }
Evan Cheng73e1dbe2010-01-30 01:22:00 +00002394 }
Evan Chengd82fae32010-01-27 06:25:16 +00002395
Evan Cheng411c0522010-02-03 03:28:02 +00002396 return true;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002397}
2398
Dan Gohmanca4857a2008-09-03 23:12:08 +00002399FastISel *
Evan Cheng00787d52010-01-26 19:04:47 +00002400X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2401 DwarfWriter *dw,
2402 DenseMap<const Value *, unsigned> &vm,
2403 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2404 DenseMap<const AllocaInst *, int> &am
Dan Gohman9dd43582008-10-14 23:54:11 +00002405#ifndef NDEBUG
Evan Cheng00787d52010-01-26 19:04:47 +00002406 , SmallSet<Instruction*, 8> &cil
Dan Gohman9dd43582008-10-14 23:54:11 +00002407#endif
2408 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00002409 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00002410#ifndef NDEBUG
2411 , cil
2412#endif
2413 );
Dan Gohman97805ee2008-08-19 21:32:53 +00002414}
2415
2416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417//===----------------------------------------------------------------------===//
2418// Other Lowering Hooks
2419//===----------------------------------------------------------------------===//
2420
2421
Dan Gohman8181bd12008-07-27 21:46:04 +00002422SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00002423 MachineFunction &MF = DAG.getMachineFunction();
2424 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2425 int ReturnAddrIndex = FuncInfo->getRAIndex();
2426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 if (ReturnAddrIndex == 0) {
2428 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00002429 uint64_t SlotSize = TD->getPointerSize();
David Greene6424ab92009-11-12 20:49:22 +00002430 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighoferf7519522010-02-22 16:18:09 +00002431 false, false);
Anton Korobeynikove844e472007-08-15 17:12:32 +00002432 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002433 }
2434
2435 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2436}
2437
2438
Anton Korobeynikovc283e152009-08-05 23:01:26 +00002439bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2440 bool hasSymbolicDisplacement) {
2441 // Offset should fit into 32 bit immediate field.
Benjamin Kramer25c5cb62010-03-29 21:13:41 +00002442 if (!isInt<32>(Offset))
Anton Korobeynikovc283e152009-08-05 23:01:26 +00002443 return false;
2444
2445 // If we don't have a symbolic displacement - we don't have any extra
2446 // restrictions.
2447 if (!hasSymbolicDisplacement)
2448 return true;
2449
2450 // FIXME: Some tweaks might be needed for medium code model.
2451 if (M != CodeModel::Small && M != CodeModel::Kernel)
2452 return false;
2453
2454 // For small code model we assume that latest object is 16MB before end of 31
2455 // bits boundary. We may also accept pretty large negative constants knowing
2456 // that all objects are in the positive half of address space.
2457 if (M == CodeModel::Small && Offset < 16*1024*1024)
2458 return true;
2459
2460 // For kernel code model we know that all object resist in the negative half
2461 // of 32bits address space. We may not accept negative offsets, since they may
2462 // be just off and we may accept pretty large positive ones.
2463 if (M == CodeModel::Kernel && Offset > 0)
2464 return true;
2465
2466 return false;
2467}
2468
Chris Lattnerebb91142008-12-24 23:53:05 +00002469/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2470/// specific condition code, returning the condition code and the LHS/RHS of the
2471/// comparison to make.
2472static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2473 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 if (!isFP) {
2475 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2476 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2477 // X > -1 -> X == 0, jump !sign.
2478 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002479 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2481 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00002482 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002483 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002484 // X < 1 -> X <= 0
2485 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002486 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002487 }
2488 }
2489
2490 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002491 default: llvm_unreachable("Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002492 case ISD::SETEQ: return X86::COND_E;
2493 case ISD::SETGT: return X86::COND_G;
2494 case ISD::SETGE: return X86::COND_GE;
2495 case ISD::SETLT: return X86::COND_L;
2496 case ISD::SETLE: return X86::COND_LE;
2497 case ISD::SETNE: return X86::COND_NE;
2498 case ISD::SETULT: return X86::COND_B;
2499 case ISD::SETUGT: return X86::COND_A;
2500 case ISD::SETULE: return X86::COND_BE;
2501 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002502 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002503 }
Scott Michel91099d62009-02-17 22:15:04 +00002504
Chris Lattnerb8397512008-12-23 23:42:27 +00002505 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002506
Chris Lattnerb8397512008-12-23 23:42:27 +00002507 // If LHS is a foldable load, but RHS is not, flip the condition.
2508 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2509 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2510 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2511 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002512 }
2513
Chris Lattnerb8397512008-12-23 23:42:27 +00002514 switch (SetCCOpcode) {
2515 default: break;
2516 case ISD::SETOLT:
2517 case ISD::SETOLE:
2518 case ISD::SETUGT:
2519 case ISD::SETUGE:
2520 std::swap(LHS, RHS);
2521 break;
2522 }
2523
2524 // On a floating point condition, the flags are set as follows:
2525 // ZF PF CF op
2526 // 0 | 0 | 0 | X > Y
2527 // 0 | 0 | 1 | X < Y
2528 // 1 | 0 | 0 | X == Y
2529 // 1 | 1 | 1 | unordered
2530 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002531 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002532 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002533 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002534 case ISD::SETOLT: // flipped
2535 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002536 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002537 case ISD::SETOLE: // flipped
2538 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002539 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002540 case ISD::SETUGT: // flipped
2541 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002542 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002543 case ISD::SETUGE: // flipped
2544 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002545 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002546 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002547 case ISD::SETNE: return X86::COND_NE;
2548 case ISD::SETUO: return X86::COND_P;
2549 case ISD::SETO: return X86::COND_NP;
Dan Gohman8ab7dd02009-10-20 16:22:37 +00002550 case ISD::SETOEQ:
2551 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattnerb8397512008-12-23 23:42:27 +00002552 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002553}
2554
2555/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2556/// code. Current x86 isa includes the following FP cmov instructions:
2557/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2558static bool hasFPCMov(unsigned X86CC) {
2559 switch (X86CC) {
2560 default:
2561 return false;
2562 case X86::COND_B:
2563 case X86::COND_BE:
2564 case X86::COND_E:
2565 case X86::COND_P:
2566 case X86::COND_A:
2567 case X86::COND_AE:
2568 case X86::COND_NE:
2569 case X86::COND_NP:
2570 return true;
2571 }
2572}
2573
Evan Cheng6337b552009-10-27 19:56:55 +00002574/// isFPImmLegal - Returns true if the target can instruction select the
2575/// specified FP immediate natively. If false, the legalizer will
2576/// materialize the FP immediate as a load from a constant pool.
Evan Chenga0e67782009-10-28 01:43:28 +00002577bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Cheng6337b552009-10-27 19:56:55 +00002578 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2579 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2580 return true;
2581 }
2582 return false;
2583}
2584
Nate Begeman543d2142009-04-27 18:41:29 +00002585/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2586/// the specified range (L, H].
2587static bool isUndefOrInRange(int Val, int Low, int Hi) {
2588 return (Val < 0) || (Val >= Low && Val < Hi);
2589}
2590
2591/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2592/// specified value.
2593static bool isUndefOrEqual(int Val, int CmpVal) {
2594 if (Val < 0 || Val == CmpVal)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595 return true;
Nate Begeman543d2142009-04-27 18:41:29 +00002596 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597}
2598
Nate Begeman543d2142009-04-27 18:41:29 +00002599/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2600/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2601/// the second operand.
Owen Andersonac9de032009-08-10 22:56:29 +00002602static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002603 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman543d2142009-04-27 18:41:29 +00002604 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002605 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman543d2142009-04-27 18:41:29 +00002606 return (Mask[0] < 2 && Mask[1] < 2);
2607 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608}
2609
Nate Begeman543d2142009-04-27 18:41:29 +00002610bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002611 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002612 N->getMask(M);
2613 return ::isPSHUFDMask(M, N->getValueType(0));
2614}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615
Nate Begeman543d2142009-04-27 18:41:29 +00002616/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2617/// is suitable for input to PSHUFHW.
Owen Andersonac9de032009-08-10 22:56:29 +00002618static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002619 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002621
Nate Begeman543d2142009-04-27 18:41:29 +00002622 // Lower quadword copied in order or undef.
2623 for (int i = 0; i != 4; ++i)
2624 if (Mask[i] >= 0 && Mask[i] != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 // Upper quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002628 for (int i = 4; i != 8; ++i)
2629 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002631
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 return true;
2633}
2634
Nate Begeman543d2142009-04-27 18:41:29 +00002635bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002636 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002637 N->getMask(M);
2638 return ::isPSHUFHWMask(M, N->getValueType(0));
2639}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640
Nate Begeman543d2142009-04-27 18:41:29 +00002641/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2642/// is suitable for input to PSHUFLW.
Owen Andersonac9de032009-08-10 22:56:29 +00002643static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002644 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002646
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002647 // Upper quadword copied in order.
Nate Begeman543d2142009-04-27 18:41:29 +00002648 for (int i = 4; i != 8; ++i)
2649 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002650 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002651
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002652 // Lower quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002653 for (int i = 0; i != 4; ++i)
2654 if (Mask[i] >= 4)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002655 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002656
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002657 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002658}
2659
Nate Begeman543d2142009-04-27 18:41:29 +00002660bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002661 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002662 N->getMask(M);
2663 return ::isPSHUFLWMask(M, N->getValueType(0));
2664}
2665
Nate Begeman080f8e22009-10-19 02:17:23 +00002666/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2667/// is suitable for input to PALIGNR.
2668static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2669 bool hasSSSE3) {
2670 int i, e = VT.getVectorNumElements();
2671
2672 // Do not handle v2i64 / v2f64 shuffles with palignr.
2673 if (e < 4 || !hasSSSE3)
2674 return false;
2675
2676 for (i = 0; i != e; ++i)
2677 if (Mask[i] >= 0)
2678 break;
2679
2680 // All undef, not a palignr.
2681 if (i == e)
2682 return false;
2683
2684 // Determine if it's ok to perform a palignr with only the LHS, since we
2685 // don't have access to the actual shuffle elements to see if RHS is undef.
2686 bool Unary = Mask[i] < (int)e;
2687 bool NeedsUnary = false;
2688
2689 int s = Mask[i] - i;
2690
2691 // Check the rest of the elements to see if they are consecutive.
2692 for (++i; i != e; ++i) {
2693 int m = Mask[i];
2694 if (m < 0)
2695 continue;
2696
2697 Unary = Unary && (m < (int)e);
2698 NeedsUnary = NeedsUnary || (m < s);
2699
2700 if (NeedsUnary && !Unary)
2701 return false;
2702 if (Unary && m != ((s+i) & (e-1)))
2703 return false;
2704 if (!Unary && m != (s+i))
2705 return false;
2706 }
2707 return true;
2708}
2709
2710bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2711 SmallVector<int, 8> M;
2712 N->getMask(M);
2713 return ::isPALIGNRMask(M, N->getValueType(0), true);
2714}
2715
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002716/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2717/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersonac9de032009-08-10 22:56:29 +00002718static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002719 int NumElems = VT.getVectorNumElements();
2720 if (NumElems != 2 && NumElems != 4)
2721 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002722
Nate Begeman543d2142009-04-27 18:41:29 +00002723 int Half = NumElems / 2;
2724 for (int i = 0; i < Half; ++i)
2725 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002727 for (int i = Half; i < NumElems; ++i)
2728 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 return true;
2732}
2733
Nate Begeman543d2142009-04-27 18:41:29 +00002734bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2735 SmallVector<int, 8> M;
2736 N->getMask(M);
2737 return ::isSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002738}
2739
2740/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2741/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2742/// half elements to come from vector 1 (which would equal the dest.) and
2743/// the upper half to come from vector 2.
Owen Andersonac9de032009-08-10 22:56:29 +00002744static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002745 int NumElems = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002746
2747 if (NumElems != 2 && NumElems != 4)
Nate Begeman543d2142009-04-27 18:41:29 +00002748 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002749
Nate Begeman543d2142009-04-27 18:41:29 +00002750 int Half = NumElems / 2;
2751 for (int i = 0; i < Half; ++i)
2752 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002754 for (int i = Half; i < NumElems; ++i)
2755 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 return false;
2757 return true;
2758}
2759
Nate Begeman543d2142009-04-27 18:41:29 +00002760static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2761 SmallVector<int, 8> M;
2762 N->getMask(M);
2763 return isCommutedSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002764}
2765
2766/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2767/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman543d2142009-04-27 18:41:29 +00002768bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2769 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770 return false;
2771
2772 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman543d2142009-04-27 18:41:29 +00002773 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2774 isUndefOrEqual(N->getMaskElt(1), 7) &&
2775 isUndefOrEqual(N->getMaskElt(2), 2) &&
2776 isUndefOrEqual(N->getMaskElt(3), 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777}
2778
Nate Begemanb13034d2009-11-07 23:17:15 +00002779/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2780/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2781/// <2, 3, 2, 3>
2782bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2783 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2784
2785 if (NumElems != 4)
2786 return false;
2787
2788 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2789 isUndefOrEqual(N->getMaskElt(1), 3) &&
2790 isUndefOrEqual(N->getMaskElt(2), 2) &&
2791 isUndefOrEqual(N->getMaskElt(3), 3);
2792}
2793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002794/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2795/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman543d2142009-04-27 18:41:29 +00002796bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2797 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002799 if (NumElems != 2 && NumElems != 4)
2800 return false;
2801
2802 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002803 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002804 return false;
2805
2806 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002807 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002808 return false;
2809
2810 return true;
2811}
2812
Nate Begemanb13034d2009-11-07 23:17:15 +00002813/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2814/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2815bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002816 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 if (NumElems != 2 && NumElems != 4)
2819 return false;
2820
2821 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002822 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002823 return false;
2824
Nate Begeman543d2142009-04-27 18:41:29 +00002825 for (unsigned i = 0; i < NumElems/2; ++i)
2826 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002827 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828
2829 return true;
2830}
2831
2832/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2833/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersonac9de032009-08-10 22:56:29 +00002834static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002835 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002836 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002837 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2838 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002839
Nate Begeman543d2142009-04-27 18:41:29 +00002840 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2841 int BitI = Mask[i];
2842 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 if (!isUndefOrEqual(BitI, j))
2844 return false;
2845 if (V2IsSplat) {
Mon P Wang56d91642009-02-04 01:16:59 +00002846 if (!isUndefOrEqual(BitI1, NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 return false;
2848 } else {
2849 if (!isUndefOrEqual(BitI1, j + NumElts))
2850 return false;
2851 }
2852 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853 return true;
2854}
2855
Nate Begeman543d2142009-04-27 18:41:29 +00002856bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2857 SmallVector<int, 8> M;
2858 N->getMask(M);
2859 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002860}
2861
2862/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2863/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002864static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002865 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002866 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2868 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002869
Nate Begeman543d2142009-04-27 18:41:29 +00002870 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2871 int BitI = Mask[i];
2872 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 if (!isUndefOrEqual(BitI, j + NumElts/2))
2874 return false;
2875 if (V2IsSplat) {
2876 if (isUndefOrEqual(BitI1, NumElts))
2877 return false;
2878 } else {
2879 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2880 return false;
2881 }
2882 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 return true;
2884}
2885
Nate Begeman543d2142009-04-27 18:41:29 +00002886bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2887 SmallVector<int, 8> M;
2888 N->getMask(M);
2889 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002890}
2891
2892/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2893/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2894/// <0, 0, 1, 1>
Owen Andersonac9de032009-08-10 22:56:29 +00002895static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002896 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2898 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002899
Nate Begeman543d2142009-04-27 18:41:29 +00002900 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2901 int BitI = Mask[i];
2902 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903 if (!isUndefOrEqual(BitI, j))
2904 return false;
2905 if (!isUndefOrEqual(BitI1, j))
2906 return false;
2907 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002908 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002909}
2910
Nate Begeman543d2142009-04-27 18:41:29 +00002911bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2912 SmallVector<int, 8> M;
2913 N->getMask(M);
2914 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2915}
2916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2918/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2919/// <2, 2, 3, 3>
Owen Andersonac9de032009-08-10 22:56:29 +00002920static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002921 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2923 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002924
Nate Begeman543d2142009-04-27 18:41:29 +00002925 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2926 int BitI = Mask[i];
2927 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 if (!isUndefOrEqual(BitI, j))
2929 return false;
2930 if (!isUndefOrEqual(BitI1, j))
2931 return false;
2932 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002933 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002934}
2935
Nate Begeman543d2142009-04-27 18:41:29 +00002936bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2937 SmallVector<int, 8> M;
2938 N->getMask(M);
2939 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2940}
2941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002942/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2943/// specifies a shuffle of elements that is suitable for input to MOVSS,
2944/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersonac9de032009-08-10 22:56:29 +00002945static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedmand49401f2009-06-06 06:05:10 +00002946 if (VT.getVectorElementType().getSizeInBits() < 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 return false;
Eli Friedmand49401f2009-06-06 06:05:10 +00002948
2949 int NumElts = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002950
Nate Begeman543d2142009-04-27 18:41:29 +00002951 if (!isUndefOrEqual(Mask[0], NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002953
Nate Begeman543d2142009-04-27 18:41:29 +00002954 for (int i = 1; i < NumElts; ++i)
2955 if (!isUndefOrEqual(Mask[i], i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 return true;
2959}
2960
Nate Begeman543d2142009-04-27 18:41:29 +00002961bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2962 SmallVector<int, 8> M;
2963 N->getMask(M);
2964 return ::isMOVLMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965}
2966
2967/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2968/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2969/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersonac9de032009-08-10 22:56:29 +00002970static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman543d2142009-04-27 18:41:29 +00002971 bool V2IsSplat = false, bool V2IsUndef = false) {
2972 int NumOps = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2974 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002975
Nate Begeman543d2142009-04-27 18:41:29 +00002976 if (!isUndefOrEqual(Mask[0], 0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002978
Nate Begeman543d2142009-04-27 18:41:29 +00002979 for (int i = 1; i < NumOps; ++i)
2980 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2981 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2982 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002983 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002984
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002985 return true;
2986}
2987
Nate Begeman543d2142009-04-27 18:41:29 +00002988static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 bool V2IsUndef = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002990 SmallVector<int, 8> M;
2991 N->getMask(M);
2992 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993}
2994
2995/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2996/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002997bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2998 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 return false;
3000
3001 // Expect 1, 1, 3, 3
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003002 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003003 int Elt = N->getMaskElt(i);
3004 if (Elt >= 0 && Elt != 1)
3005 return false;
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003006 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007
3008 bool HasHi = false;
3009 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003010 int Elt = N->getMaskElt(i);
3011 if (Elt >= 0 && Elt != 3)
3012 return false;
3013 if (Elt == 3)
3014 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003016 // Don't use movshdup if it can be done with a shufps.
Nate Begeman543d2142009-04-27 18:41:29 +00003017 // FIXME: verify that matching u, u, 3, 3 is what we want.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 return HasHi;
3019}
3020
3021/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3022/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00003023bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3024 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 return false;
3026
3027 // Expect 0, 0, 2, 2
Nate Begeman543d2142009-04-27 18:41:29 +00003028 for (unsigned i = 0; i < 2; ++i)
3029 if (N->getMaskElt(i) > 0)
3030 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031
3032 bool HasHi = false;
3033 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003034 int Elt = N->getMaskElt(i);
3035 if (Elt >= 0 && Elt != 2)
3036 return false;
3037 if (Elt == 2)
3038 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 }
Nate Begeman543d2142009-04-27 18:41:29 +00003040 // Don't use movsldup if it can be done with a shufps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041 return HasHi;
3042}
3043
Evan Chenga2497eb2008-09-25 20:50:48 +00003044/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3045/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00003046bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3047 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003048
Nate Begeman543d2142009-04-27 18:41:29 +00003049 for (int i = 0; i < e; ++i)
3050 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00003051 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00003052 for (int i = 0; i < e; ++i)
3053 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00003054 return false;
3055 return true;
3056}
3057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003058/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00003059/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00003061 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3062 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3065 unsigned Mask = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00003066 for (int i = 0; i < NumOperands; ++i) {
3067 int Val = SVOp->getMaskElt(NumOperands-i-1);
3068 if (Val < 0) Val = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003069 if (Val >= NumOperands) Val -= NumOperands;
3070 Mask |= Val;
3071 if (i != NumOperands - 1)
3072 Mask <<= Shift;
3073 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 return Mask;
3075}
3076
3077/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00003078/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00003080 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081 unsigned Mask = 0;
3082 // 8 nodes, but we only care about the last 4.
3083 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003084 int Val = SVOp->getMaskElt(i);
3085 if (Val >= 0)
Mon P Wang56d91642009-02-04 01:16:59 +00003086 Mask |= (Val - 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 if (i != 4)
3088 Mask <<= 2;
3089 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 return Mask;
3091}
3092
3093/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00003094/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00003096 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 unsigned Mask = 0;
3098 // 8 nodes, but we only care about the first 4.
3099 for (int i = 3; i >= 0; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003100 int Val = SVOp->getMaskElt(i);
3101 if (Val >= 0)
3102 Mask |= Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 if (i != 0)
3104 Mask <<= 2;
3105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106 return Mask;
3107}
3108
Nate Begeman080f8e22009-10-19 02:17:23 +00003109/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3110/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3111unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3112 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3113 EVT VVT = N->getValueType(0);
3114 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3115 int Val = 0;
3116
3117 unsigned i, e;
3118 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3119 Val = SVOp->getMaskElt(i);
3120 if (Val >= 0)
3121 break;
3122 }
3123 return (Val - i) * EltSize;
3124}
3125
Evan Chengb723fb52009-07-30 08:33:02 +00003126/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3127/// constant +0.0.
3128bool X86::isZeroNode(SDValue Elt) {
3129 return ((isa<ConstantSDNode>(Elt) &&
3130 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3131 (isa<ConstantFPSDNode>(Elt) &&
3132 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3133}
3134
Nate Begeman543d2142009-04-27 18:41:29 +00003135/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3136/// their permute mask.
3137static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3138 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003139 EVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003140 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00003141 SmallVector<int, 8> MaskVec;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003142
Nate Begemane8f61cb2009-04-29 05:20:52 +00003143 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003144 int idx = SVOp->getMaskElt(i);
3145 if (idx < 0)
3146 MaskVec.push_back(idx);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003147 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00003148 MaskVec.push_back(idx + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149 else
Nate Begeman543d2142009-04-27 18:41:29 +00003150 MaskVec.push_back(idx - NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 }
Nate Begeman543d2142009-04-27 18:41:29 +00003152 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3153 SVOp->getOperand(0), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154}
3155
Evan Chenga6769df2007-12-07 21:30:01 +00003156/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3157/// the two vector operands have swapped position.
Owen Andersonac9de032009-08-10 22:56:29 +00003158static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00003159 unsigned NumElems = VT.getVectorNumElements();
3160 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003161 int idx = Mask[i];
3162 if (idx < 0)
Evan Chengfca29242007-12-07 08:07:39 +00003163 continue;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003164 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00003165 Mask[i] = idx + NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00003166 else
Nate Begeman543d2142009-04-27 18:41:29 +00003167 Mask[i] = idx - NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00003168 }
Evan Chengfca29242007-12-07 08:07:39 +00003169}
3170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3172/// match movhlps. The lower half elements should come from upper half of
3173/// V1 (and in order), and the upper half elements should come from the upper
3174/// half of V2 (and in order).
Nate Begeman543d2142009-04-27 18:41:29 +00003175static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3176 if (Op->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 return false;
3178 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003179 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 return false;
3181 for (unsigned i = 2; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003182 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 return false;
3184 return true;
3185}
3186
3187/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00003188/// is promoted to a vector. It also returns the LoadSDNode by reference if
3189/// required.
3190static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00003191 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3192 return false;
3193 N = N->getOperand(0).getNode();
3194 if (!ISD::isNON_EXTLoad(N))
3195 return false;
3196 if (LD)
3197 *LD = cast<LoadSDNode>(N);
3198 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199}
3200
3201/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3202/// match movlp{s|d}. The lower half elements should come from lower half of
3203/// V1 (and in order), and the upper half elements should come from the upper
3204/// half of V2 (and in order). And since V1 will become the source of the
3205/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman543d2142009-04-27 18:41:29 +00003206static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3207 ShuffleVectorSDNode *Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
3209 return false;
3210 // Is V2 is a vector load, don't do this transformation. We will try to use
3211 // load folding shufps op.
3212 if (ISD::isNON_EXTLoad(V2))
3213 return false;
3214
Nate Begemane8f61cb2009-04-29 05:20:52 +00003215 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 if (NumElems != 2 && NumElems != 4)
3218 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003219 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003220 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003222 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003223 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003224 return false;
3225 return true;
3226}
3227
3228/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3229/// all the same.
3230static bool isSplatVector(SDNode *N) {
3231 if (N->getOpcode() != ISD::BUILD_VECTOR)
3232 return false;
3233
Dan Gohman8181bd12008-07-27 21:46:04 +00003234 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3236 if (N->getOperand(i) != SplatValue)
3237 return false;
3238 return true;
3239}
3240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003242/// to an zero vector.
Nate Begemane8f61cb2009-04-29 05:20:52 +00003243/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman543d2142009-04-27 18:41:29 +00003244static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003245 SDValue V1 = N->getOperand(0);
3246 SDValue V2 = N->getOperand(1);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003247 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3248 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003249 int Idx = N->getMaskElt(i);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003250 if (Idx >= (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00003251 unsigned Opc = V2.getOpcode();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003252 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3253 continue;
Evan Chengb723fb52009-07-30 08:33:02 +00003254 if (Opc != ISD::BUILD_VECTOR ||
3255 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman543d2142009-04-27 18:41:29 +00003256 return false;
3257 } else if (Idx >= 0) {
3258 unsigned Opc = V1.getOpcode();
3259 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3260 continue;
Evan Chengb723fb52009-07-30 08:33:02 +00003261 if (Opc != ISD::BUILD_VECTOR ||
3262 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003263 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 }
3265 }
3266 return true;
3267}
3268
3269/// getZeroVector - Returns a vector of specified type with all zero elements.
3270///
Owen Andersonac9de032009-08-10 22:56:29 +00003271static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003272 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003273 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00003274
Chris Lattnere6aa3862007-11-25 00:24:49 +00003275 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3276 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003277 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00003278 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003279 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3280 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003281 } else if (HasSSE2) { // SSE2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003282 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3283 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003284 } else { // SSE1
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003285 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3286 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003287 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003288 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289}
3290
Chris Lattnere6aa3862007-11-25 00:24:49 +00003291/// getOnesVector - Returns a vector of specified type with all bits set.
3292///
Owen Andersonac9de032009-08-10 22:56:29 +00003293static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003294 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00003295
Chris Lattnere6aa3862007-11-25 00:24:49 +00003296 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3297 // type. This ensures they get CSE'd.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003298 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00003300 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003302 else // SSE
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003303 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesence0805b2009-02-03 19:33:06 +00003304 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003305}
3306
3307
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3309/// that point to V2 points to its first element.
Nate Begeman543d2142009-04-27 18:41:29 +00003310static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003311 EVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003312 unsigned NumElems = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003313
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 bool Changed = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003315 SmallVector<int, 8> MaskVec;
3316 SVOp->getMask(MaskVec);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003317
Nate Begemane8f61cb2009-04-29 05:20:52 +00003318 for (unsigned i = 0; i != NumElems; ++i) {
3319 if (MaskVec[i] > (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00003320 MaskVec[i] = NumElems;
3321 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003323 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 if (Changed)
Nate Begeman543d2142009-04-27 18:41:29 +00003325 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3326 SVOp->getOperand(1), &MaskVec[0]);
3327 return SDValue(SVOp, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328}
3329
3330/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3331/// operation of specified width.
Owen Andersonac9de032009-08-10 22:56:29 +00003332static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003333 SDValue V2) {
3334 unsigned NumElems = VT.getVectorNumElements();
3335 SmallVector<int, 8> Mask;
3336 Mask.push_back(NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003338 Mask.push_back(i);
3339 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340}
3341
Nate Begeman543d2142009-04-27 18:41:29 +00003342/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersonac9de032009-08-10 22:56:29 +00003343static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003344 SDValue V2) {
3345 unsigned NumElems = VT.getVectorNumElements();
3346 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003347 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003348 Mask.push_back(i);
3349 Mask.push_back(i + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003350 }
Nate Begeman543d2142009-04-27 18:41:29 +00003351 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003352}
3353
Nate Begeman543d2142009-04-27 18:41:29 +00003354/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersonac9de032009-08-10 22:56:29 +00003355static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003356 SDValue V2) {
3357 unsigned NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003358 unsigned Half = NumElems/2;
Nate Begeman543d2142009-04-27 18:41:29 +00003359 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003361 Mask.push_back(i + Half);
3362 Mask.push_back(i + NumElems + Half);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363 }
Nate Begeman543d2142009-04-27 18:41:29 +00003364 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003365}
3366
Evan Chengbf8b2c52008-04-05 00:30:36 +00003367/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003368static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00003369 bool HasSSE2) {
3370 if (SV->getValueType(0).getVectorNumElements() <= 4)
3371 return SDValue(SV, 0);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003372
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003373 EVT PVT = MVT::v4f32;
Owen Andersonac9de032009-08-10 22:56:29 +00003374 EVT VT = SV->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00003375 DebugLoc dl = SV->getDebugLoc();
3376 SDValue V1 = SV->getOperand(0);
3377 int NumElems = VT.getVectorNumElements();
3378 int EltNo = SV->getSplatIndex();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003379
Nate Begeman543d2142009-04-27 18:41:29 +00003380 // unpack elements to the correct location
3381 while (NumElems > 4) {
3382 if (EltNo < NumElems/2) {
3383 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3384 } else {
3385 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3386 EltNo -= NumElems/2;
3387 }
3388 NumElems >>= 1;
3389 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003390
Nate Begeman543d2142009-04-27 18:41:29 +00003391 // Perform the splat.
3392 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesence0805b2009-02-03 19:33:06 +00003393 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman543d2142009-04-27 18:41:29 +00003394 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3395 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396}
3397
3398/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003399/// vector of zero or undef vector. This produces a shuffle where the low
3400/// element of V2 is swizzled into the zero/undef vector, landing at element
3401/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman8181bd12008-07-27 21:46:04 +00003402static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003403 bool isZero, bool HasSSE2,
3404 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003405 EVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003406 SDValue V1 = isZero
Nate Begeman543d2142009-04-27 18:41:29 +00003407 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3408 unsigned NumElems = VT.getVectorNumElements();
3409 SmallVector<int, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003410 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003411 // If this is the insertion idx, put the low elt of V2 here.
3412 MaskVec.push_back(i == Idx ? NumElems : i);
3413 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414}
3415
Evan Chengdea99362008-05-29 08:22:04 +00003416/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3417/// a shuffle that is zero.
3418static
Nate Begeman543d2142009-04-27 18:41:29 +00003419unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3420 bool Low, SelectionDAG &DAG) {
Evan Chengdea99362008-05-29 08:22:04 +00003421 unsigned NumZeros = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00003422 for (int i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003423 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman543d2142009-04-27 18:41:29 +00003424 int Idx = SVOp->getMaskElt(Index);
3425 if (Idx < 0) {
Evan Chengdea99362008-05-29 08:22:04 +00003426 ++NumZeros;
3427 continue;
3428 }
Nate Begeman543d2142009-04-27 18:41:29 +00003429 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Chengb723fb52009-07-30 08:33:02 +00003430 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003431 ++NumZeros;
3432 else
3433 break;
3434 }
3435 return NumZeros;
3436}
3437
3438/// isVectorShift - Returns true if the shuffle can be implemented as a
3439/// logical left or right shift of a vector.
Nate Begeman543d2142009-04-27 18:41:29 +00003440/// FIXME: split into pslldqi, psrldqi, palignr variants.
3441static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00003442 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman543d2142009-04-27 18:41:29 +00003443 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengdea99362008-05-29 08:22:04 +00003444
3445 isLeft = true;
Nate Begeman543d2142009-04-27 18:41:29 +00003446 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00003447 if (!NumZeros) {
3448 isLeft = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003449 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00003450 if (!NumZeros)
3451 return false;
3452 }
Evan Chengdea99362008-05-29 08:22:04 +00003453 bool SeenV1 = false;
3454 bool SeenV2 = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003455 for (int i = NumZeros; i < NumElems; ++i) {
3456 int Val = isLeft ? (i - NumZeros) : i;
3457 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3458 if (Idx < 0)
Evan Chengdea99362008-05-29 08:22:04 +00003459 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00003460 if (Idx < NumElems)
Evan Chengdea99362008-05-29 08:22:04 +00003461 SeenV1 = true;
3462 else {
Nate Begeman543d2142009-04-27 18:41:29 +00003463 Idx -= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00003464 SeenV2 = true;
3465 }
Nate Begeman543d2142009-04-27 18:41:29 +00003466 if (Idx != Val)
Evan Chengdea99362008-05-29 08:22:04 +00003467 return false;
3468 }
3469 if (SeenV1 && SeenV2)
3470 return false;
3471
Nate Begeman543d2142009-04-27 18:41:29 +00003472 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengdea99362008-05-29 08:22:04 +00003473 ShAmt = NumZeros;
3474 return true;
3475}
3476
3477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003478/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3479///
Dan Gohman8181bd12008-07-27 21:46:04 +00003480static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003481 unsigned NumNonZero, unsigned NumZero,
3482 SelectionDAG &DAG, TargetLowering &TLI) {
3483 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003484 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003486 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003487 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003488 bool First = true;
3489 for (unsigned i = 0; i < 16; ++i) {
3490 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3491 if (ThisIsNonZero && First) {
3492 if (NumZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003493 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003494 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003495 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003496 First = false;
3497 }
3498
3499 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003500 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003501 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3502 if (LastIsNonZero) {
Scott Michel91099d62009-02-17 22:15:04 +00003503 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003504 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003505 }
3506 if (ThisIsNonZero) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003507 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3508 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3509 ThisElt, DAG.getConstant(8, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510 if (LastIsNonZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003511 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512 } else
3513 ThisElt = LastElt;
3514
Gabor Greif1c80d112008-08-28 21:40:38 +00003515 if (ThisElt.getNode())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003516 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003517 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518 }
3519 }
3520
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003521 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522}
3523
3524/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3525///
Dan Gohman8181bd12008-07-27 21:46:04 +00003526static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527 unsigned NumNonZero, unsigned NumZero,
3528 SelectionDAG &DAG, TargetLowering &TLI) {
3529 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003530 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003531
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003532 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003533 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003534 bool First = true;
3535 for (unsigned i = 0; i < 8; ++i) {
3536 bool isNonZero = (NonZeros & (1 << i)) != 0;
3537 if (isNonZero) {
3538 if (First) {
3539 if (NumZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003540 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003542 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003543 First = false;
3544 }
Scott Michel91099d62009-02-17 22:15:04 +00003545 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003546 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003547 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548 }
3549 }
3550
3551 return V;
3552}
3553
Evan Chengdea99362008-05-29 08:22:04 +00003554/// getVShift - Return a vector logical shift node.
3555///
Owen Andersonac9de032009-08-10 22:56:29 +00003556static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman543d2142009-04-27 18:41:29 +00003557 unsigned NumBits, SelectionDAG &DAG,
3558 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003559 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003560 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003561 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003562 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3563 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3564 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003565 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003566}
3567
Dan Gohman8181bd12008-07-27 21:46:04 +00003568SDValue
Evan Chenge31a26a2009-12-09 21:00:30 +00003569X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3570 SelectionDAG &DAG) {
3571
3572 // Check if the scalar load can be widened into a vector load. And if
3573 // the address is "base + cst" see if the cst can be "absorbed" into
3574 // the shuffle mask.
3575 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3576 SDValue Ptr = LD->getBasePtr();
3577 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3578 return SDValue();
3579 EVT PVT = LD->getValueType(0);
3580 if (PVT != MVT::i32 && PVT != MVT::f32)
3581 return SDValue();
3582
3583 int FI = -1;
3584 int64_t Offset = 0;
3585 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3586 FI = FINode->getIndex();
3587 Offset = 0;
3588 } else if (Ptr.getOpcode() == ISD::ADD &&
3589 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3590 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3591 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3592 Offset = Ptr.getConstantOperandVal(1);
3593 Ptr = Ptr.getOperand(0);
3594 } else {
3595 return SDValue();
3596 }
3597
3598 SDValue Chain = LD->getChain();
3599 // Make sure the stack object alignment is at least 16.
3600 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3601 if (DAG.InferPtrAlignment(Ptr) < 16) {
3602 if (MFI->isFixedObjectIndex(FI)) {
Eric Christopherc21aa852010-01-23 06:02:43 +00003603 // Can't change the alignment. FIXME: It's possible to compute
3604 // the exact stack offset and reference FI + adjust offset instead.
3605 // If someone *really* cares about this. That's the way to implement it.
3606 return SDValue();
Evan Chenge31a26a2009-12-09 21:00:30 +00003607 } else {
3608 MFI->setObjectAlignment(FI, 16);
3609 }
3610 }
3611
3612 // (Offset % 16) must be multiple of 4. Then address is then
3613 // Ptr + (Offset & ~15).
3614 if (Offset < 0)
3615 return SDValue();
3616 if ((Offset % 16) & 3)
3617 return SDValue();
3618 int64_t StartOffset = Offset & ~15;
3619 if (StartOffset)
3620 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3621 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3622
3623 int EltNo = (Offset - StartOffset) >> 2;
3624 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3625 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene25160362010-02-15 16:53:33 +00003626 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3627 false, false, 0);
Evan Chenge31a26a2009-12-09 21:00:30 +00003628 // Canonicalize it to a v4i32 shuffle.
3629 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3630 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3631 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3632 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3633 }
3634
3635 return SDValue();
3636}
3637
Nate Begeman14d2ce62010-03-24 22:19:06 +00003638/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3639/// vector of type 'VT', see if the elements can be replaced by a single large
3640/// load which has the same value as a build_vector whose operands are 'elts'.
3641///
3642/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3643///
3644/// FIXME: we'd also like to handle the case where the last elements are zero
3645/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3646/// There's even a handy isZeroNode for that purpose.
Nate Begeman1aa900a2010-03-24 20:49:50 +00003647static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3648 DebugLoc &dl, SelectionDAG &DAG) {
3649 EVT EltVT = VT.getVectorElementType();
3650 unsigned NumElems = Elts.size();
3651
Nate Begeman1aa900a2010-03-24 20:49:50 +00003652 LoadSDNode *LDBase = NULL;
3653 unsigned LastLoadedElt = -1U;
Nate Begeman14d2ce62010-03-24 22:19:06 +00003654
3655 // For each element in the initializer, see if we've found a load or an undef.
3656 // If we don't find an initial load element, or later load elements are
3657 // non-consecutive, bail out.
Nate Begeman1aa900a2010-03-24 20:49:50 +00003658 for (unsigned i = 0; i < NumElems; ++i) {
3659 SDValue Elt = Elts[i];
3660
3661 if (!Elt.getNode() ||
3662 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3663 return SDValue();
3664 if (!LDBase) {
3665 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3666 return SDValue();
3667 LDBase = cast<LoadSDNode>(Elt.getNode());
3668 LastLoadedElt = i;
3669 continue;
3670 }
3671 if (Elt.getOpcode() == ISD::UNDEF)
3672 continue;
3673
3674 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3675 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3676 return SDValue();
3677 LastLoadedElt = i;
3678 }
Nate Begeman14d2ce62010-03-24 22:19:06 +00003679
3680 // If we have found an entire vector of loads and undefs, then return a large
3681 // load of the entire vector width starting at the base pointer. If we found
3682 // consecutive loads for the low half, generate a vzext_load node.
Nate Begeman1aa900a2010-03-24 20:49:50 +00003683 if (LastLoadedElt == NumElems - 1) {
3684 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3685 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3686 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3687 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3688 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3689 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3690 LDBase->isVolatile(), LDBase->isNonTemporal(),
3691 LDBase->getAlignment());
3692 } else if (NumElems == 4 && LastLoadedElt == 1) {
3693 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3694 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3695 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3696 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3697 }
3698 return SDValue();
3699}
3700
Evan Chenge31a26a2009-12-09 21:00:30 +00003701SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00003702X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003703 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003704 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003705 if (ISD::isBuildVectorAllZeros(Op.getNode())
3706 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003707 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3708 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3709 // eliminated on x86-32 hosts.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003710 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattnere6aa3862007-11-25 00:24:49 +00003711 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712
Gabor Greif1c80d112008-08-28 21:40:38 +00003713 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003714 return getOnesVector(Op.getValueType(), DAG, dl);
3715 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003716 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003717
Owen Andersonac9de032009-08-10 22:56:29 +00003718 EVT VT = Op.getValueType();
3719 EVT ExtVT = VT.getVectorElementType();
3720 unsigned EVTBits = ExtVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721
3722 unsigned NumElems = Op.getNumOperands();
3723 unsigned NumZero = 0;
3724 unsigned NumNonZero = 0;
3725 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003726 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003727 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003728 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003729 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003730 if (Elt.getOpcode() == ISD::UNDEF)
3731 continue;
3732 Values.insert(Elt);
3733 if (Elt.getOpcode() != ISD::Constant &&
3734 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003735 IsAllConstants = false;
Evan Chengb723fb52009-07-30 08:33:02 +00003736 if (X86::isZeroNode(Elt))
Evan Chengc1073492007-12-12 06:45:40 +00003737 NumZero++;
3738 else {
3739 NonZeros |= (1 << i);
3740 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003741 }
3742 }
3743
3744 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003745 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003746 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 }
3748
Chris Lattner66a4dda2008-03-09 05:42:06 +00003749 // Special case for single non-zero, non-undef, element.
Eli Friedmand49401f2009-06-06 06:05:10 +00003750 if (NumNonZero == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003752 SDValue Item = Op.getOperand(Idx);
Scott Michel91099d62009-02-17 22:15:04 +00003753
Chris Lattner2d91b962008-03-09 01:05:04 +00003754 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3755 // the value are obviously zero, truncate the value to i32 and do the
3756 // insertion that way. Only do this if the value is non-constant or if the
3757 // value is a constant being inserted into element 0. It is cheaper to do
3758 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003759 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner2d91b962008-03-09 01:05:04 +00003760 (!IsAllConstants || Idx == 0)) {
3761 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3762 // Handle MMX and SSE both.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003763 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3764 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michel91099d62009-02-17 22:15:04 +00003765
Chris Lattner2d91b962008-03-09 01:05:04 +00003766 // Truncate the value (which may itself be a constant) to i32, and
3767 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003768 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesence0805b2009-02-03 19:33:06 +00003769 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003770 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3771 Subtarget->hasSSE2(), DAG);
Scott Michel91099d62009-02-17 22:15:04 +00003772
Chris Lattner2d91b962008-03-09 01:05:04 +00003773 // Now we have our 32-bit value zero extended in the low element of
3774 // a vector. If Idx != 0, swizzle it into place.
3775 if (Idx != 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003776 SmallVector<int, 4> Mask;
3777 Mask.push_back(Idx);
3778 for (unsigned i = 1; i != VecElts; ++i)
3779 Mask.push_back(i);
3780 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003781 DAG.getUNDEF(Item.getValueType()),
Nate Begeman543d2142009-04-27 18:41:29 +00003782 &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003783 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003784 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003785 }
3786 }
Scott Michel91099d62009-02-17 22:15:04 +00003787
Chris Lattnerac914892008-03-08 22:59:52 +00003788 // If we have a constant or non-constant insertion into the low element of
3789 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3790 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedmand49401f2009-06-06 06:05:10 +00003791 // depending on what the source datatype is.
3792 if (Idx == 0) {
3793 if (NumZero == 0) {
3794 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003795 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3796 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedmand49401f2009-06-06 06:05:10 +00003797 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3798 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3799 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3800 DAG);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003801 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3802 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3803 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedmand49401f2009-06-06 06:05:10 +00003804 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3805 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3806 Subtarget->hasSSE2(), DAG);
3807 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3808 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003809 }
Evan Chengdea99362008-05-29 08:22:04 +00003810
3811 // Is it a vector logical left shift?
3812 if (NumElems == 2 && Idx == 1 &&
Evan Chengb723fb52009-07-30 08:33:02 +00003813 X86::isZeroNode(Op.getOperand(0)) &&
3814 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003815 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003816 return getVShift(true, VT,
Scott Michel91099d62009-02-17 22:15:04 +00003817 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00003818 VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003819 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003820 }
Scott Michel91099d62009-02-17 22:15:04 +00003821
Chris Lattner92bdcb52008-03-08 22:48:29 +00003822 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003823 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003824
Chris Lattnerac914892008-03-08 22:59:52 +00003825 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3826 // is a non-constant being inserted into an element other than the low one,
3827 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3828 // movd/movss) to move this into the low element, then shuffle it into
3829 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003830 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003831 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michel91099d62009-02-17 22:15:04 +00003832
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003833 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003834 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3835 Subtarget->hasSSE2(), DAG);
Nate Begeman543d2142009-04-27 18:41:29 +00003836 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman543d2142009-04-27 18:41:29 +00003838 MaskVec.push_back(i == Idx ? 0 : 1);
3839 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840 }
3841 }
3842
Chris Lattner66a4dda2008-03-09 05:42:06 +00003843 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chenge31a26a2009-12-09 21:00:30 +00003844 if (Values.size() == 1) {
3845 if (EVTBits == 32) {
3846 // Instead of a shuffle like this:
3847 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3848 // Check if it's possible to issue this instead.
3849 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3850 unsigned Idx = CountTrailingZeros_32(NonZeros);
3851 SDValue Item = Op.getOperand(Idx);
3852 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3853 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3854 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003855 return SDValue();
Evan Chenge31a26a2009-12-09 21:00:30 +00003856 }
Scott Michel91099d62009-02-17 22:15:04 +00003857
Dan Gohman21463242007-07-24 22:55:08 +00003858 // A vector full of immediates; various special cases are already
3859 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003860 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003861 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003862
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003864 if (EVTBits == 64) {
3865 if (NumNonZero == 1) {
3866 // One half is zero or undef.
3867 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003868 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003869 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003870 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3871 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003872 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003873 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003874 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875
3876 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3877 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003878 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003880 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003881 }
3882
3883 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003884 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003886 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 }
3888
3889 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003890 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 V.resize(NumElems);
3892 if (NumElems == 4 && NumZero > 0) {
3893 for (unsigned i = 0; i < 4; ++i) {
3894 bool isZero = !(NonZeros & (1 << i));
3895 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003896 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003898 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 }
3900
3901 for (unsigned i = 0; i < 2; ++i) {
3902 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3903 default: break;
3904 case 0:
3905 V[i] = V[i*2]; // Must be a zero vector.
3906 break;
3907 case 1:
Nate Begeman543d2142009-04-27 18:41:29 +00003908 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909 break;
3910 case 2:
Nate Begeman543d2142009-04-27 18:41:29 +00003911 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003912 break;
3913 case 3:
Nate Begeman543d2142009-04-27 18:41:29 +00003914 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003915 break;
3916 }
3917 }
3918
Nate Begeman543d2142009-04-27 18:41:29 +00003919 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 bool Reverse = (NonZeros & 0x3) == 2;
3921 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003922 MaskVec.push_back(Reverse ? 1-i : i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3924 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003925 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3926 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927 }
3928
Nate Begeman1aa900a2010-03-24 20:49:50 +00003929 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3930 // Check for a build vector of consecutive loads.
3931 for (unsigned i = 0; i < NumElems; ++i)
3932 V[i] = Op.getOperand(i);
3933
3934 // Check for elements which are consecutive loads.
3935 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3936 if (LD.getNode())
3937 return LD;
3938
3939 // For SSE 4.1, use inserts into undef.
3940 if (getSubtarget()->hasSSE41()) {
Nate Begeman543d2142009-04-27 18:41:29 +00003941 V[0] = DAG.getUNDEF(VT);
3942 for (unsigned i = 0; i < NumElems; ++i)
3943 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3944 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3945 Op.getOperand(i), DAG.getIntPtrConstant(i));
3946 return V[0];
3947 }
Nate Begeman1aa900a2010-03-24 20:49:50 +00003948
3949 // Otherwise, expand into a number of unpckl*
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950 // e.g. for v4f32
3951 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3952 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3953 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003955 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 NumElems >>= 1;
3957 while (NumElems != 0) {
3958 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003959 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 NumElems >>= 1;
3961 }
3962 return V[0];
3963 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003964 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965}
3966
Mon P Wanga8ff0dd2010-01-24 00:05:03 +00003967SDValue
3968X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3969 // We support concatenate two MMX registers and place them in a MMX
3970 // register. This is better than doing a stack convert.
3971 DebugLoc dl = Op.getDebugLoc();
3972 EVT ResVT = Op.getValueType();
3973 assert(Op.getNumOperands() == 2);
3974 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3975 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3976 int Mask[2];
3977 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3978 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3979 InVec = Op.getOperand(1);
3980 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3981 unsigned NumElts = ResVT.getVectorNumElements();
3982 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3983 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3984 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3985 } else {
3986 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3987 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3988 Mask[0] = 0; Mask[1] = 2;
3989 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3990 }
3991 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3992}
3993
Nate Begeman2c87c422009-02-23 08:49:38 +00003994// v8i16 shuffles - Prefer shuffles in the following order:
3995// 1. [all] pshuflw, pshufhw, optional move
3996// 2. [ssse3] 1 x pshufb
3997// 3. [ssse3] 2 x pshufb + 1 x por
3998// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Chengfca29242007-12-07 08:07:39 +00003999static
Nate Begeman543d2142009-04-27 18:41:29 +00004000SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
4001 SelectionDAG &DAG, X86TargetLowering &TLI) {
4002 SDValue V1 = SVOp->getOperand(0);
4003 SDValue V2 = SVOp->getOperand(1);
4004 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00004005 SmallVector<int, 8> MaskVals;
Evan Cheng75184a92007-12-11 01:46:18 +00004006
Nate Begeman2c87c422009-02-23 08:49:38 +00004007 // Determine if more than 1 of the words in each of the low and high quadwords
4008 // of the result come from the same quadword of one of the two inputs. Undef
4009 // mask values count as coming from any quadword, for better codegen.
4010 SmallVector<unsigned, 4> LoQuad(4);
4011 SmallVector<unsigned, 4> HiQuad(4);
4012 BitVector InputQuads(4);
4013 for (unsigned i = 0; i < 8; ++i) {
4014 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman543d2142009-04-27 18:41:29 +00004015 int EltIdx = SVOp->getMaskElt(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00004016 MaskVals.push_back(EltIdx);
4017 if (EltIdx < 0) {
4018 ++Quad[0];
4019 ++Quad[1];
4020 ++Quad[2];
4021 ++Quad[3];
Evan Cheng75184a92007-12-11 01:46:18 +00004022 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00004023 }
4024 ++Quad[EltIdx / 4];
4025 InputQuads.set(EltIdx / 4);
Evan Cheng75184a92007-12-11 01:46:18 +00004026 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00004027
Nate Begeman2c87c422009-02-23 08:49:38 +00004028 int BestLoQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00004029 unsigned MaxQuad = 1;
4030 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00004031 if (LoQuad[i] > MaxQuad) {
4032 BestLoQuad = i;
4033 MaxQuad = LoQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00004034 }
Evan Chengfca29242007-12-07 08:07:39 +00004035 }
4036
Nate Begeman2c87c422009-02-23 08:49:38 +00004037 int BestHiQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00004038 MaxQuad = 1;
4039 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00004040 if (HiQuad[i] > MaxQuad) {
4041 BestHiQuad = i;
4042 MaxQuad = HiQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00004043 }
4044 }
4045
Nate Begeman2c87c422009-02-23 08:49:38 +00004046 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004047 // of the two input vectors, shuffle them into one input vector so only a
Nate Begeman2c87c422009-02-23 08:49:38 +00004048 // single pshufb instruction is necessary. If There are more than 2 input
4049 // quads, disable the next transformation since it does not help SSSE3.
4050 bool V1Used = InputQuads[0] || InputQuads[1];
4051 bool V2Used = InputQuads[2] || InputQuads[3];
4052 if (TLI.getSubtarget()->hasSSSE3()) {
4053 if (InputQuads.count() == 2 && V1Used && V2Used) {
4054 BestLoQuad = InputQuads.find_first();
4055 BestHiQuad = InputQuads.find_next(BestLoQuad);
4056 }
4057 if (InputQuads.count() > 2) {
4058 BestLoQuad = -1;
4059 BestHiQuad = -1;
4060 }
4061 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00004062
Nate Begeman2c87c422009-02-23 08:49:38 +00004063 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4064 // the shuffle mask. If a quad is scored as -1, that means that it contains
4065 // words from all 4 input quadwords.
4066 SDValue NewV;
4067 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004068 SmallVector<int, 8> MaskV;
4069 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4070 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004071 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004072 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4073 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4074 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00004075
Nate Begeman2c87c422009-02-23 08:49:38 +00004076 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4077 // source words for the shuffle, to aid later transformations.
4078 bool AllWordsInNewV = true;
Mon P Wangb1db1202009-03-11 06:35:11 +00004079 bool InOrder[2] = { true, true };
Evan Cheng75184a92007-12-11 01:46:18 +00004080 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00004081 int idx = MaskVals[i];
Mon P Wangb1db1202009-03-11 06:35:11 +00004082 if (idx != (int)i)
4083 InOrder[i/4] = false;
Nate Begeman2c87c422009-02-23 08:49:38 +00004084 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng75184a92007-12-11 01:46:18 +00004085 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00004086 AllWordsInNewV = false;
4087 break;
Evan Cheng75184a92007-12-11 01:46:18 +00004088 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00004089
Nate Begeman2c87c422009-02-23 08:49:38 +00004090 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4091 if (AllWordsInNewV) {
4092 for (int i = 0; i != 8; ++i) {
4093 int idx = MaskVals[i];
4094 if (idx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00004095 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004096 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begeman2c87c422009-02-23 08:49:38 +00004097 if ((idx != i) && idx < 4)
4098 pshufhw = false;
4099 if ((idx != i) && idx > 3)
4100 pshuflw = false;
Evan Cheng75184a92007-12-11 01:46:18 +00004101 }
Nate Begeman2c87c422009-02-23 08:49:38 +00004102 V1 = NewV;
4103 V2Used = false;
4104 BestLoQuad = 0;
4105 BestHiQuad = 1;
Evan Chengfca29242007-12-07 08:07:39 +00004106 }
Evan Cheng75184a92007-12-11 01:46:18 +00004107
Nate Begeman2c87c422009-02-23 08:49:38 +00004108 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4109 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wangb1db1202009-03-11 06:35:11 +00004110 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004111 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004112 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng75184a92007-12-11 01:46:18 +00004113 }
Evan Cheng75184a92007-12-11 01:46:18 +00004114 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004115
Nate Begeman2c87c422009-02-23 08:49:38 +00004116 // If we have SSSE3, and all words of the result are from 1 input vector,
4117 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4118 // is present, fall back to case 4.
4119 if (TLI.getSubtarget()->hasSSSE3()) {
4120 SmallVector<SDValue,16> pshufbMask;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004121
Nate Begeman2c87c422009-02-23 08:49:38 +00004122 // If we have elements from both input vectors, set the high bit of the
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004123 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begeman2c87c422009-02-23 08:49:38 +00004124 // mask, and elements that come from V1 in the V2 mask, so that the two
4125 // results can be OR'd together.
4126 bool TwoInputs = V1Used && V2Used;
4127 for (unsigned i = 0; i != 8; ++i) {
4128 int EltIdx = MaskVals[i] * 2;
4129 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004130 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4131 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004132 continue;
4133 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004134 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4135 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004136 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004137 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004138 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00004139 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004140 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004141 if (!TwoInputs)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004142 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004143
Nate Begeman2c87c422009-02-23 08:49:38 +00004144 // Calculate the shuffle mask for the second input, shuffle it, and
4145 // OR it with the first shuffled input.
4146 pshufbMask.clear();
4147 for (unsigned i = 0; i != 8; ++i) {
4148 int EltIdx = MaskVals[i] * 2;
4149 if (EltIdx < 16) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004150 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4151 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004152 continue;
4153 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004154 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4155 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004156 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004157 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004158 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00004159 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004160 MVT::v16i8, &pshufbMask[0], 16));
4161 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4162 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004163 }
4164
4165 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4166 // and update MaskVals with new element order.
4167 BitVector InOrder(8);
4168 if (BestLoQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004169 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00004170 for (int i = 0; i != 4; ++i) {
4171 int idx = MaskVals[i];
4172 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004173 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004174 InOrder.set(i);
4175 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00004176 MaskV.push_back(idx & 3);
Nate Begeman2c87c422009-02-23 08:49:38 +00004177 InOrder.set(i);
4178 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004179 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004180 }
4181 }
4182 for (unsigned i = 4; i != 8; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00004183 MaskV.push_back(i);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004184 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman543d2142009-04-27 18:41:29 +00004185 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00004186 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004187
Nate Begeman2c87c422009-02-23 08:49:38 +00004188 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4189 // and update MaskVals with the new element order.
4190 if (BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004191 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00004192 for (unsigned i = 0; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00004193 MaskV.push_back(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00004194 for (unsigned i = 4; i != 8; ++i) {
4195 int idx = MaskVals[i];
4196 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004197 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004198 InOrder.set(i);
4199 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00004200 MaskV.push_back((idx & 3) + 4);
Nate Begeman2c87c422009-02-23 08:49:38 +00004201 InOrder.set(i);
4202 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004203 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004204 }
4205 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004206 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman543d2142009-04-27 18:41:29 +00004207 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00004208 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004209
Nate Begeman2c87c422009-02-23 08:49:38 +00004210 // In case BestHi & BestLo were both -1, which means each quadword has a word
4211 // from each of the four input quadwords, calculate the InOrder bitvector now
4212 // before falling through to the insert/extract cleanup.
4213 if (BestLoQuad == -1 && BestHiQuad == -1) {
4214 NewV = V1;
4215 for (int i = 0; i != 8; ++i)
4216 if (MaskVals[i] < 0 || MaskVals[i] == i)
4217 InOrder.set(i);
4218 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004219
Nate Begeman2c87c422009-02-23 08:49:38 +00004220 // The other elements are put in the right place using pextrw and pinsrw.
4221 for (unsigned i = 0; i != 8; ++i) {
4222 if (InOrder[i])
4223 continue;
4224 int EltIdx = MaskVals[i];
4225 if (EltIdx < 0)
4226 continue;
4227 SDValue ExtOp = (EltIdx < 8)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004228 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begeman2c87c422009-02-23 08:49:38 +00004229 DAG.getIntPtrConstant(EltIdx))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004230 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begeman2c87c422009-02-23 08:49:38 +00004231 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004232 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begeman2c87c422009-02-23 08:49:38 +00004233 DAG.getIntPtrConstant(i));
4234 }
4235 return NewV;
4236}
4237
4238// v16i8 shuffles - Prefer shuffles in the following order:
4239// 1. [ssse3] 1 x pshufb
4240// 2. [ssse3] 2 x pshufb + 1 x por
4241// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4242static
Nate Begeman543d2142009-04-27 18:41:29 +00004243SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4244 SelectionDAG &DAG, X86TargetLowering &TLI) {
4245 SDValue V1 = SVOp->getOperand(0);
4246 SDValue V2 = SVOp->getOperand(1);
4247 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00004248 SmallVector<int, 16> MaskVals;
Nate Begeman543d2142009-04-27 18:41:29 +00004249 SVOp->getMask(MaskVals);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004250
Nate Begeman2c87c422009-02-23 08:49:38 +00004251 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004252 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begeman2c87c422009-02-23 08:49:38 +00004253 // present, fall back to case 3.
4254 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4255 bool V1Only = true;
4256 bool V2Only = true;
4257 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00004258 int EltIdx = MaskVals[i];
Nate Begeman2c87c422009-02-23 08:49:38 +00004259 if (EltIdx < 0)
4260 continue;
4261 if (EltIdx < 16)
4262 V2Only = false;
4263 else
4264 V1Only = false;
4265 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004266
Nate Begeman2c87c422009-02-23 08:49:38 +00004267 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4268 if (TLI.getSubtarget()->hasSSSE3()) {
4269 SmallVector<SDValue,16> pshufbMask;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004270
Nate Begeman2c87c422009-02-23 08:49:38 +00004271 // If all result elements are from one input vector, then only translate
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004272 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begeman2c87c422009-02-23 08:49:38 +00004273 //
4274 // Otherwise, we have elements from both input vectors, and must zero out
4275 // elements that come from V2 in the first mask, and V1 in the second mask
4276 // so that we can OR them together.
4277 bool TwoInputs = !(V1Only || V2Only);
4278 for (unsigned i = 0; i != 16; ++i) {
4279 int EltIdx = MaskVals[i];
4280 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004281 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004282 continue;
4283 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004284 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004285 }
4286 // If all the elements are from V2, assign it to V1 and return after
4287 // building the first pshufb.
4288 if (V2Only)
4289 V1 = V2;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004290 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00004291 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004292 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004293 if (!TwoInputs)
4294 return V1;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004295
Nate Begeman2c87c422009-02-23 08:49:38 +00004296 // Calculate the shuffle mask for the second input, shuffle it, and
4297 // OR it with the first shuffled input.
4298 pshufbMask.clear();
4299 for (unsigned i = 0; i != 16; ++i) {
4300 int EltIdx = MaskVals[i];
4301 if (EltIdx < 16) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004302 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004303 continue;
4304 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004305 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004306 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004307 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00004308 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004309 MVT::v16i8, &pshufbMask[0], 16));
4310 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begeman2c87c422009-02-23 08:49:38 +00004311 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004312
Nate Begeman2c87c422009-02-23 08:49:38 +00004313 // No SSSE3 - Calculate in place words and then fix all out of place words
4314 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4315 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004316 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4317 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begeman2c87c422009-02-23 08:49:38 +00004318 SDValue NewV = V2Only ? V2 : V1;
4319 for (int i = 0; i != 8; ++i) {
4320 int Elt0 = MaskVals[i*2];
4321 int Elt1 = MaskVals[i*2+1];
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004322
Nate Begeman2c87c422009-02-23 08:49:38 +00004323 // This word of the result is all undef, skip it.
4324 if (Elt0 < 0 && Elt1 < 0)
4325 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004326
Nate Begeman2c87c422009-02-23 08:49:38 +00004327 // This word of the result is already in the correct place, skip it.
4328 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4329 continue;
4330 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4331 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004332
Nate Begeman2c87c422009-02-23 08:49:38 +00004333 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4334 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4335 SDValue InsElt;
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004336
4337 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4338 // using a single extract together, load it and store it.
4339 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004340 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004341 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004342 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004343 DAG.getIntPtrConstant(i));
4344 continue;
4345 }
4346
Nate Begeman2c87c422009-02-23 08:49:38 +00004347 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004348 // source byte is not also odd, shift the extracted word left 8 bits
4349 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begeman2c87c422009-02-23 08:49:38 +00004350 if (Elt1 >= 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004351 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begeman2c87c422009-02-23 08:49:38 +00004352 DAG.getIntPtrConstant(Elt1 / 2));
4353 if ((Elt1 & 1) == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004354 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begeman2c87c422009-02-23 08:49:38 +00004355 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004356 else if (Elt0 >= 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004357 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4358 DAG.getConstant(0xFF00, MVT::i16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004359 }
4360 // If Elt0 is defined, extract it from the appropriate source. If the
4361 // source byte is not also even, shift the extracted word right 8 bits. If
4362 // Elt1 was also defined, OR the extracted values together before
4363 // inserting them in the result.
4364 if (Elt0 >= 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004365 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begeman2c87c422009-02-23 08:49:38 +00004366 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4367 if ((Elt0 & 1) != 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004368 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begeman2c87c422009-02-23 08:49:38 +00004369 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004370 else if (Elt1 >= 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004371 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4372 DAG.getConstant(0x00FF, MVT::i16));
4373 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begeman2c87c422009-02-23 08:49:38 +00004374 : InsElt0;
4375 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004376 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begeman2c87c422009-02-23 08:49:38 +00004377 DAG.getIntPtrConstant(i));
4378 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004379 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00004380}
4381
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004382/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4383/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4384/// done when every pair / quad of shuffle mask elements point to elements in
4385/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00004386/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4387static
Nate Begeman543d2142009-04-27 18:41:29 +00004388SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4389 SelectionDAG &DAG,
4390 TargetLowering &TLI, DebugLoc dl) {
Owen Andersonac9de032009-08-10 22:56:29 +00004391 EVT VT = SVOp->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00004392 SDValue V1 = SVOp->getOperand(0);
4393 SDValue V2 = SVOp->getOperand(1);
4394 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004395 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004396 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersonac9de032009-08-10 22:56:29 +00004397 EVT MaskEltVT = MaskVT.getVectorElementType();
4398 EVT NewVT = MaskVT;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004399 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands92c43912008-06-06 12:08:01 +00004400 default: assert(false && "Unexpected!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004401 case MVT::v4f32: NewVT = MVT::v2f64; break;
4402 case MVT::v4i32: NewVT = MVT::v2i64; break;
4403 case MVT::v8i16: NewVT = MVT::v4i32; break;
4404 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004405 }
4406
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00004407 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00004408 if (VT.isInteger())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004409 NewVT = MVT::v2i64;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004410 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004411 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00004412 }
Nate Begeman543d2142009-04-27 18:41:29 +00004413 int Scale = NumElems / NewWidth;
4414 SmallVector<int, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00004415 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman543d2142009-04-27 18:41:29 +00004416 int StartIdx = -1;
4417 for (int j = 0; j < Scale; ++j) {
4418 int EltIdx = SVOp->getMaskElt(i+j);
4419 if (EltIdx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00004420 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00004421 if (StartIdx == -1)
Evan Cheng75184a92007-12-11 01:46:18 +00004422 StartIdx = EltIdx - (EltIdx % Scale);
4423 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00004424 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004425 }
Nate Begeman543d2142009-04-27 18:41:29 +00004426 if (StartIdx == -1)
4427 MaskVec.push_back(-1);
Evan Cheng75184a92007-12-11 01:46:18 +00004428 else
Nate Begeman543d2142009-04-27 18:41:29 +00004429 MaskVec.push_back(StartIdx / Scale);
Evan Chengfca29242007-12-07 08:07:39 +00004430 }
4431
Dale Johannesence0805b2009-02-03 19:33:06 +00004432 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4433 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman543d2142009-04-27 18:41:29 +00004434 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Chengfca29242007-12-07 08:07:39 +00004435}
4436
Evan Chenge9b9c672008-05-09 21:53:03 +00004437/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00004438///
Owen Andersonac9de032009-08-10 22:56:29 +00004439static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman543d2142009-04-27 18:41:29 +00004440 SDValue SrcOp, SelectionDAG &DAG,
4441 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004442 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004443 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00004444 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00004445 LD = dyn_cast<LoadSDNode>(SrcOp);
4446 if (!LD) {
4447 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4448 // instead.
Owen Anderson2dd68a22009-08-11 21:59:30 +00004449 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4450 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00004451 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4452 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson2dd68a22009-08-11 21:59:30 +00004453 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004454 // PR2108
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004455 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00004456 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4457 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4458 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4459 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00004460 SrcOp.getOperand(0)
4461 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00004462 }
4463 }
4464 }
4465
Dale Johannesence0805b2009-02-03 19:33:06 +00004466 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4467 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michel91099d62009-02-17 22:15:04 +00004468 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00004469 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00004470}
4471
Evan Chengf50554e2008-07-22 21:13:36 +00004472/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4473/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00004474static SDValue
Nate Begeman543d2142009-04-27 18:41:29 +00004475LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4476 SDValue V1 = SVOp->getOperand(0);
4477 SDValue V2 = SVOp->getOperand(1);
4478 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00004479 EVT VT = SVOp->getValueType(0);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004480
Evan Chengf50554e2008-07-22 21:13:36 +00004481 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00004482 Locs.resize(4);
Nate Begeman543d2142009-04-27 18:41:29 +00004483 SmallVector<int, 8> Mask1(4U, -1);
4484 SmallVector<int, 8> PermMask;
4485 SVOp->getMask(PermMask);
4486
Evan Chengf50554e2008-07-22 21:13:36 +00004487 unsigned NumHi = 0;
4488 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00004489 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00004490 int Idx = PermMask[i];
4491 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00004492 Locs[i] = std::make_pair(-1, -1);
4493 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004494 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4495 if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004496 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman543d2142009-04-27 18:41:29 +00004497 Mask1[NumLo] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004498 NumLo++;
4499 } else {
4500 Locs[i] = std::make_pair(1, NumHi);
4501 if (2+NumHi < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00004502 Mask1[2+NumHi] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004503 NumHi++;
4504 }
4505 }
4506 }
Evan Cheng3cae0332008-07-23 00:22:17 +00004507
Evan Chengf50554e2008-07-22 21:13:36 +00004508 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00004509 // If no more than two elements come from either vector. This can be
4510 // implemented with two shuffles. First shuffle gather the elements.
4511 // The second shuffle, which takes the first shuffle as both of its
4512 // vector operands, put the elements into the right order.
Nate Begeman543d2142009-04-27 18:41:29 +00004513 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004514
Nate Begeman543d2142009-04-27 18:41:29 +00004515 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004516
Evan Chengf50554e2008-07-22 21:13:36 +00004517 for (unsigned i = 0; i != 4; ++i) {
4518 if (Locs[i].first == -1)
4519 continue;
4520 else {
4521 unsigned Idx = (i < 2) ? 0 : 4;
4522 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00004523 Mask2[i] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004524 }
4525 }
4526
Nate Begeman543d2142009-04-27 18:41:29 +00004527 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004528 } else if (NumLo == 3 || NumHi == 3) {
4529 // Otherwise, we must have three elements from one vector, call it X, and
4530 // one element from the other, call it Y. First, use a shufps to build an
4531 // intermediate vector with the one element from Y and the element from X
4532 // that will be in the same half in the final destination (the indexes don't
4533 // matter). Then, use a shufps to build the final vector, taking the half
4534 // containing the element from Y from the intermediate, and the other half
4535 // from X.
4536 if (NumHi == 3) {
4537 // Normalize it so the 3 elements come from V1.
Nate Begeman543d2142009-04-27 18:41:29 +00004538 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng3cae0332008-07-23 00:22:17 +00004539 std::swap(V1, V2);
4540 }
4541
4542 // Find the element from V2.
4543 unsigned HiIndex;
4544 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman543d2142009-04-27 18:41:29 +00004545 int Val = PermMask[HiIndex];
4546 if (Val < 0)
Evan Cheng3cae0332008-07-23 00:22:17 +00004547 continue;
Evan Cheng3cae0332008-07-23 00:22:17 +00004548 if (Val >= 4)
4549 break;
4550 }
4551
Nate Begeman543d2142009-04-27 18:41:29 +00004552 Mask1[0] = PermMask[HiIndex];
4553 Mask1[1] = -1;
4554 Mask1[2] = PermMask[HiIndex^1];
4555 Mask1[3] = -1;
4556 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004557
4558 if (HiIndex >= 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00004559 Mask1[0] = PermMask[0];
4560 Mask1[1] = PermMask[1];
4561 Mask1[2] = HiIndex & 1 ? 6 : 4;
4562 Mask1[3] = HiIndex & 1 ? 4 : 6;
4563 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004564 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004565 Mask1[0] = HiIndex & 1 ? 2 : 0;
4566 Mask1[1] = HiIndex & 1 ? 0 : 2;
4567 Mask1[2] = PermMask[2];
4568 Mask1[3] = PermMask[3];
4569 if (Mask1[2] >= 0)
4570 Mask1[2] += 4;
4571 if (Mask1[3] >= 0)
4572 Mask1[3] += 4;
4573 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004574 }
Evan Chengf50554e2008-07-22 21:13:36 +00004575 }
4576
4577 // Break it into (shuffle shuffle_hi, shuffle_lo).
4578 Locs.clear();
Nate Begeman543d2142009-04-27 18:41:29 +00004579 SmallVector<int,8> LoMask(4U, -1);
4580 SmallVector<int,8> HiMask(4U, -1);
4581
4582 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00004583 unsigned MaskIdx = 0;
4584 unsigned LoIdx = 0;
4585 unsigned HiIdx = 2;
4586 for (unsigned i = 0; i != 4; ++i) {
4587 if (i == 2) {
4588 MaskPtr = &HiMask;
4589 MaskIdx = 1;
4590 LoIdx = 0;
4591 HiIdx = 2;
4592 }
Nate Begeman543d2142009-04-27 18:41:29 +00004593 int Idx = PermMask[i];
4594 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00004595 Locs[i] = std::make_pair(-1, -1);
Nate Begeman543d2142009-04-27 18:41:29 +00004596 } else if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004597 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00004598 (*MaskPtr)[LoIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004599 LoIdx++;
4600 } else {
4601 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00004602 (*MaskPtr)[HiIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004603 HiIdx++;
4604 }
4605 }
4606
Nate Begeman543d2142009-04-27 18:41:29 +00004607 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4608 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4609 SmallVector<int, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004610 for (unsigned i = 0; i != 4; ++i) {
4611 if (Locs[i].first == -1) {
Nate Begeman543d2142009-04-27 18:41:29 +00004612 MaskOps.push_back(-1);
Evan Chengf50554e2008-07-22 21:13:36 +00004613 } else {
4614 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00004615 MaskOps.push_back(Idx);
Evan Chengf50554e2008-07-22 21:13:36 +00004616 }
4617 }
Nate Begeman543d2142009-04-27 18:41:29 +00004618 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengf50554e2008-07-22 21:13:36 +00004619}
4620
Dan Gohman8181bd12008-07-27 21:46:04 +00004621SDValue
4622X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman543d2142009-04-27 18:41:29 +00004623 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004624 SDValue V1 = Op.getOperand(0);
4625 SDValue V2 = Op.getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00004626 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004627 DebugLoc dl = Op.getDebugLoc();
Nate Begeman543d2142009-04-27 18:41:29 +00004628 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands92c43912008-06-06 12:08:01 +00004629 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4631 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4632 bool V1IsSplat = false;
4633 bool V2IsSplat = false;
4634
Nate Begeman543d2142009-04-27 18:41:29 +00004635 if (isZeroShuffle(SVOp))
Dale Johannesence0805b2009-02-03 19:33:06 +00004636 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637
Nate Begeman543d2142009-04-27 18:41:29 +00004638 // Promote splats to v4f32.
4639 if (SVOp->isSplat()) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004640 if (isMMX || NumElems < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00004641 return Op;
4642 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 }
4644
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004645 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4646 // do it!
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004647 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004648 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004649 if (NewOp.getNode())
Scott Michel91099d62009-02-17 22:15:04 +00004650 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesence0805b2009-02-03 19:33:06 +00004651 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004652 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004653 // FIXME: Figure out a cleaner way to do this.
4654 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004655 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004656 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004657 if (NewOp.getNode()) {
Nate Begeman543d2142009-04-27 18:41:29 +00004658 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4659 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4660 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004661 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004662 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004663 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4664 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chenge9b9c672008-05-09 21:53:03 +00004665 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman543d2142009-04-27 18:41:29 +00004666 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004667 }
4668 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004669
Nate Begeman543d2142009-04-27 18:41:29 +00004670 if (X86::isPSHUFDMask(SVOp))
4671 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004672
Evan Chengdea99362008-05-29 08:22:04 +00004673 // Check if this can be converted into a logical shift.
4674 bool isLeft = false;
4675 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004676 SDValue ShVal;
Nate Begeman543d2142009-04-27 18:41:29 +00004677 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chenge31a26a2009-12-09 21:00:30 +00004678 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengdea99362008-05-29 08:22:04 +00004679 if (isShift && ShVal.hasOneUse()) {
Scott Michel91099d62009-02-17 22:15:04 +00004680 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengdea99362008-05-29 08:22:04 +00004681 // v_set0 + movlhps or movhlps, etc.
Dan Gohman3bab1f72009-09-23 21:02:20 +00004682 EVT EltVT = VT.getVectorElementType();
4683 ShAmt *= EltVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004684 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004685 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004686
Nate Begeman543d2142009-04-27 18:41:29 +00004687 if (X86::isMOVLMask(SVOp)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004688 if (V1IsUndef)
4689 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004690 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004691 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004692 if (!isMMX)
4693 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004694 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004695
Nate Begeman543d2142009-04-27 18:41:29 +00004696 // FIXME: fold these into legal mask.
4697 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4698 X86::isMOVSLDUPMask(SVOp) ||
4699 X86::isMOVHLPSMask(SVOp) ||
Nate Begemanb13034d2009-11-07 23:17:15 +00004700 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman543d2142009-04-27 18:41:29 +00004701 X86::isMOVLPMask(SVOp)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702 return Op;
4703
Nate Begeman543d2142009-04-27 18:41:29 +00004704 if (ShouldXformToMOVHLPS(SVOp) ||
4705 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4706 return CommuteVectorShuffle(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004707
Evan Chengdea99362008-05-29 08:22:04 +00004708 if (isShift) {
4709 // No better options. Use a vshl / vsrl.
Dan Gohman3bab1f72009-09-23 21:02:20 +00004710 EVT EltVT = VT.getVectorElementType();
4711 ShAmt *= EltVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004712 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004713 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004714
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004716 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4717 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004718 V1IsSplat = isSplatVector(V1.getNode());
4719 V2IsSplat = isSplatVector(V2.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00004720
Chris Lattnere6aa3862007-11-25 00:24:49 +00004721 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman543d2142009-04-27 18:41:29 +00004723 Op = CommuteVectorShuffle(SVOp, DAG);
4724 SVOp = cast<ShuffleVectorSDNode>(Op);
4725 V1 = SVOp->getOperand(0);
4726 V2 = SVOp->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004727 std::swap(V1IsSplat, V2IsSplat);
4728 std::swap(V1IsUndef, V2IsUndef);
4729 Commuted = true;
4730 }
4731
Nate Begeman543d2142009-04-27 18:41:29 +00004732 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4733 // Shuffling low element of v1 into undef, just return v1.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004734 if (V2IsUndef)
Nate Begeman543d2142009-04-27 18:41:29 +00004735 return V1;
4736 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4737 // the instruction selector will not match, so get a canonical MOVL with
4738 // swapped operands to undo the commute.
4739 return getMOVL(DAG, dl, VT, V2, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004740 }
4741
Nate Begeman543d2142009-04-27 18:41:29 +00004742 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4743 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4744 X86::isUNPCKLMask(SVOp) ||
4745 X86::isUNPCKHMask(SVOp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746 return Op;
4747
4748 if (V2IsSplat) {
4749 // Normalize mask so all entries that point to V2 points to its first
4750 // element then try to match unpck{h|l} again. If match, return a
4751 // new vector_shuffle with the corrected mask.
Nate Begeman543d2142009-04-27 18:41:29 +00004752 SDValue NewMask = NormalizeMask(SVOp, DAG);
4753 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4754 if (NSVOp != SVOp) {
4755 if (X86::isUNPCKLMask(NSVOp, true)) {
4756 return NewMask;
4757 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4758 return NewMask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 }
4760 }
4761 }
4762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763 if (Commuted) {
4764 // Commute is back and try unpck* again.
Nate Begeman543d2142009-04-27 18:41:29 +00004765 // FIXME: this seems wrong.
4766 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4767 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4768 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4769 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4770 X86::isUNPCKLMask(NewSVOp) ||
4771 X86::isUNPCKHMask(NewSVOp))
4772 return NewOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 }
4774
Nate Begeman2c87c422009-02-23 08:49:38 +00004775 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman543d2142009-04-27 18:41:29 +00004776
4777 // Normalize the node to match x86 shuffle ops if needed
4778 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4779 return CommuteVectorShuffle(SVOp, DAG);
4780
4781 // Check for legal shuffle and return?
4782 SmallVector<int, 16> PermMask;
4783 SVOp->getMask(PermMask);
4784 if (isShuffleMaskLegal(PermMask, VT))
Evan Chengbf8b2c52008-04-05 00:30:36 +00004785 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004786
Evan Cheng75184a92007-12-11 01:46:18 +00004787 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004788 if (VT == MVT::v8i16) {
Nate Begeman543d2142009-04-27 18:41:29 +00004789 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004790 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004791 return NewOp;
4792 }
4793
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004794 if (VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004795 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begeman2c87c422009-02-23 08:49:38 +00004796 if (NewOp.getNode())
4797 return NewOp;
4798 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004799
Evan Chengf50554e2008-07-22 21:13:36 +00004800 // Handle all 4 wide cases with a number of shuffles except for MMX.
4801 if (NumElems == 4 && !isMMX)
Nate Begeman543d2142009-04-27 18:41:29 +00004802 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803
Dan Gohman8181bd12008-07-27 21:46:04 +00004804 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004805}
4806
Dan Gohman8181bd12008-07-27 21:46:04 +00004807SDValue
4808X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004809 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00004810 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004811 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004812 if (VT.getSizeInBits() == 8) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004813 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004814 Op.getOperand(0), Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004815 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004816 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004817 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004818 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004819 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4820 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4821 if (Idx == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004822 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4823 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004824 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004825 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004826 Op.getOperand(0)),
4827 Op.getOperand(1)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004828 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004829 Op.getOperand(0), Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004830 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004831 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004832 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004833 } else if (VT == MVT::f32) {
Evan Cheng6c249332008-03-24 21:52:23 +00004834 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4835 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004836 // result has a single use which is a store or a bitcast to i32. And in
4837 // the case of a store, it's not worth it if the index is a constant 0,
4838 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004839 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004840 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004841 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004842 if ((User->getOpcode() != ISD::STORE ||
4843 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4844 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004845 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004846 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004847 return SDValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004848 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4849 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004850 Op.getOperand(0)),
4851 Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004852 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4853 } else if (VT == MVT::i32) {
Mon P Wangac2a3c52009-01-15 21:10:20 +00004854 // ExtractPS works with constant index.
4855 if (isa<ConstantSDNode>(Op.getOperand(1)))
4856 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004857 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004858 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004859}
4860
4861
Dan Gohman8181bd12008-07-27 21:46:04 +00004862SDValue
4863X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004865 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866
Evan Cheng6c249332008-03-24 21:52:23 +00004867 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004868 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004869 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004870 return Res;
4871 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004872
Owen Andersonac9de032009-08-10 22:56:29 +00004873 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004874 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004876 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004877 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004878 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004879 if (Idx == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004880 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4881 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michel91099d62009-02-17 22:15:04 +00004882 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004883 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004884 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004885 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck5d3fa642009-12-17 15:31:52 +00004886 EVT EltVT = MVT::i32;
Dan Gohman3bab1f72009-09-23 21:02:20 +00004887 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004888 Op.getOperand(0), Op.getOperand(1));
Dan Gohman3bab1f72009-09-23 21:02:20 +00004889 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004891 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004892 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004893 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 if (Idx == 0)
4895 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004896
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004897 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman543d2142009-04-27 18:41:29 +00004898 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersonac9de032009-08-10 22:56:29 +00004899 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004900 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman543d2142009-04-27 18:41:29 +00004901 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004902 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004903 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004904 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004905 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4906 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4907 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004908 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909 if (Idx == 0)
4910 return Op;
4911
4912 // UNPCKHPD the element to the lowest double word, then movsd.
4913 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4914 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman543d2142009-04-27 18:41:29 +00004915 int Mask[2] = { 1, -1 };
Owen Andersonac9de032009-08-10 22:56:29 +00004916 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004917 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman543d2142009-04-27 18:41:29 +00004918 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004919 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004920 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004921 }
4922
Dan Gohman8181bd12008-07-27 21:46:04 +00004923 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924}
4925
Dan Gohman8181bd12008-07-27 21:46:04 +00004926SDValue
4927X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersonac9de032009-08-10 22:56:29 +00004928 EVT VT = Op.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00004929 EVT EltVT = VT.getVectorElementType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004930 DebugLoc dl = Op.getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004931
Dan Gohman8181bd12008-07-27 21:46:04 +00004932 SDValue N0 = Op.getOperand(0);
4933 SDValue N1 = Op.getOperand(1);
4934 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004935
Dan Gohman3bab1f72009-09-23 21:02:20 +00004936 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohman5a7af042008-08-14 22:53:18 +00004937 isa<ConstantSDNode>(N2)) {
Chris Lattner5fc65c52010-02-23 02:07:48 +00004938 unsigned Opc;
4939 if (VT == MVT::v8i16)
4940 Opc = X86ISD::PINSRW;
4941 else if (VT == MVT::v4i16)
4942 Opc = X86ISD::MMX_PINSRW;
4943 else if (VT == MVT::v16i8)
4944 Opc = X86ISD::PINSRB;
4945 else
4946 Opc = X86ISD::PINSRB;
4947
Nate Begemand77e59e2008-02-11 04:19:36 +00004948 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4949 // argument.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004950 if (N1.getValueType() != MVT::i32)
4951 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4952 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004953 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004954 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman3bab1f72009-09-23 21:02:20 +00004955 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004956 // Bits [7:6] of the constant are the source select. This will always be
4957 // zero here. The DAG Combiner may combine an extract_elt index into these
4958 // bits. For example (insert (extract, 3), 2) could be matched by putting
4959 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michel91099d62009-02-17 22:15:04 +00004960 // Bits [5:4] of the constant are the destination select. This is the
Nate Begemand77e59e2008-02-11 04:19:36 +00004961 // value of the incoming immediate.
Scott Michel91099d62009-02-17 22:15:04 +00004962 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begemand77e59e2008-02-11 04:19:36 +00004963 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004964 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherefb657e2009-07-24 00:33:09 +00004965 // Create this as a scalar to vector..
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004966 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004967 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman3bab1f72009-09-23 21:02:20 +00004968 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherefb657e2009-07-24 00:33:09 +00004969 // PINSR* works with constant index.
4970 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004971 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004972 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004973}
4974
Dan Gohman8181bd12008-07-27 21:46:04 +00004975SDValue
4976X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00004977 EVT VT = Op.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00004978 EVT EltVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004979
4980 if (Subtarget->hasSSE41())
4981 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4982
Dan Gohman3bab1f72009-09-23 21:02:20 +00004983 if (EltVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004984 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004985
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004986 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004987 SDValue N0 = Op.getOperand(0);
4988 SDValue N1 = Op.getOperand(1);
4989 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004990
Dan Gohman3bab1f72009-09-23 21:02:20 +00004991 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004992 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4993 // as its second argument.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004994 if (N1.getValueType() != MVT::i32)
4995 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4996 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004997 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner5fc65c52010-02-23 02:07:48 +00004998 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
4999 dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005001 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002}
5003
Dan Gohman8181bd12008-07-27 21:46:04 +00005004SDValue
5005X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005006 DebugLoc dl = Op.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005007 if (Op.getValueType() == MVT::v2f32)
5008 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5009 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5010 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00005011 Op.getOperand(0))));
5012
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005013 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5014 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindolafe2a3972009-08-03 02:45:34 +00005015
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005016 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5017 EVT VT = MVT::v2i32;
5018 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengd1045a62008-02-18 23:04:32 +00005019 default: break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005020 case MVT::v16i8:
5021 case MVT::v8i16:
5022 VT = MVT::v4i32;
Evan Chengd1045a62008-02-18 23:04:32 +00005023 break;
5024 }
Dale Johannesence0805b2009-02-03 19:33:06 +00005025 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5026 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027}
5028
Bill Wendlingfef06052008-09-16 21:48:12 +00005029// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5030// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5031// one of the above mentioned nodes. It has to be wrapped because otherwise
5032// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5033// be used to form addressing mode. These wrapped nodes will be selected
5034// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00005035SDValue
5036X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005038
Chris Lattner5062b3b2009-06-26 19:22:52 +00005039 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5040 // global base reg.
5041 unsigned char OpFlag = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005042 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005043 CodeModel::Model M = getTargetMachine().getCodeModel();
5044
Chris Lattner28d40c62009-07-11 20:29:19 +00005045 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005046 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00005047 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00005048 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00005049 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00005050 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00005051 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005052
Evan Cheng68c18682009-03-13 07:51:59 +00005053 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner5062b3b2009-06-26 19:22:52 +00005054 CP->getAlignment(),
5055 CP->getOffset(), OpFlag);
5056 DebugLoc DL = CP->getDebugLoc();
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005057 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058 // With PIC, the address is actually $g + Offset.
Chris Lattner5062b3b2009-06-26 19:22:52 +00005059 if (OpFlag) {
5060 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesen24dd9a52009-02-07 00:55:49 +00005061 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerd2c680b2010-04-02 20:16:16 +00005062 DebugLoc(), getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 Result);
5064 }
5065
5066 return Result;
5067}
5068
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005069SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
5070 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005071
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005072 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5073 // global base reg.
5074 unsigned char OpFlag = 0;
5075 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005076 CodeModel::Model M = getTargetMachine().getCodeModel();
5077
Chris Lattner28d40c62009-07-11 20:29:19 +00005078 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005079 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00005080 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00005081 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00005082 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00005083 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00005084 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005085
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005086 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5087 OpFlag);
5088 DebugLoc DL = JT->getDebugLoc();
5089 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005090
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005091 // With PIC, the address is actually $g + Offset.
5092 if (OpFlag) {
5093 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5094 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerd2c680b2010-04-02 20:16:16 +00005095 DebugLoc(), getPointerTy()),
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005096 Result);
5097 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005098
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005099 return Result;
5100}
5101
5102SDValue
5103X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
5104 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005105
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005106 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5107 // global base reg.
5108 unsigned char OpFlag = 0;
5109 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005110 CodeModel::Model M = getTargetMachine().getCodeModel();
5111
Chris Lattner28d40c62009-07-11 20:29:19 +00005112 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005113 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00005114 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00005115 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00005116 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00005117 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00005118 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005119
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005120 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005121
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005122 DebugLoc DL = Op.getDebugLoc();
5123 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005124
5125
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005126 // With PIC, the address is actually $g + Offset.
5127 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattneraa7c6d22009-07-09 03:15:51 +00005128 !Subtarget->is64Bit()) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005129 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5130 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerd2c680b2010-04-02 20:16:16 +00005131 DebugLoc(), getPointerTy()),
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005132 Result);
5133 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005134
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005135 return Result;
5136}
5137
Dan Gohman8181bd12008-07-27 21:46:04 +00005138SDValue
Dan Gohman064403e2009-10-30 01:28:02 +00005139X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman885793b2009-11-20 23:18:13 +00005140 // Create the TargetBlockAddressAddress node.
5141 unsigned char OpFlags =
5142 Subtarget->ClassifyBlockAddressReference();
Dan Gohman064403e2009-10-30 01:28:02 +00005143 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman885793b2009-11-20 23:18:13 +00005144 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5145 DebugLoc dl = Op.getDebugLoc();
5146 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5147 /*isTarget=*/true, OpFlags);
5148
Dan Gohman064403e2009-10-30 01:28:02 +00005149 if (Subtarget->isPICStyleRIPRel() &&
5150 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman885793b2009-11-20 23:18:13 +00005151 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5152 else
5153 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman064403e2009-10-30 01:28:02 +00005154
Dan Gohman885793b2009-11-20 23:18:13 +00005155 // With PIC, the address is actually $g + Offset.
5156 if (isGlobalRelativeToPICBase(OpFlags)) {
5157 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5158 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5159 Result);
5160 }
Dan Gohman064403e2009-10-30 01:28:02 +00005161
5162 return Result;
5163}
5164
5165SDValue
Dale Johannesenea996922009-02-04 20:06:27 +00005166X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman36322c72008-10-18 02:06:02 +00005167 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00005168 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00005169 // Create the TargetGlobalAddress node, folding in the constant
5170 // offset if it is legal.
Chris Lattner505aa6c2009-07-10 07:20:05 +00005171 unsigned char OpFlags =
5172 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005173 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman36322c72008-10-18 02:06:02 +00005174 SDValue Result;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005175 if (OpFlags == X86II::MO_NO_FLAG &&
5176 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner9ab4e662009-07-09 00:58:53 +00005177 // A direct static reference to a global.
Dale Johannesenf97110c2009-07-21 00:12:29 +00005178 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman36322c72008-10-18 02:06:02 +00005179 Offset = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005180 } else {
Chris Lattner5bdaa522009-06-27 05:39:56 +00005181 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005182 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005183
Chris Lattner28d40c62009-07-11 20:29:19 +00005184 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005185 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005186 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5187 else
5188 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00005189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190 // With PIC, the address is actually $g + Offset.
Chris Lattner054532c2009-07-10 07:34:39 +00005191 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesenea996922009-02-04 20:06:27 +00005192 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5193 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 Result);
5195 }
Scott Michel91099d62009-02-17 22:15:04 +00005196
Chris Lattner054532c2009-07-10 07:34:39 +00005197 // For globals that require a load from a stub to get the address, emit the
5198 // load.
5199 if (isGlobalStubReference(OpFlags))
Dale Johannesenea996922009-02-04 20:06:27 +00005200 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene25160362010-02-15 16:53:33 +00005201 PseudoSourceValue::getGOT(), 0, false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202
Dan Gohman36322c72008-10-18 02:06:02 +00005203 // If there was a non-zero offset that we didn't fold, create an explicit
5204 // addition for it.
5205 if (Offset != 0)
Dale Johannesenea996922009-02-04 20:06:27 +00005206 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman36322c72008-10-18 02:06:02 +00005207 DAG.getConstant(Offset, getPointerTy()));
5208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005209 return Result;
5210}
5211
Evan Cheng7f250d62008-09-24 00:05:32 +00005212SDValue
5213X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5214 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00005215 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005216 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00005217}
5218
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005219static SDValue
5220GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersonac9de032009-08-10 22:56:29 +00005221 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005222 unsigned char OperandFlags) {
Anton Korobeynikov7767af52009-12-11 19:39:55 +00005223 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005224 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005225 DebugLoc dl = GA->getDebugLoc();
5226 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5227 GA->getValueType(0),
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005228 GA->getOffset(),
5229 OperandFlags);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005230 if (InFlag) {
5231 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00005232 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005233 } else {
5234 SDValue Ops[] = { Chain, TGA };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00005235 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005236 }
Anton Korobeynikov7767af52009-12-11 19:39:55 +00005237
5238 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5239 MFI->setHasCalls(true);
5240
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00005241 SDValue Flag = Chain.getValue(1);
5242 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005243}
5244
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005245// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00005246static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005247LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005248 const EVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005249 SDValue InFlag;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005250 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5251 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005252 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerd2c680b2010-04-02 20:16:16 +00005253 DebugLoc(), PtrVT), InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005254 InFlag = Chain.getValue(1);
5255
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005256 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005257}
5258
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005259// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00005260static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005261LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005262 const EVT PtrVT) {
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005263 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5264 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005265}
5266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005267// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5268// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00005269static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005270 const EVT PtrVT, TLSModel::Model model,
Rafael Espindolab93a5122009-04-13 13:02:49 +00005271 bool is64Bit) {
Dale Johannesenea996922009-02-04 20:06:27 +00005272 DebugLoc dl = GA->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005273 // Get the Thread Pointer
Rafael Espindolabca99f72009-04-08 21:14:34 +00005274 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerd2c680b2010-04-02 20:16:16 +00005275 DebugLoc(), PtrVT,
Rafael Espindolab93a5122009-04-13 13:02:49 +00005276 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005277 MVT::i32));
Rafael Espindolabca99f72009-04-08 21:14:34 +00005278
5279 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene25160362010-02-15 16:53:33 +00005280 NULL, 0, false, false, 0);
Rafael Espindolabca99f72009-04-08 21:14:34 +00005281
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005282 unsigned char OperandFlags = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005283 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5284 // initialexec.
5285 unsigned WrapperKind = X86ISD::Wrapper;
5286 if (model == TLSModel::LocalExec) {
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005287 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005288 } else if (is64Bit) {
5289 assert(model == TLSModel::InitialExec);
5290 OperandFlags = X86II::MO_GOTTPOFF;
5291 WrapperKind = X86ISD::WrapperRIP;
5292 } else {
5293 assert(model == TLSModel::InitialExec);
5294 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005295 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5298 // exec)
Chris Lattner3207f8b2009-06-21 02:22:34 +00005299 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005300 GA->getOffset(), OperandFlags);
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005301 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302
Rafael Espindola7b620af2009-02-27 13:37:18 +00005303 if (model == TLSModel::InitialExec)
Dale Johannesenea996922009-02-04 20:06:27 +00005304 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene25160362010-02-15 16:53:33 +00005305 PseudoSourceValue::getGOT(), 0, false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306
5307 // The address of the thread local variable is the add of the thread
5308 // pointer with the offset of the variable.
Dale Johannesenea996922009-02-04 20:06:27 +00005309 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005310}
5311
Dan Gohman8181bd12008-07-27 21:46:04 +00005312SDValue
5313X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314 // TODO: implement the "local dynamic" model
5315 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005316 assert(Subtarget->isTargetELF() &&
5317 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005319 const GlobalValue *GV = GA->getGlobal();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005320
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005321 // If GV is an alias then use the aliasee for determining
5322 // thread-localness.
5323 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5324 GV = GA->resolveAliasedGlobal(false);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005325
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005326 TLSModel::Model model = getTLSModel(GV,
5327 getTargetMachine().getRelocationModel());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005328
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005329 switch (model) {
5330 case TLSModel::GeneralDynamic:
5331 case TLSModel::LocalDynamic: // not implemented
5332 if (Subtarget->is64Bit())
Rafael Espindola7b620af2009-02-27 13:37:18 +00005333 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005334 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005335
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005336 case TLSModel::InitialExec:
5337 case TLSModel::LocalExec:
5338 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5339 Subtarget->is64Bit());
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005340 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005341
Edwin Törökbd448e32009-07-14 16:55:14 +00005342 llvm_unreachable("Unreachable");
Chris Lattnerda028df2009-04-01 22:14:45 +00005343 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344}
5345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005346
Chris Lattner62814a32007-10-17 06:02:13 +00005347/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michel91099d62009-02-17 22:15:04 +00005348/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00005349SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00005350 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersonac9de032009-08-10 22:56:29 +00005351 EVT VT = Op.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00005352 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005353 DebugLoc dl = Op.getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00005354 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00005355 SDValue ShOpLo = Op.getOperand(0);
5356 SDValue ShOpHi = Op.getOperand(1);
5357 SDValue ShAmt = Op.getOperand(2);
Chris Lattner996d9e52009-07-29 05:48:09 +00005358 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005359 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner996d9e52009-07-29 05:48:09 +00005360 : DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361
Dan Gohman8181bd12008-07-27 21:46:04 +00005362 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00005363 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005364 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5365 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00005366 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005367 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5368 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00005369 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005370
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005371 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5372 DAG.getConstant(VTBits, MVT::i8));
Chris Lattner44977012010-02-22 00:28:59 +00005373 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005374 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375
Dan Gohman8181bd12008-07-27 21:46:04 +00005376 SDValue Hi, Lo;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005377 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5379 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00005380
Chris Lattner62814a32007-10-17 06:02:13 +00005381 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005382 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5383 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00005384 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005385 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5386 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00005387 }
5388
Dan Gohman8181bd12008-07-27 21:46:04 +00005389 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00005390 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391}
5392
Dan Gohman8181bd12008-07-27 21:46:04 +00005393SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00005394 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005395
5396 if (SrcVT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005397 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005398 return Op;
5399 }
5400 return SDValue();
5401 }
5402
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005403 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00005404 "Unknown SINT_TO_FP to lower!");
Scott Michel91099d62009-02-17 22:15:04 +00005405
Eli Friedman9d77ac32009-05-27 00:47:34 +00005406 // These are really Legal; return the operand so the caller accepts it as
5407 // Legal.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005408 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman9d77ac32009-05-27 00:47:34 +00005409 return Op;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005410 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman9d77ac32009-05-27 00:47:34 +00005411 Subtarget->is64Bit()) {
5412 return Op;
5413 }
Scott Michel91099d62009-02-17 22:15:04 +00005414
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005415 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005416 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005417 MachineFunction &MF = DAG.getMachineFunction();
David Greene6424ab92009-11-12 20:49:22 +00005418 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005419 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00005420 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling6b42d012009-03-13 08:41:47 +00005421 StackSlot,
David Greene25160362010-02-15 16:53:33 +00005422 PseudoSourceValue::getFixedStack(SSFI), 0,
5423 false, false, 0);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005424 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5425}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426
Owen Andersonac9de032009-08-10 22:56:29 +00005427SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman8c3cb582009-05-23 09:59:16 +00005428 SDValue StackSlot,
5429 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005430 // Build the FILD
Eli Friedman8c3cb582009-05-23 09:59:16 +00005431 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00005433 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005434 if (useSSE)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005435 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005436 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005437 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005438 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesence0805b2009-02-03 19:33:06 +00005439 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005440 Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005441
Dale Johannesen2fc20782007-09-14 22:26:36 +00005442 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00005444 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005445
5446 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5447 // shouldn't be necessary except that RFP cannot be live across
5448 // multiple blocks. When stackifier is fixed, they can be uncoupled.
5449 MachineFunction &MF = DAG.getMachineFunction();
David Greene6424ab92009-11-12 20:49:22 +00005450 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005451 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005452 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005453 SDValue Ops[] = {
5454 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5455 };
5456 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesence0805b2009-02-03 19:33:06 +00005457 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene25160362010-02-15 16:53:33 +00005458 PseudoSourceValue::getFixedStack(SSFI), 0,
5459 false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005460 }
5461
5462 return Result;
5463}
5464
Bill Wendling14a30ef2009-01-17 03:56:04 +00005465// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5466SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5467 // This algorithm is not obvious. Here it is in C code, more or less:
5468 /*
5469 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5470 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5471 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00005472
Bill Wendling14a30ef2009-01-17 03:56:04 +00005473 // Copy ints to xmm registers.
5474 __m128i xh = _mm_cvtsi32_si128( hi );
5475 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005476
Bill Wendling14a30ef2009-01-17 03:56:04 +00005477 // Combine into low half of a single xmm register.
5478 __m128i x = _mm_unpacklo_epi32( xh, xl );
5479 __m128d d;
5480 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00005481
Bill Wendling14a30ef2009-01-17 03:56:04 +00005482 // Merge in appropriate exponents to give the integer bits the right
5483 // magnitude.
5484 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005485
Bill Wendling14a30ef2009-01-17 03:56:04 +00005486 // Subtract away the biases to deal with the IEEE-754 double precision
5487 // implicit 1.
5488 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005489
Bill Wendling14a30ef2009-01-17 03:56:04 +00005490 // All conversions up to here are exact. The correctly rounded result is
5491 // calculated using the current rounding mode using the following
5492 // horizontal add.
5493 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5494 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5495 // store doesn't really need to be here (except
5496 // maybe to zero the other double)
5497 return sd;
5498 }
5499 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00005500
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005501 DebugLoc dl = Op.getDebugLoc();
Owen Anderson6361f972009-07-15 21:51:10 +00005502 LLVMContext *Context = DAG.getContext();
Dale Johannesence0805b2009-02-03 19:33:06 +00005503
Dale Johannesena359b8b2008-10-21 20:50:01 +00005504 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00005505 std::vector<Constant*> CV0;
Owen Andersoneacb44d2009-07-24 23:12:02 +00005506 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5507 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5508 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5509 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Anderson2f422e02009-07-28 21:19:26 +00005510 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng68c18682009-03-13 07:51:59 +00005511 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00005512
Bill Wendling14a30ef2009-01-17 03:56:04 +00005513 std::vector<Constant*> CV1;
Owen Anderson6361f972009-07-15 21:51:10 +00005514 CV1.push_back(
Owen Andersond363a0e2009-07-27 20:59:43 +00005515 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Anderson6361f972009-07-15 21:51:10 +00005516 CV1.push_back(
Owen Andersond363a0e2009-07-27 20:59:43 +00005517 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Anderson2f422e02009-07-28 21:19:26 +00005518 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng68c18682009-03-13 07:51:59 +00005519 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00005520
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005521 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5522 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00005523 Op.getOperand(0),
5524 DAG.getIntPtrConstant(1)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005525 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5526 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00005527 Op.getOperand(0),
5528 DAG.getIntPtrConstant(0)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005529 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5530 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005531 PseudoSourceValue::getConstantPool(), 0,
David Greene25160362010-02-15 16:53:33 +00005532 false, false, 16);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005533 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5534 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5535 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005536 PseudoSourceValue::getConstantPool(), 0,
David Greene25160362010-02-15 16:53:33 +00005537 false, false, 16);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005538 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005539
Dale Johannesena359b8b2008-10-21 20:50:01 +00005540 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman543d2142009-04-27 18:41:29 +00005541 int ShufMask[2] = { 1, -1 };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005542 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5543 DAG.getUNDEF(MVT::v2f64), ShufMask);
5544 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5545 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00005546 DAG.getIntPtrConstant(0));
5547}
5548
Bill Wendling14a30ef2009-01-17 03:56:04 +00005549// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5550SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005551 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00005552 // FP constant to bias correct the final result.
5553 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005554 MVT::f64);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005555
5556 // Load the 32-bit value into an XMM register.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005557 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5558 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005559 Op.getOperand(0),
5560 DAG.getIntPtrConstant(0)));
5561
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005562 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5563 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00005564 DAG.getIntPtrConstant(0));
5565
5566 // Or the load with the bias.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005567 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5568 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005569 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005570 MVT::v2f64, Load)),
5571 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005572 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005573 MVT::v2f64, Bias)));
5574 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5575 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00005576 DAG.getIntPtrConstant(0));
5577
5578 // Subtract the bias.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005579 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005580
5581 // Handle final rounding.
Owen Andersonac9de032009-08-10 22:56:29 +00005582 EVT DestVT = Op.getValueType();
Bill Wendlingdb547de2009-01-17 07:40:19 +00005583
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005584 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005585 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00005586 DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005587 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005588 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00005589 }
5590
5591 // Handle final rounding.
5592 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00005593}
5594
5595SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00005596 SDValue N0 = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005597 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00005598
Evan Cheng44fd2392009-01-19 08:08:22 +00005599 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5600 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5601 // the optimization here.
5602 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00005603 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00005604
Owen Andersonac9de032009-08-10 22:56:29 +00005605 EVT SrcVT = N0.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005606 if (SrcVT == MVT::i64) {
Eli Friedman9d77ac32009-05-27 00:47:34 +00005607 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005608 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar00261df2009-05-26 21:27:02 +00005609 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00005610
Bill Wendling14a30ef2009-01-17 03:56:04 +00005611 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005612 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00005613 return LowerUINT_TO_FP_i32(Op, DAG);
5614 }
5615
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005616 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman8c3cb582009-05-23 09:59:16 +00005617
5618 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005619 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005620 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5621 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5622 getPointerTy(), StackSlot, WordOff);
5623 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene25160362010-02-15 16:53:33 +00005624 StackSlot, NULL, 0, false, false, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005625 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene25160362010-02-15 16:53:33 +00005626 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005627 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005628}
5629
Dan Gohman8181bd12008-07-27 21:46:04 +00005630std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman8c3cb582009-05-23 09:59:16 +00005631FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005632 DebugLoc dl = Op.getDebugLoc();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005633
Owen Andersonac9de032009-08-10 22:56:29 +00005634 EVT DstTy = Op.getValueType();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005635
5636 if (!IsSigned) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005637 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5638 DstTy = MVT::i64;
Eli Friedman8c3cb582009-05-23 09:59:16 +00005639 }
5640
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005641 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5642 DstTy.getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005643 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644
Dale Johannesen2fc20782007-09-14 22:26:36 +00005645 // These are really Legal.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005646 if (DstTy == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005647 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005648 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00005649 if (Subtarget->is64Bit() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005650 DstTy == MVT::i64 &&
Eli Friedman9d77ac32009-05-27 00:47:34 +00005651 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005652 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005653
Evan Cheng05441e62007-10-15 20:11:21 +00005654 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5655 // stack slot.
5656 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005657 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene6424ab92009-11-12 20:49:22 +00005658 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005659 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005661 unsigned Opc;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005662 switch (DstTy.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005663 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005664 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5665 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5666 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667 }
5668
Dan Gohman8181bd12008-07-27 21:46:04 +00005669 SDValue Chain = DAG.getEntryNode();
5670 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00005671 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005672 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00005673 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene25160362010-02-15 16:53:33 +00005674 PseudoSourceValue::getFixedStack(SSFI), 0,
5675 false, false, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005676 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00005677 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005678 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5679 };
Dale Johannesence0805b2009-02-03 19:33:06 +00005680 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681 Chain = Value.getValue(1);
David Greene6424ab92009-11-12 20:49:22 +00005682 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5684 }
5685
5686 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00005687 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005688 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005689
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005690 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691}
5692
Dan Gohman8181bd12008-07-27 21:46:04 +00005693SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005694 if (Op.getValueType().isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005695 if (Op.getValueType() == MVT::v2i32 &&
5696 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005697 return Op;
5698 }
5699 return SDValue();
5700 }
5701
Eli Friedman8c3cb582009-05-23 09:59:16 +00005702 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman8181bd12008-07-27 21:46:04 +00005703 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman9d77ac32009-05-27 00:47:34 +00005704 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5705 if (FIST.getNode() == 0) return Op;
Scott Michel91099d62009-02-17 22:15:04 +00005706
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005707 // Load the result.
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005708 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene25160362010-02-15 16:53:33 +00005709 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005710}
5711
Eli Friedman8c3cb582009-05-23 09:59:16 +00005712SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5713 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5714 SDValue FIST = Vals.first, StackSlot = Vals.second;
5715 assert(FIST.getNode() && "Unexpected failure");
5716
5717 // Load the result.
5718 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene25160362010-02-15 16:53:33 +00005719 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005720}
5721
Dan Gohman8181bd12008-07-27 21:46:04 +00005722SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005723 LLVMContext *Context = DAG.getContext();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005724 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005725 EVT VT = Op.getValueType();
5726 EVT EltVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00005727 if (VT.isVector())
5728 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005729 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005730 if (EltVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005731 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 CV.push_back(C);
5733 CV.push_back(C);
5734 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005735 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005736 CV.push_back(C);
5737 CV.push_back(C);
5738 CV.push_back(C);
5739 CV.push_back(C);
5740 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005741 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005742 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005743 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene25160362010-02-15 16:53:33 +00005744 PseudoSourceValue::getConstantPool(), 0,
5745 false, false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005746 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005747}
5748
Dan Gohman8181bd12008-07-27 21:46:04 +00005749SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005750 LLVMContext *Context = DAG.getContext();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005751 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005752 EVT VT = Op.getValueType();
5753 EVT EltVT = VT;
Duncan Sands831102e2009-09-06 19:29:07 +00005754 if (VT.isVector())
Duncan Sands92c43912008-06-06 12:08:01 +00005755 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005756 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005757 if (EltVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005758 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005759 CV.push_back(C);
5760 CV.push_back(C);
5761 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005762 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763 CV.push_back(C);
5764 CV.push_back(C);
5765 CV.push_back(C);
5766 CV.push_back(C);
5767 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005768 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005769 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005770 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene25160362010-02-15 16:53:33 +00005771 PseudoSourceValue::getConstantPool(), 0,
5772 false, false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005773 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005774 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005775 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5776 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005777 Op.getOperand(0)),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005778 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00005779 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005780 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00005781 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782}
5783
Dan Gohman8181bd12008-07-27 21:46:04 +00005784SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005785 LLVMContext *Context = DAG.getContext();
Dan Gohman8181bd12008-07-27 21:46:04 +00005786 SDValue Op0 = Op.getOperand(0);
5787 SDValue Op1 = Op.getOperand(1);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005788 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005789 EVT VT = Op.getValueType();
5790 EVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791
5792 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005793 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005794 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005795 SrcVT = VT;
5796 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005797 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005798 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005799 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005800 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005801 }
5802
5803 // At this point the operands and the result should have the same
5804 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005805
5806 // First get the sign bit of second operand.
5807 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005808 if (SrcVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005809 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5810 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005812 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5813 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5814 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5815 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005816 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005817 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005818 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005819 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene25160362010-02-15 16:53:33 +00005820 PseudoSourceValue::getConstantPool(), 0,
5821 false, false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005822 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823
5824 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005825 if (SrcVT.bitsGT(VT)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005826 // Op0 is MVT::f32, Op1 is MVT::f64.
5827 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5828 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5829 DAG.getConstant(32, MVT::i32));
5830 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5831 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005832 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005833 }
5834
5835 // Clear first operand sign bit.
5836 CV.clear();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005837 if (VT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005838 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5839 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005840 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005841 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5842 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5843 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5844 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005845 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005846 C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005847 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005848 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene25160362010-02-15 16:53:33 +00005849 PseudoSourceValue::getConstantPool(), 0,
5850 false, false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005851 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005852
5853 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005854 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005855}
5856
Dan Gohman99a12192009-03-04 19:44:21 +00005857/// Emit nodes that will be selected as "test Op0,Op0", or something
5858/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005859SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5860 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005861 DebugLoc dl = Op.getDebugLoc();
5862
Dan Gohmanc8b47852009-03-07 01:58:32 +00005863 // CF and OF aren't always set the way we want. Determine which
5864 // of these we need.
5865 bool NeedCF = false;
5866 bool NeedOF = false;
5867 switch (X86CC) {
5868 case X86::COND_A: case X86::COND_AE:
5869 case X86::COND_B: case X86::COND_BE:
5870 NeedCF = true;
5871 break;
5872 case X86::COND_G: case X86::COND_GE:
5873 case X86::COND_L: case X86::COND_LE:
5874 case X86::COND_O: case X86::COND_NO:
5875 NeedOF = true;
5876 break;
5877 default: break;
5878 }
5879
Dan Gohman99a12192009-03-04 19:44:21 +00005880 // See if we can use the EFLAGS value from the operand instead of
Dan Gohmanc8b47852009-03-07 01:58:32 +00005881 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5882 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5883 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman99a12192009-03-04 19:44:21 +00005884 unsigned Opcode = 0;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005885 unsigned NumOperands = 0;
Dan Gohman99a12192009-03-04 19:44:21 +00005886 switch (Op.getNode()->getOpcode()) {
5887 case ISD::ADD:
5888 // Due to an isel shortcoming, be conservative if this add is likely to
5889 // be selected as part of a load-modify-store instruction. When the root
5890 // node in a match is a store, isel doesn't know how to remap non-chain
5891 // non-flag uses of other nodes in the match, such as the ADD in this
5892 // case. This leads to the ADD being left around and reselected, with
5893 // the result being two adds in the output.
5894 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5895 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5896 if (UI->getOpcode() == ISD::STORE)
5897 goto default_case;
Dan Gohman99a12192009-03-04 19:44:21 +00005898 if (ConstantSDNode *C =
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005899 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5900 // An add of one will be selected as an INC.
Dan Gohman99a12192009-03-04 19:44:21 +00005901 if (C->getAPIntValue() == 1) {
5902 Opcode = X86ISD::INC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005903 NumOperands = 1;
Dan Gohman99a12192009-03-04 19:44:21 +00005904 break;
5905 }
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005906 // An add of negative one (subtract of one) will be selected as a DEC.
5907 if (C->getAPIntValue().isAllOnesValue()) {
5908 Opcode = X86ISD::DEC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005909 NumOperands = 1;
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005910 break;
5911 }
5912 }
Dan Gohman99a12192009-03-04 19:44:21 +00005913 // Otherwise use a regular EFLAGS-setting add.
5914 Opcode = X86ISD::ADD;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005915 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005916 break;
Dan Gohman12e03292009-09-18 19:59:53 +00005917 case ISD::AND: {
5918 // If the primary and result isn't used, don't bother using X86ISD::AND,
5919 // because a TEST instruction will be better.
5920 bool NonFlagUse = false;
5921 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Chengc429ff52010-01-07 00:54:06 +00005922 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5923 SDNode *User = *UI;
5924 unsigned UOpNo = UI.getOperandNo();
5925 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5926 // Look pass truncate.
5927 UOpNo = User->use_begin().getOperandNo();
5928 User = *User->use_begin();
5929 }
5930 if (User->getOpcode() != ISD::BRCOND &&
5931 User->getOpcode() != ISD::SETCC &&
5932 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohman12e03292009-09-18 19:59:53 +00005933 NonFlagUse = true;
5934 break;
5935 }
Evan Chengc429ff52010-01-07 00:54:06 +00005936 }
Dan Gohman12e03292009-09-18 19:59:53 +00005937 if (!NonFlagUse)
5938 break;
5939 }
5940 // FALL THROUGH
Dan Gohman99a12192009-03-04 19:44:21 +00005941 case ISD::SUB:
Dan Gohman12e03292009-09-18 19:59:53 +00005942 case ISD::OR:
5943 case ISD::XOR:
5944 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman99a12192009-03-04 19:44:21 +00005945 // likely to be selected as part of a load-modify-store instruction.
5946 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5947 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5948 if (UI->getOpcode() == ISD::STORE)
5949 goto default_case;
Dan Gohman12e03292009-09-18 19:59:53 +00005950 // Otherwise use a regular EFLAGS-setting instruction.
5951 switch (Op.getNode()->getOpcode()) {
5952 case ISD::SUB: Opcode = X86ISD::SUB; break;
5953 case ISD::OR: Opcode = X86ISD::OR; break;
5954 case ISD::XOR: Opcode = X86ISD::XOR; break;
5955 case ISD::AND: Opcode = X86ISD::AND; break;
5956 default: llvm_unreachable("unexpected operator!");
5957 }
Dan Gohman8c8a8022009-03-05 21:29:28 +00005958 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005959 break;
5960 case X86ISD::ADD:
5961 case X86ISD::SUB:
5962 case X86ISD::INC:
5963 case X86ISD::DEC:
Dan Gohman12e03292009-09-18 19:59:53 +00005964 case X86ISD::OR:
5965 case X86ISD::XOR:
5966 case X86ISD::AND:
Dan Gohman99a12192009-03-04 19:44:21 +00005967 return SDValue(Op.getNode(), 1);
5968 default:
5969 default_case:
5970 break;
5971 }
5972 if (Opcode != 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005973 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman99a12192009-03-04 19:44:21 +00005974 SmallVector<SDValue, 4> Ops;
Dan Gohmanc8b47852009-03-07 01:58:32 +00005975 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman99a12192009-03-04 19:44:21 +00005976 Ops.push_back(Op.getOperand(i));
Dan Gohmanee036282009-04-09 23:54:40 +00005977 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman99a12192009-03-04 19:44:21 +00005978 DAG.ReplaceAllUsesWith(Op, New);
5979 return SDValue(New.getNode(), 1);
5980 }
5981 }
5982
5983 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005984 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman99a12192009-03-04 19:44:21 +00005985 DAG.getConstant(0, Op.getValueType()));
5986}
5987
5988/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5989/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005990SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5991 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5993 if (C->getAPIntValue() == 0)
Dan Gohmanc8b47852009-03-07 01:58:32 +00005994 return EmitTest(Op0, X86CC, DAG);
Dan Gohman99a12192009-03-04 19:44:21 +00005995
5996 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005997 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman99a12192009-03-04 19:44:21 +00005998}
5999
Evan Cheng095dac22010-01-06 19:38:29 +00006000/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6001/// if it's possible.
Evan Chengcb611272010-02-27 07:36:59 +00006002static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Chengc621d452010-01-05 06:52:31 +00006003 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengcb611272010-02-27 07:36:59 +00006004 SDValue Op0 = And.getOperand(0);
6005 SDValue Op1 = And.getOperand(1);
6006 if (Op0.getOpcode() == ISD::TRUNCATE)
6007 Op0 = Op0.getOperand(0);
6008 if (Op1.getOpcode() == ISD::TRUNCATE)
6009 Op1 = Op1.getOperand(0);
6010
Evan Cheng095dac22010-01-06 19:38:29 +00006011 SDValue LHS, RHS;
Evan Chengcb611272010-02-27 07:36:59 +00006012 if (Op1.getOpcode() == ISD::SHL) {
6013 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6014 if (And10C->getZExtValue() == 1) {
6015 LHS = Op0;
6016 RHS = Op1.getOperand(1);
Dan Gohman22cefb02009-01-29 01:59:02 +00006017 }
Evan Chengcb611272010-02-27 07:36:59 +00006018 } else if (Op0.getOpcode() == ISD::SHL) {
6019 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6020 if (And00C->getZExtValue() == 1) {
6021 LHS = Op1;
6022 RHS = Op0.getOperand(1);
Evan Cheng095dac22010-01-06 19:38:29 +00006023 }
Evan Chengcb611272010-02-27 07:36:59 +00006024 } else if (Op1.getOpcode() == ISD::Constant) {
6025 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6026 SDValue AndLHS = Op0;
Evan Cheng095dac22010-01-06 19:38:29 +00006027 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6028 LHS = AndLHS.getOperand(0);
6029 RHS = AndLHS.getOperand(1);
Dan Gohman22cefb02009-01-29 01:59:02 +00006030 }
Evan Cheng095dac22010-01-06 19:38:29 +00006031 }
Evan Cheng950aac02007-09-25 01:57:46 +00006032
Evan Cheng095dac22010-01-06 19:38:29 +00006033 if (LHS.getNode()) {
6034 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
6035 // instruction. Since the shift amount is in-range-or-undefined, we know
6036 // that doing a bittest on the i16 value is ok. We extend to i32 because
6037 // the encoding for the i16 version is larger than the i32 version.
6038 if (LHS.getValueType() == MVT::i8)
6039 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00006040
Evan Cheng095dac22010-01-06 19:38:29 +00006041 // If the operand types disagree, extend the shift amount to match. Since
6042 // BT ignores high bits (like shifts) we can use anyextend.
6043 if (LHS.getValueType() != RHS.getValueType())
6044 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00006045
Evan Cheng095dac22010-01-06 19:38:29 +00006046 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6047 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6048 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6049 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattner77a62312008-12-25 05:34:37 +00006050 }
6051
Evan Chengc621d452010-01-05 06:52:31 +00006052 return SDValue();
6053}
6054
6055SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
6056 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6057 SDValue Op0 = Op.getOperand(0);
6058 SDValue Op1 = Op.getOperand(1);
6059 DebugLoc dl = Op.getDebugLoc();
6060 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6061
6062 // Optimize to BT if possible.
Evan Cheng095dac22010-01-06 19:38:29 +00006063 // Lower (X & (1 << N)) == 0 to BT(X, N).
6064 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6065 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6066 if (Op0.getOpcode() == ISD::AND &&
6067 Op0.hasOneUse() &&
6068 Op1.getOpcode() == ISD::Constant &&
6069 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6070 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6071 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6072 if (NewSetCC.getNode())
6073 return NewSetCC;
6074 }
Evan Chengc621d452010-01-05 06:52:31 +00006075
Evan Chengcb611272010-02-27 07:36:59 +00006076 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6077 if (Op0.getOpcode() == X86ISD::SETCC &&
6078 Op1.getOpcode() == ISD::Constant &&
6079 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6080 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6081 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6082 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6083 bool Invert = (CC == ISD::SETNE) ^
6084 cast<ConstantSDNode>(Op1)->isNullValue();
6085 if (Invert)
6086 CCode = X86::GetOppositeBranchCondition(CCode);
6087 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6088 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6089 }
6090
Chris Lattner77a62312008-12-25 05:34:37 +00006091 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
6092 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006093 if (X86CC == X86::COND_INVALID)
6094 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00006095
Dan Gohmanc8b47852009-03-07 01:58:32 +00006096 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Cheng834ae6b2009-12-15 00:53:42 +00006097
6098 // Use sbb x, x to materialize carry bit into a GPR.
Evan Chengedeb1692009-12-16 00:53:11 +00006099 if (X86CC == X86::COND_B)
Evan Cheng834ae6b2009-12-15 00:53:42 +00006100 return DAG.getNode(ISD::AND, dl, MVT::i8,
6101 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6102 DAG.getConstant(X86CC, MVT::i8), Cond),
6103 DAG.getConstant(1, MVT::i8));
Evan Cheng834ae6b2009-12-15 00:53:42 +00006104
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006105 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6106 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00006107}
6108
Dan Gohman8181bd12008-07-27 21:46:04 +00006109SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6110 SDValue Cond;
6111 SDValue Op0 = Op.getOperand(0);
6112 SDValue Op1 = Op.getOperand(1);
6113 SDValue CC = Op.getOperand(2);
Owen Andersonac9de032009-08-10 22:56:29 +00006114 EVT VT = Op.getValueType();
Nate Begeman03605a02008-07-17 16:51:19 +00006115 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6116 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006117 DebugLoc dl = Op.getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00006118
6119 if (isFP) {
6120 unsigned SSECC = 8;
Owen Andersonac9de032009-08-10 22:56:29 +00006121 EVT VT0 = Op0.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006122 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6123 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00006124 bool Swap = false;
6125
6126 switch (SetCCOpcode) {
6127 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00006128 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00006129 case ISD::SETEQ: SSECC = 0; break;
Scott Michel91099d62009-02-17 22:15:04 +00006130 case ISD::SETOGT:
Nate Begeman03605a02008-07-17 16:51:19 +00006131 case ISD::SETGT: Swap = true; // Fallthrough
6132 case ISD::SETLT:
6133 case ISD::SETOLT: SSECC = 1; break;
6134 case ISD::SETOGE:
6135 case ISD::SETGE: Swap = true; // Fallthrough
6136 case ISD::SETLE:
6137 case ISD::SETOLE: SSECC = 2; break;
6138 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00006139 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00006140 case ISD::SETNE: SSECC = 4; break;
6141 case ISD::SETULE: Swap = true;
6142 case ISD::SETUGE: SSECC = 5; break;
6143 case ISD::SETULT: Swap = true;
6144 case ISD::SETUGT: SSECC = 6; break;
6145 case ISD::SETO: SSECC = 7; break;
6146 }
6147 if (Swap)
6148 std::swap(Op0, Op1);
6149
Nate Begeman6357f9d2008-07-25 19:05:58 +00006150 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00006151 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00006152 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006153 SDValue UNORD, EQ;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006154 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6155 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00006156 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00006157 }
6158 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006159 SDValue ORD, NEQ;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006160 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6161 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00006162 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00006163 }
Edwin Törökbd448e32009-07-14 16:55:14 +00006164 llvm_unreachable("Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00006165 }
6166 // Handle all other FP comparisons here.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006167 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00006168 }
Scott Michel91099d62009-02-17 22:15:04 +00006169
Nate Begeman03605a02008-07-17 16:51:19 +00006170 // We are handling one of the integer comparisons here. Since SSE only has
6171 // GT and EQ comparisons for integer, swapping operands and multiple
6172 // operations may be required for some comparisons.
6173 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6174 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michel91099d62009-02-17 22:15:04 +00006175
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006176 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman03605a02008-07-17 16:51:19 +00006177 default: break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006178 case MVT::v8i8:
6179 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6180 case MVT::v4i16:
6181 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6182 case MVT::v2i32:
6183 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6184 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman03605a02008-07-17 16:51:19 +00006185 }
Scott Michel91099d62009-02-17 22:15:04 +00006186
Nate Begeman03605a02008-07-17 16:51:19 +00006187 switch (SetCCOpcode) {
6188 default: break;
6189 case ISD::SETNE: Invert = true;
6190 case ISD::SETEQ: Opc = EQOpc; break;
6191 case ISD::SETLT: Swap = true;
6192 case ISD::SETGT: Opc = GTOpc; break;
6193 case ISD::SETGE: Swap = true;
6194 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6195 case ISD::SETULT: Swap = true;
6196 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6197 case ISD::SETUGE: Swap = true;
6198 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6199 }
6200 if (Swap)
6201 std::swap(Op0, Op1);
Scott Michel91099d62009-02-17 22:15:04 +00006202
Nate Begeman03605a02008-07-17 16:51:19 +00006203 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6204 // bits of the inputs before performing those operations.
6205 if (FlipSigns) {
Owen Andersonac9de032009-08-10 22:56:29 +00006206 EVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00006207 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6208 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006209 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Cheng907a2d22009-02-25 22:49:59 +00006210 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6211 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00006212 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6213 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00006214 }
Scott Michel91099d62009-02-17 22:15:04 +00006215
Dale Johannesence0805b2009-02-03 19:33:06 +00006216 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00006217
6218 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00006219 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00006220 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00006221
Nate Begeman03605a02008-07-17 16:51:19 +00006222 return Result;
6223}
Evan Cheng950aac02007-09-25 01:57:46 +00006224
Evan Chengd580f022008-12-03 08:38:43 +00006225// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman99a12192009-03-04 19:44:21 +00006226static bool isX86LogicalCmp(SDValue Op) {
6227 unsigned Opc = Op.getNode()->getOpcode();
6228 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6229 return true;
6230 if (Op.getResNo() == 1 &&
6231 (Opc == X86ISD::ADD ||
6232 Opc == X86ISD::SUB ||
6233 Opc == X86ISD::SMUL ||
6234 Opc == X86ISD::UMUL ||
6235 Opc == X86ISD::INC ||
Dan Gohman12e03292009-09-18 19:59:53 +00006236 Opc == X86ISD::DEC ||
6237 Opc == X86ISD::OR ||
6238 Opc == X86ISD::XOR ||
6239 Opc == X86ISD::AND))
Dan Gohman99a12192009-03-04 19:44:21 +00006240 return true;
6241
6242 return false;
Evan Chengd580f022008-12-03 08:38:43 +00006243}
6244
Dan Gohman8181bd12008-07-27 21:46:04 +00006245SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006246 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00006247 SDValue Cond = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006248 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00006249 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006251 if (Cond.getOpcode() == ISD::SETCC) {
6252 SDValue NewCond = LowerSETCC(Cond, DAG);
6253 if (NewCond.getNode())
6254 Cond = NewCond;
6255 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006256
Evan Cheng506f6f02010-01-26 02:00:44 +00006257 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6258 SDValue Op1 = Op.getOperand(1);
6259 SDValue Op2 = Op.getOperand(2);
6260 if (Cond.getOpcode() == X86ISD::SETCC &&
6261 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6262 SDValue Cmp = Cond.getOperand(1);
6263 if (Cmp.getOpcode() == X86ISD::CMP) {
6264 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6265 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6266 ConstantSDNode *RHSC =
6267 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6268 if (N1C && N1C->isAllOnesValue() &&
6269 N2C && N2C->isNullValue() &&
6270 RHSC && RHSC->isNullValue()) {
6271 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattneraeeb8b72010-03-14 18:44:35 +00006272 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng506f6f02010-01-26 02:00:44 +00006273 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6274 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6275 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6276 }
6277 }
6278 }
6279
Evan Cheng834ae6b2009-12-15 00:53:42 +00006280 // Look pass (and (setcc_carry (cmp ...)), 1).
6281 if (Cond.getOpcode() == ISD::AND &&
6282 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6283 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6284 if (C && C->getAPIntValue() == 1)
6285 Cond = Cond.getOperand(0);
6286 }
6287
Evan Cheng50d37ab2007-10-08 22:16:29 +00006288 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6289 // setting operand in place of the X86ISD::SETCC.
Evan Cheng834ae6b2009-12-15 00:53:42 +00006290 if (Cond.getOpcode() == X86ISD::SETCC ||
6291 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006292 CC = Cond.getOperand(0);
6293
Dan Gohman8181bd12008-07-27 21:46:04 +00006294 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006295 unsigned Opc = Cmp.getOpcode();
Owen Andersonac9de032009-08-10 22:56:29 +00006296 EVT VT = Op.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00006297
Evan Cheng50d37ab2007-10-08 22:16:29 +00006298 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00006299 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00006300 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00006301 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michel91099d62009-02-17 22:15:04 +00006302
Chris Lattnere4577dc2009-03-12 06:52:53 +00006303 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6304 Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00006305 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00006306 addTest = false;
6307 }
6308 }
6309
6310 if (addTest) {
Evan Cheng095dac22010-01-06 19:38:29 +00006311 // Look pass the truncate.
6312 if (Cond.getOpcode() == ISD::TRUNCATE)
6313 Cond = Cond.getOperand(0);
6314
6315 // We know the result of AND is compared against zero. Try to match
6316 // it to BT.
6317 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6318 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6319 if (NewSetCC.getNode()) {
6320 CC = NewSetCC.getOperand(0);
6321 Cond = NewSetCC.getOperand(1);
6322 addTest = false;
6323 }
6324 }
6325 }
6326
6327 if (addTest) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006328 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00006329 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00006330 }
6331
Evan Cheng950aac02007-09-25 01:57:46 +00006332 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6333 // condition is true.
Evan Cheng506f6f02010-01-26 02:00:44 +00006334 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6335 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006336 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng950aac02007-09-25 01:57:46 +00006337}
6338
Evan Chengd580f022008-12-03 08:38:43 +00006339// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6340// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6341// from the AND / OR.
6342static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6343 Opc = Op.getOpcode();
6344 if (Opc != ISD::OR && Opc != ISD::AND)
6345 return false;
6346 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6347 Op.getOperand(0).hasOneUse() &&
6348 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6349 Op.getOperand(1).hasOneUse());
6350}
6351
Evan Cheng67f98b12009-02-02 08:19:07 +00006352// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6353// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006354static bool isXor1OfSetCC(SDValue Op) {
6355 if (Op.getOpcode() != ISD::XOR)
6356 return false;
6357 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6358 if (N1C && N1C->getAPIntValue() == 1) {
6359 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6360 Op.getOperand(0).hasOneUse();
6361 }
6362 return false;
6363}
6364
Dan Gohman8181bd12008-07-27 21:46:04 +00006365SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006366 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00006367 SDValue Chain = Op.getOperand(0);
6368 SDValue Cond = Op.getOperand(1);
6369 SDValue Dest = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006370 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00006371 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006372
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006373 if (Cond.getOpcode() == ISD::SETCC) {
6374 SDValue NewCond = LowerSETCC(Cond, DAG);
6375 if (NewCond.getNode())
6376 Cond = NewCond;
6377 }
Chris Lattner77a62312008-12-25 05:34:37 +00006378#if 0
6379 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00006380 else if (Cond.getOpcode() == X86ISD::ADD ||
6381 Cond.getOpcode() == X86ISD::SUB ||
6382 Cond.getOpcode() == X86ISD::SMUL ||
6383 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00006384 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00006385#endif
Scott Michel91099d62009-02-17 22:15:04 +00006386
Evan Cheng834ae6b2009-12-15 00:53:42 +00006387 // Look pass (and (setcc_carry (cmp ...)), 1).
6388 if (Cond.getOpcode() == ISD::AND &&
6389 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6390 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6391 if (C && C->getAPIntValue() == 1)
6392 Cond = Cond.getOperand(0);
6393 }
6394
Evan Cheng50d37ab2007-10-08 22:16:29 +00006395 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6396 // setting operand in place of the X86ISD::SETCC.
Evan Cheng834ae6b2009-12-15 00:53:42 +00006397 if (Cond.getOpcode() == X86ISD::SETCC ||
6398 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006399 CC = Cond.getOperand(0);
6400
Dan Gohman8181bd12008-07-27 21:46:04 +00006401 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00006403 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman99a12192009-03-04 19:44:21 +00006404 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00006405 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00006406 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00006407 } else {
Evan Chengd580f022008-12-03 08:38:43 +00006408 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00006409 default: break;
6410 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006411 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00006412 // These can only come from an arithmetic instruction with overflow,
6413 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00006414 Cond = Cond.getNode()->getOperand(1);
6415 addTest = false;
6416 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006417 }
Evan Cheng950aac02007-09-25 01:57:46 +00006418 }
Evan Chengd580f022008-12-03 08:38:43 +00006419 } else {
6420 unsigned CondOpc;
6421 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6422 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Chengd580f022008-12-03 08:38:43 +00006423 if (CondOpc == ISD::OR) {
6424 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6425 // two branches instead of an explicit OR instruction with a
6426 // separate test.
6427 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00006428 isX86LogicalCmp(Cmp)) {
Evan Chengd580f022008-12-03 08:38:43 +00006429 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006430 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00006431 Chain, Dest, CC, Cmp);
6432 CC = Cond.getOperand(1).getOperand(0);
6433 Cond = Cmp;
6434 addTest = false;
6435 }
6436 } else { // ISD::AND
6437 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6438 // two branches instead of an explicit AND instruction with a
6439 // separate test. However, we only do this if this block doesn't
6440 // have a fall-through edge, because this requires an explicit
6441 // jmp when the condition is false.
6442 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00006443 isX86LogicalCmp(Cmp) &&
Evan Chengd580f022008-12-03 08:38:43 +00006444 Op.getNode()->hasOneUse()) {
6445 X86::CondCode CCode =
6446 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6447 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006448 CC = DAG.getConstant(CCode, MVT::i8);
Evan Chengd580f022008-12-03 08:38:43 +00006449 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6450 // Look for an unconditional branch following this conditional branch.
6451 // We need this because we need to reverse the successors in order
6452 // to implement FCMP_OEQ.
6453 if (User.getOpcode() == ISD::BR) {
6454 SDValue FalseBB = User.getOperand(1);
6455 SDValue NewBR =
6456 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6457 assert(NewBR == User);
6458 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006459
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006460 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00006461 Chain, Dest, CC, Cmp);
6462 X86::CondCode CCode =
6463 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6464 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006465 CC = DAG.getConstant(CCode, MVT::i8);
Evan Chengd580f022008-12-03 08:38:43 +00006466 Cond = Cmp;
6467 addTest = false;
6468 }
6469 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006470 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006471 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6472 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6473 // It should be transformed during dag combiner except when the condition
6474 // is set by a arithmetics with overflow node.
6475 X86::CondCode CCode =
6476 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6477 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006478 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006479 Cond = Cond.getOperand(0).getOperand(1);
6480 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006481 }
Evan Cheng950aac02007-09-25 01:57:46 +00006482 }
6483
6484 if (addTest) {
Evan Cheng095dac22010-01-06 19:38:29 +00006485 // Look pass the truncate.
6486 if (Cond.getOpcode() == ISD::TRUNCATE)
6487 Cond = Cond.getOperand(0);
6488
6489 // We know the result of AND is compared against zero. Try to match
6490 // it to BT.
6491 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6492 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6493 if (NewSetCC.getNode()) {
6494 CC = NewSetCC.getOperand(0);
6495 Cond = NewSetCC.getOperand(1);
6496 addTest = false;
6497 }
6498 }
6499 }
6500
6501 if (addTest) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006502 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00006503 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00006504 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006505 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006506 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00006507}
6508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006509
6510// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6511// Calls to _alloca is needed to probe the stack when allocating more than 4k
6512// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6513// that the guard pages used by the OS virtual memory manager are allocated in
6514// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00006515SDValue
6516X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517 SelectionDAG &DAG) {
6518 assert(Subtarget->isTargetCygMing() &&
6519 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006520 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006522 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00006523 SDValue Chain = Op.getOperand(0);
6524 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006525 // FIXME: Ensure alignment here
6526
Dan Gohman8181bd12008-07-27 21:46:04 +00006527 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006528
Owen Andersonac9de032009-08-10 22:56:29 +00006529 EVT IntPtr = getPointerTy();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006530 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006531
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006532 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006533 Flag = Chain.getValue(1);
6534
Anton Korobeynikov7cd32422010-03-06 19:32:29 +00006535 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006536
Anton Korobeynikov7cd32422010-03-06 19:32:29 +00006537 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6538 Flag = Chain.getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006539
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006540 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006541
Dan Gohman8181bd12008-07-27 21:46:04 +00006542 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006543 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006544}
6545
Dan Gohman8181bd12008-07-27 21:46:04 +00006546SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006547X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006548 SDValue Chain,
6549 SDValue Dst, SDValue Src,
6550 SDValue Size, unsigned Align,
6551 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00006552 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006553 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006554
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006555 // If not DWORD aligned or size is more than the threshold, call the library.
6556 // The libc version is likely to be faster for these cases. It can use the
6557 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006558 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00006559 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006560 ConstantSize->getZExtValue() >
6561 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006562 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00006563
6564 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00006565 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006566
Bill Wendling4b2e3782008-10-01 00:59:58 +00006567 if (const char *bzeroEntry = V &&
6568 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersonac9de032009-08-10 22:56:29 +00006569 EVT IntPtr = getPointerTy();
Owen Anderson35b47072009-08-13 21:58:54 +00006570 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michel91099d62009-02-17 22:15:04 +00006571 TargetLowering::ArgListTy Args;
Bill Wendling4b2e3782008-10-01 00:59:58 +00006572 TargetLowering::ArgListEntry Entry;
6573 Entry.Node = Dst;
6574 Entry.Ty = IntPtrTy;
6575 Args.push_back(Entry);
6576 Entry.Node = Size;
6577 Args.push_back(Entry);
6578 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00006579 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6580 false, false, false, false,
Dan Gohman9178de12009-08-05 01:29:28 +00006581 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling1ca34452010-03-02 01:55:18 +00006582 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling4b2e3782008-10-01 00:59:58 +00006583 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00006584 }
6585
Dan Gohmane8b391e2008-04-12 04:36:06 +00006586 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00006587 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006588 }
6589
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006590 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00006591 SDValue InFlag(0, 0);
Owen Andersonac9de032009-08-10 22:56:29 +00006592 EVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00006593 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006594 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006595 unsigned BytesLeft = 0;
6596 bool TwoRepStos = false;
6597 if (ValC) {
6598 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006599 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006600
6601 // If the value is a constant, then we can potentially use larger sets.
6602 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006603 case 2: // WORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006604 AVT = MVT::i16;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006605 ValReg = X86::AX;
6606 Val = (Val << 8) | Val;
6607 break;
6608 case 0: // DWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006609 AVT = MVT::i32;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006610 ValReg = X86::EAX;
6611 Val = (Val << 8) | Val;
6612 Val = (Val << 16) | Val;
6613 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006614 AVT = MVT::i64;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006615 ValReg = X86::RAX;
6616 Val = (Val << 32) | Val;
6617 }
6618 break;
6619 default: // Byte aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006620 AVT = MVT::i8;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006621 ValReg = X86::AL;
6622 Count = DAG.getIntPtrConstant(SizeVal);
6623 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006624 }
6625
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006626 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00006627 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006628 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6629 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006630 }
6631
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006632 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633 InFlag);
6634 InFlag = Chain.getValue(1);
6635 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006636 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00006637 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006638 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006639 InFlag = Chain.getValue(1);
6640 }
6641
Scott Michel91099d62009-02-17 22:15:04 +00006642 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006643 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006644 Count, InFlag);
6645 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006646 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006647 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006648 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006649 InFlag = Chain.getValue(1);
6650
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006651 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006652 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6653 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006654
6655 if (TwoRepStos) {
6656 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00006657 Count = Size;
Owen Andersonac9de032009-08-10 22:56:29 +00006658 EVT CVT = Count.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006659 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006660 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6661 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006662 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006663 Left, InFlag);
6664 InFlag = Chain.getValue(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006665 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006666 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6667 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006668 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006669 // Handle the last 1 - 7 bytes.
6670 unsigned Offset = SizeVal - BytesLeft;
Owen Andersonac9de032009-08-10 22:56:29 +00006671 EVT AddrVT = Dst.getValueType();
6672 EVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006673
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006674 Chain = DAG.getMemset(Chain, dl,
6675 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006676 DAG.getConstant(Offset, AddrVT)),
6677 Src,
6678 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wangf9f760e2010-04-02 18:43:02 +00006679 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006680 }
6681
Dan Gohmane8b391e2008-04-12 04:36:06 +00006682 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683 return Chain;
6684}
6685
Dan Gohman8181bd12008-07-27 21:46:04 +00006686SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006687X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006688 SDValue Chain, SDValue Dst, SDValue Src,
6689 SDValue Size, unsigned Align,
Mon P Wangf9f760e2010-04-02 18:43:02 +00006690 bool AlwaysInline,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006691 const Value *DstSV, uint64_t DstSVOff,
Scott Michel91099d62009-02-17 22:15:04 +00006692 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006693 // This requires the copy size to be a constant, preferrably
6694 // within a subtarget-specific limit.
6695 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6696 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00006697 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006698 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006699 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00006700 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006701
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006702 /// If not DWORD aligned, call the library.
6703 if ((Align & 3) != 0)
6704 return SDValue();
6705
6706 // DWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006707 EVT AVT = MVT::i32;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006708 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006709 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006710
Duncan Sands92c43912008-06-06 12:08:01 +00006711 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006712 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00006713 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006714 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006715
Dan Gohman8181bd12008-07-27 21:46:04 +00006716 SDValue InFlag(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00006717 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006718 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006719 Count, InFlag);
6720 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006721 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006722 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006723 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006724 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006725 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006726 X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006727 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006728 InFlag = Chain.getValue(1);
6729
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006730 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006731 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6732 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6733 array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006734
Dan Gohman8181bd12008-07-27 21:46:04 +00006735 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00006736 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00006737 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006738 // Handle the last 1 - 7 bytes.
6739 unsigned Offset = SizeVal - BytesLeft;
Owen Andersonac9de032009-08-10 22:56:29 +00006740 EVT DstVT = Dst.getValueType();
6741 EVT SrcVT = Src.getValueType();
6742 EVT SizeVT = Size.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00006743 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006744 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00006745 DAG.getConstant(Offset, DstVT)),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006746 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00006747 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00006748 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wangf9f760e2010-04-02 18:43:02 +00006749 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00006750 DstSV, DstSVOff + Offset,
6751 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006752 }
6753
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006754 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006755 &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006756}
6757
Dan Gohman8181bd12008-07-27 21:46:04 +00006758SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00006759 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006760 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761
6762 if (!Subtarget->is64Bit()) {
6763 // vastart just stores the address of the VarArgsFrameIndex slot into the
6764 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00006765 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene25160362010-02-15 16:53:33 +00006766 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6767 false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768 }
6769
6770 // __va_list_tag:
6771 // gp_offset (0 - 6 * 8)
6772 // fp_offset (48 - 48 + 8 * 16)
6773 // overflow_arg_area (point to parameters coming in memory).
6774 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00006775 SmallVector<SDValue, 8> MemOps;
6776 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006777 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006778 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
David Greene25160362010-02-15 16:53:33 +00006779 DAG.getConstant(VarArgsGPOffset, MVT::i32),
6780 FIN, SV, 0, false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006781 MemOps.push_back(Store);
6782
6783 // Store fp_offset
Scott Michel91099d62009-02-17 22:15:04 +00006784 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006785 FIN, DAG.getIntPtrConstant(4));
6786 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006787 DAG.getConstant(VarArgsFPOffset, MVT::i32),
David Greene25160362010-02-15 16:53:33 +00006788 FIN, SV, 0, false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006789 MemOps.push_back(Store);
6790
6791 // Store ptr to overflow_arg_area
Scott Michel91099d62009-02-17 22:15:04 +00006792 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006793 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00006794 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene25160362010-02-15 16:53:33 +00006795 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6796 false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006797 MemOps.push_back(Store);
6798
6799 // Store ptr to reg_save_area.
Scott Michel91099d62009-02-17 22:15:04 +00006800 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006801 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006802 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
David Greene25160362010-02-15 16:53:33 +00006803 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6804 false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006805 MemOps.push_back(Store);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006806 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006807 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006808}
6809
Dan Gohman8181bd12008-07-27 21:46:04 +00006810SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00006811 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6812 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006813 SDValue Chain = Op.getOperand(0);
6814 SDValue SrcPtr = Op.getOperand(1);
6815 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006816
Edwin Török4d9756a2009-07-08 20:53:28 +00006817 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006818 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00006819}
6820
Dan Gohman8181bd12008-07-27 21:46:04 +00006821SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006822 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00006823 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006824 SDValue Chain = Op.getOperand(0);
6825 SDValue DstPtr = Op.getOperand(1);
6826 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00006827 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6828 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006829 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006831 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wangf9f760e2010-04-02 18:43:02 +00006832 DAG.getIntPtrConstant(24), 8, false,
6833 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006834}
6835
Dan Gohman8181bd12008-07-27 21:46:04 +00006836SDValue
6837X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006838 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006839 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006840 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006841 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006842 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006843 case Intrinsic::x86_sse_comieq_ss:
6844 case Intrinsic::x86_sse_comilt_ss:
6845 case Intrinsic::x86_sse_comile_ss:
6846 case Intrinsic::x86_sse_comigt_ss:
6847 case Intrinsic::x86_sse_comige_ss:
6848 case Intrinsic::x86_sse_comineq_ss:
6849 case Intrinsic::x86_sse_ucomieq_ss:
6850 case Intrinsic::x86_sse_ucomilt_ss:
6851 case Intrinsic::x86_sse_ucomile_ss:
6852 case Intrinsic::x86_sse_ucomigt_ss:
6853 case Intrinsic::x86_sse_ucomige_ss:
6854 case Intrinsic::x86_sse_ucomineq_ss:
6855 case Intrinsic::x86_sse2_comieq_sd:
6856 case Intrinsic::x86_sse2_comilt_sd:
6857 case Intrinsic::x86_sse2_comile_sd:
6858 case Intrinsic::x86_sse2_comigt_sd:
6859 case Intrinsic::x86_sse2_comige_sd:
6860 case Intrinsic::x86_sse2_comineq_sd:
6861 case Intrinsic::x86_sse2_ucomieq_sd:
6862 case Intrinsic::x86_sse2_ucomilt_sd:
6863 case Intrinsic::x86_sse2_ucomile_sd:
6864 case Intrinsic::x86_sse2_ucomigt_sd:
6865 case Intrinsic::x86_sse2_ucomige_sd:
6866 case Intrinsic::x86_sse2_ucomineq_sd: {
6867 unsigned Opc = 0;
6868 ISD::CondCode CC = ISD::SETCC_INVALID;
6869 switch (IntNo) {
6870 default: break;
6871 case Intrinsic::x86_sse_comieq_ss:
6872 case Intrinsic::x86_sse2_comieq_sd:
6873 Opc = X86ISD::COMI;
6874 CC = ISD::SETEQ;
6875 break;
6876 case Intrinsic::x86_sse_comilt_ss:
6877 case Intrinsic::x86_sse2_comilt_sd:
6878 Opc = X86ISD::COMI;
6879 CC = ISD::SETLT;
6880 break;
6881 case Intrinsic::x86_sse_comile_ss:
6882 case Intrinsic::x86_sse2_comile_sd:
6883 Opc = X86ISD::COMI;
6884 CC = ISD::SETLE;
6885 break;
6886 case Intrinsic::x86_sse_comigt_ss:
6887 case Intrinsic::x86_sse2_comigt_sd:
6888 Opc = X86ISD::COMI;
6889 CC = ISD::SETGT;
6890 break;
6891 case Intrinsic::x86_sse_comige_ss:
6892 case Intrinsic::x86_sse2_comige_sd:
6893 Opc = X86ISD::COMI;
6894 CC = ISD::SETGE;
6895 break;
6896 case Intrinsic::x86_sse_comineq_ss:
6897 case Intrinsic::x86_sse2_comineq_sd:
6898 Opc = X86ISD::COMI;
6899 CC = ISD::SETNE;
6900 break;
6901 case Intrinsic::x86_sse_ucomieq_ss:
6902 case Intrinsic::x86_sse2_ucomieq_sd:
6903 Opc = X86ISD::UCOMI;
6904 CC = ISD::SETEQ;
6905 break;
6906 case Intrinsic::x86_sse_ucomilt_ss:
6907 case Intrinsic::x86_sse2_ucomilt_sd:
6908 Opc = X86ISD::UCOMI;
6909 CC = ISD::SETLT;
6910 break;
6911 case Intrinsic::x86_sse_ucomile_ss:
6912 case Intrinsic::x86_sse2_ucomile_sd:
6913 Opc = X86ISD::UCOMI;
6914 CC = ISD::SETLE;
6915 break;
6916 case Intrinsic::x86_sse_ucomigt_ss:
6917 case Intrinsic::x86_sse2_ucomigt_sd:
6918 Opc = X86ISD::UCOMI;
6919 CC = ISD::SETGT;
6920 break;
6921 case Intrinsic::x86_sse_ucomige_ss:
6922 case Intrinsic::x86_sse2_ucomige_sd:
6923 Opc = X86ISD::UCOMI;
6924 CC = ISD::SETGE;
6925 break;
6926 case Intrinsic::x86_sse_ucomineq_ss:
6927 case Intrinsic::x86_sse2_ucomineq_sd:
6928 Opc = X86ISD::UCOMI;
6929 CC = ISD::SETNE;
6930 break;
6931 }
6932
Dan Gohman8181bd12008-07-27 21:46:04 +00006933 SDValue LHS = Op.getOperand(1);
6934 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00006935 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006936 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006937 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6938 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6939 DAG.getConstant(X86CC, MVT::i8), Cond);
6940 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006941 }
Eric Christopher95d79262009-07-29 00:28:05 +00006942 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher79e0e8b2009-07-29 01:01:19 +00006943 // an integer value, not just an instruction so lower it to the ptest
6944 // pattern and a setcc for the result.
Eric Christopher95d79262009-07-29 00:28:05 +00006945 case Intrinsic::x86_sse41_ptestz:
6946 case Intrinsic::x86_sse41_ptestc:
6947 case Intrinsic::x86_sse41_ptestnzc:{
6948 unsigned X86CC = 0;
6949 switch (IntNo) {
Eric Christopher6612b082009-07-29 18:14:04 +00006950 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher95d79262009-07-29 00:28:05 +00006951 case Intrinsic::x86_sse41_ptestz:
6952 // ZF = 1
6953 X86CC = X86::COND_E;
6954 break;
6955 case Intrinsic::x86_sse41_ptestc:
6956 // CF = 1
6957 X86CC = X86::COND_B;
6958 break;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00006959 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher95d79262009-07-29 00:28:05 +00006960 // ZF and CF = 0
6961 X86CC = X86::COND_A;
6962 break;
6963 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00006964
Eric Christopher95d79262009-07-29 00:28:05 +00006965 SDValue LHS = Op.getOperand(1);
6966 SDValue RHS = Op.getOperand(2);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006967 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6968 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6969 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6970 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher95d79262009-07-29 00:28:05 +00006971 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006972
6973 // Fix vector shift instructions where the last operand is a non-immediate
6974 // i32 value.
6975 case Intrinsic::x86_sse2_pslli_w:
6976 case Intrinsic::x86_sse2_pslli_d:
6977 case Intrinsic::x86_sse2_pslli_q:
6978 case Intrinsic::x86_sse2_psrli_w:
6979 case Intrinsic::x86_sse2_psrli_d:
6980 case Intrinsic::x86_sse2_psrli_q:
6981 case Intrinsic::x86_sse2_psrai_w:
6982 case Intrinsic::x86_sse2_psrai_d:
6983 case Intrinsic::x86_mmx_pslli_w:
6984 case Intrinsic::x86_mmx_pslli_d:
6985 case Intrinsic::x86_mmx_pslli_q:
6986 case Intrinsic::x86_mmx_psrli_w:
6987 case Intrinsic::x86_mmx_psrli_d:
6988 case Intrinsic::x86_mmx_psrli_q:
6989 case Intrinsic::x86_mmx_psrai_w:
6990 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006991 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006992 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006993 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006994
6995 unsigned NewIntNo = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006996 EVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006997 switch (IntNo) {
6998 case Intrinsic::x86_sse2_pslli_w:
6999 NewIntNo = Intrinsic::x86_sse2_psll_w;
7000 break;
7001 case Intrinsic::x86_sse2_pslli_d:
7002 NewIntNo = Intrinsic::x86_sse2_psll_d;
7003 break;
7004 case Intrinsic::x86_sse2_pslli_q:
7005 NewIntNo = Intrinsic::x86_sse2_psll_q;
7006 break;
7007 case Intrinsic::x86_sse2_psrli_w:
7008 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7009 break;
7010 case Intrinsic::x86_sse2_psrli_d:
7011 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7012 break;
7013 case Intrinsic::x86_sse2_psrli_q:
7014 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7015 break;
7016 case Intrinsic::x86_sse2_psrai_w:
7017 NewIntNo = Intrinsic::x86_sse2_psra_w;
7018 break;
7019 case Intrinsic::x86_sse2_psrai_d:
7020 NewIntNo = Intrinsic::x86_sse2_psra_d;
7021 break;
7022 default: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007023 ShAmtVT = MVT::v2i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00007024 switch (IntNo) {
7025 case Intrinsic::x86_mmx_pslli_w:
7026 NewIntNo = Intrinsic::x86_mmx_psll_w;
7027 break;
7028 case Intrinsic::x86_mmx_pslli_d:
7029 NewIntNo = Intrinsic::x86_mmx_psll_d;
7030 break;
7031 case Intrinsic::x86_mmx_pslli_q:
7032 NewIntNo = Intrinsic::x86_mmx_psll_q;
7033 break;
7034 case Intrinsic::x86_mmx_psrli_w:
7035 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7036 break;
7037 case Intrinsic::x86_mmx_psrli_d:
7038 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7039 break;
7040 case Intrinsic::x86_mmx_psrli_q:
7041 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7042 break;
7043 case Intrinsic::x86_mmx_psrai_w:
7044 NewIntNo = Intrinsic::x86_mmx_psra_w;
7045 break;
7046 case Intrinsic::x86_mmx_psrai_d:
7047 NewIntNo = Intrinsic::x86_mmx_psra_d;
7048 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00007049 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00007050 }
7051 break;
7052 }
7053 }
Mon P Wang04c767e2009-09-03 19:56:25 +00007054
7055 // The vector shift intrinsics with scalars uses 32b shift amounts but
7056 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7057 // to be zero.
7058 SDValue ShOps[4];
7059 ShOps[0] = ShAmt;
7060 ShOps[1] = DAG.getConstant(0, MVT::i32);
7061 if (ShAmtVT == MVT::v4i32) {
7062 ShOps[2] = DAG.getUNDEF(MVT::i32);
7063 ShOps[3] = DAG.getUNDEF(MVT::i32);
7064 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7065 } else {
7066 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7067 }
7068
Owen Andersonac9de032009-08-10 22:56:29 +00007069 EVT VT = Op.getValueType();
Mon P Wang04c767e2009-09-03 19:56:25 +00007070 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007071 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007072 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng9f69f9d2008-05-04 09:15:50 +00007073 Op.getOperand(1), ShAmt);
7074 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007075 }
7076}
7077
Dan Gohman8181bd12008-07-27 21:46:04 +00007078SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00007079 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007080 DebugLoc dl = Op.getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00007081
7082 if (Depth > 0) {
7083 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7084 SDValue Offset =
7085 DAG.getConstant(TD->getPointerSize(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007086 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007087 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michel91099d62009-02-17 22:15:04 +00007088 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007089 FrameAddr, Offset),
David Greene25160362010-02-15 16:53:33 +00007090 NULL, 0, false, false, 0);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00007091 }
7092
7093 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00007094 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michel91099d62009-02-17 22:15:04 +00007095 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene25160362010-02-15 16:53:33 +00007096 RetAddrFI, NULL, 0, false, false, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007097}
7098
Dan Gohman8181bd12008-07-27 21:46:04 +00007099SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00007100 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7101 MFI->setFrameAddressIsTaken(true);
Owen Andersonac9de032009-08-10 22:56:29 +00007102 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007103 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng33633672008-09-27 01:56:22 +00007104 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7105 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007106 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng33633672008-09-27 01:56:22 +00007107 while (Depth--)
David Greene25160362010-02-15 16:53:33 +00007108 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7109 false, false, 0);
Evan Cheng33633672008-09-27 01:56:22 +00007110 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007111}
7112
Dan Gohman8181bd12008-07-27 21:46:04 +00007113SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00007114 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00007115 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007116}
7117
Dan Gohman8181bd12008-07-27 21:46:04 +00007118SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007119{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007120 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00007121 SDValue Chain = Op.getOperand(0);
7122 SDValue Offset = Op.getOperand(1);
7123 SDValue Handler = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007124 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007125
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00007126 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7127 getPointerTy());
7128 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007129
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007130 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00007131 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007132 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene25160362010-02-15 16:53:33 +00007133 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007134 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00007135 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007136
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007137 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007138 MVT::Other,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00007139 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007140}
7141
Dan Gohman8181bd12008-07-27 21:46:04 +00007142SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007143 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007144 SDValue Root = Op.getOperand(0);
7145 SDValue Trmp = Op.getOperand(1); // trampoline
7146 SDValue FPtr = Op.getOperand(2); // nested function
7147 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007148 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007149
Dan Gohman12a9c082008-02-06 22:27:42 +00007150 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007151
7152 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007153 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007154
7155 // Large code-model.
Chris Lattner0b4334c2010-02-05 19:20:30 +00007156 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7157 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007158
Dan Gohmanb41dfba2008-05-14 01:58:56 +00007159 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7160 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007161
7162 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7163
7164 // Load the pointer to the nested function into R11.
7165 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00007166 SDValue Addr = Trmp;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007167 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene25160362010-02-15 16:53:33 +00007168 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007169
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007170 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7171 DAG.getConstant(2, MVT::i64));
David Greene25160362010-02-15 16:53:33 +00007172 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7173 false, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007174
7175 // Load the 'nest' parameter value into R10.
7176 // R10 is specified in X86CallingConv.td
7177 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007178 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7179 DAG.getConstant(10, MVT::i64));
7180 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene25160362010-02-15 16:53:33 +00007181 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007182
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007183 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7184 DAG.getConstant(12, MVT::i64));
David Greene25160362010-02-15 16:53:33 +00007185 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7186 false, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007187
7188 // Jump to the nested function.
7189 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007190 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7191 DAG.getConstant(20, MVT::i64));
7192 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene25160362010-02-15 16:53:33 +00007193 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007194
7195 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007196 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7197 DAG.getConstant(22, MVT::i64));
7198 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene25160362010-02-15 16:53:33 +00007199 TrmpAddr, 22, false, false, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007200
Dan Gohman8181bd12008-07-27 21:46:04 +00007201 SDValue Ops[] =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007202 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007203 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007204 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00007205 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007206 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel5838baa2009-09-02 08:44:58 +00007207 CallingConv::ID CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00007208 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007209
7210 switch (CC) {
7211 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00007212 llvm_unreachable("Unsupported calling convention");
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007213 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007214 case CallingConv::X86_StdCall: {
7215 // Pass 'nest' parameter in ECX.
7216 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00007217 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007218
7219 // Check that ECX wasn't needed by an 'inreg' parameter.
7220 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00007221 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007222
Chris Lattner1c8733e2008-03-12 17:45:29 +00007223 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007224 unsigned InRegCount = 0;
7225 unsigned Idx = 1;
7226
7227 for (FunctionType::param_iterator I = FTy->param_begin(),
7228 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00007229 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007230 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00007231 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007232
7233 if (InRegCount > 2) {
Edwin Török3cb88482009-07-08 18:01:40 +00007234 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007235 }
7236 }
7237 break;
7238 }
7239 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00007240 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007241 // Pass 'nest' parameter in EAX.
7242 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00007243 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007244 break;
7245 }
7246
Dan Gohman8181bd12008-07-27 21:46:04 +00007247 SDValue OutChains[4];
7248 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007249
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007250 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7251 DAG.getConstant(10, MVT::i32));
7252 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007253
Chris Lattner0b4334c2010-02-05 19:20:30 +00007254 // This is storing the opcode for MOV32ri.
7255 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanb41dfba2008-05-14 01:58:56 +00007256 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michel91099d62009-02-17 22:15:04 +00007257 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007258 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene25160362010-02-15 16:53:33 +00007259 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007260
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007261 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7262 DAG.getConstant(1, MVT::i32));
David Greene25160362010-02-15 16:53:33 +00007263 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7264 false, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007265
Chris Lattner0b4334c2010-02-05 19:20:30 +00007266 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007267 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7268 DAG.getConstant(5, MVT::i32));
7269 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene25160362010-02-15 16:53:33 +00007270 TrmpAddr, 5, false, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007271
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007272 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7273 DAG.getConstant(6, MVT::i32));
David Greene25160362010-02-15 16:53:33 +00007274 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7275 false, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007276
Dan Gohman8181bd12008-07-27 21:46:04 +00007277 SDValue Ops[] =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007278 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007279 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007280 }
7281}
7282
Dan Gohman8181bd12008-07-27 21:46:04 +00007283SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007284 /*
7285 The rounding mode is in bits 11:10 of FPSR, and has the following
7286 settings:
7287 00 Round to nearest
7288 01 Round to -inf
7289 10 Round to +inf
7290 11 Round to 0
7291
7292 FLT_ROUNDS, on the other hand, expects the following:
7293 -1 Undefined
7294 0 Round to 0
7295 1 Round to nearest
7296 2 Round to +inf
7297 3 Round to -inf
7298
7299 To perform the conversion, we do:
7300 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7301 */
7302
7303 MachineFunction &MF = DAG.getMachineFunction();
7304 const TargetMachine &TM = MF.getTarget();
7305 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7306 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersonac9de032009-08-10 22:56:29 +00007307 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007308 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007309
7310 // Save FP Control Word to stack slot
David Greene6424ab92009-11-12 20:49:22 +00007311 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00007312 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007313
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007314 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00007315 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007316
7317 // Load FP Control Word from stack slot
David Greene25160362010-02-15 16:53:33 +00007318 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7319 false, false, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007320
7321 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00007322 SDValue CWD1 =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007323 DAG.getNode(ISD::SRL, dl, MVT::i16,
7324 DAG.getNode(ISD::AND, dl, MVT::i16,
7325 CWD, DAG.getConstant(0x800, MVT::i16)),
7326 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00007327 SDValue CWD2 =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007328 DAG.getNode(ISD::SRL, dl, MVT::i16,
7329 DAG.getNode(ISD::AND, dl, MVT::i16,
7330 CWD, DAG.getConstant(0x400, MVT::i16)),
7331 DAG.getConstant(9, MVT::i8));
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007332
Dan Gohman8181bd12008-07-27 21:46:04 +00007333 SDValue RetVal =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007334 DAG.getNode(ISD::AND, dl, MVT::i16,
7335 DAG.getNode(ISD::ADD, dl, MVT::i16,
7336 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7337 DAG.getConstant(1, MVT::i16)),
7338 DAG.getConstant(3, MVT::i16));
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007339
7340
Duncan Sands92c43912008-06-06 12:08:01 +00007341 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen24dd9a52009-02-07 00:55:49 +00007342 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007343}
7344
Dan Gohman8181bd12008-07-27 21:46:04 +00007345SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007346 EVT VT = Op.getValueType();
7347 EVT OpVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00007348 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007349 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00007350
7351 Op = Op.getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007352 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007353 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007354 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007355 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007356 }
Evan Cheng48679f42007-12-14 02:13:44 +00007357
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007358 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007359 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007360 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007361
7362 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer65f60c92009-12-29 16:57:26 +00007363 SDValue Ops[] = {
7364 Op,
7365 DAG.getConstant(NumBits+NumBits-1, OpVT),
7366 DAG.getConstant(X86::COND_E, MVT::i8),
7367 Op.getValue(1)
7368 };
7369 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007370
7371 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007372 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007373
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007374 if (VT == MVT::i8)
7375 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007376 return Op;
7377}
7378
Dan Gohman8181bd12008-07-27 21:46:04 +00007379SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007380 EVT VT = Op.getValueType();
7381 EVT OpVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00007382 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007383 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00007384
7385 Op = Op.getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007386 if (VT == MVT::i8) {
7387 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007388 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007389 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007390
7391 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007392 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007393 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007394
7395 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer65f60c92009-12-29 16:57:26 +00007396 SDValue Ops[] = {
7397 Op,
7398 DAG.getConstant(NumBits, OpVT),
7399 DAG.getConstant(X86::COND_E, MVT::i8),
7400 Op.getValue(1)
7401 };
7402 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007403
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007404 if (VT == MVT::i8)
7405 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007406 return Op;
7407}
7408
Mon P Wang14edb092008-12-18 21:42:19 +00007409SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007410 EVT VT = Op.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007411 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007412 DebugLoc dl = Op.getDebugLoc();
Scott Michel91099d62009-02-17 22:15:04 +00007413
Mon P Wang14edb092008-12-18 21:42:19 +00007414 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7415 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7416 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7417 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7418 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7419 //
7420 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7421 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7422 // return AloBlo + AloBhi + AhiBlo;
7423
7424 SDValue A = Op.getOperand(0);
7425 SDValue B = Op.getOperand(1);
Scott Michel91099d62009-02-17 22:15:04 +00007426
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007427 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007428 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7429 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007430 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007431 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7432 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007433 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007434 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007435 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007436 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007437 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007438 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007439 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007440 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007441 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007442 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007443 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7444 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007445 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007446 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7447 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007448 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7449 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00007450 return Res;
7451}
7452
7453
Bill Wendling7e04be62008-12-09 22:08:41 +00007454SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7455 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7456 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00007457 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7458 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00007459 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00007460 SDValue LHS = N->getOperand(0);
7461 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00007462 unsigned BaseOp = 0;
7463 unsigned Cond = 0;
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007464 DebugLoc dl = Op.getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00007465
7466 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00007467 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling7e04be62008-12-09 22:08:41 +00007468 case ISD::SADDO:
Dan Gohman99a12192009-03-04 19:44:21 +00007469 // A subtract of one will be selected as a INC. Note that INC doesn't
7470 // set CF, so we can't do this for UADDO.
7471 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7472 if (C->getAPIntValue() == 1) {
7473 BaseOp = X86ISD::INC;
7474 Cond = X86::COND_O;
7475 break;
7476 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00007477 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00007478 Cond = X86::COND_O;
7479 break;
7480 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00007481 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007482 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007483 break;
7484 case ISD::SSUBO:
Dan Gohman99a12192009-03-04 19:44:21 +00007485 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7486 // set CF, so we can't do this for USUBO.
7487 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7488 if (C->getAPIntValue() == 1) {
7489 BaseOp = X86ISD::DEC;
7490 Cond = X86::COND_O;
7491 break;
7492 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00007493 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00007494 Cond = X86::COND_O;
7495 break;
7496 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00007497 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007498 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007499 break;
7500 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00007501 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00007502 Cond = X86::COND_O;
7503 break;
7504 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00007505 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007506 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007507 break;
7508 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00007509
Bill Wendlingd3511522008-12-02 01:06:39 +00007510 // Also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007511 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007512 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00007513
Bill Wendlingd3511522008-12-02 01:06:39 +00007514 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007515 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007516 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00007517
Bill Wendlingd3511522008-12-02 01:06:39 +00007518 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7519 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00007520}
7521
Dan Gohman8181bd12008-07-27 21:46:04 +00007522SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007523 EVT T = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007524 DebugLoc dl = Op.getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00007525 unsigned Reg = 0;
7526 unsigned size = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007527 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands92c43912008-06-06 12:08:01 +00007528 default:
7529 assert(false && "Invalid value type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007530 case MVT::i8: Reg = X86::AL; size = 1; break;
7531 case MVT::i16: Reg = X86::AX; size = 2; break;
7532 case MVT::i32: Reg = X86::EAX; size = 4; break;
7533 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007534 assert(Subtarget->is64Bit() && "Node not type legal!");
7535 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00007536 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00007537 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007538 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00007539 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00007540 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00007541 Op.getOperand(1),
7542 Op.getOperand(3),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007543 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng6617eed2008-09-24 23:26:36 +00007544 cpIn.getValue(1) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007545 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007546 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michel91099d62009-02-17 22:15:04 +00007547 SDValue cpOut =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007548 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00007549 return cpOut;
7550}
7551
Duncan Sands7d9834b2008-12-01 11:39:25 +00007552SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00007553 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00007554 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007555 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007556 SDValue TheChain = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007557 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007558 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007559 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7560 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007561 rax.getValue(2));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007562 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7563 DAG.getConstant(32, MVT::i8));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007564 SDValue Ops[] = {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007565 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00007566 rdx.getValue(1)
7567 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007568 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00007569}
7570
Dale Johannesen9011d872008-09-29 22:25:26 +00007571SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7572 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007573 DebugLoc dl = Node->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00007574 EVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007575 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Chengef356282009-02-23 09:03:22 +00007576 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007577 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007578 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00007579 Node->getOperand(0),
7580 Node->getOperand(1), negOp,
7581 cast<AtomicSDNode>(Node)->getSrcValue(),
7582 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00007583}
7584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007585/// LowerOperation - Provide custom lowering hooks for some operations.
7586///
Dan Gohman8181bd12008-07-27 21:46:04 +00007587SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007588 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00007589 default: llvm_unreachable("Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007590 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7591 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007592 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wanga8ff0dd2010-01-24 00:05:03 +00007593 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007594 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7595 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7596 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7597 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7598 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7599 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
7600 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00007601 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohman064403e2009-10-30 01:28:02 +00007602 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007603 case ISD::SHL_PARTS:
7604 case ISD::SRA_PARTS:
7605 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7606 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00007607 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007608 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman8c3cb582009-05-23 09:59:16 +00007609 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007610 case ISD::FABS: return LowerFABS(Op, DAG);
7611 case ISD::FNEG: return LowerFNEG(Op, DAG);
7612 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00007613 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00007614 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00007615 case ISD::SELECT: return LowerSELECT(Op, DAG);
7616 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007617 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007618 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00007619 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007620 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
7621 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
7622 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7623 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
7624 case ISD::FRAME_TO_ARGS_OFFSET:
7625 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
7626 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
7627 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007628 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00007629 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00007630 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7631 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00007632 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00007633 case ISD::SADDO:
7634 case ISD::UADDO:
7635 case ISD::SSUBO:
7636 case ISD::USUBO:
7637 case ISD::SMULO:
7638 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007639 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007640 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007641}
7642
Duncan Sands7d9834b2008-12-01 11:39:25 +00007643void X86TargetLowering::
7644ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7645 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersonac9de032009-08-10 22:56:29 +00007646 EVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007647 DebugLoc dl = Node->getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007648 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands7d9834b2008-12-01 11:39:25 +00007649
7650 SDValue Chain = Node->getOperand(0);
7651 SDValue In1 = Node->getOperand(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007652 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007653 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007654 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007655 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007656 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007657 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007658 SDValue Result =
7659 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7660 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands7d9834b2008-12-01 11:39:25 +00007661 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007662 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007663 Results.push_back(Result.getValue(2));
7664}
7665
Duncan Sandsac496a12008-07-04 11:47:58 +00007666/// ReplaceNodeResults - Replace a node with an illegal result type
7667/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00007668void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7669 SmallVectorImpl<SDValue>&Results,
7670 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007671 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007672 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00007673 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007674 assert(false && "Do not know how to custom type legalize this operation!");
7675 return;
7676 case ISD::FP_TO_SINT: {
Eli Friedman8c3cb582009-05-23 09:59:16 +00007677 std::pair<SDValue,SDValue> Vals =
7678 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007679 SDValue FIST = Vals.first, StackSlot = Vals.second;
7680 if (FIST.getNode() != 0) {
Owen Andersonac9de032009-08-10 22:56:29 +00007681 EVT VT = N->getValueType(0);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007682 // Return a load from the stack slot.
David Greene25160362010-02-15 16:53:33 +00007683 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7684 false, false, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007685 }
7686 return;
7687 }
7688 case ISD::READCYCLECOUNTER: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007689 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007690 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007691 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007692 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007693 rd.getValue(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007694 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007695 eax.getValue(2));
7696 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7697 SDValue Ops[] = { eax, edx };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007698 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007699 Results.push_back(edx.getValue(1));
7700 return;
7701 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007702 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersonac9de032009-08-10 22:56:29 +00007703 EVT T = N->getValueType(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007704 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands7d9834b2008-12-01 11:39:25 +00007705 SDValue cpInL, cpInH;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007706 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7707 DAG.getConstant(0, MVT::i32));
7708 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7709 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007710 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7711 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007712 cpInL.getValue(1));
7713 SDValue swapInL, swapInH;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007714 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7715 DAG.getConstant(0, MVT::i32));
7716 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7717 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007718 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007719 cpInH.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007720 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007721 swapInL.getValue(1));
7722 SDValue Ops[] = { swapInH.getValue(0),
7723 N->getOperand(1),
7724 swapInH.getValue(1) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007725 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007726 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007727 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007728 MVT::i32, Result.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007729 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007730 MVT::i32, cpOutL.getValue(2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007731 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007732 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007733 Results.push_back(cpOutH.getValue(1));
7734 return;
7735 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007736 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007737 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7738 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007739 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007740 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7741 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007742 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007743 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7744 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007745 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007746 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7747 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007748 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007749 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7750 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007751 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007752 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7753 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007754 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007755 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7756 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007757 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007758}
7759
7760const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7761 switch (Opcode) {
7762 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00007763 case X86ISD::BSF: return "X86ISD::BSF";
7764 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007765 case X86ISD::SHLD: return "X86ISD::SHLD";
7766 case X86ISD::SHRD: return "X86ISD::SHRD";
7767 case X86ISD::FAND: return "X86ISD::FAND";
7768 case X86ISD::FOR: return "X86ISD::FOR";
7769 case X86ISD::FXOR: return "X86ISD::FXOR";
7770 case X86ISD::FSRL: return "X86ISD::FSRL";
7771 case X86ISD::FILD: return "X86ISD::FILD";
7772 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
7773 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7774 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7775 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
7776 case X86ISD::FLD: return "X86ISD::FLD";
7777 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007778 case X86ISD::CALL: return "X86ISD::CALL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007779 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00007780 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007781 case X86ISD::CMP: return "X86ISD::CMP";
7782 case X86ISD::COMI: return "X86ISD::COMI";
7783 case X86ISD::UCOMI: return "X86ISD::UCOMI";
7784 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng834ae6b2009-12-15 00:53:42 +00007785 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007786 case X86ISD::CMOV: return "X86ISD::CMOV";
7787 case X86ISD::BRCOND: return "X86ISD::BRCOND";
7788 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
7789 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7790 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007791 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
7792 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattnerdc6fc472009-06-27 04:16:01 +00007793 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begemand77e59e2008-02-11 04:19:36 +00007794 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007795 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00007796 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7797 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007798 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner5fc65c52010-02-23 02:07:48 +00007799 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begeman2c87c422009-02-23 08:49:38 +00007800 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007801 case X86ISD::FMAX: return "X86ISD::FMAX";
7802 case X86ISD::FMIN: return "X86ISD::FMIN";
7803 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7804 case X86ISD::FRCP: return "X86ISD::FRCP";
7805 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindolabca99f72009-04-08 21:14:34 +00007806 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007807 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00007808 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007809 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00007810 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7811 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00007812 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7813 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7814 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7815 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7816 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7817 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00007818 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7819 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00007820 case X86ISD::VSHL: return "X86ISD::VSHL";
7821 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00007822 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7823 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7824 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7825 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7826 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7827 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7828 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7829 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7830 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7831 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00007832 case X86ISD::ADD: return "X86ISD::ADD";
7833 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00007834 case X86ISD::SMUL: return "X86ISD::SMUL";
7835 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman99a12192009-03-04 19:44:21 +00007836 case X86ISD::INC: return "X86ISD::INC";
7837 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohman12e03292009-09-18 19:59:53 +00007838 case X86ISD::OR: return "X86ISD::OR";
7839 case X86ISD::XOR: return "X86ISD::XOR";
7840 case X86ISD::AND: return "X86ISD::AND";
Evan Chengc3495762009-03-30 21:36:47 +00007841 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher95d79262009-07-29 00:28:05 +00007842 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohman34228bf2009-08-15 01:38:56 +00007843 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov7cd32422010-03-06 19:32:29 +00007844 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007845 }
7846}
7847
7848// isLegalAddressingMode - Return true if the addressing mode represented
7849// by AM is legal for this target, for a load/store of the specified type.
Scott Michel91099d62009-02-17 22:15:04 +00007850bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007851 const Type *Ty) const {
7852 // X86 supports extremely general addressing modes.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007853 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michel91099d62009-02-17 22:15:04 +00007854
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007855 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007856 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007857 return false;
Scott Michel91099d62009-02-17 22:15:04 +00007858
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007859 if (AM.BaseGV) {
Chris Lattner01e39942009-07-10 07:38:24 +00007860 unsigned GVFlags =
7861 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007862
Chris Lattner01e39942009-07-10 07:38:24 +00007863 // If a reference to this global requires an extra load, we can't fold it.
7864 if (isGlobalStubReference(GVFlags))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007865 return false;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007866
Chris Lattner01e39942009-07-10 07:38:24 +00007867 // If BaseGV requires a register for the PIC base, we cannot also have a
7868 // BaseReg specified.
7869 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen64660e92008-12-05 21:47:27 +00007870 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00007871
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007872 // If lower 4G is not available, then we must use rip-relative addressing.
7873 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7874 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007875 }
Scott Michel91099d62009-02-17 22:15:04 +00007876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007877 switch (AM.Scale) {
7878 case 0:
7879 case 1:
7880 case 2:
7881 case 4:
7882 case 8:
7883 // These scales always work.
7884 break;
7885 case 3:
7886 case 5:
7887 case 9:
7888 // These scales are formed with basereg+scalereg. Only accept if there is
7889 // no basereg yet.
7890 if (AM.HasBaseReg)
7891 return false;
7892 break;
7893 default: // Other stuff never works.
7894 return false;
7895 }
Scott Michel91099d62009-02-17 22:15:04 +00007896
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007897 return true;
7898}
7899
7900
Evan Cheng27a820a2007-10-26 01:56:11 +00007901bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandse92dee12010-02-15 16:12:20 +00007902 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng27a820a2007-10-26 01:56:11 +00007903 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00007904 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7905 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00007906 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00007907 return false;
Dan Gohman9e2bdca2010-02-25 03:04:36 +00007908 return true;
Evan Cheng27a820a2007-10-26 01:56:11 +00007909}
7910
Owen Andersonac9de032009-08-10 22:56:29 +00007911bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands92c43912008-06-06 12:08:01 +00007912 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00007913 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00007914 unsigned NumBits1 = VT1.getSizeInBits();
7915 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00007916 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00007917 return false;
Dan Gohman9e2bdca2010-02-25 03:04:36 +00007918 return true;
Evan Cheng9decb332007-10-29 19:58:20 +00007919}
Evan Cheng27a820a2007-10-26 01:56:11 +00007920
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007921bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00007922 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandse92dee12010-02-15 16:12:20 +00007923 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007924}
7925
Owen Andersonac9de032009-08-10 22:56:29 +00007926bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00007927 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007928 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007929}
7930
Owen Andersonac9de032009-08-10 22:56:29 +00007931bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng2f5d3a52009-05-28 00:35:15 +00007932 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007933 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00007934}
7935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007936/// isShuffleMaskLegal - Targets can use this to indicate that they only
7937/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7938/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7939/// are assumed to be legal.
7940bool
Eric Christopher3d82bbd2009-08-27 18:07:15 +00007941X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersonac9de032009-08-10 22:56:29 +00007942 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007943 // Only do shuffles on 128-bit vector types for now.
Nate Begeman543d2142009-04-27 18:41:29 +00007944 if (VT.getSizeInBits() == 64)
7945 return false;
7946
Nate Begeman080f8e22009-10-19 02:17:23 +00007947 // FIXME: pshufb, blends, shifts.
Nate Begeman543d2142009-04-27 18:41:29 +00007948 return (VT.getVectorNumElements() == 2 ||
7949 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7950 isMOVLMask(M, VT) ||
7951 isSHUFPMask(M, VT) ||
7952 isPSHUFDMask(M, VT) ||
7953 isPSHUFHWMask(M, VT) ||
7954 isPSHUFLWMask(M, VT) ||
Nate Begeman080f8e22009-10-19 02:17:23 +00007955 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman543d2142009-04-27 18:41:29 +00007956 isUNPCKLMask(M, VT) ||
7957 isUNPCKHMask(M, VT) ||
7958 isUNPCKL_v_undef_Mask(M, VT) ||
7959 isUNPCKH_v_undef_Mask(M, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007960}
7961
Dan Gohman48d5f062008-04-09 20:09:42 +00007962bool
Nate Begemane8f61cb2009-04-29 05:20:52 +00007963X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersonac9de032009-08-10 22:56:29 +00007964 EVT VT) const {
Nate Begeman543d2142009-04-27 18:41:29 +00007965 unsigned NumElts = VT.getVectorNumElements();
7966 // FIXME: This collection of masks seems suspect.
7967 if (NumElts == 2)
7968 return true;
7969 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7970 return (isMOVLMask(Mask, VT) ||
7971 isCommutedMOVLMask(Mask, VT, true) ||
7972 isSHUFPMask(Mask, VT) ||
7973 isCommutedSHUFPMask(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007974 }
7975 return false;
7976}
7977
7978//===----------------------------------------------------------------------===//
7979// X86 Scheduler Hooks
7980//===----------------------------------------------------------------------===//
7981
Mon P Wang078a62d2008-05-05 19:05:59 +00007982// private utility function
7983MachineBasicBlock *
7984X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7985 MachineBasicBlock *MBB,
7986 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007987 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00007988 unsigned LoadOpc,
7989 unsigned CXchgOpc,
7990 unsigned copyOpc,
7991 unsigned notOpc,
7992 unsigned EAXreg,
7993 TargetRegisterClass *RC,
Dan Gohman96d60922009-02-07 16:15:20 +00007994 bool invSrc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00007995 // For the atomic bitwise operator, we generate
7996 // thisMBB:
7997 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007998 // ld t1 = [bitinstr.addr]
7999 // op t2 = t1, [bitinstr.val]
8000 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00008001 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8002 // bz newMBB
8003 // fallthrough -->nextMBB
8004 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8005 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00008006 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00008007 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00008008
Mon P Wang078a62d2008-05-05 19:05:59 +00008009 /// First build the CFG
8010 MachineFunction *F = MBB->getParent();
8011 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00008012 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8013 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8014 F->insert(MBBIter, newMBB);
8015 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008016
Mon P Wang078a62d2008-05-05 19:05:59 +00008017 // Move all successors to thisMBB to nextMBB
8018 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008019
Mon P Wang078a62d2008-05-05 19:05:59 +00008020 // Update thisMBB to fall through to newMBB
8021 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008022
Mon P Wang078a62d2008-05-05 19:05:59 +00008023 // newMBB jumps to itself and fall through to nextMBB
8024 newMBB->addSuccessor(nextMBB);
8025 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008026
Mon P Wang078a62d2008-05-05 19:05:59 +00008027 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008028 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00008029 "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008030 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00008031 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008032 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00008033 int numArgs = bInstr->getNumOperands() - 1;
8034 for (int i=0; i < numArgs; ++i)
8035 argOpers[i] = &bInstr->getOperand(i+1);
8036
8037 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008038 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8039 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00008040
Dale Johannesend20e4452008-08-19 18:47:28 +00008041 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008042 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00008043 for (int i=0; i <= lastAddrIndx; ++i)
8044 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008045
Dale Johannesend20e4452008-08-19 18:47:28 +00008046 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008047 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008048 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008049 }
Scott Michel91099d62009-02-17 22:15:04 +00008050 else
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008051 tt = t1;
8052
Dale Johannesend20e4452008-08-19 18:47:28 +00008053 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008054 assert((argOpers[valArgIndx]->isReg() ||
8055 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00008056 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008057 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008058 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00008059 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008060 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008061 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00008062 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008063
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008064 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00008065 MIB.addReg(t1);
Scott Michel91099d62009-02-17 22:15:04 +00008066
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008067 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00008068 for (int i=0; i <= lastAddrIndx; ++i)
8069 (*MIB).addOperand(*argOpers[i]);
8070 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00008071 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008072 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8073 bInstr->memoperands_end());
Mon P Wang50584a62008-07-17 04:54:06 +00008074
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008075 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00008076 MIB.addReg(EAXreg);
Scott Michel91099d62009-02-17 22:15:04 +00008077
Mon P Wang078a62d2008-05-05 19:05:59 +00008078 // insert branch
Chris Lattnerb112c022010-02-11 19:25:55 +00008079 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00008080
Dan Gohman221a4372008-07-07 23:14:23 +00008081 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00008082 return nextMBB;
8083}
8084
Dale Johannesen44eb5372008-10-03 19:41:08 +00008085// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00008086MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00008087X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8088 MachineBasicBlock *MBB,
8089 unsigned regOpcL,
8090 unsigned regOpcH,
8091 unsigned immOpcL,
8092 unsigned immOpcH,
Dan Gohman96d60922009-02-07 16:15:20 +00008093 bool invSrc) const {
Dale Johannesenf160d802008-10-02 18:53:47 +00008094 // For the atomic bitwise operator, we generate
8095 // thisMBB (instructions are in pairs, except cmpxchg8b)
8096 // ld t1,t2 = [bitinstr.addr]
8097 // newMBB:
8098 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8099 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008100 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00008101 // mov ECX, EBX <- t5, t6
8102 // mov EAX, EDX <- t1, t2
8103 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8104 // mov t3, t4 <- EAX, EDX
8105 // bz newMBB
8106 // result in out1, out2
8107 // fallthrough -->nextMBB
8108
8109 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8110 const unsigned LoadOpc = X86::MOV32rm;
8111 const unsigned copyOpc = X86::MOV32rr;
8112 const unsigned NotOpc = X86::NOT32r;
8113 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8114 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8115 MachineFunction::iterator MBBIter = MBB;
8116 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00008117
Dale Johannesenf160d802008-10-02 18:53:47 +00008118 /// First build the CFG
8119 MachineFunction *F = MBB->getParent();
8120 MachineBasicBlock *thisMBB = MBB;
8121 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8122 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8123 F->insert(MBBIter, newMBB);
8124 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008125
Dale Johannesenf160d802008-10-02 18:53:47 +00008126 // Move all successors to thisMBB to nextMBB
8127 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008128
Dale Johannesenf160d802008-10-02 18:53:47 +00008129 // Update thisMBB to fall through to newMBB
8130 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008131
Dale Johannesenf160d802008-10-02 18:53:47 +00008132 // newMBB jumps to itself and fall through to nextMBB
8133 newMBB->addSuccessor(nextMBB);
8134 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008135
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008136 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00008137 // Insert instructions into newMBB based on incoming instruction
8138 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008139 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00008140 "unexpected number of operands");
Dale Johannesenf160d802008-10-02 18:53:47 +00008141 MachineOperand& dest1Oper = bInstr->getOperand(0);
8142 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008143 MachineOperand* argOpers[2 + X86AddrNumOperands];
8144 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00008145 argOpers[i] = &bInstr->getOperand(i+2);
8146
Evan Cheng4460e1b2010-01-08 19:14:57 +00008147 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008148 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michel91099d62009-02-17 22:15:04 +00008149
Dale Johannesenf160d802008-10-02 18:53:47 +00008150 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008151 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00008152 for (int i=0; i <= lastAddrIndx; ++i)
8153 (*MIB).addOperand(*argOpers[i]);
8154 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008155 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008156 // add 4 to displacement.
Rafael Espindolabca99f72009-04-08 21:14:34 +00008157 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00008158 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008159 MachineOperand newOp3 = *(argOpers[3]);
8160 if (newOp3.isImm())
8161 newOp3.setImm(newOp3.getImm()+4);
8162 else
8163 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00008164 (*MIB).addOperand(newOp3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00008165 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesenf160d802008-10-02 18:53:47 +00008166
8167 // t3/4 are defined later, at the bottom of the loop
8168 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8169 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008170 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00008171 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008172 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00008173 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8174
Evan Chengcdd58c32010-01-08 23:41:50 +00008175 // The subsequent operations should be using the destination registers of
8176 //the PHI instructions.
Scott Michel91099d62009-02-17 22:15:04 +00008177 if (invSrc) {
Evan Chengcdd58c32010-01-08 23:41:50 +00008178 t1 = F->getRegInfo().createVirtualRegister(RC);
8179 t2 = F->getRegInfo().createVirtualRegister(RC);
8180 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8181 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesenf160d802008-10-02 18:53:47 +00008182 } else {
Evan Chengcdd58c32010-01-08 23:41:50 +00008183 t1 = dest1Oper.getReg();
8184 t2 = dest2Oper.getReg();
Dale Johannesenf160d802008-10-02 18:53:47 +00008185 }
8186
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008187 int valArgIndx = lastAddrIndx + 1;
8188 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendlingc1946742009-05-30 01:09:53 +00008189 argOpers[valArgIndx]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00008190 "invalid operand");
8191 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8192 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008193 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008194 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00008195 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008196 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008197 if (regOpcL != X86::MOV32rr)
Evan Chengcdd58c32010-01-08 23:41:50 +00008198 MIB.addReg(t1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008199 (*MIB).addOperand(*argOpers[valArgIndx]);
8200 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendlingc1946742009-05-30 01:09:53 +00008201 argOpers[valArgIndx]->isReg());
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008202 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendlingc1946742009-05-30 01:09:53 +00008203 argOpers[valArgIndx]->isImm());
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008204 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008205 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00008206 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008207 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008208 if (regOpcH != X86::MOV32rr)
Evan Chengcdd58c32010-01-08 23:41:50 +00008209 MIB.addReg(t2);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008210 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesenf160d802008-10-02 18:53:47 +00008211
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008212 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008213 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008214 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008215 MIB.addReg(t2);
8216
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008217 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008218 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008219 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008220 MIB.addReg(t6);
Scott Michel91099d62009-02-17 22:15:04 +00008221
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008222 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00008223 for (int i=0; i <= lastAddrIndx; ++i)
8224 (*MIB).addOperand(*argOpers[i]);
8225
8226 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008227 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8228 bInstr->memoperands_end());
Dale Johannesenf160d802008-10-02 18:53:47 +00008229
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008230 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00008231 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008232 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00008233 MIB.addReg(X86::EDX);
Scott Michel91099d62009-02-17 22:15:04 +00008234
Dale Johannesenf160d802008-10-02 18:53:47 +00008235 // insert branch
Chris Lattnerb112c022010-02-11 19:25:55 +00008236 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00008237
8238 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8239 return nextMBB;
8240}
8241
8242// private utility function
8243MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00008244X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8245 MachineBasicBlock *MBB,
Dan Gohman96d60922009-02-07 16:15:20 +00008246 unsigned cmovOpc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00008247 // For the atomic min/max operator, we generate
8248 // thisMBB:
8249 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00008250 // ld t1 = [min/max.addr]
Scott Michel91099d62009-02-17 22:15:04 +00008251 // mov t2 = [min/max.val]
Mon P Wang078a62d2008-05-05 19:05:59 +00008252 // cmp t1, t2
8253 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00008254 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00008255 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8256 // bz newMBB
8257 // fallthrough -->nextMBB
8258 //
8259 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8260 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00008261 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00008262 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00008263
Mon P Wang078a62d2008-05-05 19:05:59 +00008264 /// First build the CFG
8265 MachineFunction *F = MBB->getParent();
8266 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00008267 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8268 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8269 F->insert(MBBIter, newMBB);
8270 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008271
Dan Gohman34228bf2009-08-15 01:38:56 +00008272 // Move all successors of thisMBB to nextMBB
Mon P Wang078a62d2008-05-05 19:05:59 +00008273 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008274
Mon P Wang078a62d2008-05-05 19:05:59 +00008275 // Update thisMBB to fall through to newMBB
8276 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008277
Mon P Wang078a62d2008-05-05 19:05:59 +00008278 // newMBB jumps to newMBB and fall through to nextMBB
8279 newMBB->addSuccessor(nextMBB);
8280 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008281
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008282 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00008283 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008284 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00008285 "unexpected number of operands");
Mon P Wang078a62d2008-05-05 19:05:59 +00008286 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008287 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00008288 int numArgs = mInstr->getNumOperands() - 1;
8289 for (int i=0; i < numArgs; ++i)
8290 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michel91099d62009-02-17 22:15:04 +00008291
Mon P Wang078a62d2008-05-05 19:05:59 +00008292 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008293 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8294 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00008295
Mon P Wang318b0372008-05-05 22:56:23 +00008296 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008297 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00008298 for (int i=0; i <= lastAddrIndx; ++i)
8299 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00008300
Mon P Wang078a62d2008-05-05 19:05:59 +00008301 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008302 assert((argOpers[valArgIndx]->isReg() ||
8303 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00008304 "invalid operand");
Scott Michel91099d62009-02-17 22:15:04 +00008305
8306 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008307 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008308 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michel91099d62009-02-17 22:15:04 +00008309 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008310 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00008311 (*MIB).addOperand(*argOpers[valArgIndx]);
8312
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008313 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00008314 MIB.addReg(t1);
8315
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008316 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00008317 MIB.addReg(t1);
8318 MIB.addReg(t2);
8319
8320 // Generate movc
8321 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008322 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00008323 MIB.addReg(t2);
8324 MIB.addReg(t1);
8325
8326 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008327 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00008328 for (int i=0; i <= lastAddrIndx; ++i)
8329 (*MIB).addOperand(*argOpers[i]);
8330 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00008331 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008332 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8333 mInstr->memoperands_end());
Scott Michel91099d62009-02-17 22:15:04 +00008334
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008335 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00008336 MIB.addReg(X86::EAX);
Scott Michel91099d62009-02-17 22:15:04 +00008337
Mon P Wang078a62d2008-05-05 19:05:59 +00008338 // insert branch
Chris Lattnerb112c022010-02-11 19:25:55 +00008339 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00008340
Dan Gohman221a4372008-07-07 23:14:23 +00008341 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00008342 return nextMBB;
8343}
8344
Eric Christopher20391ca62009-08-27 18:08:16 +00008345// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8346// all of this code can be replaced with that in the .td file.
Dan Gohman34228bf2009-08-15 01:38:56 +00008347MachineBasicBlock *
Eric Christopher22a39402009-08-18 22:50:32 +00008348X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008349 unsigned numArgs, bool memArg) const {
Eric Christopher22a39402009-08-18 22:50:32 +00008350
8351 MachineFunction *F = BB->getParent();
8352 DebugLoc dl = MI->getDebugLoc();
8353 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8354
8355 unsigned Opc;
Evan Cheng5f3a5402009-09-19 09:51:03 +00008356 if (memArg)
8357 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8358 else
8359 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopher22a39402009-08-18 22:50:32 +00008360
8361 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8362
8363 for (unsigned i = 0; i < numArgs; ++i) {
8364 MachineOperand &Op = MI->getOperand(i+1);
8365
8366 if (!(Op.isReg() && Op.isImplicit()))
8367 MIB.addOperand(Op);
8368 }
8369
8370 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8371 .addReg(X86::XMM0);
8372
8373 F->DeleteMachineInstr(MI);
8374
8375 return BB;
8376}
8377
8378MachineBasicBlock *
Dan Gohman34228bf2009-08-15 01:38:56 +00008379X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8380 MachineInstr *MI,
8381 MachineBasicBlock *MBB) const {
8382 // Emit code to save XMM registers to the stack. The ABI says that the
8383 // number of registers to save is given in %al, so it's theoretically
8384 // possible to do an indirect jump trick to avoid saving all of them,
8385 // however this code takes a simpler approach and just executes all
8386 // of the stores if %al is non-zero. It's less code, and it's probably
8387 // easier on the hardware branch predictor, and stores aren't all that
8388 // expensive anyway.
8389
8390 // Create the new basic blocks. One block contains all the XMM stores,
8391 // and one block is the final destination regardless of whether any
8392 // stores were performed.
8393 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8394 MachineFunction *F = MBB->getParent();
8395 MachineFunction::iterator MBBIter = MBB;
8396 ++MBBIter;
8397 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8398 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8399 F->insert(MBBIter, XMMSaveMBB);
8400 F->insert(MBBIter, EndMBB);
8401
8402 // Set up the CFG.
8403 // Move any original successors of MBB to the end block.
8404 EndMBB->transferSuccessors(MBB);
8405 // The original block will now fall through to the XMM save block.
8406 MBB->addSuccessor(XMMSaveMBB);
8407 // The XMMSaveMBB will fall through to the end block.
8408 XMMSaveMBB->addSuccessor(EndMBB);
8409
8410 // Now add the instructions.
8411 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8412 DebugLoc DL = MI->getDebugLoc();
8413
8414 unsigned CountReg = MI->getOperand(0).getReg();
8415 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8416 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8417
8418 if (!Subtarget->isTargetWin64()) {
8419 // If %al is 0, branch around the XMM save block.
8420 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerb112c022010-02-11 19:25:55 +00008421 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohman34228bf2009-08-15 01:38:56 +00008422 MBB->addSuccessor(EndMBB);
8423 }
8424
8425 // In the XMM save block, save all the XMM argument registers.
8426 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8427 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008428 MachineMemOperand *MMO =
Evan Cheng174e2cf2009-10-18 18:16:27 +00008429 F->getMachineMemOperand(
8430 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8431 MachineMemOperand::MOStore, Offset,
8432 /*Size=*/16, /*Align=*/16);
Dan Gohman34228bf2009-08-15 01:38:56 +00008433 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8434 .addFrameIndex(RegSaveFrameIndex)
8435 .addImm(/*Scale=*/1)
8436 .addReg(/*IndexReg=*/0)
8437 .addImm(/*Disp=*/Offset)
8438 .addReg(/*Segment=*/0)
8439 .addReg(MI->getOperand(i).getReg())
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008440 .addMemOperand(MMO);
Dan Gohman34228bf2009-08-15 01:38:56 +00008441 }
8442
8443 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8444
8445 return EndMBB;
8446}
Mon P Wang078a62d2008-05-05 19:05:59 +00008447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008448MachineBasicBlock *
Chris Lattner84a67202009-09-02 05:57:00 +00008449X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Cheng5f3a5402009-09-19 09:51:03 +00008450 MachineBasicBlock *BB,
8451 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner84a67202009-09-02 05:57:00 +00008452 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8453 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008454
Chris Lattner84a67202009-09-02 05:57:00 +00008455 // To "insert" a SELECT_CC instruction, we actually have to insert the
8456 // diamond control-flow pattern. The incoming instruction knows the
8457 // destination vreg to set, the condition code register to branch on, the
8458 // true/false values to select between, and a branch opcode to use.
8459 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8460 MachineFunction::iterator It = BB;
8461 ++It;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008462
Chris Lattner84a67202009-09-02 05:57:00 +00008463 // thisMBB:
8464 // ...
8465 // TrueVal = ...
8466 // cmpTY ccX, r1, r2
8467 // bCC copy1MBB
8468 // fallthrough --> copy0MBB
8469 MachineBasicBlock *thisMBB = BB;
8470 MachineFunction *F = BB->getParent();
8471 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8472 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8473 unsigned Opc =
8474 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8475 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8476 F->insert(It, copy0MBB);
8477 F->insert(It, sinkMBB);
Evan Cheng5f3a5402009-09-19 09:51:03 +00008478 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner84a67202009-09-02 05:57:00 +00008479 // block to the new block which will contain the Phi node for the select.
Evan Cheng5f3a5402009-09-19 09:51:03 +00008480 // Also inform sdisel of the edge changes.
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008481 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Cheng5f3a5402009-09-19 09:51:03 +00008482 E = BB->succ_end(); I != E; ++I) {
8483 EM->insert(std::make_pair(*I, sinkMBB));
8484 sinkMBB->addSuccessor(*I);
8485 }
8486 // Next, remove all successors of the current block, and add the true
8487 // and fallthrough blocks as its successors.
8488 while (!BB->succ_empty())
8489 BB->removeSuccessor(BB->succ_begin());
Chris Lattner84a67202009-09-02 05:57:00 +00008490 // Add the true and fallthrough blocks as its successors.
8491 BB->addSuccessor(copy0MBB);
8492 BB->addSuccessor(sinkMBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008493
Chris Lattner84a67202009-09-02 05:57:00 +00008494 // copy0MBB:
8495 // %FalseValue = ...
8496 // # fallthrough to sinkMBB
8497 BB = copy0MBB;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008498
Chris Lattner84a67202009-09-02 05:57:00 +00008499 // Update machine-CFG edges
8500 BB->addSuccessor(sinkMBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008501
Chris Lattner84a67202009-09-02 05:57:00 +00008502 // sinkMBB:
8503 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8504 // ...
8505 BB = sinkMBB;
8506 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8507 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8508 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8509
8510 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8511 return BB;
8512}
8513
Anton Korobeynikov7cd32422010-03-06 19:32:29 +00008514MachineBasicBlock *
8515X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8516 MachineBasicBlock *BB,
8517 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8518 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8519 DebugLoc DL = MI->getDebugLoc();
8520 MachineFunction *F = BB->getParent();
8521
8522 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8523 // non-trivial part is impdef of ESP.
8524 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8525 // mingw-w64.
8526
8527 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8528 .addExternalSymbol("_alloca")
8529 .addReg(X86::EAX, RegState::Implicit)
8530 .addReg(X86::ESP, RegState::Implicit)
8531 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8532 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8533
8534 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8535 return BB;
8536}
Chris Lattner84a67202009-09-02 05:57:00 +00008537
8538MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00008539X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengd7dc9832009-09-18 21:02:19 +00008540 MachineBasicBlock *BB,
8541 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008542 switch (MI->getOpcode()) {
8543 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov7cd32422010-03-06 19:32:29 +00008544 case X86::MINGW_ALLOCA:
8545 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohman29b998f2009-08-27 00:14:12 +00008546 case X86::CMOV_GR8:
Mon P Wang83edba52008-12-12 01:25:51 +00008547 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008548 case X86::CMOV_FR32:
8549 case X86::CMOV_FR64:
8550 case X86::CMOV_V4F32:
8551 case X86::CMOV_V2F64:
Chris Lattner84a67202009-09-02 05:57:00 +00008552 case X86::CMOV_V2I64:
Chris Lattner8d76aeb2010-03-14 18:31:44 +00008553 case X86::CMOV_GR16:
8554 case X86::CMOV_GR32:
8555 case X86::CMOV_RFP32:
8556 case X86::CMOV_RFP64:
8557 case X86::CMOV_RFP80:
Evan Cheng5f3a5402009-09-19 09:51:03 +00008558 return EmitLoweredSelect(MI, BB, EM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008559
8560 case X86::FP32_TO_INT16_IN_MEM:
8561 case X86::FP32_TO_INT32_IN_MEM:
8562 case X86::FP32_TO_INT64_IN_MEM:
8563 case X86::FP64_TO_INT16_IN_MEM:
8564 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00008565 case X86::FP64_TO_INT64_IN_MEM:
8566 case X86::FP80_TO_INT16_IN_MEM:
8567 case X86::FP80_TO_INT32_IN_MEM:
8568 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner84a67202009-09-02 05:57:00 +00008569 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8570 DebugLoc DL = MI->getDebugLoc();
8571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008572 // Change the floating point control register to use "round towards zero"
8573 // mode when truncating to an integer value.
8574 MachineFunction *F = BB->getParent();
David Greene6424ab92009-11-12 20:49:22 +00008575 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner84a67202009-09-02 05:57:00 +00008576 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008577
8578 // Load the old value of the high byte of the control word...
8579 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00008580 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner84a67202009-09-02 05:57:00 +00008581 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008582 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008583
8584 // Set the high part to be round to zero...
Chris Lattner84a67202009-09-02 05:57:00 +00008585 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008586 .addImm(0xC7F);
8587
8588 // Reload the modified control word now...
Chris Lattner84a67202009-09-02 05:57:00 +00008589 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008590
8591 // Restore the memory image of control word to original value
Chris Lattner84a67202009-09-02 05:57:00 +00008592 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008593 .addReg(OldCW);
8594
8595 // Get the X86 opcode to use.
8596 unsigned Opc;
8597 switch (MI->getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00008598 default: llvm_unreachable("illegal opcode!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008599 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8600 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8601 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8602 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8603 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8604 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00008605 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8606 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8607 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008608 }
8609
8610 X86AddressMode AM;
8611 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008612 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008613 AM.BaseType = X86AddressMode::RegBase;
8614 AM.Base.Reg = Op.getReg();
8615 } else {
8616 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00008617 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008618 }
8619 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008620 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008621 AM.Scale = Op.getImm();
8622 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008623 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008624 AM.IndexReg = Op.getImm();
8625 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008626 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008627 AM.GV = Op.getGlobal();
8628 } else {
8629 AM.Disp = Op.getImm();
8630 }
Chris Lattner84a67202009-09-02 05:57:00 +00008631 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindolafee9c0f2009-04-08 08:09:33 +00008632 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008633
8634 // Reload the original control word now.
Chris Lattner84a67202009-09-02 05:57:00 +00008635 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008636
Dan Gohman221a4372008-07-07 23:14:23 +00008637 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008638 return BB;
8639 }
Dale Johannesenf4243fb2010-03-10 22:13:47 +00008640 // DBG_VALUE. Only the frame index case is done here.
8641 case X86::DBG_VALUE: {
8642 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8643 DebugLoc DL = MI->getDebugLoc();
8644 X86AddressMode AM;
8645 MachineFunction *F = BB->getParent();
8646 AM.BaseType = X86AddressMode::FrameIndexBase;
8647 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8648 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8649 addImm(MI->getOperand(1).getImm()).
8650 addMetadata(MI->getOperand(2).getMetadata());
8651 F->DeleteMachineInstr(MI); // Remove pseudo.
8652 return BB;
8653 }
8654
Eric Christopher22a39402009-08-18 22:50:32 +00008655 // String/text processing lowering.
8656 case X86::PCMPISTRM128REG:
8657 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8658 case X86::PCMPISTRM128MEM:
8659 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8660 case X86::PCMPESTRM128REG:
8661 return EmitPCMP(MI, BB, 5, false /* in mem */);
8662 case X86::PCMPESTRM128MEM:
8663 return EmitPCMP(MI, BB, 5, true /* in mem */);
8664
8665 // Atomic Lowering.
Mon P Wang078a62d2008-05-05 19:05:59 +00008666 case X86::ATOMAND32:
8667 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michel91099d62009-02-17 22:15:04 +00008668 X86::AND32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008669 X86::LCMPXCHG32, X86::MOV32rr,
8670 X86::NOT32r, X86::EAX,
8671 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00008672 case X86::ATOMOR32:
Scott Michel91099d62009-02-17 22:15:04 +00008673 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8674 X86::OR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008675 X86::LCMPXCHG32, X86::MOV32rr,
8676 X86::NOT32r, X86::EAX,
8677 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00008678 case X86::ATOMXOR32:
8679 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michel91099d62009-02-17 22:15:04 +00008680 X86::XOR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008681 X86::LCMPXCHG32, X86::MOV32rr,
8682 X86::NOT32r, X86::EAX,
8683 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008684 case X86::ATOMNAND32:
8685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008686 X86::AND32ri, X86::MOV32rm,
8687 X86::LCMPXCHG32, X86::MOV32rr,
8688 X86::NOT32r, X86::EAX,
8689 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00008690 case X86::ATOMMIN32:
8691 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8692 case X86::ATOMMAX32:
8693 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8694 case X86::ATOMUMIN32:
8695 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8696 case X86::ATOMUMAX32:
8697 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00008698
8699 case X86::ATOMAND16:
8700 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8701 X86::AND16ri, X86::MOV16rm,
8702 X86::LCMPXCHG16, X86::MOV16rr,
8703 X86::NOT16r, X86::AX,
8704 X86::GR16RegisterClass);
8705 case X86::ATOMOR16:
Scott Michel91099d62009-02-17 22:15:04 +00008706 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008707 X86::OR16ri, X86::MOV16rm,
8708 X86::LCMPXCHG16, X86::MOV16rr,
8709 X86::NOT16r, X86::AX,
8710 X86::GR16RegisterClass);
8711 case X86::ATOMXOR16:
8712 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8713 X86::XOR16ri, X86::MOV16rm,
8714 X86::LCMPXCHG16, X86::MOV16rr,
8715 X86::NOT16r, X86::AX,
8716 X86::GR16RegisterClass);
8717 case X86::ATOMNAND16:
8718 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8719 X86::AND16ri, X86::MOV16rm,
8720 X86::LCMPXCHG16, X86::MOV16rr,
8721 X86::NOT16r, X86::AX,
8722 X86::GR16RegisterClass, true);
8723 case X86::ATOMMIN16:
8724 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8725 case X86::ATOMMAX16:
8726 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8727 case X86::ATOMUMIN16:
8728 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8729 case X86::ATOMUMAX16:
8730 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8731
8732 case X86::ATOMAND8:
8733 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8734 X86::AND8ri, X86::MOV8rm,
8735 X86::LCMPXCHG8, X86::MOV8rr,
8736 X86::NOT8r, X86::AL,
8737 X86::GR8RegisterClass);
8738 case X86::ATOMOR8:
Scott Michel91099d62009-02-17 22:15:04 +00008739 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008740 X86::OR8ri, X86::MOV8rm,
8741 X86::LCMPXCHG8, X86::MOV8rr,
8742 X86::NOT8r, X86::AL,
8743 X86::GR8RegisterClass);
8744 case X86::ATOMXOR8:
8745 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8746 X86::XOR8ri, X86::MOV8rm,
8747 X86::LCMPXCHG8, X86::MOV8rr,
8748 X86::NOT8r, X86::AL,
8749 X86::GR8RegisterClass);
8750 case X86::ATOMNAND8:
8751 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8752 X86::AND8ri, X86::MOV8rm,
8753 X86::LCMPXCHG8, X86::MOV8rr,
8754 X86::NOT8r, X86::AL,
8755 X86::GR8RegisterClass, true);
8756 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00008757 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008758 case X86::ATOMAND64:
8759 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michel91099d62009-02-17 22:15:04 +00008760 X86::AND64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008761 X86::LCMPXCHG64, X86::MOV64rr,
8762 X86::NOT64r, X86::RAX,
8763 X86::GR64RegisterClass);
8764 case X86::ATOMOR64:
Scott Michel91099d62009-02-17 22:15:04 +00008765 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8766 X86::OR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008767 X86::LCMPXCHG64, X86::MOV64rr,
8768 X86::NOT64r, X86::RAX,
8769 X86::GR64RegisterClass);
8770 case X86::ATOMXOR64:
8771 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michel91099d62009-02-17 22:15:04 +00008772 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008773 X86::LCMPXCHG64, X86::MOV64rr,
8774 X86::NOT64r, X86::RAX,
8775 X86::GR64RegisterClass);
8776 case X86::ATOMNAND64:
8777 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8778 X86::AND64ri32, X86::MOV64rm,
8779 X86::LCMPXCHG64, X86::MOV64rr,
8780 X86::NOT64r, X86::RAX,
8781 X86::GR64RegisterClass, true);
8782 case X86::ATOMMIN64:
8783 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8784 case X86::ATOMMAX64:
8785 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8786 case X86::ATOMUMIN64:
8787 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8788 case X86::ATOMUMAX64:
8789 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00008790
8791 // This group does 64-bit operations on a 32-bit host.
8792 case X86::ATOMAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00008793 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008794 X86::AND32rr, X86::AND32rr,
8795 X86::AND32ri, X86::AND32ri,
8796 false);
8797 case X86::ATOMOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00008798 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008799 X86::OR32rr, X86::OR32rr,
8800 X86::OR32ri, X86::OR32ri,
8801 false);
8802 case X86::ATOMXOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00008803 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008804 X86::XOR32rr, X86::XOR32rr,
8805 X86::XOR32ri, X86::XOR32ri,
8806 false);
8807 case X86::ATOMNAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00008808 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008809 X86::AND32rr, X86::AND32rr,
8810 X86::AND32ri, X86::AND32ri,
8811 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00008812 case X86::ATOMADD6432:
Scott Michel91099d62009-02-17 22:15:04 +00008813 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008814 X86::ADD32rr, X86::ADC32rr,
8815 X86::ADD32ri, X86::ADC32ri,
8816 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00008817 case X86::ATOMSUB6432:
Scott Michel91099d62009-02-17 22:15:04 +00008818 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008819 X86::SUB32rr, X86::SBB32rr,
8820 X86::SUB32ri, X86::SBB32ri,
8821 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008822 case X86::ATOMSWAP6432:
Scott Michel91099d62009-02-17 22:15:04 +00008823 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008824 X86::MOV32rr, X86::MOV32rr,
8825 X86::MOV32ri, X86::MOV32ri,
8826 false);
Dan Gohman34228bf2009-08-15 01:38:56 +00008827 case X86::VASTART_SAVE_XMM_REGS:
8828 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008829 }
8830}
8831
8832//===----------------------------------------------------------------------===//
8833// X86 Optimization Hooks
8834//===----------------------------------------------------------------------===//
8835
Dan Gohman8181bd12008-07-27 21:46:04 +00008836void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00008837 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00008838 APInt &KnownZero,
8839 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008840 const SelectionDAG &DAG,
8841 unsigned Depth) const {
8842 unsigned Opc = Op.getOpcode();
8843 assert((Opc >= ISD::BUILTIN_OP_END ||
8844 Opc == ISD::INTRINSIC_WO_CHAIN ||
8845 Opc == ISD::INTRINSIC_W_CHAIN ||
8846 Opc == ISD::INTRINSIC_VOID) &&
8847 "Should use MaskedValueIsZero if you don't know whether Op"
8848 " is a target node!");
8849
Dan Gohman1d79e432008-02-13 23:07:24 +00008850 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008851 switch (Opc) {
8852 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00008853 case X86ISD::ADD:
8854 case X86ISD::SUB:
8855 case X86ISD::SMUL:
8856 case X86ISD::UMUL:
Dan Gohman99a12192009-03-04 19:44:21 +00008857 case X86ISD::INC:
8858 case X86ISD::DEC:
Dan Gohman12e03292009-09-18 19:59:53 +00008859 case X86ISD::OR:
8860 case X86ISD::XOR:
8861 case X86ISD::AND:
Evan Cheng8e9b21c2009-02-02 09:15:04 +00008862 // These nodes' second result is a boolean.
8863 if (Op.getResNo() == 0)
8864 break;
8865 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008866 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00008867 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8868 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008869 break;
8870 }
8871}
8872
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008873/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00008874/// node is a GlobalAddress + offset.
8875bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8876 GlobalValue* &GA, int64_t &Offset) const{
8877 if (N->getOpcode() == X86ISD::Wrapper) {
8878 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008879 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00008880 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008881 return true;
8882 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008883 }
Evan Chengef7be082008-05-12 19:56:52 +00008884 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008885}
8886
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008887/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8888/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8889/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begeman1aa900a2010-03-24 20:49:50 +00008890/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00008891static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00008892 const TargetLowering &TLI) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008893 DebugLoc dl = N->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00008894 EVT VT = N->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00008895 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang6e30ad02009-04-03 02:43:30 +00008896
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008897 if (VT.getSizeInBits() != 128)
8898 return SDValue();
8899
Nate Begeman1aa900a2010-03-24 20:49:50 +00008900 SmallVector<SDValue, 16> Elts;
8901 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8902 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8903
8904 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michel91099d62009-02-17 22:15:04 +00008905}
Evan Chenge9b9c672008-05-09 21:53:03 +00008906
Dan Gohmanb115d052010-03-15 23:23:03 +00008907/// PerformShuffleCombine - Detect vector gather/scatter index generation
8908/// and convert it from being a bunch of shuffles and extracts to a simple
8909/// store and scalar loads to extract the elements.
8910static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8911 const TargetLowering &TLI) {
8912 SDValue InputVector = N->getOperand(0);
8913
8914 // Only operate on vectors of 4 elements, where the alternative shuffling
8915 // gets to be more expensive.
8916 if (InputVector.getValueType() != MVT::v4i32)
8917 return SDValue();
8918
8919 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8920 // single use which is a sign-extend or zero-extend, and all elements are
8921 // used.
8922 SmallVector<SDNode *, 4> Uses;
8923 unsigned ExtractedElements = 0;
8924 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8925 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8926 if (UI.getUse().getResNo() != InputVector.getResNo())
8927 return SDValue();
8928
8929 SDNode *Extract = *UI;
8930 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8931 return SDValue();
8932
8933 if (Extract->getValueType(0) != MVT::i32)
8934 return SDValue();
8935 if (!Extract->hasOneUse())
8936 return SDValue();
8937 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8938 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8939 return SDValue();
8940 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8941 return SDValue();
8942
8943 // Record which element was extracted.
8944 ExtractedElements |=
8945 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8946
8947 Uses.push_back(Extract);
8948 }
8949
8950 // If not all the elements were used, this may not be worthwhile.
8951 if (ExtractedElements != 15)
8952 return SDValue();
8953
8954 // Ok, we've now decided to do the transformation.
8955 DebugLoc dl = InputVector.getDebugLoc();
8956
8957 // Store the value to a temporary stack slot.
8958 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8959 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8960 false, false, 0);
8961
8962 // Replace each use (extract) with a load of the appropriate element.
8963 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
8964 UE = Uses.end(); UI != UE; ++UI) {
8965 SDNode *Extract = *UI;
8966
8967 // Compute the element's address.
8968 SDValue Idx = Extract->getOperand(1);
8969 unsigned EltSize =
8970 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
8971 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
8972 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
8973
8974 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
8975
8976 // Load the scalar.
8977 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
8978 NULL, 0, false, false, 0);
8979
8980 // Replace the exact with the load.
8981 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
8982 }
8983
8984 // The replacement was made in place; don't return anything.
8985 return SDValue();
8986}
8987
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008988/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008989static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner472f1d52009-03-11 05:48:52 +00008990 const X86Subtarget *Subtarget) {
8991 DebugLoc DL = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00008992 SDValue Cond = N->getOperand(0);
Chris Lattner472f1d52009-03-11 05:48:52 +00008993 // Get the LHS/RHS of the select.
8994 SDValue LHS = N->getOperand(1);
8995 SDValue RHS = N->getOperand(2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008996
Dan Gohman19488552009-09-21 18:03:22 +00008997 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohmandaa74bd2010-02-22 04:03:39 +00008998 // instructions match the semantics of the common C idiom x<y?x:y but not
8999 // x<=y?x:y, because of how they handle negative zero (which can be
9000 // ignored in unsafe-math mode).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009001 if (Subtarget->hasSSE2() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009002 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner472f1d52009-03-11 05:48:52 +00009003 Cond.getOpcode() == ISD::SETCC) {
9004 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009005
Chris Lattner472f1d52009-03-11 05:48:52 +00009006 unsigned Opcode = 0;
Dan Gohman19488552009-09-21 18:03:22 +00009007 // Check for x CC y ? x : y.
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009008 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9009 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner472f1d52009-03-11 05:48:52 +00009010 switch (CC) {
9011 default: break;
Dan Gohman19488552009-09-21 18:03:22 +00009012 case ISD::SETULT:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009013 // Converting this to a min would handle NaNs incorrectly, and swapping
9014 // the operands would cause it to handle comparisons between positive
9015 // and negative zero incorrectly.
9016 if (!FiniteOnlyFPMath() &&
9017 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9018 if (!UnsafeFPMath &&
9019 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9020 break;
9021 std::swap(LHS, RHS);
9022 }
Dan Gohman19488552009-09-21 18:03:22 +00009023 Opcode = X86ISD::FMIN;
9024 break;
9025 case ISD::SETOLE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009026 // Converting this to a min would handle comparisons between positive
9027 // and negative zero incorrectly.
9028 if (!UnsafeFPMath &&
9029 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9030 break;
Dan Gohman19488552009-09-21 18:03:22 +00009031 Opcode = X86ISD::FMIN;
9032 break;
Chris Lattner472f1d52009-03-11 05:48:52 +00009033 case ISD::SETULE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009034 // Converting this to a min would handle both negative zeros and NaNs
9035 // incorrectly, but we can swap the operands to fix both.
9036 std::swap(LHS, RHS);
Dan Gohman19488552009-09-21 18:03:22 +00009037 case ISD::SETOLT:
Chris Lattner472f1d52009-03-11 05:48:52 +00009038 case ISD::SETLT:
Dan Gohman19488552009-09-21 18:03:22 +00009039 case ISD::SETLE:
Chris Lattner472f1d52009-03-11 05:48:52 +00009040 Opcode = X86ISD::FMIN;
9041 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009042
Dan Gohman19488552009-09-21 18:03:22 +00009043 case ISD::SETOGE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009044 // Converting this to a max would handle comparisons between positive
9045 // and negative zero incorrectly.
9046 if (!UnsafeFPMath &&
9047 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9048 break;
Dan Gohman19488552009-09-21 18:03:22 +00009049 Opcode = X86ISD::FMAX;
9050 break;
Chris Lattner472f1d52009-03-11 05:48:52 +00009051 case ISD::SETUGT:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009052 // Converting this to a max would handle NaNs incorrectly, and swapping
9053 // the operands would cause it to handle comparisons between positive
9054 // and negative zero incorrectly.
9055 if (!FiniteOnlyFPMath() &&
9056 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9057 if (!UnsafeFPMath &&
9058 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9059 break;
9060 std::swap(LHS, RHS);
9061 }
Dan Gohman19488552009-09-21 18:03:22 +00009062 Opcode = X86ISD::FMAX;
9063 break;
9064 case ISD::SETUGE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009065 // Converting this to a max would handle both negative zeros and NaNs
9066 // incorrectly, but we can swap the operands to fix both.
9067 std::swap(LHS, RHS);
Dan Gohman19488552009-09-21 18:03:22 +00009068 case ISD::SETOGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00009069 case ISD::SETGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00009070 case ISD::SETGE:
9071 Opcode = X86ISD::FMAX;
9072 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009073 }
Dan Gohman19488552009-09-21 18:03:22 +00009074 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009075 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9076 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner472f1d52009-03-11 05:48:52 +00009077 switch (CC) {
9078 default: break;
Dan Gohman19488552009-09-21 18:03:22 +00009079 case ISD::SETOGE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009080 // Converting this to a min would handle comparisons between positive
9081 // and negative zero incorrectly, and swapping the operands would
9082 // cause it to handle NaNs incorrectly.
9083 if (!UnsafeFPMath &&
9084 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9085 if (!FiniteOnlyFPMath() &&
9086 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9087 break;
9088 std::swap(LHS, RHS);
9089 }
Dan Gohman19488552009-09-21 18:03:22 +00009090 Opcode = X86ISD::FMIN;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00009091 break;
Dan Gohman19488552009-09-21 18:03:22 +00009092 case ISD::SETUGT:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009093 // Converting this to a min would handle NaNs incorrectly.
9094 if (!UnsafeFPMath &&
9095 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9096 break;
Dan Gohman19488552009-09-21 18:03:22 +00009097 Opcode = X86ISD::FMIN;
9098 break;
9099 case ISD::SETUGE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009100 // Converting this to a min would handle both negative zeros and NaNs
9101 // incorrectly, but we can swap the operands to fix both.
9102 std::swap(LHS, RHS);
Dan Gohman19488552009-09-21 18:03:22 +00009103 case ISD::SETOGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00009104 case ISD::SETGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00009105 case ISD::SETGE:
9106 Opcode = X86ISD::FMIN;
9107 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009108
Dan Gohman19488552009-09-21 18:03:22 +00009109 case ISD::SETULT:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009110 // Converting this to a max would handle NaNs incorrectly.
9111 if (!FiniteOnlyFPMath() &&
9112 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9113 break;
Dan Gohman19488552009-09-21 18:03:22 +00009114 Opcode = X86ISD::FMAX;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00009115 break;
Dan Gohman19488552009-09-21 18:03:22 +00009116 case ISD::SETOLE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009117 // Converting this to a max would handle comparisons between positive
9118 // and negative zero incorrectly, and swapping the operands would
9119 // cause it to handle NaNs incorrectly.
9120 if (!UnsafeFPMath &&
9121 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9122 if (!FiniteOnlyFPMath() &&
9123 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9124 break;
9125 std::swap(LHS, RHS);
9126 }
Dan Gohman19488552009-09-21 18:03:22 +00009127 Opcode = X86ISD::FMAX;
9128 break;
9129 case ISD::SETULE:
Dan Gohmane8cc39f2010-02-24 06:52:40 +00009130 // Converting this to a max would handle both negative zeros and NaNs
9131 // incorrectly, but we can swap the operands to fix both.
9132 std::swap(LHS, RHS);
Dan Gohman19488552009-09-21 18:03:22 +00009133 case ISD::SETOLT:
Chris Lattner472f1d52009-03-11 05:48:52 +00009134 case ISD::SETLT:
Dan Gohman19488552009-09-21 18:03:22 +00009135 case ISD::SETLE:
Chris Lattner472f1d52009-03-11 05:48:52 +00009136 Opcode = X86ISD::FMAX;
9137 break;
9138 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009139 }
9140
Chris Lattner472f1d52009-03-11 05:48:52 +00009141 if (Opcode)
9142 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009143 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009144
Chris Lattnere4577dc2009-03-12 06:52:53 +00009145 // If this is a select between two integer constants, try to do some
9146 // optimizations.
Chris Lattnera054e842009-03-13 05:53:31 +00009147 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9148 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnere4577dc2009-03-12 06:52:53 +00009149 // Don't do this for crazy integer types.
9150 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9151 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnera054e842009-03-13 05:53:31 +00009152 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnere4577dc2009-03-12 06:52:53 +00009153 bool NeedsCondInvert = false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009154
Chris Lattnera054e842009-03-13 05:53:31 +00009155 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnere4577dc2009-03-12 06:52:53 +00009156 // Efficiently invertible.
9157 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9158 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9159 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9160 NeedsCondInvert = true;
Chris Lattnera054e842009-03-13 05:53:31 +00009161 std::swap(TrueC, FalseC);
Chris Lattnere4577dc2009-03-12 06:52:53 +00009162 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009163
Chris Lattnere4577dc2009-03-12 06:52:53 +00009164 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00009165 if (FalseC->getAPIntValue() == 0 &&
9166 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnere4577dc2009-03-12 06:52:53 +00009167 if (NeedsCondInvert) // Invert the condition if needed.
9168 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9169 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009170
Chris Lattnere4577dc2009-03-12 06:52:53 +00009171 // Zero extend the condition if needed.
9172 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009173
Chris Lattnera054e842009-03-13 05:53:31 +00009174 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnere4577dc2009-03-12 06:52:53 +00009175 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009176 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnere4577dc2009-03-12 06:52:53 +00009177 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009178
Chris Lattner938d6652009-03-13 05:22:11 +00009179 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnera054e842009-03-13 05:53:31 +00009180 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner938d6652009-03-13 05:22:11 +00009181 if (NeedsCondInvert) // Invert the condition if needed.
9182 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9183 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009184
Chris Lattner938d6652009-03-13 05:22:11 +00009185 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00009186 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9187 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00009188 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnera054e842009-03-13 05:53:31 +00009189 SDValue(FalseC, 0));
Chris Lattner938d6652009-03-13 05:22:11 +00009190 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009191
Chris Lattnera054e842009-03-13 05:53:31 +00009192 // Optimize cases that will turn into an LEA instruction. This requires
9193 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009194 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnera054e842009-03-13 05:53:31 +00009195 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009196 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009197
Chris Lattnera054e842009-03-13 05:53:31 +00009198 bool isFastMultiplier = false;
9199 if (Diff < 10) {
9200 switch ((unsigned char)Diff) {
9201 default: break;
9202 case 1: // result = add base, cond
9203 case 2: // result = lea base( , cond*2)
9204 case 3: // result = lea base(cond, cond*2)
9205 case 4: // result = lea base( , cond*4)
9206 case 5: // result = lea base(cond, cond*4)
9207 case 8: // result = lea base( , cond*8)
9208 case 9: // result = lea base(cond, cond*8)
9209 isFastMultiplier = true;
9210 break;
9211 }
9212 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009213
Chris Lattnera054e842009-03-13 05:53:31 +00009214 if (isFastMultiplier) {
9215 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9216 if (NeedsCondInvert) // Invert the condition if needed.
9217 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9218 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009219
Chris Lattnera054e842009-03-13 05:53:31 +00009220 // Zero extend the condition if needed.
9221 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9222 Cond);
9223 // Scale the condition by the difference.
9224 if (Diff != 1)
9225 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9226 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009227
Chris Lattnera054e842009-03-13 05:53:31 +00009228 // Add the base if non-zero.
9229 if (FalseC->getAPIntValue() != 0)
9230 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9231 SDValue(FalseC, 0));
9232 return Cond;
9233 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009234 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00009235 }
9236 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009237
Dan Gohman8181bd12008-07-27 21:46:04 +00009238 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009239}
9240
Chris Lattnere4577dc2009-03-12 06:52:53 +00009241/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9242static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9243 TargetLowering::DAGCombinerInfo &DCI) {
9244 DebugLoc DL = N->getDebugLoc();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009245
Chris Lattnere4577dc2009-03-12 06:52:53 +00009246 // If the flag operand isn't dead, don't touch this CMOV.
9247 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9248 return SDValue();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009249
Chris Lattnere4577dc2009-03-12 06:52:53 +00009250 // If this is a select between two integer constants, try to do some
9251 // optimizations. Note that the operands are ordered the opposite of SELECT
9252 // operands.
9253 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9254 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9255 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9256 // larger than FalseC (the false value).
9257 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009258
Chris Lattnere4577dc2009-03-12 06:52:53 +00009259 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9260 CC = X86::GetOppositeBranchCondition(CC);
9261 std::swap(TrueC, FalseC);
9262 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009263
Chris Lattnere4577dc2009-03-12 06:52:53 +00009264 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00009265 // This is efficient for any integer data type (including i8/i16) and
9266 // shift amount.
Chris Lattnere4577dc2009-03-12 06:52:53 +00009267 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9268 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009269 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9270 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009271
Chris Lattnere4577dc2009-03-12 06:52:53 +00009272 // Zero extend the condition if needed.
9273 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009274
Chris Lattnere4577dc2009-03-12 06:52:53 +00009275 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9276 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009277 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnere4577dc2009-03-12 06:52:53 +00009278 if (N->getNumValues() == 2) // Dead flag value?
9279 return DCI.CombineTo(N, Cond, SDValue());
9280 return Cond;
9281 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009282
Chris Lattnera054e842009-03-13 05:53:31 +00009283 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9284 // for any integer data type, including i8/i16.
Chris Lattner938d6652009-03-13 05:22:11 +00009285 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9286 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009287 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9288 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009289
Chris Lattner938d6652009-03-13 05:22:11 +00009290 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00009291 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9292 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00009293 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9294 SDValue(FalseC, 0));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009295
Chris Lattner938d6652009-03-13 05:22:11 +00009296 if (N->getNumValues() == 2) // Dead flag value?
9297 return DCI.CombineTo(N, Cond, SDValue());
9298 return Cond;
9299 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009300
Chris Lattnera054e842009-03-13 05:53:31 +00009301 // Optimize cases that will turn into an LEA instruction. This requires
9302 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009303 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnera054e842009-03-13 05:53:31 +00009304 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009305 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009306
Chris Lattnera054e842009-03-13 05:53:31 +00009307 bool isFastMultiplier = false;
9308 if (Diff < 10) {
9309 switch ((unsigned char)Diff) {
9310 default: break;
9311 case 1: // result = add base, cond
9312 case 2: // result = lea base( , cond*2)
9313 case 3: // result = lea base(cond, cond*2)
9314 case 4: // result = lea base( , cond*4)
9315 case 5: // result = lea base(cond, cond*4)
9316 case 8: // result = lea base( , cond*8)
9317 case 9: // result = lea base(cond, cond*8)
9318 isFastMultiplier = true;
9319 break;
9320 }
9321 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009322
Chris Lattnera054e842009-03-13 05:53:31 +00009323 if (isFastMultiplier) {
9324 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9325 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009326 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9327 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnera054e842009-03-13 05:53:31 +00009328 // Zero extend the condition if needed.
9329 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9330 Cond);
9331 // Scale the condition by the difference.
9332 if (Diff != 1)
9333 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9334 DAG.getConstant(Diff, Cond.getValueType()));
9335
9336 // Add the base if non-zero.
9337 if (FalseC->getAPIntValue() != 0)
9338 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9339 SDValue(FalseC, 0));
9340 if (N->getNumValues() == 2) // Dead flag value?
9341 return DCI.CombineTo(N, Cond, SDValue());
9342 return Cond;
9343 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009344 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00009345 }
9346 }
9347 return SDValue();
9348}
9349
9350
Evan Cheng04ecee12009-03-28 05:57:29 +00009351/// PerformMulCombine - Optimize a single multiply with constant into two
9352/// in order to implement it with two cheaper instructions, e.g.
9353/// LEA + SHL, LEA + LEA.
9354static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9355 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng04ecee12009-03-28 05:57:29 +00009356 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9357 return SDValue();
9358
Owen Andersonac9de032009-08-10 22:56:29 +00009359 EVT VT = N->getValueType(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009360 if (VT != MVT::i64)
Evan Cheng04ecee12009-03-28 05:57:29 +00009361 return SDValue();
9362
9363 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9364 if (!C)
9365 return SDValue();
9366 uint64_t MulAmt = C->getZExtValue();
9367 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9368 return SDValue();
9369
9370 uint64_t MulAmt1 = 0;
9371 uint64_t MulAmt2 = 0;
9372 if ((MulAmt % 9) == 0) {
9373 MulAmt1 = 9;
9374 MulAmt2 = MulAmt / 9;
9375 } else if ((MulAmt % 5) == 0) {
9376 MulAmt1 = 5;
9377 MulAmt2 = MulAmt / 5;
9378 } else if ((MulAmt % 3) == 0) {
9379 MulAmt1 = 3;
9380 MulAmt2 = MulAmt / 3;
9381 }
9382 if (MulAmt2 &&
9383 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9384 DebugLoc DL = N->getDebugLoc();
9385
9386 if (isPowerOf2_64(MulAmt2) &&
9387 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9388 // If second multiplifer is pow2, issue it first. We want the multiply by
9389 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9390 // is an add.
9391 std::swap(MulAmt1, MulAmt2);
9392
9393 SDValue NewMul;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009394 if (isPowerOf2_64(MulAmt1))
Evan Cheng04ecee12009-03-28 05:57:29 +00009395 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009396 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng04ecee12009-03-28 05:57:29 +00009397 else
Evan Chengc3495762009-03-30 21:36:47 +00009398 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng04ecee12009-03-28 05:57:29 +00009399 DAG.getConstant(MulAmt1, VT));
9400
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009401 if (isPowerOf2_64(MulAmt2))
Evan Cheng04ecee12009-03-28 05:57:29 +00009402 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009403 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009404 else
Evan Chengc3495762009-03-30 21:36:47 +00009405 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng04ecee12009-03-28 05:57:29 +00009406 DAG.getConstant(MulAmt2, VT));
9407
9408 // Do not add new nodes to DAG combiner worklist.
9409 DCI.CombineTo(N, NewMul, false);
9410 }
9411 return SDValue();
9412}
9413
Evan Cheng834ae6b2009-12-15 00:53:42 +00009414static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9415 SDValue N0 = N->getOperand(0);
9416 SDValue N1 = N->getOperand(1);
9417 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9418 EVT VT = N0.getValueType();
9419
9420 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9421 // since the result of setcc_c is all zero's or all ones.
9422 if (N1C && N0.getOpcode() == ISD::AND &&
9423 N0.getOperand(1).getOpcode() == ISD::Constant) {
9424 SDValue N00 = N0.getOperand(0);
9425 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9426 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9427 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9428 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9429 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9430 APInt ShAmt = N1C->getAPIntValue();
9431 Mask = Mask.shl(ShAmt);
9432 if (Mask != 0)
9433 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9434 N00, DAG.getConstant(Mask, VT));
9435 }
9436 }
9437
9438 return SDValue();
9439}
Evan Cheng04ecee12009-03-28 05:57:29 +00009440
sampo025b75c2009-01-26 00:52:55 +00009441/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9442/// when possible.
9443static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9444 const X86Subtarget *Subtarget) {
Evan Cheng834ae6b2009-12-15 00:53:42 +00009445 EVT VT = N->getValueType(0);
9446 if (!VT.isVector() && VT.isInteger() &&
9447 N->getOpcode() == ISD::SHL)
9448 return PerformSHLCombine(N, DAG);
9449
sampo025b75c2009-01-26 00:52:55 +00009450 // On X86 with SSE2 support, we can transform this to a vector shift if
9451 // all elements are shifted by the same amount. We can't do this in legalize
9452 // because the a constant vector is typically transformed to a constant pool
9453 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00009454 if (!Subtarget->hasSSE2())
9455 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00009456
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009457 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
sampo087d53c2009-01-26 03:15:31 +00009458 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00009459
Mon P Wanga91e9642009-01-28 08:12:05 +00009460 SDValue ShAmtOp = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00009461 EVT EltVT = VT.getVectorElementType();
Chris Lattner472f1d52009-03-11 05:48:52 +00009462 DebugLoc DL = N->getDebugLoc();
Mon P Wang04c767e2009-09-03 19:56:25 +00009463 SDValue BaseShAmt = SDValue();
Mon P Wanga91e9642009-01-28 08:12:05 +00009464 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9465 unsigned NumElts = VT.getVectorNumElements();
9466 unsigned i = 0;
9467 for (; i != NumElts; ++i) {
9468 SDValue Arg = ShAmtOp.getOperand(i);
9469 if (Arg.getOpcode() == ISD::UNDEF) continue;
9470 BaseShAmt = Arg;
9471 break;
9472 }
9473 for (; i != NumElts; ++i) {
9474 SDValue Arg = ShAmtOp.getOperand(i);
9475 if (Arg.getOpcode() == ISD::UNDEF) continue;
9476 if (Arg != BaseShAmt) {
9477 return SDValue();
9478 }
9479 }
9480 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman543d2142009-04-27 18:41:29 +00009481 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wang04c767e2009-09-03 19:56:25 +00009482 SDValue InVec = ShAmtOp.getOperand(0);
9483 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9484 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9485 unsigned i = 0;
9486 for (; i != NumElts; ++i) {
9487 SDValue Arg = InVec.getOperand(i);
9488 if (Arg.getOpcode() == ISD::UNDEF) continue;
9489 BaseShAmt = Arg;
9490 break;
9491 }
9492 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9493 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Cheng97ffc6e2010-02-16 21:09:44 +00009494 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wang04c767e2009-09-03 19:56:25 +00009495 if (C->getZExtValue() == SplatIdx)
9496 BaseShAmt = InVec.getOperand(1);
9497 }
9498 }
9499 if (BaseShAmt.getNode() == 0)
9500 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9501 DAG.getIntPtrConstant(0));
Mon P Wanga91e9642009-01-28 08:12:05 +00009502 } else
sampo087d53c2009-01-26 03:15:31 +00009503 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00009504
Mon P Wang04c767e2009-09-03 19:56:25 +00009505 // The shift amount is an i32.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009506 if (EltVT.bitsGT(MVT::i32))
9507 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9508 else if (EltVT.bitsLT(MVT::i32))
Mon P Wang04c767e2009-09-03 19:56:25 +00009509 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00009510
sampo087d53c2009-01-26 03:15:31 +00009511 // The shift amount is identical so we can do a vector shift.
9512 SDValue ValOp = N->getOperand(0);
9513 switch (N->getOpcode()) {
9514 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00009515 llvm_unreachable("Unknown shift opcode!");
sampo087d53c2009-01-26 03:15:31 +00009516 break;
9517 case ISD::SHL:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009518 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00009519 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009520 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009521 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009522 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009523 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009524 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009525 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009526 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009527 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009528 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009529 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009530 break;
9531 case ISD::SRA:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009532 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009533 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009534 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009535 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009536 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009537 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009538 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009539 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009540 break;
9541 case ISD::SRL:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009542 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00009543 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009544 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009545 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009546 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009547 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009548 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009549 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009550 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009551 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009552 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009553 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009554 break;
sampo025b75c2009-01-26 00:52:55 +00009555 }
9556 return SDValue();
9557}
9558
Evan Cheng10957b82010-01-04 21:22:48 +00009559static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9560 const X86Subtarget *Subtarget) {
9561 EVT VT = N->getValueType(0);
9562 if (VT != MVT::i64 || !Subtarget->is64Bit())
9563 return SDValue();
9564
9565 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9566 SDValue N0 = N->getOperand(0);
9567 SDValue N1 = N->getOperand(1);
9568 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9569 std::swap(N0, N1);
9570 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9571 return SDValue();
9572
9573 SDValue ShAmt0 = N0.getOperand(1);
9574 if (ShAmt0.getValueType() != MVT::i8)
9575 return SDValue();
9576 SDValue ShAmt1 = N1.getOperand(1);
9577 if (ShAmt1.getValueType() != MVT::i8)
9578 return SDValue();
9579 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9580 ShAmt0 = ShAmt0.getOperand(0);
9581 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9582 ShAmt1 = ShAmt1.getOperand(0);
9583
9584 DebugLoc DL = N->getDebugLoc();
9585 unsigned Opc = X86ISD::SHLD;
9586 SDValue Op0 = N0.getOperand(0);
9587 SDValue Op1 = N1.getOperand(0);
9588 if (ShAmt0.getOpcode() == ISD::SUB) {
9589 Opc = X86ISD::SHRD;
9590 std::swap(Op0, Op1);
9591 std::swap(ShAmt0, ShAmt1);
9592 }
9593
9594 if (ShAmt1.getOpcode() == ISD::SUB) {
9595 SDValue Sum = ShAmt1.getOperand(0);
9596 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9597 if (SumC->getSExtValue() == 64 &&
9598 ShAmt1.getOperand(1) == ShAmt0)
9599 return DAG.getNode(Opc, DL, VT,
9600 Op0, Op1,
9601 DAG.getNode(ISD::TRUNCATE, DL,
9602 MVT::i8, ShAmt0));
9603 }
9604 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9605 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9606 if (ShAmt0C &&
9607 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9608 return DAG.getNode(Opc, DL, VT,
9609 N0.getOperand(0), N1.getOperand(0),
9610 DAG.getNode(ISD::TRUNCATE, DL,
9611 MVT::i8, ShAmt0));
9612 }
9613
9614 return SDValue();
9615}
9616
Chris Lattnerce84ae42008-02-22 02:09:43 +00009617/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009618static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Chengc944c5d2009-03-12 05:59:15 +00009619 const X86Subtarget *Subtarget) {
Chris Lattnerce84ae42008-02-22 02:09:43 +00009620 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9621 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00009622 // A preferable solution to the general problem is to figure out the right
9623 // places to insert EMMS. This qualifies as a quick hack.
Evan Chengc944c5d2009-03-12 05:59:15 +00009624
9625 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng40ee6e52008-05-08 00:57:18 +00009626 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersonac9de032009-08-10 22:56:29 +00009627 EVT VT = St->getValue().getValueType();
Evan Chengc944c5d2009-03-12 05:59:15 +00009628 if (VT.getSizeInBits() != 64)
9629 return SDValue();
9630
Devang Patelc386c842009-06-05 21:57:13 +00009631 const Function *F = DAG.getMachineFunction().getFunction();
9632 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009633 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patelc386c842009-06-05 21:57:13 +00009634 && Subtarget->hasSSE2();
Evan Chengc944c5d2009-03-12 05:59:15 +00009635 if ((VT.isVector() ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009636 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesend112b802008-02-25 19:20:14 +00009637 isa<LoadSDNode>(St->getValue()) &&
9638 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9639 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00009640 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00009641 LoadSDNode *Ld = 0;
9642 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00009643 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00009644 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00009645 // Must be a store of a load. We currently handle two cases: the load
9646 // is a direct child, and it's under an intervening TokenFactor. It is
9647 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00009648 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00009649 Ld = cast<LoadSDNode>(St->getChain());
9650 else if (St->getValue().hasOneUse() &&
9651 ChainVal->getOpcode() == ISD::TokenFactor) {
9652 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00009653 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00009654 TokenFactorIndex = i;
9655 Ld = cast<LoadSDNode>(St->getValue());
9656 } else
9657 Ops.push_back(ChainVal->getOperand(i));
9658 }
9659 }
Dale Johannesend112b802008-02-25 19:20:14 +00009660
Evan Chengc944c5d2009-03-12 05:59:15 +00009661 if (!Ld || !ISD::isNormalLoad(Ld))
9662 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00009663
Evan Chengc944c5d2009-03-12 05:59:15 +00009664 // If this is not the MMX case, i.e. we are just turning i64 load/store
9665 // into f64 load/store, avoid the transformation if there are multiple
9666 // uses of the loaded value.
9667 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9668 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00009669
Evan Chengc944c5d2009-03-12 05:59:15 +00009670 DebugLoc LdDL = Ld->getDebugLoc();
9671 DebugLoc StDL = N->getDebugLoc();
9672 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9673 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9674 // pair instead.
9675 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009676 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Chengc944c5d2009-03-12 05:59:15 +00009677 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9678 Ld->getBasePtr(), Ld->getSrcValue(),
9679 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene25160362010-02-15 16:53:33 +00009680 Ld->isNonTemporal(), Ld->getAlignment());
Evan Chengc944c5d2009-03-12 05:59:15 +00009681 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00009682 if (TokenFactorIndex != -1) {
Evan Chengc944c5d2009-03-12 05:59:15 +00009683 Ops.push_back(NewChain);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009684 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00009685 Ops.size());
9686 }
Evan Chengc944c5d2009-03-12 05:59:15 +00009687 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattnerce84ae42008-02-22 02:09:43 +00009688 St->getSrcValue(), St->getSrcValueOffset(),
David Greene25160362010-02-15 16:53:33 +00009689 St->isVolatile(), St->isNonTemporal(),
9690 St->getAlignment());
Chris Lattnerce84ae42008-02-22 02:09:43 +00009691 }
Evan Chengc944c5d2009-03-12 05:59:15 +00009692
9693 // Otherwise, lower to two pairs of 32-bit loads / stores.
9694 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009695 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9696 DAG.getConstant(4, MVT::i32));
Evan Chengc944c5d2009-03-12 05:59:15 +00009697
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009698 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Chengc944c5d2009-03-12 05:59:15 +00009699 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene25160362010-02-15 16:53:33 +00009700 Ld->isVolatile(), Ld->isNonTemporal(),
9701 Ld->getAlignment());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009702 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Chengc944c5d2009-03-12 05:59:15 +00009703 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene25160362010-02-15 16:53:33 +00009704 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Chengc944c5d2009-03-12 05:59:15 +00009705 MinAlign(Ld->getAlignment(), 4));
9706
9707 SDValue NewChain = LoLd.getValue(1);
9708 if (TokenFactorIndex != -1) {
9709 Ops.push_back(LoLd);
9710 Ops.push_back(HiLd);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009711 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Chengc944c5d2009-03-12 05:59:15 +00009712 Ops.size());
9713 }
9714
9715 LoAddr = St->getBasePtr();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009716 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9717 DAG.getConstant(4, MVT::i32));
Evan Chengc944c5d2009-03-12 05:59:15 +00009718
9719 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9720 St->getSrcValue(), St->getSrcValueOffset(),
David Greene25160362010-02-15 16:53:33 +00009721 St->isVolatile(), St->isNonTemporal(),
9722 St->getAlignment());
Evan Chengc944c5d2009-03-12 05:59:15 +00009723 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9724 St->getSrcValue(),
9725 St->getSrcValueOffset() + 4,
9726 St->isVolatile(),
David Greene25160362010-02-15 16:53:33 +00009727 St->isNonTemporal(),
Evan Chengc944c5d2009-03-12 05:59:15 +00009728 MinAlign(St->getAlignment(), 4));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009729 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00009730 }
Dan Gohman8181bd12008-07-27 21:46:04 +00009731 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00009732}
9733
Chris Lattner470d5dc2008-01-25 06:14:17 +00009734/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9735/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009736static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00009737 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9738 // F[X]OR(0.0, x) -> x
9739 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00009740 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9741 if (C->getValueAPF().isPosZero())
9742 return N->getOperand(1);
9743 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9744 if (C->getValueAPF().isPosZero())
9745 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00009746 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00009747}
9748
9749/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009750static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00009751 // FAND(0.0, x) -> 0.0
9752 // FAND(x, 0.0) -> 0.0
9753 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9754 if (C->getValueAPF().isPosZero())
9755 return N->getOperand(0);
9756 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9757 if (C->getValueAPF().isPosZero())
9758 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00009759 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00009760}
9761
Dan Gohman22cefb02009-01-29 01:59:02 +00009762static SDValue PerformBTCombine(SDNode *N,
9763 SelectionDAG &DAG,
9764 TargetLowering::DAGCombinerInfo &DCI) {
9765 // BT ignores high bits in the bit index operand.
9766 SDValue Op1 = N->getOperand(1);
9767 if (Op1.hasOneUse()) {
9768 unsigned BitWidth = Op1.getValueSizeInBits();
9769 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9770 APInt KnownZero, KnownOne;
9771 TargetLowering::TargetLoweringOpt TLO(DAG);
9772 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9773 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9774 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9775 DCI.CommitTargetLoweringOpt(TLO);
9776 }
9777 return SDValue();
9778}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009779
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009780static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9781 SDValue Op = N->getOperand(0);
9782 if (Op.getOpcode() == ISD::BIT_CONVERT)
9783 Op = Op.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00009784 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009785 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009786 VT.getVectorElementType().getSizeInBits() ==
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009787 OpVT.getVectorElementType().getSizeInBits()) {
9788 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9789 }
9790 return SDValue();
9791}
9792
Owen Anderson58155b22009-06-29 18:04:45 +00009793// On X86 and X86-64, atomic operations are lowered to locked instructions.
9794// Locked instructions, in turn, have implicit fence semantics (all memory
9795// operations are flushed before issuing the locked instruction, and the
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009796// are not buffered), so we can fold away the common pattern of
Owen Anderson58155b22009-06-29 18:04:45 +00009797// fence-atomic-fence.
9798static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9799 SDValue atomic = N->getOperand(0);
9800 switch (atomic.getOpcode()) {
9801 case ISD::ATOMIC_CMP_SWAP:
9802 case ISD::ATOMIC_SWAP:
9803 case ISD::ATOMIC_LOAD_ADD:
9804 case ISD::ATOMIC_LOAD_SUB:
9805 case ISD::ATOMIC_LOAD_AND:
9806 case ISD::ATOMIC_LOAD_OR:
9807 case ISD::ATOMIC_LOAD_XOR:
9808 case ISD::ATOMIC_LOAD_NAND:
9809 case ISD::ATOMIC_LOAD_MIN:
9810 case ISD::ATOMIC_LOAD_MAX:
9811 case ISD::ATOMIC_LOAD_UMIN:
9812 case ISD::ATOMIC_LOAD_UMAX:
9813 break;
9814 default:
9815 return SDValue();
9816 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009817
Owen Anderson58155b22009-06-29 18:04:45 +00009818 SDValue fence = atomic.getOperand(0);
9819 if (fence.getOpcode() != ISD::MEMBARRIER)
9820 return SDValue();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009821
Owen Anderson58155b22009-06-29 18:04:45 +00009822 switch (atomic.getOpcode()) {
9823 case ISD::ATOMIC_CMP_SWAP:
9824 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9825 atomic.getOperand(1), atomic.getOperand(2),
9826 atomic.getOperand(3));
9827 case ISD::ATOMIC_SWAP:
9828 case ISD::ATOMIC_LOAD_ADD:
9829 case ISD::ATOMIC_LOAD_SUB:
9830 case ISD::ATOMIC_LOAD_AND:
9831 case ISD::ATOMIC_LOAD_OR:
9832 case ISD::ATOMIC_LOAD_XOR:
9833 case ISD::ATOMIC_LOAD_NAND:
9834 case ISD::ATOMIC_LOAD_MIN:
9835 case ISD::ATOMIC_LOAD_MAX:
9836 case ISD::ATOMIC_LOAD_UMIN:
9837 case ISD::ATOMIC_LOAD_UMAX:
9838 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9839 atomic.getOperand(1), atomic.getOperand(2));
9840 default:
9841 return SDValue();
9842 }
9843}
9844
Evan Chengedeb1692009-12-16 00:53:11 +00009845static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9846 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9847 // (and (i32 x86isd::setcc_carry), 1)
9848 // This eliminates the zext. This transformation is necessary because
9849 // ISD::SETCC is always legalized to i8.
9850 DebugLoc dl = N->getDebugLoc();
9851 SDValue N0 = N->getOperand(0);
9852 EVT VT = N->getValueType(0);
9853 if (N0.getOpcode() == ISD::AND &&
9854 N0.hasOneUse() &&
9855 N0.getOperand(0).hasOneUse()) {
9856 SDValue N00 = N0.getOperand(0);
9857 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9858 return SDValue();
9859 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9860 if (!C || C->getZExtValue() != 1)
9861 return SDValue();
9862 return DAG.getNode(ISD::AND, dl, VT,
9863 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9864 N00.getOperand(0), N00.getOperand(1)),
9865 DAG.getConstant(1, VT));
9866 }
9867
9868 return SDValue();
9869}
9870
Dan Gohman8181bd12008-07-27 21:46:04 +00009871SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00009872 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009873 SelectionDAG &DAG = DCI.DAG;
9874 switch (N->getOpcode()) {
9875 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00009876 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohmanb115d052010-03-15 23:23:03 +00009877 case ISD::EXTRACT_VECTOR_ELT:
9878 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00009879 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnere4577dc2009-03-12 06:52:53 +00009880 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng04ecee12009-03-28 05:57:29 +00009881 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
sampo025b75c2009-01-26 00:52:55 +00009882 case ISD::SHL:
9883 case ISD::SRA:
9884 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng10957b82010-01-04 21:22:48 +00009885 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00009886 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00009887 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00009888 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9889 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00009890 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009891 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson58155b22009-06-29 18:04:45 +00009892 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Chengedeb1692009-12-16 00:53:11 +00009893 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009894 }
9895
Dan Gohman8181bd12008-07-27 21:46:04 +00009896 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009897}
9898
9899//===----------------------------------------------------------------------===//
9900// X86 Inline Assembly Support
9901//===----------------------------------------------------------------------===//
9902
Chris Lattner7fce21c2009-07-20 17:51:36 +00009903static bool LowerToBSwap(CallInst *CI) {
9904 // FIXME: this should verify that we are targetting a 486 or better. If not,
9905 // we will turn this bswap into something that will be lowered to logical ops
9906 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9907 // so don't worry about this.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009908
Chris Lattner7fce21c2009-07-20 17:51:36 +00009909 // Verify this is a simple bswap.
9910 if (CI->getNumOperands() != 2 ||
9911 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandse92dee12010-02-15 16:12:20 +00009912 !CI->getType()->isIntegerTy())
Chris Lattner7fce21c2009-07-20 17:51:36 +00009913 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009914
Chris Lattner7fce21c2009-07-20 17:51:36 +00009915 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9916 if (!Ty || Ty->getBitWidth() % 16 != 0)
9917 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009918
Chris Lattner7fce21c2009-07-20 17:51:36 +00009919 // Okay, we can do this xform, do so now.
9920 const Type *Tys[] = { Ty };
9921 Module *M = CI->getParent()->getParent()->getParent();
9922 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009923
Chris Lattner7fce21c2009-07-20 17:51:36 +00009924 Value *Op = CI->getOperand(1);
9925 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009926
Chris Lattner7fce21c2009-07-20 17:51:36 +00009927 CI->replaceAllUsesWith(Op);
9928 CI->eraseFromParent();
9929 return true;
9930}
9931
9932bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9933 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9934 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9935
9936 std::string AsmStr = IA->getAsmString();
9937
9938 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramer3601d1b2010-01-11 18:03:24 +00009939 SmallVector<StringRef, 4> AsmPieces;
Chris Lattner7fce21c2009-07-20 17:51:36 +00009940 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9941
9942 switch (AsmPieces.size()) {
9943 default: return false;
9944 case 1:
9945 AsmStr = AsmPieces[0];
9946 AsmPieces.clear();
9947 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9948
9949 // bswap $0
9950 if (AsmPieces.size() == 2 &&
9951 (AsmPieces[0] == "bswap" ||
9952 AsmPieces[0] == "bswapq" ||
9953 AsmPieces[0] == "bswapl") &&
9954 (AsmPieces[1] == "$0" ||
9955 AsmPieces[1] == "${0:q}")) {
9956 // No need to check constraints, nothing other than the equivalent of
9957 // "=r,0" would be valid here.
9958 return LowerToBSwap(CI);
9959 }
9960 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandse92dee12010-02-15 16:12:20 +00009961 if (CI->getType()->isIntegerTy(16) &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009962 AsmPieces.size() == 3 &&
Dan Gohman4bf40df2010-03-04 19:58:08 +00009963 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009964 AsmPieces[1] == "$$8," &&
9965 AsmPieces[2] == "${0:w}" &&
Dan Gohman4bf40df2010-03-04 19:58:08 +00009966 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9967 AsmPieces.clear();
Benjamin Kramer73753f12010-03-12 13:54:59 +00009968 const std::string &Constraints = IA->getConstraintString();
9969 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman4bf40df2010-03-04 19:58:08 +00009970 std::sort(AsmPieces.begin(), AsmPieces.end());
9971 if (AsmPieces.size() == 4 &&
9972 AsmPieces[0] == "~{cc}" &&
9973 AsmPieces[1] == "~{dirflag}" &&
9974 AsmPieces[2] == "~{flags}" &&
9975 AsmPieces[3] == "~{fpsr}") {
9976 return LowerToBSwap(CI);
9977 }
Chris Lattner7fce21c2009-07-20 17:51:36 +00009978 }
9979 break;
9980 case 3:
Duncan Sandse92dee12010-02-15 16:12:20 +00009981 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson35b47072009-08-13 21:58:54 +00009982 Constraints.size() >= 2 &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009983 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9984 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9985 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer3601d1b2010-01-11 18:03:24 +00009986 SmallVector<StringRef, 4> Words;
Chris Lattner7fce21c2009-07-20 17:51:36 +00009987 SplitString(AsmPieces[0], Words, " \t");
9988 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9989 Words.clear();
9990 SplitString(AsmPieces[1], Words, " \t");
9991 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9992 Words.clear();
9993 SplitString(AsmPieces[2], Words, " \t,");
9994 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9995 Words[2] == "%edx") {
9996 return LowerToBSwap(CI);
9997 }
9998 }
9999 }
10000 }
10001 break;
10002 }
10003 return false;
10004}
10005
10006
10007
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010008/// getConstraintType - Given a constraint letter, return the type of
10009/// constraint it is for this target.
10010X86TargetLowering::ConstraintType
10011X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10012 if (Constraint.size() == 1) {
10013 switch (Constraint[0]) {
10014 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +000010015 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +000010016 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010017 case 'r':
10018 case 'R':
10019 case 'l':
10020 case 'q':
10021 case 'Q':
10022 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +000010023 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010024 case 'Y':
10025 return C_RegisterClass;
Dale Johannesenf190a032009-02-12 20:58:09 +000010026 case 'e':
10027 case 'Z':
10028 return C_Other;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010029 default:
10030 break;
10031 }
10032 }
10033 return TargetLowering::getConstraintType(Constraint);
10034}
10035
Dale Johannesene99fc902008-01-29 02:21:21 +000010036/// LowerXConstraint - try to replace an X constraint, which matches anything,
10037/// with another that has more specific requirements based on the type of the
10038/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +000010039const char *X86TargetLowering::
Owen Andersonac9de032009-08-10 22:56:29 +000010040LowerXConstraint(EVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +000010041 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10042 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +000010043 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +000010044 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +000010045 return "Y";
10046 if (Subtarget->hasSSE1())
10047 return "x";
10048 }
Scott Michel91099d62009-02-17 22:15:04 +000010049
Chris Lattnereca405c2008-04-26 23:02:14 +000010050 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +000010051}
10052
Chris Lattnera531abc2007-08-25 00:47:38 +000010053/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10054/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +000010055void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +000010056 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +000010057 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +000010058 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +000010059 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +000010060 SDValue Result(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +000010061
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010062 switch (Constraint) {
10063 default: break;
10064 case 'I':
10065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +000010066 if (C->getZExtValue() <= 31) {
10067 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +000010068 break;
10069 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010070 }
Chris Lattnera531abc2007-08-25 00:47:38 +000010071 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +000010072 case 'J':
10073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerb84a1ac2009-06-15 04:39:05 +000010074 if (C->getZExtValue() <= 63) {
Chris Lattner6552d0c2009-06-15 04:01:39 +000010075 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10076 break;
10077 }
10078 }
10079 return;
10080 case 'K':
10081 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerb84a1ac2009-06-15 04:39:05 +000010082 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng4fb2c0f2008-09-22 23:57:37 +000010083 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10084 break;
10085 }
10086 }
10087 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010088 case 'N':
10089 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +000010090 if (C->getZExtValue() <= 255) {
10091 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +000010092 break;
10093 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010094 }
Chris Lattnera531abc2007-08-25 00:47:38 +000010095 return;
Dale Johannesenf190a032009-02-12 20:58:09 +000010096 case 'e': {
10097 // 32-bit signed value
10098 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10099 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson35b47072009-08-13 21:58:54 +000010100 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10101 C->getSExtValue())) {
Dale Johannesenf190a032009-02-12 20:58:09 +000010102 // Widen to 64 bits here to get it sign extended.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010103 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesenf190a032009-02-12 20:58:09 +000010104 break;
10105 }
10106 // FIXME gcc accepts some relocatable values here too, but only in certain
10107 // memory models; it's complicated.
10108 }
10109 return;
10110 }
10111 case 'Z': {
10112 // 32-bit unsigned value
10113 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10114 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson35b47072009-08-13 21:58:54 +000010115 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10116 C->getZExtValue())) {
Dale Johannesenf190a032009-02-12 20:58:09 +000010117 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10118 break;
10119 }
10120 }
10121 // FIXME gcc accepts some relocatable values here too, but only in certain
10122 // memory models; it's complicated.
10123 return;
10124 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010125 case 'i': {
10126 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +000010127 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesenf190a032009-02-12 20:58:09 +000010128 // Widen to 64 bits here to get it sign extended.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010129 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattnera531abc2007-08-25 00:47:38 +000010130 break;
10131 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010132
10133 // If we are in non-pic codegen mode, we allow the address of a global (with
10134 // an optional displacement) to be used with 'i'.
Chris Lattnerd73ba7f2009-05-08 18:23:14 +000010135 GlobalAddressSDNode *GA = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010136 int64_t Offset = 0;
Scott Michel91099d62009-02-17 22:15:04 +000010137
Chris Lattnerd73ba7f2009-05-08 18:23:14 +000010138 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10139 while (1) {
10140 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10141 Offset += GA->getOffset();
10142 break;
10143 } else if (Op.getOpcode() == ISD::ADD) {
10144 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10145 Offset += C->getZExtValue();
10146 Op = Op.getOperand(0);
10147 continue;
10148 }
10149 } else if (Op.getOpcode() == ISD::SUB) {
10150 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10151 Offset += -C->getZExtValue();
10152 Op = Op.getOperand(0);
10153 continue;
10154 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010155 }
Dale Johannesen69976cf2009-07-07 00:18:49 +000010156
Chris Lattnerd73ba7f2009-05-08 18:23:14 +000010157 // Otherwise, this isn't something we can handle, reject it.
10158 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010159 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +000010160
Chris Lattner054532c2009-07-10 07:34:39 +000010161 GlobalValue *GV = GA->getGlobal();
Dale Johannesen69976cf2009-07-07 00:18:49 +000010162 // If we require an extra load to get this address, as in PIC mode, we
10163 // can't accept it.
Chris Lattner054532c2009-07-10 07:34:39 +000010164 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10165 getTargetMachine())))
Dale Johannesen69976cf2009-07-07 00:18:49 +000010166 return;
Scott Michel91099d62009-02-17 22:15:04 +000010167
Dale Johannesenf97110c2009-07-21 00:12:29 +000010168 if (hasMemory)
10169 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10170 else
10171 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattnerd73ba7f2009-05-08 18:23:14 +000010172 Result = Op;
10173 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010174 }
10175 }
Scott Michel91099d62009-02-17 22:15:04 +000010176
Gabor Greif1c80d112008-08-28 21:40:38 +000010177 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +000010178 Ops.push_back(Result);
10179 return;
10180 }
Evan Cheng7f250d62008-09-24 00:05:32 +000010181 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10182 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010183}
10184
10185std::vector<unsigned> X86TargetLowering::
10186getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +000010187 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010188 if (Constraint.size() == 1) {
10189 // FIXME: not handling fp-stack yet!
10190 switch (Constraint[0]) { // GCC X86 Constraint Letters
10191 default: break; // Unknown constraint letter
Evan Chengf8993d42009-07-17 22:13:25 +000010192 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10193 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010194 if (VT == MVT::i32)
Evan Chengf8993d42009-07-17 22:13:25 +000010195 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10196 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10197 X86::R10D,X86::R11D,X86::R12D,
10198 X86::R13D,X86::R14D,X86::R15D,
10199 X86::EBP, X86::ESP, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010200 else if (VT == MVT::i16)
Evan Chengf8993d42009-07-17 22:13:25 +000010201 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10202 X86::SI, X86::DI, X86::R8W,X86::R9W,
10203 X86::R10W,X86::R11W,X86::R12W,
10204 X86::R13W,X86::R14W,X86::R15W,
10205 X86::BP, X86::SP, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010206 else if (VT == MVT::i8)
Evan Chengf8993d42009-07-17 22:13:25 +000010207 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10208 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10209 X86::R10B,X86::R11B,X86::R12B,
10210 X86::R13B,X86::R14B,X86::R15B,
10211 X86::BPL, X86::SPL, 0);
10212
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010213 else if (VT == MVT::i64)
Evan Chengf8993d42009-07-17 22:13:25 +000010214 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10215 X86::RSI, X86::RDI, X86::R8, X86::R9,
10216 X86::R10, X86::R11, X86::R12,
10217 X86::R13, X86::R14, X86::R15,
10218 X86::RBP, X86::RSP, 0);
10219
10220 break;
10221 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +000010222 // 32-bit fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010223 case 'Q': // Q_REGS
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010224 if (VT == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010225 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010226 else if (VT == MVT::i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010227 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010228 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +000010229 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010230 else if (VT == MVT::i64)
Chris Lattner35032592007-11-04 06:51:12 +000010231 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10232 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010233 }
10234 }
10235
10236 return std::vector<unsigned>();
10237}
10238
10239std::pair<unsigned, const TargetRegisterClass*>
10240X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +000010241 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010242 // First, see if this is a constraint that directly corresponds to an LLVM
10243 // register class.
10244 if (Constraint.size() == 1) {
10245 // GCC Constraint Letters
10246 switch (Constraint[0]) {
10247 default: break;
10248 case 'r': // GENERAL_REGS
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010249 case 'l': // INDEX_REGS
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010250 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010251 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010252 if (VT == MVT::i16)
Chris Lattnerbbfea052008-10-17 18:15:05 +000010253 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010254 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michel91099d62009-02-17 22:15:04 +000010255 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +000010256 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen1bf03f72009-10-07 22:47:20 +000010257 case 'R': // LEGACY_REGS
10258 if (VT == MVT::i8)
10259 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10260 if (VT == MVT::i16)
10261 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10262 if (VT == MVT::i32 || !Subtarget->is64Bit())
10263 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10264 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +000010265 case 'f': // FP Stack registers.
10266 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10267 // value to the correct fpstack register class.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010268 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattner267805f2008-03-11 19:06:29 +000010269 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010270 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattner267805f2008-03-11 19:06:29 +000010271 return std::make_pair(0U, X86::RFP64RegisterClass);
10272 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010273 case 'y': // MMX_REGS if MMX allowed.
10274 if (!Subtarget->hasMMX()) break;
10275 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010276 case 'Y': // SSE_REGS if SSE2 allowed
10277 if (!Subtarget->hasSSE2()) break;
10278 // FALL THROUGH.
10279 case 'x': // SSE_REGS if SSE1 allowed
10280 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +000010281
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010282 switch (VT.getSimpleVT().SimpleTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010283 default: break;
10284 // Scalar SSE types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010285 case MVT::f32:
10286 case MVT::i32:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010287 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010288 case MVT::f64:
10289 case MVT::i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010290 return std::make_pair(0U, X86::FR64RegisterClass);
10291 // Vector types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010292 case MVT::v16i8:
10293 case MVT::v8i16:
10294 case MVT::v4i32:
10295 case MVT::v2i64:
10296 case MVT::v4f32:
10297 case MVT::v2f64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010298 return std::make_pair(0U, X86::VR128RegisterClass);
10299 }
10300 break;
10301 }
10302 }
Scott Michel91099d62009-02-17 22:15:04 +000010303
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010304 // Use the default implementation in TargetLowering to convert the register
10305 // constraint into a member of a register class.
10306 std::pair<unsigned, const TargetRegisterClass*> Res;
10307 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10308
10309 // Not found as a standard register?
10310 if (Res.second == 0) {
Chris Lattner1063d242009-09-13 22:41:48 +000010311 // Map st(0) -> st(7) -> ST0
10312 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10313 tolower(Constraint[1]) == 's' &&
10314 tolower(Constraint[2]) == 't' &&
10315 Constraint[3] == '(' &&
10316 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10317 Constraint[5] == ')' &&
10318 Constraint[6] == '}') {
Daniel Dunbar3be44e62009-09-20 02:20:51 +000010319
Chris Lattner1063d242009-09-13 22:41:48 +000010320 Res.first = X86::ST0+Constraint[4]-'0';
10321 Res.second = X86::RFP80RegisterClass;
10322 return Res;
10323 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +000010324
Chris Lattner1063d242009-09-13 22:41:48 +000010325 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramerea862b02009-11-12 20:36:59 +000010326 if (StringRef("{st}").equals_lower(Constraint)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010327 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +000010328 Res.second = X86::RFP80RegisterClass;
Chris Lattner1063d242009-09-13 22:41:48 +000010329 return Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010330 }
Chris Lattner1063d242009-09-13 22:41:48 +000010331
10332 // flags -> EFLAGS
Benjamin Kramerea862b02009-11-12 20:36:59 +000010333 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner1063d242009-09-13 22:41:48 +000010334 Res.first = X86::EFLAGS;
10335 Res.second = X86::CCRRegisterClass;
10336 return Res;
10337 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +000010338
Dale Johannesen73920c02008-11-13 21:52:36 +000010339 // 'A' means EAX + EDX.
10340 if (Constraint == "A") {
10341 Res.first = X86::EAX;
Dan Gohmanb4439d02009-07-30 17:02:08 +000010342 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner1063d242009-09-13 22:41:48 +000010343 return Res;
Dale Johannesen73920c02008-11-13 21:52:36 +000010344 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010345 return Res;
10346 }
10347
10348 // Otherwise, check to see if this is a register class of the wrong value
10349 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10350 // turn into {ax},{dx}.
10351 if (Res.second->hasType(VT))
10352 return Res; // Correct type already, nothing to do.
10353
10354 // All of the single-register GCC register classes map their values onto
10355 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10356 // really want an 8-bit or 32-bit register, map to the appropriate register
10357 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +000010358 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010359 if (VT == MVT::i8) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010360 unsigned DestReg = 0;
10361 switch (Res.first) {
10362 default: break;
10363 case X86::AX: DestReg = X86::AL; break;
10364 case X86::DX: DestReg = X86::DL; break;
10365 case X86::CX: DestReg = X86::CL; break;
10366 case X86::BX: DestReg = X86::BL; break;
10367 }
10368 if (DestReg) {
10369 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010370 Res.second = X86::GR8RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010371 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010372 } else if (VT == MVT::i32) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010373 unsigned DestReg = 0;
10374 switch (Res.first) {
10375 default: break;
10376 case X86::AX: DestReg = X86::EAX; break;
10377 case X86::DX: DestReg = X86::EDX; break;
10378 case X86::CX: DestReg = X86::ECX; break;
10379 case X86::BX: DestReg = X86::EBX; break;
10380 case X86::SI: DestReg = X86::ESI; break;
10381 case X86::DI: DestReg = X86::EDI; break;
10382 case X86::BP: DestReg = X86::EBP; break;
10383 case X86::SP: DestReg = X86::ESP; break;
10384 }
10385 if (DestReg) {
10386 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010387 Res.second = X86::GR32RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010388 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010389 } else if (VT == MVT::i64) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010390 unsigned DestReg = 0;
10391 switch (Res.first) {
10392 default: break;
10393 case X86::AX: DestReg = X86::RAX; break;
10394 case X86::DX: DestReg = X86::RDX; break;
10395 case X86::CX: DestReg = X86::RCX; break;
10396 case X86::BX: DestReg = X86::RBX; break;
10397 case X86::SI: DestReg = X86::RSI; break;
10398 case X86::DI: DestReg = X86::RDI; break;
10399 case X86::BP: DestReg = X86::RBP; break;
10400 case X86::SP: DestReg = X86::RSP; break;
10401 }
10402 if (DestReg) {
10403 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010404 Res.second = X86::GR64RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010405 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010406 }
Chris Lattnere9d7f792008-08-26 06:19:02 +000010407 } else if (Res.second == X86::FR32RegisterClass ||
10408 Res.second == X86::FR64RegisterClass ||
10409 Res.second == X86::VR128RegisterClass) {
10410 // Handle references to XMM physical registers that got mapped into the
10411 // wrong class. This can happen with constraints like {xmm0} where the
10412 // target independent register mapper will just pick the first match it can
10413 // find, ignoring the required type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010414 if (VT == MVT::f32)
Chris Lattnere9d7f792008-08-26 06:19:02 +000010415 Res.second = X86::FR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010416 else if (VT == MVT::f64)
Chris Lattnere9d7f792008-08-26 06:19:02 +000010417 Res.second = X86::FR64RegisterClass;
10418 else if (X86::VR128RegisterClass->hasType(VT))
10419 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010420 }
10421
10422 return Res;
10423}